Skip to content

Commit

Permalink
Merge pull request #221 from OHDSI/unitTests
Browse files Browse the repository at this point in the history
Modify unit tests structure
  • Loading branch information
ginberg authored Jan 17, 2024
2 parents 8b0e7d7 + 145e97f commit fc74301
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 171 deletions.
6 changes: 6 additions & 0 deletions tests/testOracle.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library(testthat)
options(dbms = "oracle")
runTestsOnOracle <- !(Sys.getenv("CDM5_ORACLE_USER") == "" & Sys.getenv("CDM5_ORACLE_PASSWORD") == "" & Sys.getenv("CDM5_ORACLE_SERVER") == "" & Sys.getenv("CDM5_ORACLE_CDM_SCHEMA") == "" & Sys.getenv("CDM5_ORACLE_OHDSI_SCHEMA") == "")
if (runTestsOnOracle) {
test_check("FeatureExtraction")
}
6 changes: 6 additions & 0 deletions tests/testPostgres.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library(testthat)
options(dbms = "postgresql")
runTestsOnPostgreSQL <- !(Sys.getenv("CDM5_POSTGRESQL_USER") == "" & Sys.getenv("CDM5_POSTGRESQL_PASSWORD") == "" & Sys.getenv("CDM5_POSTGRESQL_SERVER") == "" & Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA") == "" & Sys.getenv("CDM5_POSTGRESQL_OHDSI_SCHEMA") == "")
if (runTestsOnPostgreSQL) {
test_check("FeatureExtraction")
}
6 changes: 6 additions & 0 deletions tests/testRedshift.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library(testthat)
options(dbms = "redshift")
runTestsOnRedshift <- FALSE #!(Sys.getenv("CDM5_REDSHIFT_USER") == "" & Sys.getenv("CDM5_REDSHIFT_PASSWORD") == "" & Sys.getenv("CDM5_REDSHIFT_SERVER") == "" & Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA") == "" & Sys.getenv("CDM5_REDSHIFT_OHDSI_SCHEMA") == "")
if (runTestsOnRedshift) {
test_check("FeatureExtraction")
}
6 changes: 6 additions & 0 deletions tests/testSqlServer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library(testthat)
options(dbms = "sql server")
runTestsOnSQLServer <- !(Sys.getenv("CDM5_SQL_SERVER_USER") == "" & Sys.getenv("CDM5_SQL_SERVER_PASSWORD") == "" & Sys.getenv("CDM5_SQL_SERVER_SERVER") == "" & Sys.getenv("CDM5_SQL_SERVER_CDM_SCHEMA") == "" & Sys.getenv("CDM5_SQL_SERVER_OHDSI_SCHEMA") == "")
if (runTestsOnSQLServer) {
test_check("FeatureExtraction")
}
6 changes: 6 additions & 0 deletions tests/testSqlite.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library(testthat)
options(dbms = "sqlite")
runTestsOnEunomia <- TRUE
if (runTestsOnEunomia) {
test_check("FeatureExtraction")
}
3 changes: 0 additions & 3 deletions tests/testthat.R

This file was deleted.

36 changes: 8 additions & 28 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ library(testthat)
library(FeatureExtraction)
library(dplyr)

dbms <- getOption("dbms", default = "sqlite")
message("************* Testing on ", dbms, " *************\n")

# Unit Test Settings ------
# Set a directory for the JDBC drivers used in the tests
oldJarFolder <- Sys.getenv("DATABASECONNECTOR_JAR_FOLDER")
Expand All @@ -17,15 +20,6 @@ withr::defer(
testthat::teardown_env()
)


# Get all environment variables to determine which DBMS to use for testing
runTestsOnPostgreSQL <- !(Sys.getenv("CDM5_POSTGRESQL_USER") == "" & Sys.getenv("CDM5_POSTGRESQL_PASSWORD") == "" & Sys.getenv("CDM5_POSTGRESQL_SERVER") == "" & Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA") == "" & Sys.getenv("CDM5_POSTGRESQL_OHDSI_SCHEMA") == "")
runTestsOnSQLServer <- !(Sys.getenv("CDM5_SQL_SERVER_USER") == "" & Sys.getenv("CDM5_SQL_SERVER_PASSWORD") == "" & Sys.getenv("CDM5_SQL_SERVER_SERVER") == "" & Sys.getenv("CDM5_SQL_SERVER_CDM_SCHEMA") == "" & Sys.getenv("CDM5_SQL_SERVER_OHDSI_SCHEMA") == "")
runTestsOnOracle <- !(Sys.getenv("CDM5_ORACLE_USER") == "" & Sys.getenv("CDM5_ORACLE_PASSWORD") == "" & Sys.getenv("CDM5_ORACLE_SERVER") == "" & Sys.getenv("CDM5_ORACLE_CDM_SCHEMA") == "" & Sys.getenv("CDM5_ORACLE_OHDSI_SCHEMA") == "")
runTestsOnImpala <- !(Sys.getenv("CDM5_IMPALA_USER") == "" & Sys.getenv("CDM5_IMPALA_PASSWORD") == "" & Sys.getenv("CDM5_IMPALA_SERVER") == "" & Sys.getenv("CDM5_IMPALA_CDM_SCHEMA") == "" & Sys.getenv("CDM5_IMPALA_OHDSI_SCHEMA") == "")
runTestsOnRedshift <- !(Sys.getenv("CDM5_REDSHIFT_USER") == "" & Sys.getenv("CDM5_REDSHIFT_PASSWORD") == "" & Sys.getenv("CDM5_REDSHIFT_SERVER") == "" & Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA") == "" & Sys.getenv("CDM5_REDSHIFT_OHDSI_SCHEMA") == "")
runTestsOnEunomia <- TRUE

# The cohort table is a temp table but uses the same platform/datetime suffix to avoid collisions when running
# tests in parallel
tableSuffix <- paste0(substr(.Platform$OS.type, 1, 3), format(Sys.time(), "%y%m%d%H%M%S"), sample(1:100, 1))
Expand Down Expand Up @@ -81,7 +75,7 @@ dropUnitTestData <- function(connection, ohdsiDatabaseSchema, cohortTable, cohor

# Database Test Settings -----------
# postgres
if (runTestsOnPostgreSQL) {
if (dbms == "postgresql") {
DatabaseConnector::downloadJdbcDrivers("postgresql")
pgConnectionDetails <- createConnectionDetails(
dbms = "postgresql",
Expand All @@ -94,7 +88,7 @@ if (runTestsOnPostgreSQL) {
}

# sql server
if (runTestsOnSQLServer) {
if (dbms == "sql server") {
DatabaseConnector::downloadJdbcDrivers("sql server")
sqlServerConnectionDetails <- createConnectionDetails(
dbms = "sql server",
Expand All @@ -107,7 +101,7 @@ if (runTestsOnSQLServer) {
}

# oracle
if (runTestsOnOracle) {
if (dbms == "oracle") {
DatabaseConnector::downloadJdbcDrivers("oracle")
oracleConnectionDetails <- createConnectionDetails(
dbms = "oracle",
Expand All @@ -121,22 +115,8 @@ if (runTestsOnOracle) {
options(sqlRenderTempEmulationSchema = oracleOhdsiDatabaseSchema)
}

# impala
if (runTestsOnImpala) {
# NOTE: Driver for IMPALA requires manual installation
impalaConnectionDetails <- createConnectionDetails(
dbms = "impala",
user = Sys.getenv("CDM5_IMPALA_USER"),
password = URLdecode(Sys.getenv("CDM5_IMPALA_PASSWORD")),
server = Sys.getenv("CDM5_IMPALA_SERVER"),
pathToDriver = Sys.getenv("CDM5_IMPALA_PATH_TO_DRIVER")
)
impalaCdmDatabaseSchema <- Sys.getenv("CDM5_IMPALA_CDM_SCHEMA")
impalaOhdsiDatabaseSchema <- Sys.getenv("CDM5_IMPALA_OHDSI_SCHEMA")
}

# redshift
if (runTestsOnRedshift) {
if (dbms == "redshift") {
DatabaseConnector::downloadJdbcDrivers("redshift")
redshiftConnectionDetails <- createConnectionDetails(
dbms = "redshift",
Expand All @@ -149,7 +129,7 @@ if (runTestsOnRedshift) {
}

# eunomia
if (runTestsOnEunomia) {
if (dbms == "sqlite") {
eunomiaConnectionDetails <- Eunomia::getEunomiaConnectionDetails(databaseFile = "testEunomia.sqlite")
eunomiaCdmDatabaseSchema <- "main"
eunomiaOhdsiDatabaseSchema <- "main"
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-Aggregation.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# covr::file_report(covr::file_coverage("R/Aggregation.R", "tests/testthat/test-Aggregation.R"))

test_that("aggregateCovariates works", {
skip_if_not(runTestsOnEunomia)
skip_if_not(dbms == "sqlite")
settings <- createCovariateSettings(useDemographicsAgeGroup = TRUE, useChads2Vasc = TRUE)
covariateData <- getDbCovariateData(connectionDetails = eunomiaConnectionDetails,
cdmDatabaseSchema = eunomiaCdmDatabaseSchema,
Expand All @@ -27,7 +27,7 @@ test_that("aggregateCovariates works", {
})

test_that("aggregateCovariates handles temporalCovariates", {
skip_if_not(runTestsOnEunomia)
skip_if_not(dbms == "sqlite")
settings <- createTemporalCovariateSettings(useDemographicsGender = TRUE)
covariateData <- getDbCovariateData(connectionDetails = eunomiaConnectionDetails,
cdmDatabaseSchema = eunomiaCdmDatabaseSchema,
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-CovariateData.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# covr::file_report(covr::file_coverage("R/CovariateData.R", "tests/testthat/test-CovariateData.R"))

test_that("test CovariateData Class on Empty", {
skip_if_not(runTestsOnEunomia)
skip_if_not(dbms == "sqlite")
# create 4 scenarios of Covariate Data
# 1) error (non class), 2) covariate data, 3) aggregatedCovariate Data,
# 4) temporalCovariate Data
Expand Down Expand Up @@ -46,7 +46,7 @@ test_that("test CovariateData Class on Empty", {
})

test_that("test saveCovariateData error cases", {
skip_if_not(runTestsOnEunomia)
skip_if_not(dbms == "sqlite")
saveFileTest <- tempfile("covDatSave")
settings <- createDefaultCovariateSettings()
covariateData <- getDbCovariateData(connectionDetails = eunomiaConnectionDetails,
Expand All @@ -70,7 +70,7 @@ test_that("test saveCovariateData error cases", {
})

test_that("test summary call for covariateData class", {
skip_if_not(runTestsOnEunomia)
skip_if_not(dbms == "sqlite")
settings <- createDefaultCovariateSettings()
covariateData <- getDbCovariateData(connectionDetails = eunomiaConnectionDetails,
cdmDatabaseSchema = eunomiaCdmDatabaseSchema,
Expand Down Expand Up @@ -110,7 +110,7 @@ test_that("Test show method", {
on.exit(rm(cvData))
})
test_that("getDbCovariateData cohortId warning", {
skip_if_not(runTestsOnEunomia)
skip_if_not(dbms == "sqlite")
settings <- createDefaultCovariateSettings()
expect_warning(getDbCovariateData(connectionDetails = eunomiaConnectionDetails,
cdmDatabaseSchema = eunomiaCdmDatabaseSchema,
Expand Down
Loading

0 comments on commit fc74301

Please sign in to comment.