Add tests for oracle and sql server. Move setup.R file.

This commit is contained in:
Adam Black 2021-08-19 22:23:39 -04:00
parent 2b0acc8d53
commit cedf334df6
3 changed files with 144 additions and 159 deletions

View File

@ -1,47 +0,0 @@
# Download the JDBC drivers used in the tests
library(DatabaseConnector)
driverPath <- file.path(Sys.getenv("HOME"), "drivers")
if(!dir.exists(driverPath)) dir.create(driverPath)
if(Sys.getenv("LOCAL_TEST") != "TRUE") {
cat("downloading drivers")
downloadJdbcDrivers("all", pathToDriver = driverPath)
}
postgresDetails <- createConnectionDetails(
dbms = "postgresql",
user = Sys.getenv("CDMDDLBASE_POSTGRESQL_USER"),
password = Sys.getenv("CDMDDLBASE_POSTGRESQL_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_POSTGRESQL_SERVER"),
pathToDriver = file.path(Sys.getenv("HOME"), "drivers"),
cdmSchema = Sys.getenv("CDMDDLBASE_POSTGRESQL_SCHEMA")
)
redshiftDetails <- createConnectionDetails(
dbms = "redshift",
user = Sys.getenv("CDMDDLBASE_REDSHIFT_USER"),
password = Sys.getenv("CDMDDLBASE_REDSHIFT_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_REDSHIFT_SERVER"),
pathToDriver = file.path(Sys.getenv("HOME"), "drivers"),
cdmSchema = Sys.getenv("CDMDDLBASE_REDSHIFT_SCHEMA")
)
sqlserverDetails <- createConnectionDetails(
dbms = "sql server",
user = Sys.getenv("CDMDDLBASE_SQL_SERVER_USER"),
password = Sys.getenv("CDMDDLBASE_SQL_SERVER_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_SQL_SERVER_SERVER"),
pathToDriver = file.path(Sys.getenv("HOME"), "drivers"),
cdmSchema = Sys.getenv("CDMDDLBASE_SQL_SERVER_CDM_SCHEMA")
)
oracleDetails <- createConnectionDetails(
dbms = "oracle",
user = Sys.getenv("CDMDDLBASE_ORACLE_USER"),
password = Sys.getenv("CDMDDLBASE_ORACLE_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_ORACLE_SERVER"),
pathToDriver = file.path(Sys.getenv("HOME"), "drivers"),
cdmSchema = Sys.getenv("CDMDDLBASE_ORACLE_CDM_SCHEMA")
)

108
tests/testthat/setup.R Normal file
View File

@ -0,0 +1,108 @@
# Download the JDBC drivers used in the tests
library(DatabaseConnector)
driverPath <- file.path(Sys.getenv("HOME"), "drivers")
if(!dir.exists(driverPath)) dir.create(driverPath)
if(Sys.getenv("LOCAL_TEST") != "TRUE") {
cat("downloading drivers")
downloadJdbcDrivers("all", pathToDriver = driverPath)
}
# Helper functions used in tests -----------------------------------------------
getConnectionDetails <- function(dbms) {
switch (dbms,
"postgresql" = createConnectionDetails(
dbms = "postgresql",
user = Sys.getenv("CDMDDLBASE_POSTGRESQL_USER"),
password = Sys.getenv("CDMDDLBASE_POSTGRESQL_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_POSTGRESQL_SERVER"),
pathToDriver = file.path(Sys.getenv("HOME"), "drivers")
),
"redshift" = createConnectionDetails(
dbms = "redshift",
user = Sys.getenv("CDMDDLBASE_REDSHIFT_USER"),
password = Sys.getenv("CDMDDLBASE_REDSHIFT_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_REDSHIFT_SERVER"),
pathToDriver = file.path(Sys.getenv("HOME"), "drivers")
),
"sql server" = createConnectionDetails(
dbms = "sql server",
user = Sys.getenv("CDMDDLBASE_SQL_SERVER_USER"),
password = Sys.getenv("CDMDDLBASE_SQL_SERVER_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_SQL_SERVER_SERVER"),
pathToDriver = file.path(Sys.getenv("HOME"), "drivers")
),
"oracle" = createConnectionDetails(
dbms = "oracle",
user = Sys.getenv("CDMDDLBASE_ORACLE_USER"),
password = Sys.getenv("CDMDDLBASE_ORACLE_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_ORACLE_SERVER"),
pathToDriver = file.path(Sys.getenv("HOME"), "drivers")
)
)
}
getSchema <- function(dbms) {
switch (dbms,
"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")
)
}
listTablesInSchema <- function(connectionDetails, schema) {
stopifnot(class(connectionDetails) == "connectionDetails", class(schema) == "character", length(schema) == 1)
con <- DatabaseConnector::connect(connectionDetails)
on.exit(DatabaseConnector::disconnect(con))
dbms <- connectionDetails$dbms
if(dbms %in% c("postgresql", "redshift", "sql server")) {
tables <- dbGetQuery(con, paste0("select table_name from information_schema.tables where table_schema = '", schema, "'"))[[1]]
} else if(dbms == "oracle") {
query <- paste0("select table_name from all_tables where owner = '", toupper(schema), "' and tablespace_name = 'USERS'")
tables <- dbGetQuery(con, query)[[1]]
}
return(tables)
}
dropAllTablesFromSchema <- function(connectionDetails, schema) {
stopifnot(class(connectionDetails) == "connectionDetails", class(schema) == "character", length(schema) == 1)
tables <- listTablesInSchema(connectionDetails, schema)
con <- DatabaseConnector::connect(connectionDetails)
on.exit(DatabaseConnector::disconnect(con))
dbms <- connectionDetails$dbms
if(dbms %in% c("redshift", "postgresql", "sql server")) {
for(table in tables) {
DBI::dbRemoveTable(con, name = DBI::SQL(paste(schema, table, sep = ".")))
}
} else if(dbms == "oracle") {
for(table in tables) {
DBI::dbRemoveTable(con, name = table)
}
}
}
# .removeConstraintsPostgresql <- function(connectionDetails, schema) {
# # the order of removal of constraints matters!
# con <- DatabaseConnector::connect(connectionDetails)
# constraints <- DBI::dbGetQuery(con,
# "SELECT con.conname, rel.relname as relname
# FROM pg_catalog.pg_constraint con
# INNER JOIN pg_catalog.pg_class rel
# ON rel.oid = con.conrelid
# INNER JOIN pg_catalog.pg_namespace nsp
# ON nsp.oid = connamespace
# WHERE nsp.nspname = 'cdmddlbase';")
#
#
# constraints <- dplyr::mutate(constraints, sql = paste0("alter table ", schema, ".", relname, " drop constraint if exists ", conname, ";\n" ))
#
# sql <- paste(rev(constraints$sql), collapse = "")
# executeSql(con, sql)
#
# disconnect(con)
#
# }

View File

@ -1,126 +1,50 @@
library(DatabaseConnector)
# To run tests for one dbms and one cdmVersion use the variables below
# dbms <- "postgresql"; cdmVersion <- "5.4"
# Helper functions used in tests
.listTablesInSchema <- function(connectionDetails, schema) {
con <- DatabaseConnector::connect(connectionDetails)
tables <- DBI::dbListObjects(con, prefix = schema)
testDatabases <- c("oracle", "postgresql", "sql server", "redshift")
DatabaseConnector::disconnect(con)
tables <- subset(tables, is_prefix == FALSE)
tables <- subset(tables, grepl("table", table))$table
tables <- vapply(tables, function(.) .@name, FUN.VALUE = character(1))
return(tables)
}
.dropAllTablesFromSchema <- function(connectionDetails, schema) {
tables <- .listTablesInSchema(connectionDetails, schema)
con <- DatabaseConnector::connect(connectionDetails)
for(table in tables) {
DBI::dbRemoveTable(con, name = DBI::SQL(paste(schema, table, sep = ".")))
}
DatabaseConnector::disconnect(con)
}
# .removeConstraintsPostgresql <- function(connectionDetails, schema) {
# # the order of removal of constraints matters!
# con <- DatabaseConnector::connect(connectionDetails)
# constraints <- DBI::dbGetQuery(con,
# "SELECT con.conname, rel.relname as relname
# FROM pg_catalog.pg_constraint con
# INNER JOIN pg_catalog.pg_class rel
# ON rel.oid = con.conrelid
# INNER JOIN pg_catalog.pg_namespace nsp
# ON nsp.oid = connamespace
# WHERE nsp.nspname = 'cdmddlbase';")
#
#
# constraints <- dplyr::mutate(constraints, sql = paste0("alter table ", schema, ".", relname, " drop constraint if exists ", conname, ";\n" ))
#
# sql <- paste(rev(constraints$sql), collapse = "")
# executeSql(con, sql)
#
# disconnect(con)
#
# }
test_that("Execute DDL on Postgres", {
connectionDetails <- createConnectionDetails(
dbms = "postgresql",
user = Sys.getenv("CDMDDLBASE_POSTGRESQL_USER"),
password = Sys.getenv("CDMDDLBASE_POSTGRESQL_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_POSTGRESQL_SERVER"),
pathToDriver = file.path(Sys.getenv("HOME"), "drivers")
)
cdmDatabaseSchema <- Sys.getenv("CDMDDLBASE_POSTGRESQL_SCHEMA")
expect_error(con <- connect(connectionDetails), NA)
disconnect(con)
for(cdmVersion in listSupportedVersions()) {
# make sure the schema is cleared out
.dropAllTablesFromSchema(connectionDetails, cdmDatabaseSchema)
cat(paste("Connecting to schema", cdmDatabaseSchema, "\n"))
executeDdl(connectionDetails,
cdmVersion = cdmVersion,
cdmDatabaseSchema = cdmDatabaseSchema,
executeDdl = TRUE,
executePrimaryKey = TRUE,
executeForeignKey = FALSE)
tables <- .listTablesInSchema(connectionDetails, schema = cdmDatabaseSchema)
cdmTableCsvLoc <- system.file(file.path("csv", paste0("OMOP_CDMv", cdmVersion, "_Table_Level.csv")), package = "CommonDataModel", mustWork = TRUE)
tableSpecs <- read.csv(cdmTableCsvLoc, stringsAsFactors = FALSE)$cdmTableName
# check that the tables in the database match the tables in the specification
expect_equal(sort(tables), sort(tableSpecs))
# clear schema
.dropAllTablesFromSchema(connectionDetails, cdmDatabaseSchema)
test_that("getConnectionDetails works", {
for(dbms in testDatabases) {
expect_s3_class(getConnectionDetails(dbms), "connectionDetails")
}
})
test_that("Execute DDL on Redshift", {
test_that("getSchema works", {
for(dbms in testDatabases) {
expect_type(getSchema(dbms), "character")
}
})
connectionDetails <- createConnectionDetails(
dbms = "redshift",
user = Sys.getenv("CDMDDLBASE_REDSHIFT_USER"),
password = Sys.getenv("CDMDDLBASE_REDSHIFT_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_REDSHIFT_SERVER"),
pathToDriver = file.path(Sys.getenv("HOME"), "drivers")
)
for(dbms in testDatabases) {
connectionDetails <- getConnectionDetails(dbms)
cdmDatabaseSchema <- getSchema(dbms)
expect_error(con <- connect(connectionDetails), NA)
disconnect(con)
test_that(paste("Can connect to", dbms), {
expect_error(con <- connect(connectionDetails), NA)
disconnect(con)
})
cdmDatabaseSchema <- Sys.getenv("CDMDDLBASE_REDSHIFT_SCHEMA")
for(cdmVersion in listSupportedVersions()) {
# make sure the schema is cleared out
.dropAllTablesFromSchema(connectionDetails, cdmDatabaseSchema)
executeDdl(connectionDetails,
cdmVersion = cdmVersion,
cdmDatabaseSchema = cdmDatabaseSchema,
executeDdl = TRUE,
executePrimaryKey = TRUE,
executeForeignKey = FALSE)
test_that(paste("DDL", cdmVersion, "runs on", dbms), {
dropAllTablesFromSchema(connectionDetails, cdmDatabaseSchema)
tables <- listTablesInSchema(connectionDetails, cdmDatabaseSchema)
expect_equal(tables, character(0))
tables <- .listTablesInSchema(connectionDetails, schema = cdmDatabaseSchema)
executeDdl(connectionDetails,
cdmVersion = cdmVersion,
cdmDatabaseSchema = cdmDatabaseSchema,
executeDdl = TRUE,
executePrimaryKey = TRUE,
executeForeignKey = FALSE)
cdmTableCsvLoc <- system.file(file.path("csv", paste0("OMOP_CDMv", cdmVersion, "_Table_Level.csv")), package = "CommonDataModel", mustWork = TRUE)
tableSpecs <- read.csv(cdmTableCsvLoc, stringsAsFactors = FALSE)$cdmTableName
tables <- listTablesInSchema(connectionDetails, cdmDatabaseSchema)
cdmTableCsvLoc <- system.file(file.path("csv", paste0("OMOP_CDMv", cdmVersion, "_Table_Level.csv")), package = "CommonDataModel", mustWork = TRUE)
tableSpecs <- read.csv(cdmTableCsvLoc, stringsAsFactors = FALSE)$cdmTableName
# check that the tables in the database match the tables in the specification
expect_equal(sort(tables), sort(tableSpecs))
# clear schema
.dropAllTablesFromSchema(connectionDetails, cdmDatabaseSchema)
# check that the tables in the database match the tables in the specification
expect_equal(sort(tolower(tables)), sort(tolower(tableSpecs)))
dropAllTablesFromSchema(connectionDetails, cdmDatabaseSchema)
})
}
})
}