"mock db: check events overlapping with start of a period"
"mock db: compare results from months and years"
"mock db: check entry and event on same day"
"mock db: cohort start overlaps with the outcome"
"mock db: check outcome in previous obeservation period"
"mock db: multiple overlapping outcomes"
"mock db: cohort before period start ending after period"
"mock db: check full period requirement - year"
"mock db: check full period requirement - month"
"mock db: check insufficient study days"
"mock db: check with and without study start and end date"
"mock db: check study start and end date 1000"

# some pops with people, but some without
cdm <- generateDenominatorCohortSet(cdm, name = "denominator",
dplyr::summarise(n = sum(.data$number_records)) == 0)
dplyr::summarise(n = sum(.data$number_records)) > 0)
test_that("mock db: check example we expect to work", {
test_that("mock db: check another example we expect to work", {
# check min age change cohort start date
dplyr::summarise(check = cohort_start_date == as.Date("2005-07-25")) %>%
dplyr::summarise(check = cohort_start_date == as.Date("2003-01-01")) %>%
dplyr::summarise(check = cohort_start_date == as.Date("2004-06-01")) %>%
dplyr::summarise(check = cohort_start_date == as.Date("2006-05-02")) %>%
# check max age change cohort start date
dplyr::summarise(check = cohort_end_date == as.Date("2006-07-24")) %>%
dplyr::summarise(check = cohort_end_date == as.Date("2003-12-31")) %>%
dplyr::summarise(check = cohort_end_date == as.Date("2005-05-31")) %>%
dplyr::summarise(check = cohort_end_date == as.Date("2007-05-01")) %>%
test_that("mock db: mock example 1000", {
# all options being used except study start and end
test_that("mock db: subset denominator by cohort", {
cdm = cdm, name = "target_cohort_mult1",
# without specifying target, should run for both
cdm = cdm, name = "target_cohort_mult2",
# stratifying cohort multiple events per person
# multiple observation periods and multiple outcomes for a person
test_that("mock db: one male, one female", {
test_that("mock db: check example with restriction on sex", {
test_that("mock db: check example with restriction on age", {
# three people, born in 2000, 2005, and 2010
# entry once they reach the min age criteria
# start date is now date of 10th birthday
# exit once they reach the max age criteria
# end date is the day before their 11th birthday
test_that("mock db: check age edge cases", {
test_that("mock db check age target entry and exit", {
# if we have two age groups 1) 11 to 12, and 2) 13 to 14
# we expect the person to be in the first cohort up
# and in the second from their 13th birthday
# up to the day before their 15th birthday
test_that("mock db check target prior observation requirement", {
targetCohortTable <- tibble::tibble( # same as obs period
# (as prior observation based on obs period achieved before target cohort start)
cdm = cdm, name = "denominator", targetCohortTable = "target",
# enter when they satisfy prior hist reqs
# subject 1 should be in both cohorts, subject 2 only in first with 0 day req
# in all cases subject 1 should start on their target start "2012-01-05"
# in all cases subject 2 should start on their target start "2013-01-05"
# they don´t satisfy age req on cohort start date
# but they won´t contribute to the next age cohort
test_that("mock db: check example with multiple observation periods", {
expect_true(nrow(cdm$denominator %>% dplyr::collect()) == 2)
# expect one rows- if start date is 1st Jan 2011
expect_true(nrow(cdm$denominator %>% dplyr::collect()) == 1)
# expect one row- if start date is end of 2020
test_that("mock db: check imputation of date of birth", {
# one person with all info, one missing month, one missing day, and one both
dplyr::summarise(check = cohort_start_date == as.Date("2010-03-03")) %>%
dplyr::summarise(check = cohort_start_date == as.Date("2010-01-03")) %>%
dplyr::summarise(check = cohort_start_date == as.Date("2010-03-01")) %>%
dplyr::summarise(check = cohort_start_date == as.Date("2010-01-01")) %>%
test_that("mock db: check edge cases (zero results expected)", {
# note could include people as it would go up to day before first birthday
# but given observation period, here we would expect a null
test_that("mock db: check attrition table logic", {
# check last n_current equals the number of rows of the denominator pop
dplyr::filter(reason == "Not Female") %>%
# multiple observation periods per person
test_that("mock db: check attrition with multiple cohorts", {
cdm <- generateDenominatorCohortSet(cdm,name = "denominator",
# for male cohort we should have a row for those excluded for not being male
expect_true(any("Not Male" == CDMConnector::settings(cdm$denominator) %>%
expect_true(any("Not Female" == CDMConnector::settings(cdm$denominator) %>%
# for both cohort we should have a row for those excluded for not being male
# nobody dropped for prior hist when req is 0
dplyr::filter(reason == "No observation time available after applying age, prior observation and, if applicable, target criteria") %>%
# some people dropped for prior hist when req is 1000
test_that("mock db: check tables were cleaned up", {
startTables <- CDMConnector::listTables(attr(attr(cdm, "cdm_source"), "dbcon"),
endTables <- CDMConnector::listTables(attr(attr(cdm, "cdm_source"), "dbcon"),
# we should only have added one temp table that contains our cohorts
# all intermediate permanent tables created along the way should have been dropped
con = attr(attr(cdm, "cdm_source"), "dbcon"),
write_schema = "main", cdm_name = "mock"
test_that("mock db: requirement interactions", {
expect_true(nrow(CDMConnector::settings(cdm$denominator)) == 4 * 3 * 2)
dplyr::filter(age_group == "0 to 100") %>%
dplyr::filter(days_prior_observation == 0)) == 1
dplyr::filter(age_group == "11 to 15") %>%
dplyr::filter(days_prior_observation == 30)) == 1
dplyr::filter(days_prior_observation == 0)) == 0
dplyr::filter(days_prior_observation == 30)) == 0