Skip to content

Commit

Permalink
Merge pull request #437 from OHDSI/v5.4_unittest
Browse files Browse the repository at this point in the history
Add more unit tests
  • Loading branch information
clairblacketer committed Aug 31, 2021
2 parents 5a27d02 + 3949b9e commit 9779c07
Show file tree
Hide file tree
Showing 85 changed files with 357 additions and 172 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
^\.Rproj\.user$
^LICENSE\.md$
.github/*
extras/*
12 changes: 12 additions & 0 deletions .github/workflows/R_CMD_check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ jobs:
CDMDDLBASE_POSTGRESQL_PASSWORD: ${{ secrets.CDMDDLBASE_POSTGRESQL_PASSWORD }}
CDMDDLBASE_POSTGRESQL_SERVER: ${{ secrets.CDMDDLBASE_POSTGRESQL_SERVER }}
CDMDDLBASE_POSTGRESQL_SCHEMA: ${{ secrets.CDMDDLBASE_POSTGRESQL_SCHEMA }}
CDMDDLBASE_REDSHIFT_USER: ${{ secrets.CDMDDLBASE_REDSHIFT_USER }}
CDMDDLBASE_REDSHIFT_PASSWORD: ${{ secrets.CDMDDLBASE_REDSHIFT_PASSWORD }}
CDMDDLBASE_REDSHIFT_SERVER: ${{ secrets.CDMDDLBASE_REDSHIFT_SERVER }}
CDMDDLBASE_REDSHIFT_SCHEMA: ${{ secrets.CDMDDLBASE_REDSHIFT_SCHEMA }}
CDMDDLBASE_SQL_SERVER_USER: ${{ secrets.CDMDDLBASE_SQL_SERVER_USER }}
CDMDDLBASE_SQL_SERVER_PASSWORD: ${{ secrets.CDMDDLBASE_SQL_SERVER_PASSWORD }}
CDMDDLBASE_SQL_SERVER_SERVER: ${{ secrets.CDMDDLBASE_SQL_SERVER_SERVER }}
CDMDDLBASE_SQL_SERVER_CDM_SCHEMA: ${{ secrets.CDMDDLBASE_SQL_SERVER_CDM_SCHEMA }}
CDMDDLBASE_ORACLE_USER: ${{ secrets.CDMDDLBASE_ORACLE_USER }}
CDMDDLBASE_ORACLE_PASSWORD: ${{ secrets.CDMDDLBASE_ORACLE_PASSWORD }}
CDMDDLBASE_ORACLE_SERVER: ${{ secrets.CDMDDLBASE_ORACLE_SERVER }}
CDMDDLBASE_ORACLE_CDM_SCHEMA: ${{ secrets.CDMDDLBASE_ORACLE_CDM_SCHEMA }}

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(buildRelease)
export(buildReleaseZip)
export(createDdl)
export(createForeignKeys)
Expand All @@ -13,3 +14,6 @@ export(writeDdl)
export(writeForeignKeys)
export(writeIndex)
export(writePrimaryKeys)
importFrom(utils,download.file)
importFrom(utils,read.csv)
importFrom(utils,write.csv)
2 changes: 1 addition & 1 deletion R/WikiParser.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#'
#' @param mdFilesLocation Path to the root folder of the Wiki repository.
#' @param output_file Path to where the output CSV file should be written.
#'
#' @importFrom utils write.csv
#' @export
parseWiki <- function(mdFilesLocation, output_file) {
# mdFilesLocation <- "../CommonDataModel.wiki"
Expand Down
39 changes: 25 additions & 14 deletions R/buildRelease.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,36 @@
#' Writes DDL, ForeignKey, PrimaryKey and index SQL files for given cdmVersion
#' and targetDialect to the 'ddl' folder in current working directory.
#'
#' @param cdmVersion The version of the CDM you are creating, e.g. 5.3, 5.4.
#' @param cdmVersions The versions of the CDM you are creating, e.g. 5.3, 5.4.
#' Defaults to all supported CDM versions.
#' @param targetDialect The target dialect
#'
buildRelease <- function(cdmVersion = listSupportedVersions(),
targetDialect = listSupportedDialects()){
for (cdmVersion in cdmVersion) {
for (targetDialect in targetDialect) {
#' @param targetDialects A character vector of target dialects.
#' Defaults to all supported dialects.
#' @param outputfolder The base folder where the SQL files will be written.
#' Subfolders will be created for each cdmVersion and targetDialect.
#' @export
buildRelease <- function(cdmVersions = listSupportedVersions(),
targetDialects = listSupportedDialects(),
outputfolder = file.path(getwd(), "inst", "ddl")){
basefolder <- outputfolder
for (cdmVersion in cdmVersions) {
for (targetDialect in targetDialects) {
outputfolder <- file.path(basefolder, cdmVersion, gsub(" ", "_", targetDialect))

writeDdl(targetDialect = targetDialect,
cdmVersion = cdmVersion)
cdmVersion = cdmVersion,
outputfolder = outputfolder)

writePrimaryKeys(targetDialect = targetDialect,
cdmVersion = cdmVersion)
cdmVersion = cdmVersion,
outputfolder = outputfolder)

writeForeignKeys(targetDialect = targetDialect,
cdmVersion = cdmVersion)
cdmVersion = cdmVersion,
outputfolder = outputfolder)

writeIndex(targetDialect = targetDialect,
cdmVersion = cdmVersion)
cdmVersion = cdmVersion,
outputfolder = outputfolder)
}
}
}
Expand All @@ -62,7 +73,7 @@ buildRelease <- function(cdmVersion = listSupportedVersions(),
#'
buildReleaseZip <- function(cdmVersion,
targetDialect = listSupportedDialects(),
outputfolder = "output"){
outputfolder = file.path(getwd(), "inst", "ddl")){
# argument checks
stopifnot(is.character(cdmVersion), length(cdmVersion) == 1, cdmVersion %in% listSupportedVersions())

Expand All @@ -72,8 +83,8 @@ buildReleaseZip <- function(cdmVersion,

files <- c()
for (dialect in targetDialect) {
buildRelease(cdmVersion, dialect)
files <- c(files, list.files(file.path('ddl', cdmVersion, gsub(" ", "_", dialect)),
buildRelease(cdmVersion, dialect, outputfolder = outputfolder)
files <- c(files, list.files(file.path(outputfolder, cdmVersion, gsub(" ", "_", dialect)),
pattern = ".*\\.sql$",
full.names = TRUE))
}
Expand Down
61 changes: 61 additions & 0 deletions R/createDdl.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#'
#' @param cdmVersion The version of the CDM you are creating, e.g. 5.3, 5.4
#' @return A character string containing the OHDSQL DDL
#' @importFrom utils read.csv
#' @export
#' @examples
#' ddl <- createDdl("5.4")
Expand Down Expand Up @@ -138,3 +139,63 @@ createForeignKeys <- function(cdmVersion){
}
return(paste0(sql_result, collapse = ""))
}



# A helper function that will return a character string with the omop ascii art given a major and minor cdm version
# example: cat(createAsciiHeader(5, 3))
createAsciiHeader <- function(major, minor) {

stopifnot(is.numeric(major), is.numeric(minor), length(major) == 1, length(minor) == 1)
stopifnot(major %in% 0:99, minor %in% 0:99)

# An inner function that returns an ascii art matrix for any number between 0 and 99
numberMatrix <- function(num){
stopifnot(is.numeric(num), num %in% 0:99)

# An inner function that returns a 7x7 matrix of number ascii art for the number 0 through 9
# for the number 1 a 7x5 matrix is returned because 1 is narrower than other numbers.
singleDigit <- function(num) {
nums <- c(' ### # ##### ##### # ####### ##### ####### ##### ##### # # ## # ## ## # # # ## # # ## ## # # # # ## # # # # # ## ## # # ##### ##### # # ###### ###### # ##### ####### # # # ######## ## # # # # # # # # # # # # # ## # # # ## # ### ##### ####### ##### # ##### ##### # ##### ##### ')
numsMatrix <- matrix(data = strsplit(nums, character(0))[[1]], nrow = 7, byrow = T)
cols <- seq(num*7+1, num*7+7, by = 1)
out <- numsMatrix[1:7, cols]
# the number 1 is narrower than the other numbers
if(num == 1) out<- out[1:7, 2:6]
out
}

if(num < 10){
return(singleDigit(num))
} else {
space <- matrix(rep(" ", 7), nrow = 7)
return(cbind(singleDigit(floor(num/10)), space, singleDigit(num %% 10)))
}
}

omop <- c('.
####### # # ####### ###### ##### ###### # # .
# # ## ## # # # # # # # # ## ## # #.
# # # # # # # # # # # # # # # # # # #.
# # # # # # # ###### # # # # # # # #.
# # # # # # # # # # # # # #.
# # # # # # # # # # # # # # # .
####### # # ####### # ##### ###### # # ## ')

# convert to matrix and remove first column
omop <- matrix(strsplit(omop, character(0))[[1]], nrow = 7, byrow = TRUE)
omop <- omop[,c(-1, -2)]

dot <- matrix(c(rep(" ", 3*4), rep("#", 3*3)), nrow = 7, byrow = TRUE)
space <- matrix(rep(" ", 7), nrow = 7)
newline <- matrix(rep("\n", 7, nrow = 7))


header <- character(0)
headerMatrix <- cbind(omop, space, numberMatrix(major), space, dot, space, numberMatrix(minor), newline)
for(i in 1:7) {
header <- c(header, as.character(headerMatrix[i,]))
}
header <- paste(header, collapse = "")
return(header)
}
2 changes: 1 addition & 1 deletion R/downloadCurrentDdl.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' downloadCurrentDdl("OHDSI/CommonDataModel",
#' pathToCsv="Sql%20Server/OMOP%20CDM%20sql%20server%20ddl.txt")
#' }
#'
#' @importFrom utils download.file
#' @export

downloadCurrentDdl <- function(githubPath="OHDSI/CommonDataModel",
Expand Down
14 changes: 7 additions & 7 deletions R/executeDdl.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ executeDdl <- function(connectionDetails,
executeForeignKey = TRUE,
...) {

outputpath <- tempdir(check = TRUE)
outputfolder <- tempdir(check = TRUE)


if(executeDdl) {
filename <- writeDdl(targetDialect = connectionDetails$dbms,
cdmVersion = cdmVersion,
cdmDatabaseSchema = cdmDatabaseSchema,
outputpath = outputpath)
outputfolder = outputfolder)

sql <- readr::read_file(file.path(outputpath, filename))
sql <- readr::read_file(file.path(outputfolder, filename))
} else {
sql <- ""
}
Expand All @@ -60,18 +60,18 @@ executeDdl <- function(connectionDetails,
filename <- writePrimaryKeys(targetDialect = connectionDetails$dbms,
cdmVersion = cdmVersion,
cdmDatabaseSchema = cdmDatabaseSchema,
outputpath = outputpath)
outputfolder = outputfolder)

sql <- paste(sql, readr::read_file(file.path(outputpath, filename)), sep = "\n")
sql <- paste(sql, readr::read_file(file.path(outputfolder, filename)), sep = "\n")
}

if(executeForeignKey) {
filename <- writeForeignKeys(targetDialect = connectionDetails$dbms,
cdmVersion = cdmVersion,
cdmDatabaseSchema = cdmDatabaseSchema,
outputpath = outputpath)
outputfolder = outputfolder)

sql <- paste(sql, readr::read_file(file.path(outputpath, filename)), sep = "\n")
sql <- paste(sql, readr::read_file(file.path(outputfolder, filename)), sep = "\n")
}

con <- DatabaseConnector::connect(connectionDetails = connectionDetails)
Expand Down
2 changes: 1 addition & 1 deletion R/listSupportedVersions.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' List CDM versions supported by this package
#'
#' @return A character vector containing the support CDM versions in {major}.{minor} format.
#' @export

listSupportedVersions <- function() {
supportedVersions <- c("5.3", "5.4")
return(supportedVersions)
Expand Down
42 changes: 21 additions & 21 deletions R/writeDDL.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,102 +22,102 @@
#'
#' @param targetDialect The dialect of the target database. Choices are "oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"
#' @param cdmVersion The version of the CDM you are creating, e.g. 5.3, 5.4
#' @param outputpath The directory or folder where the SQL file should be saved.
#' @param outputfolder The directory or folder where the SQL file should be saved.
#' @param cdmDatabaseSchema The schema of the CDM instance where the DDL will be run. For example, this would be "ohdsi.dbo" when testing on sql server.
#' Defaults to "@cdmDatabaseSchema"
#'
#' @export
writeDdl <- function(targetDialect, cdmVersion, outputpath, cdmDatabaseSchema = "@cdmDatabaseSchema") {
writeDdl <- function(targetDialect, cdmVersion, outputfolder, cdmDatabaseSchema = "@cdmDatabaseSchema") {

# argument checks
stopifnot(targetDialect %in% c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"))
stopifnot(cdmVersion %in% listSupportedVersions())
stopifnot(is.character(cdmDatabaseSchema))

if(missing(outputpath)) {
outputpath <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
if(missing(outputfolder)) {
outputfolder <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
}

if(!dir.exists(outputpath)) dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
if(!dir.exists(outputfolder)) dir.create(outputfolder, showWarnings = FALSE, recursive = TRUE)

sql <- createDdl(cdmVersion)
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema, targetDialect = targetDialect)
sql <- SqlRender::translate(sql, targetDialect = targetDialect)

filename <- paste("OMOPCDM", gsub(" ", "_", targetDialect), cdmVersion, "ddl.sql", sep = "_")
SqlRender::writeSql(sql = sql, targetFile = file.path(outputpath, filename))
SqlRender::writeSql(sql = sql, targetFile = file.path(outputfolder, filename))
invisible(filename)
}

#' @describeIn writeDdl writePrimaryKeys Write the SQL code that creates the primary keys to a file.
#' @export
writePrimaryKeys <- function(targetDialect, cdmVersion, outputpath, cdmDatabaseSchema = "@cdmDatabaseSchema") {
writePrimaryKeys <- function(targetDialect, cdmVersion, outputfolder, cdmDatabaseSchema = "@cdmDatabaseSchema") {

# argument checks
stopifnot(targetDialect %in% c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"))
stopifnot(cdmVersion %in% listSupportedVersions())
stopifnot(is.character(cdmDatabaseSchema))

if(missing(outputpath)) {
outputpath <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
if(missing(outputfolder)) {
outputfolder <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
}

if(!dir.exists(outputpath)) dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
if(!dir.exists(outputfolder)) dir.create(outputfolder, showWarnings = FALSE, recursive = TRUE)

sql <- createPrimaryKeys(cdmVersion)
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema, targetDialect = targetDialect)
sql <- SqlRender::translate(sql, targetDialect = targetDialect)

filename <- paste("OMOPCDM", gsub(" ", "_", targetDialect), cdmVersion, "primary", "keys.sql", sep = "_")
SqlRender::writeSql(sql = sql, targetFile = file.path(outputpath, filename))
SqlRender::writeSql(sql = sql, targetFile = file.path(outputfolder, filename))
invisible(filename)
}

#' @describeIn writeDdl writeForeignKeys Write the SQL code that creates the foreign keys to a file.
#' @export
writeForeignKeys <- function(targetDialect, cdmVersion, outputpath, cdmDatabaseSchema = "@cdmDatabaseSchema") {
writeForeignKeys <- function(targetDialect, cdmVersion, outputfolder, cdmDatabaseSchema = "@cdmDatabaseSchema") {

# argument checks
stopifnot(targetDialect %in% c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"))
stopifnot(cdmVersion %in% listSupportedVersions())
stopifnot(is.character(cdmDatabaseSchema))

if(missing(outputpath)) {
outputpath <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
if(missing(outputfolder)) {
outputfolder <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
}

if(!dir.exists(outputpath)) dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
if(!dir.exists(outputfolder)) dir.create(outputfolder, showWarnings = FALSE, recursive = TRUE)

sql <- createForeignKeys(cdmVersion)
sql <- SqlRender::render(sql = sql, cdmDatabaseSchema = cdmDatabaseSchema, targetDialect = targetDialect)
sql <- SqlRender::translate(sql, targetDialect = targetDialect)

filename <- paste("OMOPCDM", gsub(" ", "_", targetDialect), cdmVersion, "constraints.sql", sep = "_")
SqlRender::writeSql(sql = sql, targetFile = file.path(outputpath, filename))
SqlRender::writeSql(sql = sql, targetFile = file.path(outputfolder, filename))
invisible(filename)
}

#' @describeIn writeDdl writeIndex Write the rendered and translated sql that creates recommended indexes to a file.
#' @export
writeIndex <- function(targetDialect, cdmVersion, outputpath, cdmDatabaseSchema = "@cdmDatabaseSchema") {
writeIndex <- function(targetDialect, cdmVersion, outputfolder, cdmDatabaseSchema = "@cdmDatabaseSchema") {

# argument checks
stopifnot(targetDialect %in% c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server"))
stopifnot(cdmVersion %in% listSupportedVersions())
stopifnot(is.character(cdmDatabaseSchema))

if(missing(outputpath)) {
outputpath <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
if(missing(outputfolder)) {
outputfolder <- file.path("ddl", cdmVersion, gsub(" ", "_", targetDialect))
}

if(!dir.exists(outputpath)) dir.create(outputpath, showWarnings = FALSE, recursive = TRUE)
if(!dir.exists(outputfolder)) dir.create(outputfolder, showWarnings = FALSE, recursive = TRUE)

sqlFilename <- paste0("OMOP_CDM_indices_v", cdmVersion, ".sql")
sql <- readr::read_file(system.file(file.path("sql", "sql_server", sqlFilename), package = "CommonDataModel"))
sql <- SqlRender::render(sql, targetDialect = targetDialect, cdmDatabaseSchema = cdmDatabaseSchema)
sql <- SqlRender::translate(sql, targetDialect = targetDialect)

filename <- paste("OMOPCDM", gsub(" ", "_", targetDialect), cdmVersion, "indices.sql", sep = "_")
SqlRender::writeSql(sql = sql, targetFile = file.path(outputpath, filename))
SqlRender::writeSql(sql = sql, targetFile = file.path(outputfolder, filename))
invisible(filename)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 9 additions & 4 deletions man/buildRelease.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9779c07

Please sign in to comment.