Skip to content

Commit

Permalink
Ensure database_id applied to all returned cohort counts (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonysena authored May 15, 2024
1 parent 7adc2f6 commit 1f0fda0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
6 changes: 4 additions & 2 deletions R/CohortCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ getCohortCounts <- function(connectionDetails = NULL,
sql = sql,
cohort_database_schema = cohortDatabaseSchema,
cohort_table = cohortTable,
cohort_ids = cohortIds,
database_id = ifelse(test = is.null(databaseId), yes = "", no = databaseId)
cohort_ids = cohortIds
)
sql <- SqlRender::translate(sql = sql, targetDialect = connection@dbms)
tablesInServer <- tolower(DatabaseConnector::getTableNames(conn = connection, databaseSchema = cohortDatabaseSchema))
Expand All @@ -88,6 +87,9 @@ getCohortCounts <- function(connectionDetails = NULL,
cohortSubjects = ifelse(is.na(cohortSubjects), 0L, cohortSubjects)
)
}
if (!is.null(databaseId)) {
counts$databaseId <- databaseId
}
return(counts)
} else {
warning("Cohort table was not found. Was it created?")
Expand Down
1 change: 0 additions & 1 deletion inst/sql/sql_server/CohortCounts.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
SELECT
{@database_id != ''}?{CAST('@database_id' as VARCHAR(255)) as database_id,}
cohort_definition_id AS cohort_id,
COUNT(*) AS cohort_entries,
COUNT(DISTINCT subject_id) AS cohort_subjects
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-CohortCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ test_that("Call getCohortCounts with a cohortDefinitionSet returns 0 counts for
cohortDatabaseSchema = "main",
cohortTable = "cohort",
cohortIds = c(1, 2, 100),
cohortDefinitionSet = cohortDefinitionSet
cohortDefinitionSet = cohortDefinitionSet,
databaseId = 999
)

expect_true(nrow(testCohortCounts) == 3)
expect_true(testCohortCounts[testCohortCounts$cohortId == 100, "cohortEntries"] == 0)
expect_true(testCohortCounts[testCohortCounts$cohortId == 100, "cohortSubjects"] == 0)
expect_true(all(testCohortCounts$databaseId == 999))
})

test_that("Call getCohortCounts with no cohortId specified and cohortDefinitionSet returns 0 counts for cohortId not in cohort table", {
Expand Down

0 comments on commit 1f0fda0

Please sign in to comment.