Extend unit tests to cover InterSystems IRIS

This commit is contained in:
Benjamin De Boe 2024-10-23 09:56:57 +02:00
parent 5cf916e3e9
commit 44bc47e5f1
1 changed files with 136 additions and 128 deletions

View File

@ -1,128 +1,136 @@
testDatabases <- c("postgresql") testDatabases <- c("postgresql")
if (Sys.getenv("CDM5_POSTGRESQL_SERVER") != "") { if (Sys.getenv("CDM5_POSTGRESQL_SERVER") != "") {
library(DatabaseConnector) library(DatabaseConnector)
if (dir.exists(Sys.getenv("DATABASECONNECTOR_JAR_FOLDER"))) { if (dir.exists(Sys.getenv("DATABASECONNECTOR_JAR_FOLDER"))) {
jdbcDriverFolder <- Sys.getenv("DATABASECONNECTOR_JAR_FOLDER") jdbcDriverFolder <- Sys.getenv("DATABASECONNECTOR_JAR_FOLDER")
} else { } else {
jdbcDriverFolder <- file.path(tempdir(), "jdbcDrivers") jdbcDriverFolder <- file.path(tempdir(), "jdbcDrivers")
dir.create(jdbcDriverFolder, showWarnings = FALSE) dir.create(jdbcDriverFolder, showWarnings = FALSE)
DatabaseConnector::downloadJdbcDrivers("all", pathToDriver = jdbcDriverFolder) DatabaseConnector::downloadJdbcDrivers("all", pathToDriver = jdbcDriverFolder)
Sys.setenv(DATABASECONNECTOR_JAR_FOLDER = jdbcDriverFolder) Sys.setenv(DATABASECONNECTOR_JAR_FOLDER = jdbcDriverFolder)
withr::defer({ withr::defer({
unlink(jdbcDriverFolder, unlink(jdbcDriverFolder,
recursive = TRUE, recursive = TRUE,
force = TRUE) force = TRUE)
}, },
testthat::teardown_env()) testthat::teardown_env())
} }
} else { } else {
message("Skipping driver setup because environmental variables not set") message("Skipping driver setup because environmental variables not set")
} }
# Helper functions used in tests # Helper functions used in tests
getConnectionDetails <- function(dbms) { getConnectionDetails <- function(dbms) {
switch ( switch (
dbms, dbms,
"postgresql" = createConnectionDetails( "postgresql" = createConnectionDetails(
dbms = "postgresql", dbms = "postgresql",
user = Sys.getenv("CDMDDLBASE_POSTGRESQL_USER"), user = Sys.getenv("CDMDDLBASE_POSTGRESQL_USER"),
password = Sys.getenv("CDMDDLBASE_POSTGRESQL_PASSWORD"), password = Sys.getenv("CDMDDLBASE_POSTGRESQL_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_POSTGRESQL_SERVER"), server = Sys.getenv("CDMDDLBASE_POSTGRESQL_SERVER"),
pathToDriver = jdbcDriverFolder pathToDriver = jdbcDriverFolder
), ),
"redshift" = createConnectionDetails( "redshift" = createConnectionDetails(
dbms = "redshift", dbms = "redshift",
user = Sys.getenv("CDMDDLBASE_REDSHIFT_USER"), user = Sys.getenv("CDMDDLBASE_REDSHIFT_USER"),
password = Sys.getenv("CDMDDLBASE_REDSHIFT_PASSWORD"), password = Sys.getenv("CDMDDLBASE_REDSHIFT_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_REDSHIFT_SERVER"), server = Sys.getenv("CDMDDLBASE_REDSHIFT_SERVER"),
pathToDriver = jdbcDriverFolder pathToDriver = jdbcDriverFolder
), ),
"sql server" = createConnectionDetails( "sql server" = createConnectionDetails(
dbms = "sql server", dbms = "sql server",
user = Sys.getenv("CDMDDLBASE_SQL_SERVER_USER"), user = Sys.getenv("CDMDDLBASE_SQL_SERVER_USER"),
password = Sys.getenv("CDMDDLBASE_SQL_SERVER_PASSWORD"), password = Sys.getenv("CDMDDLBASE_SQL_SERVER_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_SQL_SERVER_SERVER"), server = Sys.getenv("CDMDDLBASE_SQL_SERVER_SERVER"),
pathToDriver = jdbcDriverFolder pathToDriver = jdbcDriverFolder
), ),
"oracle" = createConnectionDetails( "oracle" = createConnectionDetails(
dbms = "oracle", dbms = "oracle",
user = Sys.getenv("CDMDDLBASE_ORACLE_USER"), user = Sys.getenv("CDMDDLBASE_ORACLE_USER"),
password = Sys.getenv("CDMDDLBASE_ORACLE_PASSWORD"), password = Sys.getenv("CDMDDLBASE_ORACLE_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_ORACLE_SERVER"), server = Sys.getenv("CDMDDLBASE_ORACLE_SERVER"),
pathToDriver = jdbcDriverFolder pathToDriver = jdbcDriverFolder
) ),
) "iris" = createConnectionDetails(
} dbms = "iris",
user = Sys.getenv("CDMDDLBASE_IRIS_USER"),
getSchema <- function(dbms) { password = Sys.getenv("CDMDDLBASE_IRIS_PASSWORD"),
switch ( connectionString = Sys.getenv("CDMDDLBASE_IRIS_URL"),
dbms, pathToDriver = jdbcDriverFolder
"postgresql" = Sys.getenv("CDMDDLBASE_POSTGRESQL_SCHEMA"), )
"redshift" = Sys.getenv("CDMDDLBASE_REDSHIFT_SCHEMA"), )
"sql server" = Sys.getenv("CDMDDLBASE_SQL_SERVER_CDM_SCHEMA"), }
"oracle" = Sys.getenv("CDMDDLBASE_ORACLE_CDM_SCHEMA")
) getSchema <- function(dbms) {
} switch (
dbms,
listTablesInSchema <- function(connectionDetails, schema) { "postgresql" = Sys.getenv("CDMDDLBASE_POSTGRESQL_SCHEMA"),
stopifnot( "redshift" = Sys.getenv("CDMDDLBASE_REDSHIFT_SCHEMA"),
"ConnectionDetails" %in% class(connectionDetails), "sql server" = Sys.getenv("CDMDDLBASE_SQL_SERVER_CDM_SCHEMA"),
class(schema) == "character", "oracle" = Sys.getenv("CDMDDLBASE_ORACLE_CDM_SCHEMA"),
length(schema) == 1 "iris" = Sys.getenv("CDMDDLBASE_IRIS_CDM_SCHEMA")
) )
stopifnot(connectionDetails$dbms %in% c("postgresql", "redshift", "sql server", "oracle")) }
con <- DatabaseConnector::connect(connectionDetails)
on.exit(DatabaseConnector::disconnect(con)) listTablesInSchema <- function(connectionDetails, schema) {
dbms <- connectionDetails$dbms stopifnot(
if (dbms %in% c("postgresql", "redshift", "sql server")) { "ConnectionDetails" %in% class(connectionDetails),
tables <- class(schema) == "character",
dbGetQuery( length(schema) == 1
con, )
paste0( stopifnot(connectionDetails$dbms %in% c("postgresql", "redshift", "sql server", "oracle", "iris"))
"select table_name from information_schema.tables where table_schema = '", con <- DatabaseConnector::connect(connectionDetails)
schema, on.exit(DatabaseConnector::disconnect(con))
"'" dbms <- connectionDetails$dbms
) if (dbms %in% c("postgresql", "redshift", "sql server", "iris")) {
)[[1]] tables <-
} else if (dbms == "oracle") { dbGetQuery(
query <- con,
paste0( paste0(
"select table_name from all_tables where owner = '", "select table_name from information_schema.tables where table_schema = '",
toupper(schema), schema,
"' and tablespace_name = 'USERS'" "'"
) )
tables <- dbGetQuery(con, query)[[1]] )[[1]]
} } else if (dbms == "oracle") {
return(tables) query <-
} paste0(
"select table_name from all_tables where owner = '",
dropAllTablesFromSchema <- function(connectionDetails, schema) { toupper(schema),
stopifnot( "' and tablespace_name = 'USERS'"
"ConnectionDetails" %in% class(connectionDetails), )
class(schema) == "character", tables <- dbGetQuery(con, query)[[1]]
length(schema) == 1 }
) return(tables)
stopifnot(connectionDetails$dbms %in% c("postgresql", "redshift", "sql server", "oracle")) }
tableNames <- listTablesInSchema(connectionDetails, schema)
dropAllTablesFromSchema <- function(connectionDetails, schema) {
con <- DatabaseConnector::connect(connectionDetails) stopifnot(
on.exit(DatabaseConnector::disconnect(con)) "ConnectionDetails" %in% class(connectionDetails),
dbms <- connectionDetails$dbms class(schema) == "character",
if (dbms %in% c("redshift", "postgresql", "sql server")) { length(schema) == 1
for (tableName in tableNames) { )
DBI::dbExecute(con, paste( stopifnot(connectionDetails$dbms %in% c("postgresql", "redshift", "sql server", "oracle", "iris"))
"DROP TABLE IF EXISTS", tableNames <- listTablesInSchema(connectionDetails, schema)
paste(schema, tableName, sep = "."),
"CASCADE" con <- DatabaseConnector::connect(connectionDetails)
)) on.exit(DatabaseConnector::disconnect(con))
} dbms <- connectionDetails$dbms
} else if (dbms == "oracle") { if (dbms %in% c("redshift", "postgresql", "sql server", "iris")) {
for (tableName in tableNames) { for (tableName in tableNames) {
DBI::dbExecute(con, paste("DROP TABLE IF EXISTS", tableName, "CASCADE")) DBI::dbExecute(con, paste(
} "DROP TABLE IF EXISTS",
} paste(schema, tableName, sep = "."),
} "CASCADE"
))
}
} else if (dbms == "oracle") {
for (tableName in tableNames) {
DBI::dbExecute(con, paste("DROP TABLE IF EXISTS", tableName, "CASCADE"))
}
}
}