Skip to content

Commit

Permalink
Restore RedShift unit tests (#109)
Browse files Browse the repository at this point in the history
* Update DatabaseConnector ref in test module; provide env var for test module location
  • Loading branch information
anthonysena authored Dec 19, 2023
1 parent 28bacf4 commit db8c2ce
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/R_CMD_check_Hades.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- {os: macOS-latest, r: '4.2.3', rtools: '42', rspm: "https://cloud.r-project.org"}
- {os: ubuntu-20.04, r: '4.2.3', rtools: '42', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: windows-latest, r: 'release', rtools: '', rspm: "https://cloud.r-project.org"}
#- {os: macOS-latest, r: 'release', rtools: '', rspm: "https://cloud.r-project.org"}
- {os: macOS-latest, r: 'release', rtools: '', rspm: "https://cloud.r-project.org"}
- {os: ubuntu-20.04, r: 'release', rtools: '', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}

env:
Expand Down
6 changes: 0 additions & 6 deletions R/ModuleInstantiation.R
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,6 @@ instantiateModule <- function(module, version, remoteRepo, remoteUsername, modul

withModuleRenv(
code = {
ParallelLogger::addDefaultFileLogger(
fileName = file.path(moduleFolder, "moduleInitLog.txt")
)
ParallelLogger::addDefaultErrorReportLogger(
fileName = file.path(moduleFolder, "moduleInitErrorReport.txt")
)
renv::restore(prompt = FALSE)
},
moduleFolder = moduleFolder,
Expand Down
9 changes: 7 additions & 2 deletions extras/TestModule1-0.0.1/renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
},
"DatabaseConnector": {
"Package": "DatabaseConnector",
"Version": "6.2.3",
"Version": "6.3.2",
"Source": "Repository",
"Repository": "RSPM",
"Requirements": [
Expand Down Expand Up @@ -185,7 +185,7 @@
},
"SqlRender": {
"Package": "SqlRender",
"Version": "1.15.1",
"Version": "1.16.1",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
Expand Down Expand Up @@ -513,6 +513,11 @@
],
"Hash": "a4269a09a9b865579b2635c77e572374"
},
"keyring": {
"Package": "keyring",
"Version": "1.3.2",
"Source": "Repository"
},
"later": {
"Package": "later",
"Version": "1.3.0",
Expand Down
Binary file modified inst/testdata/TestModule1_0.0.1.zip
Binary file not shown.
74 changes: 39 additions & 35 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,37 @@ withr::defer(
testthat::teardown_env()
)

if (Sys.getenv("DONT_DOWNLOAD_JDBC_DRIVERS", "") != "TRUE") {
oldJarFolder <- Sys.getenv("DATABASECONNECTOR_JAR_FOLDER")
Sys.setenv("DATABASECONNECTOR_JAR_FOLDER" = tempfile("jdbcDrivers"))
dir.create(Sys.getenv("DATABASECONNECTOR_JAR_FOLDER"))

if (testthat::is_testing()) {
withr::defer({
unlink(Sys.getenv("DATABASECONNECTOR_JAR_FOLDER"), recursive = TRUE, force = TRUE)
Sys.setenv("DATABASECONNECTOR_JAR_FOLDER" = oldJarFolder)
if (dir.exists(Sys.getenv("DATABASECONNECTOR_JAR_FOLDER"))) {
jdbcDriverFolder <- Sys.getenv("DATABASECONNECTOR_JAR_FOLDER")
} else {
jdbcDriverFolder <- "~/jdbcDrivers"
dir.create(jdbcDriverFolder, showWarnings = FALSE)
baseDatabaseConnectorJarFolder <- Sys.getenv("DATABASECONNECTOR_JAR_FOLDER")
Sys.setenv("DATABASECONNECTOR_JAR_FOLDER" = jdbcDriverFolder)
withr::defer(
{
unlink(jdbcDriverFolder, recursive = TRUE, force = TRUE)
Sys.setenv("DATABASECONNECTOR_JAR_FOLDER" = baseDatabaseConnectorJarFolder)
},
testthat::teardown_env()
)
}
)
}

# Create a unique ID for the table identifiers
tableSuffix <- paste0(substr(.Platform$OS.type, 1, 3), format(Sys.time(), "%y%m%d%H%M%S"), sample(1:100, 1))
tableSuffix <- abs(digest::digest2int(tableSuffix))

tempDir <- tempfile()
usingTempDir <- Sys.getenv("STRATEGUS_UNIT_TEST_FOLDER") == ""
tempDir <- ifelse(usingTempDir, tempfile(), Sys.getenv("STRATEGUS_UNIT_TEST_FOLDER"))
tempDir <- gsub("\\\\", "/", tempDir) # Correct windows path
renvCachePath <- file.path(tempDir, "strategus/renv")
moduleFolder <- file.path(tempDir, "strategus/modules")
Sys.setenv("INSTANTIATED_MODULES_FOLDER" = moduleFolder)
withr::defer(
{
unlink(c(tempDir, renvCachePath, moduleFolder), recursive = TRUE, force = TRUE)
if (usingTempDir) {
unlink(c(tempDir, renvCachePath, moduleFolder), recursive = TRUE, force = TRUE)
}
},
testthat::teardown_env()
)
Expand Down Expand Up @@ -125,28 +129,28 @@ if (!(Sys.getenv("CDM5_ORACLE_USER") == "" &
}

# RedShift
# if (!(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") == "")) {
# DatabaseConnector::downloadJdbcDrivers("redshift")
# connectionDetailsList[[length(connectionDetailsList) + 1]] <- list(
# connectionDetails = DatabaseConnector::createConnectionDetails(
# dbms = "redshift",
# user = Sys.getenv("CDM5_REDSHIFT_USER"),
# password = URLdecode(Sys.getenv("CDM5_REDSHIFT_PASSWORD")),
# server = Sys.getenv("CDM5_REDSHIFT_SERVER"),
# port = 5439,
# pathToDriver = Sys.getenv("DATABASECONNECTOR_JAR_FOLDER")
# ),
# cdmDatabaseSchema = Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA"),
# workDatabaseSchema = Sys.getenv("CDM5_REDSHIFT_OHDSI_SCHEMA"),
# vocabularyDatabaseSchema = Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA"),
# cohortTable = "cohort",
# tempEmulationSchema = NULL
# )
# }
if (!(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") == "")) {
DatabaseConnector::downloadJdbcDrivers("redshift")
connectionDetailsList[[length(connectionDetailsList) + 1]] <- list(
connectionDetails = DatabaseConnector::createConnectionDetails(
dbms = "redshift",
user = Sys.getenv("CDM5_REDSHIFT_USER"),
password = URLdecode(Sys.getenv("CDM5_REDSHIFT_PASSWORD")),
server = Sys.getenv("CDM5_REDSHIFT_SERVER"),
port = 5439,
pathToDriver = Sys.getenv("DATABASECONNECTOR_JAR_FOLDER")
),
cdmDatabaseSchema = Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA"),
workDatabaseSchema = Sys.getenv("CDM5_REDSHIFT_OHDSI_SCHEMA"),
vocabularyDatabaseSchema = Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA"),
cohortTable = "cohort",
tempEmulationSchema = NULL
)
}

# SQL Server
if (!(Sys.getenv("CDM5_SQL_SERVER_USER") == "" &
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-Strategus.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ test_that("Run unit test study", {

withr::defer(
{
unlink(file.path(tempDir, "EunomiaTestStudy"), recursive = TRUE, force = TRUE)
if (usingTempDir) {
unlink(file.path(tempDir, "EunomiaTestStudy"), recursive = TRUE, force = TRUE)
}
},
testthat::teardown_env()
)
Expand Down

0 comments on commit db8c2ce

Please sign in to comment.