add tests for write and execute functions

This commit is contained in:
Adam Black 2021-08-18 23:35:38 -04:00
parent e6a2940474
commit ae921ede24
2 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,78 @@
library(DatabaseConnector)
connectionDetails <- createConnectionDetails(
dbms = "postgresql",
user = Sys.getenv("CDMDDLBASE_POSTGRESQL_USER"),
password = Sys.getenv("CDMDDLBASE_POSTGRESQL_PASSWORD"),
server = Sys.getenv("CDMDDLBASE_POSTGRESQL_SERVER")
)
# Helper functions used in tests
.listTablesInSchema <- function(connectionDetails, schema) {
con <- DatabaseConnector::connect(connectionDetails)
tables <- DBI::dbListObjects(con, prefix = "cdmddlbase")
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", {
# make sure the schema is cleared out
cdmDatabaseSchema <- Sys.getenv("CDMDDLBASE_POSTGRESQL_SCHEMA")
cdmVersion <- "5.4"
# .removeConstraintsPostgresql(connectionDetails, cdmDatabaseSchema)
.dropAllTablesFromSchema(connectionDetails, cdmDatabaseSchema)
con <- connect(connectionDetails)
executeDdl(connectionDetails,
cdmVersion = cdmVersion,
cdmDatabaseSchema = cdmDatabaseSchema,
executeDdl = TRUE,
executePrimaryKey = FALSE,
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
# .removeConstraintsPostgresql(connectionDetails, cdmDatabaseSchema)
.dropAllTablesFromSchema(connectionDetails, cdmDatabaseSchema)
})

View File

@ -0,0 +1,51 @@
test_that("writeDdl works", {
outputpath <- tempdir(TRUE)
filename <- writeDdl(targetDialect = "postgresql", cdmVersion = "5.4", outputpath = outputpath)
expect_true(file.exists(file.path(outputpath, filename)))
sql <- readr::read_file(file.path(outputpath, filename))
expect_type(sql, "character")
expect_gt(nchar(sql), 10)
})
test_that("writePrimaryKeys works", {
outputpath <- tempdir(TRUE)
filename <- writePrimaryKeys(targetDialect = "postgresql", cdmVersion = "5.4", outputpath = outputpath)
expect_true(file.exists(file.path(outputpath, filename)))
sql <- readr::read_file(file.path(outputpath, filename))
expect_type(sql, "character")
expect_gt(nchar(sql), 10)
})
test_that("writeForeignKeys works", {
outputpath <- tempdir(TRUE)
filename <- writeForeignKeys(targetDialect = "postgresql", cdmVersion = "5.4", outputpath = outputpath)
expect_true(file.exists(file.path(outputpath, filename)))
sql <- readr::read_file(file.path(outputpath, filename))
expect_type(sql, "character")
expect_gt(nchar(sql), 10)
})
test_that("writeIndex works", {
outputpath <- tempdir(TRUE)
filename <- writeIndex(targetDialect = "postgresql", cdmVersion = "5.4", outputpath = outputpath)
expect_true(file.exists(file.path(outputpath, filename)))
sql <- readr::read_file(file.path(outputpath, filename))
expect_type(sql, "character")
expect_gt(nchar(sql), 10)
})