test_that("runAllCosts validates required inputs", { valid_window <- list(startWith = "start", startOffset = 0, endWith = "end", endOffset = 30) expect_error( OdysseusCostModule::runAllCosts( cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "cohort", cohortTable = "cohort_table", window = valid_window ), "Either connection or connectionDetails must be provided" ) expect_error( OdysseusCostModule::runAllCosts( connection = fake_connection(), cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "cohort", cohortTable = "cohort_table", window = valid_window, which = c("overall", "conceptSet") ), "conceptSets must be provided" ) }) test_that("runAllCosts orchestrates selected computations and returns lower-cased results", { connection <- fake_connection() valid_window <- list(startWith = "start", startOffset = 0, endWith = "end", endOffset = 30) state <- new.env(parent = emptyenv()) state$calls <- character() state$stats_calls <- 0 state$query_tables <- character() state$dropped <- NULL local_namespace_mock("DatabaseConnector", "renderTranslateQuerySql", function(connection, sql, table = NULL, ...) { state$query_tables <- c(state$query_tables, table) data.frame(FOO = 1, BAR = 2) }) local_namespace_mock("OdysseusCostModule", ".createAnalysisWindows", function(...) { state$calls <- c(state$calls, "windows") invisible(NULL) }) local_namespace_mock("OdysseusCostModule", ".calculatePersonTime", function(...) { state$calls <- c(state$calls, "person_time") invisible(NULL) }) local_namespace_mock("OdysseusCostModule", "computeOverallCost", function(...) { state$calls <- c(state$calls, "overall") invisible(NULL) }) local_namespace_mock("OdysseusCostModule", "computeCostByDomain", function(...) { state$calls <- c(state$calls, "domain") invisible(NULL) }) local_namespace_mock("OdysseusCostModule", "computeCostByConceptSet", function(...) { state$calls <- c(state$calls, "conceptSet") invisible(NULL) }) local_namespace_mock("OdysseusCostModule", "computeCostStatistics", function(connection, workDatabaseSchema, tempEmulationSchema) { state$stats_calls <- state$stats_calls + 1 invisible(NULL) }) local_namespace_mock("OdysseusCostModule", ".dropTempTables", function(connection, tablesToDrop, tempEmulationSchema) { state$dropped <- list(tablesToDrop = tablesToDrop) invisible(NULL) }) results <- OdysseusCostModule::runAllCosts( connection = connection, cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "cohort", cohortTable = "cohort_table", window = valid_window, which = c("overall", "domain", "conceptSet"), conceptSets = list(example = list(items = list(list(concept = list(CONCEPT_ID = 1))))) ) expect_identical(state$calls, c("windows", "person_time", "overall", "domain", "conceptSet")) expect_equal(state$stats_calls, 1) expect_identical(state$query_tables, c("#result_cost_metrics_tmp", "#cost_dist_tmp")) expect_true(is.list(results)) expect_length(results, 2) expect_identical(names(results[[1]]), c("foo", "bar")) expect_identical( state$dropped$tablesToDrop, c("#cost_aw_tmp", "#person_time_tmp", "#overall_cost_tmp", "#result_cost_metrics_tmp", "#cost_dist_tmp") ) }) test_that("runAllCosts connects, removes zero-cost rows, and returns raw results", { connection_details <- structure(list(), class = "ConnectionDetails") valid_window <- list(startWith = "start", startOffset = 0, endWith = "end", endOffset = 30) state <- new.env(parent = emptyenv()) state$connected <- 0 state$disconnected <- 0 state$zero_cost_sql <- character() state$dropped <- NULL local_namespace_mock("DatabaseConnector", "connect", function(details) { state$connected <- state$connected + 1 fake_connection() }) local_namespace_mock("DatabaseConnector", "disconnect", function(connection) { state$disconnected <- state$disconnected + 1 invisible(NULL) }) local_namespace_mock("DatabaseConnector", "renderTranslateExecuteSql", function(connection, sql, ...) { state$zero_cost_sql <- c(state$zero_cost_sql, sql) invisible(NULL) }) local_namespace_mock("DatabaseConnector", "renderTranslateQuerySql", function(connection, sql, ...) { data.frame(COST_VALUE = 1) }) local_namespace_mock("OdysseusCostModule", ".createAnalysisWindows", function(...) invisible(NULL)) local_namespace_mock("OdysseusCostModule", ".calculatePersonTime", function(...) invisible(NULL)) local_namespace_mock("OdysseusCostModule", "computeOverallCost", function(...) invisible(NULL)) local_namespace_mock("OdysseusCostModule", ".dropTempTables", function(connection, tablesToDrop, tempEmulationSchema) { state$dropped <- list(tablesToDrop = tablesToDrop) invisible(NULL) }) results <- OdysseusCostModule::runAllCosts( connectionDetails = connection_details, cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "cohort", cohortTable = "cohort_table", window = valid_window, which = "overall", excludeZeroCostPatients = TRUE, aggregateResults = FALSE ) expect_equal(state$connected, 1) expect_equal(state$disconnected, 1) expect_length(state$zero_cost_sql, 1) expect_match(state$zero_cost_sql, "#zero_cost_pts") expect_identical(names(results$rawResults), "cost_value") expect_identical( state$dropped$tablesToDrop, c("#cost_aw_tmp", "#person_time_tmp", "#overall_cost_tmp", "#result_cost_metrics_tmp", "#cost_dist_tmp") ) }) test_that("runAllCosts creates windows for each entry in `windows`", { connection <- fake_connection() state <- new.env(parent = emptyenv()) state$window_ids <- character() local_namespace_mock("DatabaseConnector", "renderTranslateQuerySql", function(connection, sql, table = NULL, ...) { data.frame(FOO = 1, BAR = 2) }) local_namespace_mock("OdysseusCostModule", ".createAnalysisWindows", function(..., windowId = "default") { state$window_ids <- c(state$window_ids, windowId) invisible(NULL) }) local_namespace_mock("OdysseusCostModule", ".calculatePersonTime", function(...) invisible(NULL)) local_namespace_mock("OdysseusCostModule", "computeOverallCost", function(...) invisible(NULL)) local_namespace_mock("OdysseusCostModule", "computeCostStatistics", function(...) invisible(NULL)) local_namespace_mock("OdysseusCostModule", ".dropTempTables", function(...) invisible(NULL)) OdysseusCostModule::runAllCosts( connection = connection, cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "cohort", cohortTable = "cohort_table", windows = list( baseline = list(startWith = "start", startOffset = -365, endWith = "start", endOffset = -1), first_year = list(startWith = "start", startOffset = 0, endWith = "start", endOffset = 365) ), which = "overall" ) expect_identical(state$window_ids, c("baseline", "first_year")) }) test_that("runAllCosts errors when neither window nor windows is given", { expect_error( OdysseusCostModule::runAllCosts( connection = fake_connection(), cdmDatabaseSchema = "cdm", cohortDatabaseSchema = "cohort", cohortTable = "cohort_table", which = "overall" ), "window.*must be provided" ) })