Skip to content

Commit

Permalink
Add unit test for min cell count
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonysena committed Aug 30, 2024
1 parent dac1b94 commit 26aea83
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/testthat/test-RunCohortGeneration.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,63 @@ test_that("Call runCohortGeneration happy path", {
expect_true(all(data$databaseId == expectedDatabaseId))
}
})

test_that("Call runCohortGeneration and verify censoring of minimum cell counts", {
testOutputFolder1 <- file.path(outputFolder, "minCellCountRun1")
testOutputFolder2 <- file.path(outputFolder, "minCellCountRun2")
on.exit(expr = {
unlink(testOutputFolder1, recursive = TRUE)
unlink(testOutputFolder2, recursive = TRUE)
})
minCellCountForTest <- 500
cohortsWithStats <- getCohortsForTest(cohorts, generateStats = TRUE)
ncSet <- getNegativeControlOutcomeCohortsForTest()
expectedDatabaseId <- "db1"

runCohortGeneration(
connectionDetails = connectionDetails,
cdmDatabaseSchema = "main",
cohortDatabaseSchema = "main",
cohortTableNames = CohortGenerator::getCohortTableNames("runCG"),
cohortDefinitionSet = cohortsWithStats,
negativeControlOutcomeCohortSet = ncSet,
occurrenceType = "all",
detectOnDescendants = TRUE,
stopOnError = FALSE,
outputFolder = testOutputFolder1,
databaseId = expectedDatabaseId,
minCellCount = 0,
incremental = F
)

runCohortGeneration(
connectionDetails = connectionDetails,
cdmDatabaseSchema = "main",
cohortDatabaseSchema = "main",
cohortTableNames = CohortGenerator::getCohortTableNames("runCG"),
cohortDefinitionSet = cohortsWithStats,
negativeControlOutcomeCohortSet = ncSet,
occurrenceType = "all",
detectOnDescendants = TRUE,
stopOnError = FALSE,
outputFolder = testOutputFolder2,
databaseId = expectedDatabaseId,
minCellCount = minCellCountForTest,
incremental = F
)


# Verify that values below the minCellCountForTest are censored properly
spec <- CohortGenerator::readCsv(
file = system.file("csv", "resultsDataModelSpecification.csv", package = "CohortGenerator")
) %>%
dplyr::filter(tolower(.data$minCellCount) == "yes") %>%
dplyr::arrange(.data$tableName, .data$columnName)

for (i in 1:nrow(spec)) {
data1 <- readr::read_csv(file = file.path(testOutputFolder1, paste0(spec$tableName[i], ".csv")), col_types = readr::cols(), lazy = F)
data2 <- readr::read_csv(file = file.path(testOutputFolder2, paste0(spec$tableName[i], ".csv")), col_types = readr::cols(), lazy = F)
rowsBelowMinCellCount <- which(data1[[spec$columnName[i]]] < minCellCountForTest)
expect_true(all(data2[rowsBelowMinCellCount, spec$columnName[i]] == -minCellCountForTest))
}
})

0 comments on commit 26aea83

Please sign in to comment.