# ---------- executeCostAnalysis: validation ------------------------------------ test_that("executeCostAnalysis rejects non-CostAnalysisPlan object", { expect_error( executeCostAnalysis( plan = list(which = "overall"), connection = fake_connection(), cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "res", cohortTable = "cohort" ), "CostAnalysisPlan" ) }) test_that("executeCostAnalysis rejects missing connection and connectionDetails", { plan <- planAnalysis( useOverallCost = list(compute = TRUE), useDomainCost = list(compute = FALSE), useConceptSetCost = list(compute = FALSE) ) expect_error( executeCostAnalysis( plan = plan, cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "res", cohortTable = "cohort", window = list(startWith = "start", startOffset = 0, endWith = "end", endOffset = 0) ), "connection.*connectionDetails" ) }) test_that("executeCostAnalysis errors when no windows provided at all", { plan <- planAnalysis( useOverallCost = list(compute = TRUE), useDomainCost = list(compute = FALSE), useConceptSetCost = list(compute = FALSE) ) expect_error( executeCostAnalysis( plan = plan, connection = fake_connection(), cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "res", cohortTable = "cohort" ), "windows must be provided" ) }) # ---------- executeCostAnalysis: delegates to runAllCosts ---------------------- test_that("executeCostAnalysis passes plan fields and window to runAllCosts", { plan <- planAnalysis( useOverallCost = list(compute = TRUE, costMeasures = "total_charge", excludeZeroCostPatients = TRUE), useDomainCost = list(compute = FALSE), useConceptSetCost = list(compute = FALSE) ) state <- new.env(parent = emptyenv()) state$args <- NULL local_namespace_mock("OdysseusCostModule", "runAllCosts", function(...) { state$args <- list(...) list(costMetrics = data.frame(), costDistribution = data.frame()) }) w <- list(startWith = "start", startOffset = 0, endWith = "end", endOffset = 0) result <- executeCostAnalysis( plan = plan, connection = fake_connection(), cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "res", cohortTable = "cohort", window = w ) expect_identical(state$args$which, "overall") expect_identical(state$args$costMeasures, "total_charge") expect_true(state$args$excludeZeroCostPatients) expect_true(state$args$excludeNegativeCosts) expect_identical(state$args$window, w) expect_null(state$args$windows) expect_true(state$args$aggregateResults) }) test_that("executeCostAnalysis passes windows parameter", { plan <- planAnalysis( useOverallCost = list(compute = TRUE), useDomainCost = list(compute = FALSE), useConceptSetCost = list(compute = FALSE) ) state <- new.env(parent = emptyenv()) local_namespace_mock("OdysseusCostModule", "runAllCosts", function(...) { state$args <- list(...) list(costMetrics = data.frame(), costDistribution = data.frame()) }) ws <- list( baseline = list(startWith = "start", startOffset = -365, endWith = "start", endOffset = -1), year1 = list(startWith = "start", startOffset = 0, endWith = "start", endOffset = 365) ) executeCostAnalysis( plan = plan, connection = fake_connection(), cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "res", cohortTable = "cohort", windows = ws ) expect_identical(state$args$windows, ws) expect_null(state$args$window) }) test_that("executeCostAnalysis prefers plan$analysisWindows over window/windows args", { plan_ws <- list( planned = list(startWith = "start", startOffset = 0, endWith = "end", endOffset = 0) ) plan <- planAnalysis( useOverallCost = list(compute = TRUE), useDomainCost = list(compute = FALSE), useConceptSetCost = list(compute = FALSE), analysisWindows = plan_ws ) state <- new.env(parent = emptyenv()) local_namespace_mock("OdysseusCostModule", "runAllCosts", function(...) { state$args <- list(...) list(costMetrics = data.frame(), costDistribution = data.frame()) }) arg_window <- list(startWith = "start", startOffset = 0, endWith = "start", endOffset = 30) executeCostAnalysis( plan = plan, connection = fake_connection(), cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "res", cohortTable = "cohort", window = arg_window ) # plan$analysisWindows should override the window arg expect_named(state$args$windows, "planned") # window arg is still passed through but runAllCosts will prefer windows expect_identical(state$args$window, arg_window) }) test_that("executeCostAnalysis passes aggregateResults = FALSE", { plan <- planAnalysis( useOverallCost = list(compute = TRUE), useDomainCost = list(compute = FALSE), useConceptSetCost = list(compute = FALSE) ) state <- new.env(parent = emptyenv()) local_namespace_mock("OdysseusCostModule", "runAllCosts", function(...) { state$args <- list(...) list(rawResults = data.frame()) }) executeCostAnalysis( plan = plan, connection = fake_connection(), cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "res", cohortTable = "cohort", window = list(startWith = "start", startOffset = 0, endWith = "end", endOffset = 0), aggregateResults = FALSE ) expect_false(state$args$aggregateResults) }) test_that("executeCostAnalysis passes concept set fields", { cs <- list(diab = list(items = list( list(concept = list(CONCEPT_ID = 201820L), includeDescendants = TRUE) ))) plan <- planAnalysis( useOverallCost = list(compute = FALSE), useDomainCost = list(compute = FALSE), useConceptSetCost = list( compute = TRUE, conceptSets = cs, domains = c("Drug", "Condition") ) ) state <- new.env(parent = emptyenv()) local_namespace_mock("OdysseusCostModule", "runAllCosts", function(...) { state$args <- list(...) list(costMetrics = data.frame(), costDistribution = data.frame()) }) executeCostAnalysis( plan = plan, connection = fake_connection(), cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "res", cohortTable = "cohort", window = list(startWith = "start", startOffset = 0, endWith = "end", endOffset = 0) ) expect_identical(state$args$which, "conceptSet") expect_identical(names(state$args$conceptSets), "diab") expect_identical(state$args$conceptSetDomains, c("Drug", "Condition")) })