Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test conceptCohort #326

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading