From ae921ede2450eb36ee4664ed51ab62cfa145738e Mon Sep 17 00:00:00 2001 From: Adam Black Date: Wed, 18 Aug 2021 23:35:38 -0400 Subject: [PATCH] add tests for write and execute functions --- tests/testthat/test-executeDdl.R | 78 ++++++++++++++++++++++++++++++++ tests/testthat/test-writeDdl.R | 51 +++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 tests/testthat/test-executeDdl.R create mode 100644 tests/testthat/test-writeDdl.R diff --git a/tests/testthat/test-executeDdl.R b/tests/testthat/test-executeDdl.R new file mode 100644 index 0000000..b2d43eb --- /dev/null +++ b/tests/testthat/test-executeDdl.R @@ -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) +}) diff --git a/tests/testthat/test-writeDdl.R b/tests/testthat/test-writeDdl.R new file mode 100644 index 0000000..1c95f2b --- /dev/null +++ b/tests/testthat/test-writeDdl.R @@ -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) + +})