Skip to content

Commit

Permalink
test conceptCohort
Browse files Browse the repository at this point in the history
  • Loading branch information
edward-burn committed Sep 24, 2024
1 parent b5ebcf5 commit 76a1093
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
14 changes: 14 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,17 @@ copyCdm <- function(cdm) {
con = connection(), cdm = cdm, schema = writeSchema(), overwrite = TRUE
)
}
countDuckdbTempTables <- function(con){
duckdb_temp_tables <- DBI::dbGetQuery(con, "SHOW ALL TABLES")
duckdb_temp_tables |>
dplyr::filter(database == "temp") |>
dplyr::tally() |>
dplyr::pull("n")
}
countDuckdbPermanentTables <- function(con){
duckdb_temp_tables <- DBI::dbGetQuery(con, "SHOW ALL TABLES")
duckdb_temp_tables |>
dplyr::filter(database != "temp") |>
dplyr::tally() |>
dplyr::pull("n")
}
29 changes: 27 additions & 2 deletions tests/testthat/test-conceptCohort.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,33 @@ test_that("simple example", {
)

cdm <- cdm |> copyCdm()

expect_no_error(cohort <- conceptCohort(cdm = cdm, conceptSet = list(a = 1), name = "cohort"))
isDuckdb <- attr(omopgenerics::cdmSource(cdm), "source_type") == "duckdb"
if(isDuckdb){
startTempTables <- countDuckdbTempTables(
con = attr(omopgenerics::cdmSource(cdm),
"dbcon"))
startPermanentTables <- countDuckdbPermanentTables(
con = attr(omopgenerics::cdmSource(cdm),
"dbcon"))
}
expect_no_error(cohort <- conceptCohort(cdm = cdm,
conceptSet = list(a = 1),
name = "my_cohort"))
if(isDuckdb){
endTempTables <- countDuckdbTempTables(
con = attr(omopgenerics::cdmSource(cdm),
"dbcon"))
endPermanentTables <- countDuckdbPermanentTables(
con = attr(omopgenerics::cdmSource(cdm),
"dbcon"))
# we should have only added 4 permanent tables (the new cohort table and
# three tables with settings, attrition, and codelist)
# no temp tables will have been created
expect_true(startTempTables == endTempTables)
expect_true(
startPermanentTables + 4 == endPermanentTables
)
}

expect_true(cohort |> dplyr::tally() |> dplyr::pull() == 4)
expect_true(cohortCount(cohort)$number_records == 4)
Expand Down

0 comments on commit 76a1093

Please sign in to comment.