Skip to content

Commit

Permalink
Merge pull request #146 from OHDSI/getDbCohortMethodData-tests
Browse files Browse the repository at this point in the history
getDbCohortMethodData unit-tests
  • Loading branch information
schuemie authored Aug 28, 2023
2 parents 2e28004 + 56c3647 commit e1b07c6
Show file tree
Hide file tree
Showing 3 changed files with 847 additions and 10 deletions.
28 changes: 21 additions & 7 deletions R/DataLoadingSaving.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ getDbCohortMethodData <- function(connectionDetails,
checkmate::assertCharacter(exposureTable, len = 1, add = errorMessages)
checkmate::assertCharacter(outcomeDatabaseSchema, len = 1, add = errorMessages)
checkmate::assertCharacter(outcomeTable, len = 1, add = errorMessages)
checkmate::assertCharacter(cdmVersion, len = 1, add = errorMessages)
checkmate::assertCharacter(cdmVersion, len = 1, n.chars = 1, pattern = "^[5]$", add = errorMessages)
checkmate::assertLogical(firstExposureOnly, len = 1, add = errorMessages)
checkmate::assertChoice(removeDuplicateSubjects, c("keep all", "keep first", "remove all"), add = errorMessages)
checkmate::assertLogical(restrictToCommonPeriod, len = 1, add = errorMessages)
Expand All @@ -145,12 +145,9 @@ getDbCohortMethodData <- function(connectionDetails,
checkmate::assertList(covariateSettings, add = errorMessages)
checkmate::reportAssertions(collection = errorMessages)

if (is.null(studyStartDate)) {
studyStartDate <- ""
}
if (is.null(studyEndDate)) {
studyEndDate <- ""
}
dateCheck(studyStartDate)
dateCheck(studyEndDate)

if (studyStartDate != "" &&
regexpr("^[12][0-9]{3}[01][0-9][0-3][0-9]$", studyStartDate) == -1) {
stop("Study start date must have format YYYYMMDD")
Expand Down Expand Up @@ -469,3 +466,20 @@ handleCohortCovariateBuilders <- function(covariateSettings,
}
return(covariateSettings)
}

dateCheck <- function(date) {
if (date != "") {
tryCatch({
dateFormatted <- paste0(
substr(x = date, start = 1, stop = 4), "-",
substr(x = date, start = 5, stop = 6), "-",
substr(x = date, start = 7, stop = 8)
)

date <- as.Date(dateFormatted)
}, error = function(e) {
stop(sprintf("Date: %s (%s) is not valid", date, dateFormatted))
})
checkmate::assertDate(date, lower = "1000-01-01", upper = "2999-12-31")
}
}
18 changes: 15 additions & 3 deletions R/Viewer.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ uploadExportedResults <- function(connectionDetails,

ensureInstalled("ResultModelManager")
connection <- DatabaseConnector::connect(connectionDetails)
on.exit(DatabaseConnector::disconnect(connection))

withr::defer({
tryCatch({
DatabaseConnector::disconnect(connection)
}, error = function(e) {
message(e)
})
})

if (!append) {
# Create tables
Expand Down Expand Up @@ -137,8 +144,13 @@ uploadExportedResults <- function(connectionDetails,
dropTableIfExists = FALSE,
createTable = !append
)
on.exit(unlink(databaseIdentifierFile), add = TRUE)

withr::defer({
tryCatch({
unlink(databaseIdentifierFile)
}, error = function(e) {
message(e)
})
})
# Upload results
ResultModelManager::uploadResults(
connection = connection,
Expand Down
Loading

0 comments on commit e1b07c6

Please sign in to comment.