From 28c36f979ce6da4fd6aa3c1562d82890b9fffe4c Mon Sep 17 00:00:00 2001 From: Anthony Sena Date: Wed, 16 Aug 2023 17:07:13 -0400 Subject: [PATCH] Fix cmd checks and revise test --- R/Execution.R | 4 +- R/ModuleEnv.R | 6 +- R/Settings.R | 1 + man/createCdmExecutionSettings.Rd | 2 + man/createResultDataModels.Rd | 6 +- man/withModuleRenv.Rd | 5 +- tests/testthat/test-Strategus.R | 146 +++++++++++++++--------------- 7 files changed, 90 insertions(+), 80 deletions(-) diff --git a/R/Execution.R b/R/Execution.R index c0cc017a..3be40be1 100644 --- a/R/Execution.R +++ b/R/Execution.R @@ -158,9 +158,7 @@ generateTargetsScript <- function(analysisSpecifications, executionSettings, dep ) } } - } - - targetList + targetList }, script = fileName) #Store settings objects in the temp folder so they are available in targets diff --git a/R/ModuleEnv.R b/R/ModuleEnv.R index 696b929d..16783f51 100644 --- a/R/ModuleEnv.R +++ b/R/ModuleEnv.R @@ -44,9 +44,11 @@ #' #' @param code code block to execute #' @param moduleFolder Instantiated Strategus module folder -#' @param injectVars list of var names list(name=value) to replace (e.g. replace list(foo = "some string") will +#' @param injectVars list of var names list(name=value) to replace (e.g. replace list(foo = "some string") will #' find the pattern foo and replace it with the string some string - be careful! -#' @param tempScriptFile tempFile to write script to (ret +#' @param tempScriptFile tempFile to write script to +#' @param useLocalStrategusLibrary Use the locally installed Strategus library? TRUE will use the Strategus +#' installation from the calling R process. #' @param job run as rstudio job #' @param processName String name for process #' @returns NULL invisibly diff --git a/R/Settings.R b/R/Settings.R index fe0e3722..737202d6 100644 --- a/R/Settings.R +++ b/R/Settings.R @@ -87,6 +87,7 @@ addModuleSpecifications <- function(analysisSpecifications, moduleSpecifications #' @param cohortTableNames An object identifying the various cohort table names that will be created in the #' `workDatabaseSchema`. This object can be created using the #' [CohortGenerator::getCohortTableNames()] function. +#' @param tempEmulationSchema Some database platforms like Oracle and Impala do not truly support temp tables. To emulate temp tables, provide a schema with write privileges where temp tables can be created. #' @param workFolder A folder in the local file system where intermediate results can be written. #' @param resultsFolder A folder in the local file system where the module output will be written. #' @param minCellCount The minimum number of subjects contributing to a count before it can be included diff --git a/man/createCdmExecutionSettings.Rd b/man/createCdmExecutionSettings.Rd index c8d447ba..97f05fa3 100644 --- a/man/createCdmExecutionSettings.Rd +++ b/man/createCdmExecutionSettings.Rd @@ -31,6 +31,8 @@ connection details) will need to have read access to this database schema.} \code{workDatabaseSchema}. This object can be created using the \code{\link[CohortGenerator:getCohortTableNames]{CohortGenerator::getCohortTableNames()}} function.} +\item{tempEmulationSchema}{Some database platforms like Oracle and Impala do not truly support temp tables. To emulate temp tables, provide a schema with write privileges where temp tables can be created.} + \item{workFolder}{A folder in the local file system where intermediate results can be written.} \item{resultsFolder}{A folder in the local file system where the module output will be written.} diff --git a/man/createResultDataModels.Rd b/man/createResultDataModels.Rd index e3ee1364..fe3d16df 100644 --- a/man/createResultDataModels.Rd +++ b/man/createResultDataModels.Rd @@ -8,7 +8,8 @@ createResultDataModels( analysisSpecifications, executionSettings, executionScriptFolder = NULL, - keyringName = NULL + keyringName = NULL, + restart = FALSE ) } \arguments{ @@ -28,6 +29,9 @@ NULL to select the default keyring. If the keyring is password protected, the password must be stored in the environment variable STRATEGUS_KEYRING_PASSWORD so it is retrieved using the command Sys.getenv("STRATEGUS_KEYRING_PASSWORD")} + +\item{restart}{Restart run? Requires \code{executionScriptFolder} to be specified, and be +the same as the \code{executionScriptFolder} used in the run to restart.} } \description{ Use this at the study design stage to create data models for modules diff --git a/man/withModuleRenv.Rd b/man/withModuleRenv.Rd index 64afb036..61407a23 100644 --- a/man/withModuleRenv.Rd +++ b/man/withModuleRenv.Rd @@ -23,7 +23,10 @@ withModuleRenv( \item{injectVars}{list of var names list(name=value) to replace (e.g. replace list(foo = "some string") will find the pattern foo and replace it with the string some string - be careful!} -\item{tempScriptFile}{tempFile to write script to (ret} +\item{tempScriptFile}{tempFile to write script to} + +\item{useLocalStrategusLibrary}{Use the locally installed Strategus library? TRUE will use the Strategus +installation from the calling R process.} \item{job}{run as rstudio job} diff --git a/tests/testthat/test-Strategus.R b/tests/testthat/test-Strategus.R index a9689fcd..8116164b 100644 --- a/tests/testthat/test-Strategus.R +++ b/tests/testthat/test-Strategus.R @@ -1,73 +1,73 @@ -# test_that("Run Eunomia 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()) -# -# # Using a named and secured keyring -# Strategus::storeConnectionDetails( -# connectionDetails = connectionDetails, -# connectionDetailsReference = dbms, -# keyringName = keyringName -# ) -# -# analysisSpecifications <- ParallelLogger::loadSettingsFromJson( -# fileName = system.file("testdata/analysisSpecification.json", -# package = "Strategus" -# ) -# ) -# -# # Create a unique set of cohort tables for this test run and -# # ensure they are removed when complete -# cohortTableNames <- CohortGenerator::getCohortTableNames(cohortTable = paste0("s", tableSuffix)) -# -# # Use this line to limit to only running the CohortGeneratorModule -# # for testing purposes. -# analysisSpecifications$moduleSpecifications <- analysisSpecifications$moduleSpecifications[-c(2:length(analysisSpecifications$moduleSpecifications))] -# executionSettings <- createCdmExecutionSettings( -# connectionDetailsReference = dbms, -# workDatabaseSchema = workDatabaseSchema, -# cdmDatabaseSchema = cdmDatabaseSchema, -# cohortTableNames = cohortTableNames, -# workFolder = file.path(tempDir, "EunomiaTestStudy/work_folder"), -# resultsFolder = file.path(tempDir, "EunomiaTestStudy/results_folder"), -# minCellCount = 5 -# ) -# -# if (!dir.exists(file.path(tempDir, "EunomiaTestStudy"))) { -# dir.create(file.path(tempDir, "EunomiaTestStudy"), recursive = TRUE) -# } -# ParallelLogger::saveSettingsToJson( -# object = executionSettings, -# file.path(tempDir, "EunomiaTestStudy/eunomiaExecutionSettings.json") -# ) -# -# executionSettings <- ParallelLogger::loadSettingsFromJson( -# fileName = file.path(tempDir, "EunomiaTestStudy/eunomiaExecutionSettings.json") -# ) -# -# Strategus::execute( -# analysisSpecifications = analysisSpecifications, -# executionSettings = executionSettings, -# executionScriptFolder = file.path(tempDir, "EunomiaTestStudy/script_folder"), -# keyringName = keyringName -# ) -# -# expect_true(file.exists(file.path(tempDir, "EunomiaTestStudy/results_folder/CohortGeneratorModule_1/done"))) -# -# withr::defer( -# { -# CohortGenerator::dropCohortStatsTables( -# connectionDetails = connectionDetails, -# cohortDatabaseSchema = workDatabaseSchema, -# cohortTableNames = cohortTableNames, -# dropCohortTable = TRUE -# ) -# unlink(file.path(tempDir, "EunomiaTestStudy"), recursive = TRUE, force = TRUE) -# }, -# testthat::teardown_env() -# ) -# }) +test_that("Run Eunomia 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()) + + # Using a named and secured keyring + Strategus::storeConnectionDetails( + connectionDetails = connectionDetails, + connectionDetailsReference = dbms, + keyringName = keyringName + ) + + analysisSpecifications <- ParallelLogger::loadSettingsFromJson( + fileName = system.file("testdata/analysisSpecification.json", + package = "Strategus" + ) + ) + + # Create a unique set of cohort tables for this test run and + # ensure they are removed when complete + cohortTableNames <- CohortGenerator::getCohortTableNames(cohortTable = paste0("s", tableSuffix)) + + # Use this line to limit to only running the CohortGeneratorModule + # for testing purposes. + analysisSpecifications$moduleSpecifications <- analysisSpecifications$moduleSpecifications[-c(2:length(analysisSpecifications$moduleSpecifications))] + executionSettings <- createCdmExecutionSettings( + connectionDetailsReference = dbms, + workDatabaseSchema = workDatabaseSchema, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortTableNames = cohortTableNames, + workFolder = file.path(tempDir, "EunomiaTestStudy/work_folder"), + resultsFolder = file.path(tempDir, "EunomiaTestStudy/results_folder"), + minCellCount = 5 + ) + + if (!dir.exists(file.path(tempDir, "EunomiaTestStudy"))) { + dir.create(file.path(tempDir, "EunomiaTestStudy"), recursive = TRUE) + } + ParallelLogger::saveSettingsToJson( + object = executionSettings, + file.path(tempDir, "EunomiaTestStudy/eunomiaExecutionSettings.json") + ) + + executionSettings <- ParallelLogger::loadSettingsFromJson( + fileName = file.path(tempDir, "EunomiaTestStudy/eunomiaExecutionSettings.json") + ) + + Strategus::execute( + analysisSpecifications = analysisSpecifications, + executionSettings = executionSettings, + executionScriptFolder = file.path(tempDir, "EunomiaTestStudy/script_folder"), + keyringName = keyringName + ) + + expect_true(file.exists(file.path(tempDir, "EunomiaTestStudy/results_folder/CohortGeneratorModule_1/done"))) + + withr::defer( + { + CohortGenerator::dropCohortStatsTables( + connectionDetails = connectionDetails, + cohortDatabaseSchema = workDatabaseSchema, + cohortTableNames = cohortTableNames, + dropCohortTable = TRUE + ) + unlink(file.path(tempDir, "EunomiaTestStudy"), recursive = TRUE, force = TRUE) + }, + testthat::teardown_env() + ) +})