From 23969d6143894db32620e609f52e31e2dff69be8 Mon Sep 17 00:00:00 2001 From: Anthony Sena Date: Thu, 30 May 2024 15:59:13 -0400 Subject: [PATCH] Add all supported database servers --- tests/testthat/helper.R | 109 ++++++++++++++++++++------- tests/testthat/setup.R | 2 +- tests/testthat/test-dbms-platforms.R | 3 +- 3 files changed, 84 insertions(+), 30 deletions(-) diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index 122d3b9..1e171bd 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -84,49 +84,102 @@ getPlatformConnectionDetails <- function(dbmsPlatform) { options("sqlRenderTempEmulationSchema" = NULL) cohortTable <- "cohort" } else { - if (dbmsPlatform == "postgresql") { - dbUser <- Sys.getenv("CDM5_POSTGRESQL_USER") - dbPassword <- Sys.getenv("CDM5_POSTGRESQL_PASSWORD") - dbServer <- Sys.getenv("CDM5_POSTGRESQL_SERVER") - cdmDatabaseSchema <- Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA") - vocabularyDatabaseSchema <- Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA") - cohortDatabaseSchema <- Sys.getenv("CDM5_POSTGRESQL_OHDSI_SCHEMA") + if (dbmsPlatform == "bigquery") { + # To avoid rate limit on BigQuery, only test on 1 OS: + if (.Platform$OS.type == "windows") { + bqKeyFile <- tempfile(fileext = ".json") + writeLines(Sys.getenv("CDM_BIG_QUERY_KEY_FILE"), bqKeyFile) + if (testthat::is_testing()) { + withr::defer(unlink(bqKeyFile, force = TRUE), testthat::teardown_env()) + } + bqConnectionString <- gsub("", + normalizePath(bqKeyFile, winslash = "/"), + Sys.getenv("CDM_BIG_QUERY_CONNECTION_STRING")) + connectionDetails = DatabaseConnector::createConnectionDetails( + dbms = dbmsPlatform, + user = "", + password = "", + connectionString = !!bqConnectionString, + pathToDriver = jdbcDriverFolder + ) + cdmDatabaseSchema <- Sys.getenv("CDM_BIG_QUERY_CDM_SCHEMA") + vocabularyDatabaseSchema <- Sys.getenv("CDM_BIG_QUERY_CDM_SCHEMA") + cohortDatabaseSchema <- Sys.getenv("CDM_BIG_QUERY_OHDSI_SCHEMA") + options(sqlRenderTempEmulationSchema = Sys.getenv("CDM_BIG_QUERY_OHDSI_SCHEMA")) + } else { + return(NULL) + } } else if (dbmsPlatform == "oracle") { - dbUser <- Sys.getenv("CDM5_ORACLE_USER") - dbPassword <- Sys.getenv("CDM5_ORACLE_PASSWORD") - dbServer <- Sys.getenv("CDM5_ORACLE_SERVER") + connectionDetails <- DatabaseConnector::createConnectionDetails( + dbms = dbmsPlatform, + user = Sys.getenv("CDM5_ORACLE_USER"), + password = URLdecode(Sys.getenv("CDM5_ORACLE_PASSWORD")), + server = Sys.getenv("CDM5_ORACLE_SERVER"), + pathToDriver = jdbcDriverFolder + ) cdmDatabaseSchema <- Sys.getenv("CDM5_ORACLE_CDM_SCHEMA") vocabularyDatabaseSchema <- Sys.getenv("CDM5_ORACLE_CDM_SCHEMA") cohortDatabaseSchema <- Sys.getenv("CDM5_ORACLE_OHDSI_SCHEMA") options(sqlRenderTempEmulationSchema = Sys.getenv("CDM5_ORACLE_OHDSI_SCHEMA")) + } else if (dbmsPlatform == "postgresql") { + connectionDetails <- DatabaseConnector::createConnectionDetails( + dbms = dbmsPlatform, + user = Sys.getenv("CDM5_POSTGRESQL_USER"), + password = URLdecode(Sys.getenv("CDM5_POSTGRESQL_PASSWORD")), + server = Sys.getenv("CDM5_POSTGRESQL_SERVER"), + pathToDriver = jdbcDriverFolder + ) + cdmDatabaseSchema <- Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA") + vocabularyDatabaseSchema <- Sys.getenv("CDM5_POSTGRESQL_CDM_SCHEMA") + cohortDatabaseSchema <- Sys.getenv("CDM5_POSTGRESQL_OHDSI_SCHEMA") } else if (dbmsPlatform == "redshift") { - dbUser <- Sys.getenv("CDM5_REDSHIFT_USER") - dbPassword <- Sys.getenv("CDM5_REDSHIFT_PASSWORD") - dbServer <- Sys.getenv("CDM5_REDSHIFT_SERVER") + connectionDetails <- DatabaseConnector::createConnectionDetails( + dbms = dbmsPlatform, + user = Sys.getenv("CDM5_REDSHIFT_USER"), + password = URLdecode(Sys.getenv("CDM5_REDSHIFT_PASSWORD")), + server = Sys.getenv("CDM5_REDSHIFT_SERVER"), + pathToDriver = jdbcDriverFolder + ) cdmDatabaseSchema <- Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA") vocabularyDatabaseSchema <- Sys.getenv("CDM5_REDSHIFT_CDM_SCHEMA") cohortDatabaseSchema <- Sys.getenv("CDM5_REDSHIFT_OHDSI_SCHEMA") + } else if (dbmsPlatform == "snowflake") { + connectionDetails = DatabaseConnector::createConnectionDetails( + dbms = dbmsPlatform, + user = Sys.getenv("CDM_SNOWFLAKE_USER"), + password = URLdecode(Sys.getenv("CDM_SNOWFLAKE_PASSWORD")), + connectionString = Sys.getenv("CDM_SNOWFLAKE_CONNECTION_STRING"), + pathToDriver = jdbcDriverFolder + ) + cdmDatabaseSchema <- Sys.getenv("CDM_SNOWFLAKE_CDM53_SCHEMA") + vocabularyDatabaseSchema <- Sys.getenv("CDM_SNOWFLAKE_CDM53_SCHEMA") + cohortDatabaseSchema <- Sys.getenv("CDM_SNOWFLAKE_OHDSI_SCHEMA") + options(sqlRenderTempEmulationSchema = Sys.getenv("CDM_SNOWFLAKE_OHDSI_SCHEMA")) + } else if (dbmsPlatform == "spark") { + connectionDetails = DatabaseConnector::createConnectionDetails( + dbms = dbmsPlatform, + user = Sys.getenv("CDM5_SPARK_USER"), + password = URLdecode(Sys.getenv("CDM5_SPARK_PASSWORD")), + connectionString = Sys.getenv("CDM5_SPARK_CONNECTION_STRING"), + pathToDriver = jdbcDriverFolder + ) + cdmDatabaseSchema <- Sys.getenv("CDM5_SPARK_CDM_SCHEMA") + vocabularyDatabaseSchema <- Sys.getenv("CDM5_SPARK_CDM_SCHEMA") + cohortDatabaseSchema <- Sys.getenv("CDM5_SPARK_OHDSI_SCHEMA") + options(sqlRenderTempEmulationSchema = Sys.getenv("CDM5_SPARK_OHDSI_SCHEMA")) } else if (dbmsPlatform == "sql server") { - dbUser <- Sys.getenv("CDM5_SQL_SERVER_USER") - dbPassword <- Sys.getenv("CDM5_SQL_SERVER_PASSWORD") - dbServer <- Sys.getenv("CDM5_SQL_SERVER_SERVER") + connectionDetails <- createConnectionDetails( + dbms = dbmsPlatform, + user = Sys.getenv("CDM5_SQL_SERVER_USER"), + password = URLdecode(Sys.getenv("CDM5_SQL_SERVER_PASSWORD")), + server = Sys.getenv("CDM5_SQL_SERVER_SERVER"), + pathToDriver = jdbcDriverFolder + ) cdmDatabaseSchema <- Sys.getenv("CDM5_SQL_SERVER_CDM_SCHEMA") vocabularyDatabaseSchema <- Sys.getenv("CDM5_SQL_SERVER_CDM_SCHEMA") cohortDatabaseSchema <- Sys.getenv("CDM5_SQL_SERVER_OHDSI_SCHEMA") } - if (dbServer == "") { - return(NULL) - } - - connectionDetails <- DatabaseConnector::createConnectionDetails( - dbms = dbmsPlatform, - user = dbUser, - password = URLdecode(dbPassword), - server = dbServer, - pathToDriver = jdbcDriverFolder - ) - # Add drivers DatabaseConnector::downloadJdbcDrivers(dbmsPlatform, pathToDriver = jdbcDriverFolder) # Table created to avoid collisions diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index 8ec0077..1974b23 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -1,4 +1,4 @@ -dbmsPlatforms <- c("redshift", "postgresql", "oracle", "sql server") +dbmsPlatforms <- c("bigquery", "oracle", "postgresql", "redshift", "snowflake", "spark", "sql server") connectionDetails <- Eunomia::getEunomiaConnectionDetails() outputFolder <- tempfile() dir.create(outputFolder) diff --git a/tests/testthat/test-dbms-platforms.R b/tests/testthat/test-dbms-platforms.R index 4332f74..c49d02a 100644 --- a/tests/testthat/test-dbms-platforms.R +++ b/tests/testthat/test-dbms-platforms.R @@ -110,8 +110,9 @@ test_that("platform specific create cohorts with stats, Incremental, get results for (dbmsPlatform in dbmsPlatforms) { dbmsDetails <- getPlatformConnectionDetails(dbmsPlatform) if (is.null(dbmsDetails)) { - print(paste("No pltatform details available for", dbmsPlatform)) + print(paste("No platform details available for", dbmsPlatform)) } else { + print(paste("Testing", dbmsPlatform)) testPlatform(dbmsDetails) } }