Skip to content

Commit

Permalink
Merge pull request #326 from OHDSI/tbl_clean_up_tests
Browse files Browse the repository at this point in the history
test conceptCohort
  • Loading branch information
edward-burn authored Sep 25, 2024
2 parents d13ea61 + f31eb4f commit ff25b25
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 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
27 changes: 27 additions & 0 deletions tests/testthat/test-demographicsCohort.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ test_that("Example: mixture of parameters", {
omock::mockObservationPeriod() |>
omock::mockCohort(name = c("cohort1"), numberCohorts = 5, seed = 2)
cdm <- cdm_local |> copyCdm()

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(
cdm$cohort3 <- cdm |>
demographicsCohort(name = "cohort3",
Expand All @@ -155,6 +166,22 @@ test_that("Example: mixture of parameters", {
minPriorObservation = 25)
)

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
)
}

cdm$cohort3 <- cdm$cohort3 |>
PatientProfiles::addPriorObservation()

Expand Down
30 changes: 29 additions & 1 deletion tests/testthat/test-measurementCohort.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,42 @@ test_that("mearurementCohorts works", {
)
cdm <- cdm |> copyCdm()

# simple example ----
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"))
}

# simple example
cdm$cohort <- measurementCohort(
cdm = cdm,
name = "cohort",
conceptSet = list("normal_blood_pressure" = c(4326744, 4298393, 45770407)),
valueAsConcept = c(4124457),
valueAsNumber = list("8876" = c(70, 120))
)

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_equal(
collectCohort(cdm$cohort, 1),
dplyr::tibble(
Expand Down

0 comments on commit ff25b25

Please sign in to comment.