Skip to content

Commit

Permalink
Test coverage can be 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
gowthamrao committed Oct 23, 2023
1 parent 6789389 commit 0bc4c8e
Showing 1 changed file with 61 additions and 48 deletions.
109 changes: 61 additions & 48 deletions tests/testthat/test-createCohortExplorerApp.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
test_that("Create app with cohort data in temp table", {
skip_if(skipCdmTests, "CDM settings not configured")

library(dplyr)

createCohortTableSql <- "
DROP TABLE IF EXISTS #temp_cohort_table;
Expand All @@ -25,11 +25,11 @@ test_that("Create app with cohort data in temp table", {
ON o1.person_id = b.person_id
WHERE new_id < 10
;"

connection <-
DatabaseConnector::connect(connectionDetails = connectionDetails)
on.exit(DatabaseConnector::disconnect(connection))

DatabaseConnector::renderTranslateExecuteSql(
connection = connection,
sql = createCohortTableSql,
Expand All @@ -38,9 +38,9 @@ test_that("Create app with cohort data in temp table", {
reportOverallTime = FALSE,
cdm_database_schema = cdmDatabaseSchema
)

outputDir <- tempfile()

outputLocation <- createCohortExplorerApp(
connection = connection,
cohortDatabaseSchema = NULL,
Expand All @@ -52,16 +52,28 @@ test_that("Create app with cohort data in temp table", {
databaseId = "databaseData",
exportFolder = outputDir
)


testthat::expect_error(
createCohortExplorerApp(
cdmDatabaseSchema = cdmDatabaseSchema,
vocabularyDatabaseSchema = vocabularyDatabaseSchema,
cohortTable = cohortTable,
cohortDefinitionId = c(1),
sampleSize = 100,
databaseId = "database Data",
exportFolder = outputDir
)
)

testthat::expect_true(file.exists(file.path(outputDir, "data")))
})


test_that("Error because database has space", {
skip_if(skipCdmTests, "CDM settings not configured")

outputDir <- tempfile()

testthat::expect_error(
createCohortExplorerApp(
connectionDetails = connectionDetails,
Expand All @@ -75,7 +87,7 @@ test_that("Error because database has space", {
exportFolder = outputDir
)
)

unlink(
x = outputDir,
recursive = TRUE,
Expand All @@ -85,21 +97,22 @@ test_that("Error because database has space", {

test_that("no connection or connection details", {
skip_if(skipCdmTests, "CDM settings not configured")

outputDir <- tempfile()

testthat::expect_error(
createCohortExplorerApp(
cdmDatabaseSchema = cdmDatabaseSchema,
cohortDatabaseSchema = cohortDatabaseSchema,
vocabularyDatabaseSchema = vocabularyDatabaseSchema,
cohortTable = cohortTable,
cohortDefinitionId = c(1),
sampleSize = 100,
databaseId = "database Data",
databaseId = "databaseIdRandom",
exportFolder = outputDir
)
)

unlink(
x = outputDir,
recursive = TRUE,
Expand All @@ -109,9 +122,9 @@ test_that("no connection or connection details", {

test_that("Cohort has no data", {
skip_if(skipCdmTests, "CDM settings not configured")

library(dplyr)

createCohortTableSql <- "
DROP TABLE IF EXISTS @cohort_database_schema.@cohort_table;
Expand All @@ -121,7 +134,7 @@ test_that("Cohort has no data", {
cohort_start_date DATE,
cohort_end_date DATE
);"

DatabaseConnector::renderTranslateExecuteSql(
connection = DatabaseConnector::connect(connectionDetails),
sql = createCohortTableSql,
Expand All @@ -131,9 +144,9 @@ test_that("Cohort has no data", {
cohort_database_schema = cohortDatabaseSchema,
cohort_table = cohortTable
)

outputDir <- tempfile()

# cohort table has no subjects
testthat::expect_warning(
createCohortExplorerApp(
Expand All @@ -148,7 +161,7 @@ test_that("Cohort has no data", {
exportFolder = outputDir
)
)

unlink(
x = outputDir,
recursive = TRUE,
Expand All @@ -159,13 +172,13 @@ test_that("Cohort has no data", {

test_that("create rand 100 in cohort", {
skip_if(skipCdmTests, "CDM settings not configured")

library(dplyr)

connection <-
DatabaseConnector::connect(connectionDetails = connectionDetails)
on.exit(DatabaseConnector::disconnect(connection))

# create a cohort with 1000 persons
createCohortTableSql <- "
DROP TABLE IF EXISTS @cohort_database_schema.@cohort_table;
Expand All @@ -189,7 +202,7 @@ test_that("create rand 100 in cohort", {
ON o1.person_id = b.person_id
WHERE new_id <= 1000
;"

DatabaseConnector::renderTranslateExecuteSql(
connection = connection,
sql = createCohortTableSql,
Expand All @@ -200,9 +213,9 @@ test_that("create rand 100 in cohort", {
cdm_database_schema = cdmDatabaseSchema,
cohort_table = cohortTable
)

outputDir <- tempfile()

createCohortExplorerApp(
connection = connection,
cohortDatabaseSchema = cohortDatabaseSchema,
Expand All @@ -214,7 +227,7 @@ test_that("create rand 100 in cohort", {
databaseId = "databaseData",
exportFolder = outputDir
)

testthat::expect_true(file.exists(file.path(outputDir)))
testthat::expect_true(file.exists(file.path(outputDir, "data")))
})
Expand All @@ -223,13 +236,13 @@ test_that("create rand 100 in cohort", {

test_that("create rand 100 in cohort with date shifting", {
skip_if(skipCdmTests, "CDM settings not configured")

library(dplyr)

connection <-
DatabaseConnector::connect(connectionDetails = connectionDetails)
on.exit(DatabaseConnector::disconnect(connection))

# create a cohort with 1000 persons
createCohortTableSql <- "
DROP TABLE IF EXISTS @cohort_database_schema.@cohort_table;
Expand All @@ -253,7 +266,7 @@ test_that("create rand 100 in cohort with date shifting", {
ON o1.person_id = b.person_id
WHERE new_id <= 1000
;"

DatabaseConnector::renderTranslateExecuteSql(
connection = connection,
sql = createCohortTableSql,
Expand All @@ -264,9 +277,9 @@ test_that("create rand 100 in cohort with date shifting", {
cdm_database_schema = cdmDatabaseSchema,
cohort_table = cohortTable
)

outputDir <- tempfile()

createCohortExplorerApp(
connection = connection,
cohortDatabaseSchema = cohortDatabaseSchema,
Expand All @@ -281,24 +294,24 @@ test_that("create rand 100 in cohort with date shifting", {
assignNewId = TRUE,
shiftDates = TRUE
)

testthat::expect_true(file.exists(file.path(outputDir)))
testthat::expect_true(file.exists(file.path(outputDir, "data")))
})


test_that("do Not Export CohortData", {
skip_if(skipCdmTests, "CDM settings not configured")

library(dplyr)

connection <-
DatabaseConnector::connect(connectionDetails = connectionDetails)
on.exit(DatabaseConnector::disconnect(connection))

createCohortTableSql <- "
DROP TABLE IF EXISTS @cohort_database_schema.@cohort_table;"

DatabaseConnector::renderTranslateExecuteSql(
connection = connection,
sql = createCohortTableSql,
Expand All @@ -319,7 +332,7 @@ test_that("do Not Export CohortData", {
databaseId = "databaseData",
exportFolder = outputDir
)

testthat::expect_true(file.exists(
file.path(
outputDir,
Expand All @@ -332,13 +345,13 @@ test_that("do Not Export CohortData", {

test_that("use cohort features", {
skip_if(skipCdmTests, "CDM settings not configured")

library(dplyr)

connection <-
DatabaseConnector::connect(connectionDetails = connectionDetails)
on.exit(DatabaseConnector::disconnect(connection))

# create a cohort with 1000 persons
createCohortTableSql <- "
DROP TABLE IF EXISTS @cohort_database_schema.@cohort_table;
Expand Down Expand Up @@ -384,12 +397,12 @@ test_that("use cohort features", {
cohort_end_date
FROM @cohort_database_schema.@cohort_table
WHERE cohort_definition_id = 1;"

featureCohortDefinitionSet <- dplyr::tibble(
cohortId = 2,
cohortName = "same cohort"
)

DatabaseConnector::renderTranslateExecuteSql(
connection = connection,
sql = createCohortTableSql,
Expand All @@ -401,9 +414,9 @@ test_that("use cohort features", {
cohort_table = cohortTable,
tempEmulationSchema = tempEmulationSchema
)

outputDir <- tempfile()

outputPath <- createCohortExplorerApp(
connection = connection,
cdmDatabaseSchema = cdmDatabaseSchema,
Expand All @@ -417,15 +430,15 @@ test_that("use cohort features", {
featureCohortTable = cohortTable,
cohortDefinitionId = 1
)

testthat::expect_true(file.exists(
file.path(
outputPath,
"data",
"CohortExplorer_1_databaseData.rds"
)
))

testthat::expect_error(
createCohortExplorerApp(
connection = connection,
Expand Down

0 comments on commit 0bc4c8e

Please sign in to comment.