Skip to content

Commit

Permalink
Update test approach and fix tempEmulationSchema bug
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonysena committed Aug 31, 2023
1 parent 5a7008d commit 5bcbd1d
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 167 deletions.
2 changes: 1 addition & 1 deletion R/Execution.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ execute <- function(analysisSpecifications,
)
DatabaseConnector::assertTempEmulationSchemaSet(
dbms = connectionDetails$dbms,
tempEmulationSchema = getOption("sqlRenderTempEmulationSchema")
tempEmulationSchema = executionSettings$tempEmulationSchema
)
}
modules <- ensureAllModulesInstantiated(analysisSpecifications)
Expand Down
3 changes: 0 additions & 3 deletions tests/testPostgres.R

This file was deleted.

3 changes: 0 additions & 3 deletions tests/testRedshift.R

This file was deleted.

3 changes: 0 additions & 3 deletions tests/testSqlServer.R

This file was deleted.

3 changes: 0 additions & 3 deletions tests/testSqlite.R

This file was deleted.

2 changes: 1 addition & 1 deletion tests/testOracle.R → tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
library(testthat)
options(dbms = "oracle")
library(Strategus)
test_check("Strategus")
196 changes: 123 additions & 73 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ library(dplyr)
baseBackend <- Sys.getenv("R_KEYRING_BACKEND")
Sys.setenv("R_KEYRING_BACKEND" = "file")

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

if (dir.exists(Sys.getenv("DATABASECONNECTOR_JAR_FOLDER"))) {
jdbcDriverFolder <- Sys.getenv("DATABASECONNECTOR_JAR_FOLDER")
} else {
jdbcDriverFolder <- "~/.jdbcDrivers"
dir.create(jdbcDriverFolder, showWarnings = FALSE)
}

if (dbms != "sqlite") {
DatabaseConnector::downloadJdbcDrivers(dbms, pathToDriver = jdbcDriverFolder)
}

withr::defer(
{
unlink(jdbcDriverFolder, recursive = TRUE, force = TRUE)
Expand All @@ -29,92 +22,149 @@ withr::defer(
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()
tempDir <- "D:" #tempfile()
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)
# },
# testthat::teardown_env()
# )

# Assemble a list of connectionDetails for the tests -----------
connectionDetailsList <- list()

# SQLite
eunomiaDbFile <- file.path(tempDir, "data", "testEunomia.sqlite")
if (!dir.exists(file.path(tempDir, "data"))) {
dir.create(file.path(tempDir, "data"), recursive = T, showWarnings = F)
}
connectionDetails <- Eunomia::getEunomiaConnectionDetails(
databaseFile = eunomiaDbFile
)
withr::defer(
{
unlink(c(tempDir, renvCachePath, moduleFolder), recursive = TRUE, force = TRUE)
unlink(eunomiaDbFile, recursive = TRUE, force = TRUE)
},
testthat::teardown_env()
)
cdmDatabaseSchema <- "main"
workDatabaseSchema <- "main"
vocabularyDatabaseSchema <- workDatabaseSchema
cohortTable <- "cohort"
tempEmulationSchema <- NULL

if (dbms == "sqlite") {
eunomiaDbFile <- file.path(tempDir, "data", "testEunomia.sqlite")
if (!dir.exists(file.path(tempDir, "data"))) {
dir.create(file.path(tempDir, "data"), recursive = T, showWarnings = F)
}
connectionDetails <- Eunomia::getEunomiaConnectionDetails(
databaseFile = eunomiaDbFile
connectionDetailsList[[length(connectionDetailsList) + 1]] <- list(
connectionDetails = connectionDetails,
cdmDatabaseSchema = "main",
workDatabaseSchema = "main",
vocabularyDatabaseSchema = "main",
cohortTable = "cohort",
tempEmulationSchema = NULL
)

# PostgreSQL
if (!(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") == "")) {
DatabaseConnector::downloadJdbcDrivers("postgresql")
connectionDetailsList[[length(connectionDetailsList) + 1]] <- list(
connectionDetails = DatabaseConnector::createConnectionDetails(
dbms = "postgresql",
user = Sys.getenv("CDM5_POSTGRESQL_USER"),
password = URLdecode(Sys.getenv("CDM5_POSTGRESQL_PASSWORD")),
server = Sys.getenv("CDM5_POSTGRESQL_SERVER"),
port = 5432,
pathToDriver = jdbcDriverFolder
),
cdmDatabaseSchema = Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA"),
workDatabaseSchema = Sys.getenv("CDM5_POSTGRESQL_OHDSI_SCHEMA"),
vocabularyDatabaseSchema = Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA"),
cohortTable = "cohort",
tempEmulationSchema = NULL
)
withr::defer(
{
unlink(eunomiaDbFile, recursive = TRUE, force = TRUE)
},
testthat::teardown_env()
}

# Oracle
if (!(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") == "")) {
DatabaseConnector::downloadJdbcDrivers("oracle")
connectionDetailsList[[length(connectionDetailsList) + 1]] <- list(
connectionDetails = DatabaseConnector::createConnectionDetails(
dbms = "oracle",
user = Sys.getenv("CDM5_ORACLE_USER"),
password = URLdecode(Sys.getenv("CDM5_ORACLE_PASSWORD")),
server = Sys.getenv("CDM5_ORACLE_SERVER"),
port = 1521,
pathToDriver = jdbcDriverFolder
),
cdmDatabaseSchema = Sys.getenv("CDM5_ORACLE_CDM_SCHEMA"),
workDatabaseSchema = Sys.getenv("CDM5_ORACLE_OHDSI_SCHEMA"),
vocabularyDatabaseSchema = Sys.getenv("CDM5_ORACLE_CDM_SCHEMA"),
cohortTable = "cohort",
tempEmulationSchema = Sys.getenv("CDM5_ORACLE_OHDSI_SCHEMA")
)
cdmDatabaseSchema <- "main"
workDatabaseSchema <- "main"
vocabularyDatabaseSchema <- workDatabaseSchema
cohortTable <- "cohort"
tempEmulationSchema <- NULL
} else {
if (dbms == "postgresql") {
dbUser <- Sys.getenv("CDM5_POSTGRESQL_USER")
dbPassword <- Sys.getenv("CDM5_POSTGRESQL_PASSWORD")
dbServer <- Sys.getenv("CDM5_POSTGRESQL_SERVER")
dbPort <- 5432
cdmDatabaseSchema <- Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA")
vocabularyDatabaseSchema <- Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA")
tempEmulationSchema <- NULL
workDatabaseSchema <- Sys.getenv("CDM5_POSTGRESQL_OHDSI_SCHEMA")
} else if (dbms == "oracle") {
dbUser <- Sys.getenv("CDM5_ORACLE_USER")
dbPassword <- Sys.getenv("CDM5_ORACLE_PASSWORD")
dbServer <- Sys.getenv("CDM5_ORACLE_SERVER")
dbPort <- 1521
cdmDatabaseSchema <- Sys.getenv("CDM5_ORACLE_CDM_SCHEMA")
vocabularyDatabaseSchema <- Sys.getenv("CDM5_ORACLE_CDM_SCHEMA")
tempEmulationSchema <- Sys.getenv("CDM5_ORACLE_OHDSI_SCHEMA")
workDatabaseSchema <- Sys.getenv("CDM5_ORACLE_OHDSI_SCHEMA")
options(sqlRenderTempEmulationSchema = tempEmulationSchema)
} else if (dbms == "redshift") {
dbUser <- Sys.getenv("CDM5_REDSHIFT_USER")
dbPassword <- Sys.getenv("CDM5_REDSHIFT_PASSWORD")
dbServer <- Sys.getenv("CDM5_REDSHIFT_SERVER")
dbPort <- 5439
cdmDatabaseSchema <- Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA")
vocabularyDatabaseSchema <- Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA")
tempEmulationSchema <- NULL
workDatabaseSchema <- Sys.getenv("CDM5_REDSHIFT_OHDSI_SCHEMA")
} else if (dbms == "sql server") {
dbUser <- Sys.getenv("CDM5_SQL_SERVER_USER")
dbPassword <- Sys.getenv("CDM5_SQL_SERVER_PASSWORD")
dbServer <- Sys.getenv("CDM5_SQL_SERVER_SERVER")
dbPort <- 1433
cdmDatabaseSchema <- Sys.getenv("CDM5_SQL_SERVER_CDM_SCHEMA")
vocabularyDatabaseSchema <- Sys.getenv("CDM5_SQL_SERVER_CDM_SCHEMA")
tempEmulationSchema <- NULL
workDatabaseSchema <- Sys.getenv("CDM5_SQL_SERVER_OHDSI_SCHEMA")
}
}

connectionDetails <- DatabaseConnector::createConnectionDetails(
dbms = dbms,
user = dbUser,
password = URLdecode(dbPassword),
server = dbServer,
port = dbPort,
pathToDriver = jdbcDriverFolder
# 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 = jdbcDriverFolder
),
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") == ""
& 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") == "")) {
DatabaseConnector::downloadJdbcDrivers("sql server")
connectionDetailsList[[length(connectionDetailsList) + 1]] <- list(
connectionDetails = DatabaseConnector::createConnectionDetails(
dbms = "sql server",
user = Sys.getenv("CDM5_SQL_SERVER_USER"),
password = URLdecode(Sys.getenv("CDM5_SQL_SERVER_PASSWORD")),
server = Sys.getenv("CDM5_SQL_SERVER_SERVER"),
port = 1433,
pathToDriver = jdbcDriverFolder
),
cdmDatabaseSchema = Sys.getenv("CDM5_SQL_SERVER_CDM_SCHEMA"),
workDatabaseSchema = Sys.getenv("CDM5_SQL_SERVER_OHDSI_SCHEMA"),
vocabularyDatabaseSchema = Sys.getenv("CDM5_SQL_SERVER_CDM_SCHEMA"),
cohortTable = "cohort",
tempEmulationSchema = NULL
)
}

# Keyring helpers --------------
# Set the keyring name & password for testing
Expand Down
36 changes: 19 additions & 17 deletions tests/testthat/test-DatabaseMetaData.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
test_that("Test DatabaseMetaData error conditions", {
skip_if_not(dbms == "sqlite")
skip_if_not_secret_service()
# Run this test in isolation as it will make changes to the CDM schema.
eunomiaConnectionDetails <- Eunomia::getEunomiaConnectionDetails()
Expand All @@ -18,22 +17,25 @@ test_that("Test DatabaseMetaData error conditions", {
)
}

on.exit(expr = {
# Put the DB back the way we found it
restoreTableSql <- "DROP TABLE IF EXISTS @cdm_table; ALTER TABLE @backup_table RENAME TO @cdm_table;"
for (i in 1:length(requiredTables)) {
DatabaseConnector::renderTranslateExecuteSql(
connection = connection,
sql = restoreTableSql,
progressBar = FALSE,
reportOverallTime = FALSE,
backup_table = paste0(requiredTables[i], "_bak"),
cdm_table = requiredTables[i]
)
}
DatabaseConnector::disconnect(connection)
unlink(eunomiaConnectionDetails$server, recursive = TRUE, force = TRUE)
})
withr::defer(
{
# Put the DB back the way we found it
restoreTableSql <- "DROP TABLE IF EXISTS @cdm_table; ALTER TABLE @backup_table RENAME TO @cdm_table;"
for (i in 1:length(requiredTables)) {
DatabaseConnector::renderTranslateExecuteSql(
connection = connection,
sql = restoreTableSql,
progressBar = FALSE,
reportOverallTime = FALSE,
backup_table = paste0(requiredTables[i], "_bak"),
cdm_table = requiredTables[i]
)
}
DatabaseConnector::disconnect(connection)
unlink(eunomiaConnectionDetails$server, recursive = TRUE, force = TRUE)
},
testthat::teardown_env()
)

# Setup keyring for the test
Sys.setenv("STRATEGUS_KEYRING_PASSWORD" = keyringPassword)
Expand Down
51 changes: 29 additions & 22 deletions tests/testthat/test-Settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,36 @@ test_that("Store and retrieve connection details", {
createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword)
on.exit(deleteKeyringForUnitTest())

# Verify that the connection details are valid
# by connecting to the DB
conn <- DatabaseConnector::connect(
connectionDetails
)
on.exit(DatabaseConnector::disconnect(conn))
for (i in 1:length(connectionDetailsList)) {
connectionDetails <- connectionDetailsList[[i]]$connectionDetails
dbms <- connectionDetailsList[[i]]$connectionDetails$dbms

# Store the connection details in keyring
storeConnectionDetails(
connectionDetails = connectionDetails,
connectionDetailsReference = dbms,
keyringName = keyringName
)
message("************* Store connection details for ", dbms, " *************")

connectionDetailsFromKeyring <- retrieveConnectionDetails(
connectionDetailsReference = dbms,
keyringName = keyringName
)
# Verify that the connection details are valid
# by connecting to the DB
conn <- DatabaseConnector::connect(
connectionDetails
)
DatabaseConnector::disconnect(conn)

# Verify that the connection details retrieved
# allow for a connection to the DB
connFromKeyring <- DatabaseConnector::connect(
connectionDetailsFromKeyring
)
on.exit(DatabaseConnector::disconnect(connFromKeyring))
# Store the connection details in keyring
storeConnectionDetails(
connectionDetails = connectionDetails,
connectionDetailsReference = dbms,
keyringName = keyringName
)

connectionDetailsFromKeyring <- retrieveConnectionDetails(
connectionDetailsReference = dbms,
keyringName = keyringName
)

# Verify that the connection details retrieved
# allow for a connection to the DB
connFromKeyring <- DatabaseConnector::connect(
connectionDetailsFromKeyring
)
DatabaseConnector::disconnect(connFromKeyring)
}
})
Loading

0 comments on commit 5bcbd1d

Please sign in to comment.