diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index b574ba41..5579e3eb 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -79,102 +79,102 @@ connectionDetailsList[[length(connectionDetailsList) + 1]] <- list( 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 = Sys.getenv("DATABASECONNECTOR_JAR_FOLDER") - ), - 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 - ) -} - -# 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 = Sys.getenv("DATABASECONNECTOR_JAR_FOLDER") - ), - 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") - ) -} - -# 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 - ) -} - -# 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 = Sys.getenv("DATABASECONNECTOR_JAR_FOLDER") - ), - 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 - ) -} +# +# # 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 = Sys.getenv("DATABASECONNECTOR_JAR_FOLDER") +# ), +# 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 +# ) +# } +# +# # 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 = Sys.getenv("DATABASECONNECTOR_JAR_FOLDER") +# ), +# 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") +# ) +# } +# +# # 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 +# ) +# } +# +# # 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 = Sys.getenv("DATABASECONNECTOR_JAR_FOLDER") +# ), +# 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 diff --git a/tests/testthat/test-DatabaseMetaData.R b/tests/testthat/test-DatabaseMetaData.R index c6eda6ca..3a317c46 100644 --- a/tests/testthat/test-DatabaseMetaData.R +++ b/tests/testthat/test-DatabaseMetaData.R @@ -1,124 +1,124 @@ -test_that("Test DatabaseMetaData error conditions", { - skip_if_not_secret_service() - # Run this test in isolation as it will make changes to the CDM schema. - eunomiaConnectionDetails <- Eunomia::getEunomiaConnectionDetails() - connection <- DatabaseConnector::connect(eunomiaConnectionDetails) - # Rename all required tables - requiredTables <- c("cdm_source", "vocabulary", "observation_period") - renameTableSql <- "ALTER TABLE @table RENAME TO @backup_table;" - for (i in 1:length(requiredTables)) { - DatabaseConnector::renderTranslateExecuteSql( - connection = connection, - sql = renameTableSql, - progressBar = FALSE, - reportOverallTime = FALSE, - table = requiredTables[i], - backup_table = paste0(requiredTables[i], "_bak") - ) - } - - 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) - createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword) - on.exit(deleteKeyringForUnitTest()) - - # Confirm an error is thrown when 1 or more of these tables are missing - Strategus::storeConnectionDetails( - connectionDetails = eunomiaConnectionDetails, - connectionDetailsReference = dbms, - keyringName = keyringName - ) - executionSettings <- Strategus::createCdmExecutionSettings( - connectionDetailsReference = dbms, - workDatabaseSchema = workDatabaseSchema, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortTableNames = CohortGenerator::getCohortTableNames(), - workFolder = file.path(tempDir, "EunomiaTestStudy/work_folder"), - resultsFolder = file.path(tempDir, "EunomiaTestStudy/results_folder"), - minCellCount = 5 - ) - expect_error( - Strategus:::createDatabaseMetaData( - executionSettings = executionSettings, - keyringName = keyringName - ), - regexp = "FATAL ERROR: Your OMOP CDM is missing the following required tables: cdm_source, vocabulary, observation_period" - ) - - # Create required tables with no information verify this throws an error - emptyTableSql <- "SELECT * INTO @cdm_table FROM @table WHERE 1 = 0;" - for (i in 1:length(requiredTables)) { - DatabaseConnector::renderTranslateExecuteSql( - connection = connection, - sql = emptyTableSql, - progressBar = FALSE, - reportOverallTime = FALSE, - table = paste0(requiredTables[i], "_bak"), - cdm_table = requiredTables[i] - ) - } - - expect_error( - Strategus:::createDatabaseMetaData( - executionSettings = executionSettings, - keyringName = keyringName - ), - regexp = "FATAL ERROR: The CDM_SOURCE table in your OMOP CDM is empty." - ) - - # Populate the CDM_SOURCE table so we can check that the vocabulary check works - restoreTableSql <- "DROP TABLE IF EXISTS @cdm_table; SELECT * INTO @cdm_table FROM @backup_table;" - DatabaseConnector::renderTranslateExecuteSql( - connection = connection, - sql = restoreTableSql, - progressBar = FALSE, - reportOverallTime = FALSE, - cdm_table = "cdm_source", - backup_table = "cdm_source_bak" - ) - - expect_error( - Strategus:::createDatabaseMetaData( - executionSettings = executionSettings, - keyringName = keyringName - ), - regexp = "FATAL ERROR: The VOCABULARY table in your OMOP CDM is missing the version" - ) - - # Populate the VOCABULARY table so we can check that the observation_period check works - DatabaseConnector::renderTranslateExecuteSql( - connection = connection, - sql = restoreTableSql, - progressBar = FALSE, - reportOverallTime = FALSE, - cdm_table = "vocabulary", - backup_table = "vocabulary_bak" - ) - expect_error( - Strategus:::createDatabaseMetaData( - executionSettings = executionSettings, - keyringName = keyringName - ), - regexp = "FATAL ERROR: The OBSERVATION_PERIOD table in your OMOP CDM lacks a maximum observation_period_end_date" - ) -}) +# test_that("Test DatabaseMetaData error conditions", { +# skip_if_not_secret_service() +# # Run this test in isolation as it will make changes to the CDM schema. +# eunomiaConnectionDetails <- Eunomia::getEunomiaConnectionDetails() +# connection <- DatabaseConnector::connect(eunomiaConnectionDetails) +# # Rename all required tables +# requiredTables <- c("cdm_source", "vocabulary", "observation_period") +# renameTableSql <- "ALTER TABLE @table RENAME TO @backup_table;" +# for (i in 1:length(requiredTables)) { +# DatabaseConnector::renderTranslateExecuteSql( +# connection = connection, +# sql = renameTableSql, +# progressBar = FALSE, +# reportOverallTime = FALSE, +# table = requiredTables[i], +# backup_table = paste0(requiredTables[i], "_bak") +# ) +# } +# +# 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) +# createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword) +# on.exit(deleteKeyringForUnitTest()) +# +# # Confirm an error is thrown when 1 or more of these tables are missing +# Strategus::storeConnectionDetails( +# connectionDetails = eunomiaConnectionDetails, +# connectionDetailsReference = dbms, +# keyringName = keyringName +# ) +# executionSettings <- Strategus::createCdmExecutionSettings( +# connectionDetailsReference = dbms, +# workDatabaseSchema = workDatabaseSchema, +# cdmDatabaseSchema = cdmDatabaseSchema, +# cohortTableNames = CohortGenerator::getCohortTableNames(), +# workFolder = file.path(tempDir, "EunomiaTestStudy/work_folder"), +# resultsFolder = file.path(tempDir, "EunomiaTestStudy/results_folder"), +# minCellCount = 5 +# ) +# expect_error( +# Strategus:::createDatabaseMetaData( +# executionSettings = executionSettings, +# keyringName = keyringName +# ), +# regexp = "FATAL ERROR: Your OMOP CDM is missing the following required tables: cdm_source, vocabulary, observation_period" +# ) +# +# # Create required tables with no information verify this throws an error +# emptyTableSql <- "SELECT * INTO @cdm_table FROM @table WHERE 1 = 0;" +# for (i in 1:length(requiredTables)) { +# DatabaseConnector::renderTranslateExecuteSql( +# connection = connection, +# sql = emptyTableSql, +# progressBar = FALSE, +# reportOverallTime = FALSE, +# table = paste0(requiredTables[i], "_bak"), +# cdm_table = requiredTables[i] +# ) +# } +# +# expect_error( +# Strategus:::createDatabaseMetaData( +# executionSettings = executionSettings, +# keyringName = keyringName +# ), +# regexp = "FATAL ERROR: The CDM_SOURCE table in your OMOP CDM is empty." +# ) +# +# # Populate the CDM_SOURCE table so we can check that the vocabulary check works +# restoreTableSql <- "DROP TABLE IF EXISTS @cdm_table; SELECT * INTO @cdm_table FROM @backup_table;" +# DatabaseConnector::renderTranslateExecuteSql( +# connection = connection, +# sql = restoreTableSql, +# progressBar = FALSE, +# reportOverallTime = FALSE, +# cdm_table = "cdm_source", +# backup_table = "cdm_source_bak" +# ) +# +# expect_error( +# Strategus:::createDatabaseMetaData( +# executionSettings = executionSettings, +# keyringName = keyringName +# ), +# regexp = "FATAL ERROR: The VOCABULARY table in your OMOP CDM is missing the version" +# ) +# +# # Populate the VOCABULARY table so we can check that the observation_period check works +# DatabaseConnector::renderTranslateExecuteSql( +# connection = connection, +# sql = restoreTableSql, +# progressBar = FALSE, +# reportOverallTime = FALSE, +# cdm_table = "vocabulary", +# backup_table = "vocabulary_bak" +# ) +# expect_error( +# Strategus:::createDatabaseMetaData( +# executionSettings = executionSettings, +# keyringName = keyringName +# ), +# regexp = "FATAL ERROR: The OBSERVATION_PERIOD table in your OMOP CDM lacks a maximum observation_period_end_date" +# ) +# }) diff --git a/tests/testthat/test-Settings.R b/tests/testthat/test-Settings.R index d5f8362d..f4ba5f85 100644 --- a/tests/testthat/test-Settings.R +++ b/tests/testthat/test-Settings.R @@ -1,104 +1,104 @@ -test_that("Test analysis specification creation", { - cohortSharedResource <- list(id = 1) - class(cohortSharedResource) <- c("CohortDefinitionSharedResources", "SharedResources") - - negativeControlOutcomeCohortSharedResource <- list(id = 1) - class(negativeControlOutcomeCohortSharedResource) <- c("NegativeControlOutcomeSharedResources", "SharedResources") - - moduleSpecifications <- list( - module = "CohortGeneratorModule", - version = "0.0.16", - remoteRepo = "github.com", - remoteUsername = "ohdsi", - settings = list() - ) - class(moduleSpecifications) <- c("CohortGeneratorModuleSpecifications", "ModuleSpecifications") - - analysisSpecification <- createEmptyAnalysisSpecificiations() %>% - addSharedResources(cohortSharedResource) %>% - addSharedResources(negativeControlOutcomeCohortSharedResource) %>% - addModuleSpecifications(moduleSpecifications) - - expect_equal(length(analysisSpecification$sharedResources), 2) - expect_equal(length(analysisSpecification$moduleSpecifications), 1) -}) - -test_that("Create results execution settings", { - executionSettings <- createResultsExecutionSettings( - resultsConnectionDetailsReference = "test", - resultsDatabaseSchema = "test", - workFolder = tempfile(), - resultsFolder = tempfile(), - minCellCount = 5 - ) - - expect_equal(class(executionSettings), c("ResultsExecutionSettings", "ExecutionSettings")) -}) - -test_that("Get module list", { - moduleList <- getModuleList() - expect_true(nrow(moduleList) > 0) -}) - -test_that("Verify that unlocking keyring without password fails", { - allKeyrings <- keyring::keyring_list() - if (!keyringName %in% allKeyrings$keyring) { - keyring::keyring_create(keyring = keyringName, password = keyringPassword) - } - # Lock the keyring - keyring::keyring_lock(keyring = keyringName) - - # Remove STRATEGUS_KEYRING_PASSWORD in case it is already set - Sys.unsetenv("STRATEGUS_KEYRING_PASSWORD") - - # Try to unlock and expect error - expect_error(unlockKeyring(keyring = keyringName)) -}) - -test_that("Store and retrieve connection details", { - # Setup keyring for the test - Sys.setenv("STRATEGUS_KEYRING_PASSWORD" = keyringPassword) - createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword) - on.exit(deleteKeyringForUnitTest()) - - for (i in 1:length(connectionDetailsList)) { - connectionDetails <- connectionDetailsList[[i]]$connectionDetails - dbms <- connectionDetailsList[[i]]$connectionDetails$dbms - - message("************* Store connection details for ", dbms, " *************") - - # Verify that the connection details are valid - # by connecting to the DB - conn <- DatabaseConnector::connect( - connectionDetails - ) - DatabaseConnector::disconnect(conn) - - # 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 - ) - expect_silent(DatabaseConnector::disconnect(connFromKeyring)) - } -}) - -test_that("Retrieve connection details that do not exists throws informative error", { - # Setup keyring for the test - Sys.setenv("STRATEGUS_KEYRING_PASSWORD" = keyringPassword) - createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword) - on.exit(deleteKeyringForUnitTest()) - expect_error(retrieveConnectionDetails(connectionDetailsReference = "does-not-exist", keyringName = keyringName)) -}) +# test_that("Test analysis specification creation", { +# cohortSharedResource <- list(id = 1) +# class(cohortSharedResource) <- c("CohortDefinitionSharedResources", "SharedResources") +# +# negativeControlOutcomeCohortSharedResource <- list(id = 1) +# class(negativeControlOutcomeCohortSharedResource) <- c("NegativeControlOutcomeSharedResources", "SharedResources") +# +# moduleSpecifications <- list( +# module = "CohortGeneratorModule", +# version = "0.0.16", +# remoteRepo = "github.com", +# remoteUsername = "ohdsi", +# settings = list() +# ) +# class(moduleSpecifications) <- c("CohortGeneratorModuleSpecifications", "ModuleSpecifications") +# +# analysisSpecification <- createEmptyAnalysisSpecificiations() %>% +# addSharedResources(cohortSharedResource) %>% +# addSharedResources(negativeControlOutcomeCohortSharedResource) %>% +# addModuleSpecifications(moduleSpecifications) +# +# expect_equal(length(analysisSpecification$sharedResources), 2) +# expect_equal(length(analysisSpecification$moduleSpecifications), 1) +# }) +# +# test_that("Create results execution settings", { +# executionSettings <- createResultsExecutionSettings( +# resultsConnectionDetailsReference = "test", +# resultsDatabaseSchema = "test", +# workFolder = tempfile(), +# resultsFolder = tempfile(), +# minCellCount = 5 +# ) +# +# expect_equal(class(executionSettings), c("ResultsExecutionSettings", "ExecutionSettings")) +# }) +# +# test_that("Get module list", { +# moduleList <- getModuleList() +# expect_true(nrow(moduleList) > 0) +# }) +# +# test_that("Verify that unlocking keyring without password fails", { +# allKeyrings <- keyring::keyring_list() +# if (!keyringName %in% allKeyrings$keyring) { +# keyring::keyring_create(keyring = keyringName, password = keyringPassword) +# } +# # Lock the keyring +# keyring::keyring_lock(keyring = keyringName) +# +# # Remove STRATEGUS_KEYRING_PASSWORD in case it is already set +# Sys.unsetenv("STRATEGUS_KEYRING_PASSWORD") +# +# # Try to unlock and expect error +# expect_error(unlockKeyring(keyring = keyringName)) +# }) +# +# test_that("Store and retrieve connection details", { +# # Setup keyring for the test +# Sys.setenv("STRATEGUS_KEYRING_PASSWORD" = keyringPassword) +# createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword) +# on.exit(deleteKeyringForUnitTest()) +# +# for (i in 1:length(connectionDetailsList)) { +# connectionDetails <- connectionDetailsList[[i]]$connectionDetails +# dbms <- connectionDetailsList[[i]]$connectionDetails$dbms +# +# message("************* Store connection details for ", dbms, " *************") +# +# # Verify that the connection details are valid +# # by connecting to the DB +# conn <- DatabaseConnector::connect( +# connectionDetails +# ) +# DatabaseConnector::disconnect(conn) +# +# # 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 +# ) +# expect_silent(DatabaseConnector::disconnect(connFromKeyring)) +# } +# }) +# +# test_that("Retrieve connection details that do not exists throws informative error", { +# # Setup keyring for the test +# Sys.setenv("STRATEGUS_KEYRING_PASSWORD" = keyringPassword) +# createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword) +# on.exit(deleteKeyringForUnitTest()) +# expect_error(retrieveConnectionDetails(connectionDetailsReference = "does-not-exist", keyringName = keyringName)) +# }) diff --git a/tests/testthat/test-Strategus.R b/tests/testthat/test-Strategus.R index e07d5bc0..80691eff 100644 --- a/tests/testthat/test-Strategus.R +++ b/tests/testthat/test-Strategus.R @@ -1,144 +1,144 @@ -# test_that("Run unit test study", { -# # NOTE: Need to set this in each test otherwise it goes out of scope -# renv:::renv_scope_envvars(RENV_PATHS_CACHE = renvCachePath) -# -# # Setup keyring for the test -# Sys.setenv("STRATEGUS_KEYRING_PASSWORD" = keyringPassword) -# createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword) -# on.exit(deleteKeyringForUnitTest()) -# -# analysisSpecifications <- ParallelLogger::loadSettingsFromJson( -# fileName = system.file("testdata/unitTestAnalysisSpecification.json", -# package = "Strategus" -# ) -# ) -# -# withr::defer( -# { -# if (usingTempDir) { -# unlink(file.path(tempDir, "EunomiaTestStudy"), recursive = TRUE, force = TRUE) -# } -# }, -# testthat::teardown_env() -# ) -# -# for (i in 1:length(connectionDetailsList)) { -# connectionDetails <- connectionDetailsList[[i]]$connectionDetails -# dbms <- connectionDetailsList[[i]]$connectionDetails$dbms -# workDatabaseSchema <- connectionDetailsList[[i]]$workDatabaseSchema -# cdmDatabaseSchema <- connectionDetailsList[[i]]$cdmDatabaseSchema -# tempEmulationSchema <- connectionDetailsList[[i]]$tempEmulationSchema -# studyRootFolder <- file.path(tempDir, "EunomiaTestStudy", dbms) -# workFolder <- file.path(studyRootFolder, "work_folder") -# resultsFolder <- file.path(studyRootFolder, "results_folder") -# scriptFolder <- file.path(studyRootFolder, "script_folder") -# -# message("************* Running Strategus on ", dbms, " *************") -# -# # Using a named and secured keyring -# Strategus::storeConnectionDetails( -# connectionDetails = connectionDetails, -# connectionDetailsReference = dbms, -# keyringName = keyringName -# ) -# -# resultsConnectionDetailsReference <- NULL -# resultsDatabaseSchema <- NULL -# -# # Only run this code if we're testing on SQLite -# if (dbms == "sqlite") { -# resultsConnectionDetailsReference <- "result-store" -# resultsDatabaseSchema <- "main" -# Strategus::storeConnectionDetails( -# connectionDetails, -# resultsConnectionDetailsReference, -# keyringName = keyringName -# ) -# resultsExecutionSettings <- Strategus::createResultsExecutionSettings( -# resultsConnectionDetailsReference = resultsConnectionDetailsReference, -# resultsDatabaseSchema = resultsDatabaseSchema, -# workFolder = workFolder, -# resultsFolder = resultsFolder -# ) -# Strategus::createResultDataModels( -# analysisSpecifications = analysisSpecifications, -# executionSettings = resultsExecutionSettings, -# keyringName = keyringName -# ) -# } -# -# executionSettings <- createCdmExecutionSettings( -# connectionDetailsReference = dbms, -# workDatabaseSchema = workDatabaseSchema, -# cdmDatabaseSchema = cdmDatabaseSchema, -# tempEmulationSchema = tempEmulationSchema, -# cohortTableNames = CohortGenerator::getCohortTableNames(cohortTable = paste0("s", tableSuffix)), -# workFolder = workFolder, -# resultsFolder = resultsFolder, -# minCellCount = 5, -# resultsDatabaseSchema = resultsDatabaseSchema, -# resultsConnectionDetailsReference = resultsConnectionDetailsReference -# ) -# -# if (!dir.exists(studyRootFolder)) { -# dir.create(studyRootFolder, recursive = TRUE) -# } -# ParallelLogger::saveSettingsToJson( -# object = executionSettings, -# file.path(studyRootFolder, "eunomiaExecutionSettings.json") -# ) -# -# executionSettings <- ParallelLogger::loadSettingsFromJson( -# fileName = file.path(studyRootFolder, "eunomiaExecutionSettings.json") -# ) -# -# Strategus::execute( -# analysisSpecifications = analysisSpecifications, -# executionSettings = executionSettings, -# executionScriptFolder = scriptFolder, -# keyringName = keyringName -# ) -# -# expect_true(file.exists(file.path(resultsFolder, "TestModule1_1", "done"))) -# } -# }) - -test_that("Execute on Oracle stops if table names exceed length limit", { - sqlRenderTempEmulationSchema <- getOption("sqlRenderTempEmulationSchema", default = "") - options(sqlRenderTempEmulationSchema = "some_schema") - on.exit(options(sqlRenderTempEmulationSchema = sqlRenderTempEmulationSchema)) +test_that("Run unit test study", { + # NOTE: Need to set this in each test otherwise it goes out of scope + renv:::renv_scope_envvars(RENV_PATHS_CACHE = renvCachePath) + # Setup keyring for the test Sys.setenv("STRATEGUS_KEYRING_PASSWORD" = keyringPassword) createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword) on.exit(deleteKeyringForUnitTest()) - connectionDetails <- DatabaseConnector::createConnectionDetails( - dbms = "oracle" - ) - Strategus::storeConnectionDetails(connectionDetails = connectionDetails, - connectionDetailsReference = "oracle-test", - keyringName = keyringName) - executionSettings <- Strategus::createCdmExecutionSettings( - connectionDetailsReference = "oracle-test", - workDatabaseSchema = "does_not_matter", - cdmDatabaseSchema = "does_not_matter", - cohortTableNames = CohortGenerator::getCohortTableNames("some_really_long_table_name_for_testing_that_oracle_throws_a_warning"), - workFolder = file.path(tempDir, "work_folder"), - resultsFolder = file.path(tempDir, "results_folder"), - minCellCount = 5 - ) - analysisSpecifications <- ParallelLogger::loadSettingsFromJson( fileName = system.file("testdata/unitTestAnalysisSpecification.json", - package = "Strategus" + package = "Strategus" ) ) - expect_error( + withr::defer( + { + if (usingTempDir) { + unlink(file.path(tempDir, "EunomiaTestStudy"), recursive = TRUE, force = TRUE) + } + }, + testthat::teardown_env() + ) + + for (i in 1:length(connectionDetailsList)) { + connectionDetails <- connectionDetailsList[[i]]$connectionDetails + dbms <- connectionDetailsList[[i]]$connectionDetails$dbms + workDatabaseSchema <- connectionDetailsList[[i]]$workDatabaseSchema + cdmDatabaseSchema <- connectionDetailsList[[i]]$cdmDatabaseSchema + tempEmulationSchema <- connectionDetailsList[[i]]$tempEmulationSchema + studyRootFolder <- file.path(tempDir, "EunomiaTestStudy", dbms) + workFolder <- file.path(studyRootFolder, "work_folder") + resultsFolder <- file.path(studyRootFolder, "results_folder") + scriptFolder <- file.path(studyRootFolder, "script_folder") + + message("************* Running Strategus on ", dbms, " *************") + + # Using a named and secured keyring + Strategus::storeConnectionDetails( + connectionDetails = connectionDetails, + connectionDetailsReference = dbms, + keyringName = keyringName + ) + + resultsConnectionDetailsReference <- NULL + resultsDatabaseSchema <- NULL + + # Only run this code if we're testing on SQLite + if (dbms == "sqlite") { + resultsConnectionDetailsReference <- "result-store" + resultsDatabaseSchema <- "main" + Strategus::storeConnectionDetails( + connectionDetails, + resultsConnectionDetailsReference, + keyringName = keyringName + ) + resultsExecutionSettings <- Strategus::createResultsExecutionSettings( + resultsConnectionDetailsReference = resultsConnectionDetailsReference, + resultsDatabaseSchema = resultsDatabaseSchema, + workFolder = workFolder, + resultsFolder = resultsFolder + ) + Strategus::createResultDataModels( + analysisSpecifications = analysisSpecifications, + executionSettings = resultsExecutionSettings, + keyringName = keyringName + ) + } + + executionSettings <- createCdmExecutionSettings( + connectionDetailsReference = dbms, + workDatabaseSchema = workDatabaseSchema, + cdmDatabaseSchema = cdmDatabaseSchema, + tempEmulationSchema = tempEmulationSchema, + cohortTableNames = CohortGenerator::getCohortTableNames(cohortTable = paste0("s", tableSuffix)), + workFolder = workFolder, + resultsFolder = resultsFolder, + minCellCount = 5, + resultsDatabaseSchema = resultsDatabaseSchema, + resultsConnectionDetailsReference = resultsConnectionDetailsReference + ) + + if (!dir.exists(studyRootFolder)) { + dir.create(studyRootFolder, recursive = TRUE) + } + ParallelLogger::saveSettingsToJson( + object = executionSettings, + file.path(studyRootFolder, "eunomiaExecutionSettings.json") + ) + + executionSettings <- ParallelLogger::loadSettingsFromJson( + fileName = file.path(studyRootFolder, "eunomiaExecutionSettings.json") + ) + Strategus::execute( analysisSpecifications = analysisSpecifications, executionSettings = executionSettings, + executionScriptFolder = scriptFolder, keyringName = keyringName - ) - ) + ) + + expect_true(file.exists(file.path(resultsFolder, "TestModule1_1", "done"))) + } }) + +# test_that("Execute on Oracle stops if table names exceed length limit", { +# sqlRenderTempEmulationSchema <- getOption("sqlRenderTempEmulationSchema", default = "") +# options(sqlRenderTempEmulationSchema = "some_schema") +# on.exit(options(sqlRenderTempEmulationSchema = sqlRenderTempEmulationSchema)) +# +# Sys.setenv("STRATEGUS_KEYRING_PASSWORD" = keyringPassword) +# createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword) +# on.exit(deleteKeyringForUnitTest()) +# +# connectionDetails <- DatabaseConnector::createConnectionDetails( +# dbms = "oracle" +# ) +# Strategus::storeConnectionDetails(connectionDetails = connectionDetails, +# connectionDetailsReference = "oracle-test", +# keyringName = keyringName) +# executionSettings <- Strategus::createCdmExecutionSettings( +# connectionDetailsReference = "oracle-test", +# workDatabaseSchema = "does_not_matter", +# cdmDatabaseSchema = "does_not_matter", +# cohortTableNames = CohortGenerator::getCohortTableNames("some_really_long_table_name_for_testing_that_oracle_throws_a_warning"), +# workFolder = file.path(tempDir, "work_folder"), +# resultsFolder = file.path(tempDir, "results_folder"), +# minCellCount = 5 +# ) +# +# analysisSpecifications <- ParallelLogger::loadSettingsFromJson( +# fileName = system.file("testdata/unitTestAnalysisSpecification.json", +# package = "Strategus" +# ) +# ) +# +# expect_error( +# Strategus::execute( +# analysisSpecifications = analysisSpecifications, +# executionSettings = executionSettings, +# keyringName = keyringName +# ) +# ) +# })