Skip to content

Commit

Permalink
checkmate parameters
Browse files Browse the repository at this point in the history
ref #190
  • Loading branch information
wibeasley committed May 31, 2018
1 parent c8bf54d commit 1686b2e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
10 changes: 9 additions & 1 deletion R/project-simple.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

populate_project_simple <- function( batch = FALSE ) {
checkmate::assert_logical(batch, any.missing=F, len=1)

if( !requireNamespace("testthat") ) stop("The function REDCapR:::populate_project_simple() cannot run if the `testthat` package is not installed. Please install it and try again.")

#Declare the server & user information
Expand Down Expand Up @@ -30,7 +32,7 @@ populate_project_simple <- function( batch = FALSE ) {
#Import the data into the REDCap project
testthat::expect_message(
returned_object <- if( batch ) {
REDCapR::redcap_write(ds=dsToWrite, redcap_uri=uri, token=token, verbose=TRUE)
REDCapR::redcap_write( ds=dsToWrite, redcap_uri=uri, token=token, verbose=TRUE)
} else {
REDCapR::redcap_write_oneshot(ds=dsToWrite, redcap_uri=uri, token=token, verbose=TRUE)
}
Expand Down Expand Up @@ -62,6 +64,9 @@ clear_project_simple <- function( verbose = TRUE ) {
}

clean_start_simple <- function( batch = FALSE, delay_in_seconds = 1 ) {
checkmate::assert_logical( batch , any.missing=F, len=1)
checkmate::assert_numeric( delay_in_seconds , any.missing=F, len=1, lower=0)

if( !requireNamespace("testthat") ) stop("The function REDCapR:::populate_project_simple() cannot run if the `testthat` package is not installed. Please install it and try again.")
testthat::expect_message(
clear_result <- clear_project_simple(),
Expand All @@ -81,6 +86,9 @@ clean_start_simple <- function( batch = FALSE, delay_in_seconds = 1 ) {
}

upload_file_simple <- function( redcap_uri, token=token ) {
checkmate::assert_character(redcap_uri, any.missing=F, len=1, min.chars = 5)
checkmate::assert_character(token , any.missing=F, len=1, pattern="^\\w{32}$")

records <- 1:5
file_paths <- base::file.path(pkgload::inst(name="REDCapR"), paste0("test-data/mugshot-", records, ".jpg"))

Expand Down
5 changes: 5 additions & 0 deletions R/redcap-column-sanitize.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ redcap_column_sanitize <- function(
encoding_initial = "latin1",
substitution_character = "?" ) {

checkmate::assert_data_frame(d , any.missing=F)
checkmate::assert_character( column_names , any.missing=F)
checkmate::assert_character( encoding_initial , any.missing=F, len=1)
checkmate::assert_character( substitution_character , any.missing=F, len=1, pattern="^.$")

for( column in column_names ) {
d[[column]] <- base::iconv(
x = d[[column]],
Expand Down
10 changes: 9 additions & 1 deletion R/validate.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
#' )
#' validate_for_write(d = d)


validate_no_logical <- function( data_types, stop_on_error=FALSE ) {
checkmate::assert_character(data_types, any.missing=F, min.len=1, min.chars=2)
checkmate::assert_logical(stop_on_error, any.missing=F, len=1)

# indices <- which(sapply(d, class)=="logical")
indices <- which(data_types=="logical")

Expand All @@ -64,7 +66,11 @@ validate_no_logical <- function( data_types, stop_on_error=FALSE ) {
)
}
}

validate_field_names <- function( field_names, stop_on_error=FALSE ) {
checkmate::assert_character(field_names, any.missing=F, null.ok=T, min.len=1, min.chars=2)
checkmate::assert_logical(stop_on_error, any.missing=F, len=1)

pattern <- "^[0-9a-z_]+$"

indices <- which(!grepl(pattern, x=field_names, perl=TRUE))
Expand All @@ -88,6 +94,8 @@ validate_field_names <- function( field_names, stop_on_error=FALSE ) {
}

validate_for_write <- function( d ) {
checkmate::assert_data_frame(d, any.missing=F)

lst_concerns <- list(
validate_no_logical(sapply(d, class)),
validate_field_names(colnames(d))
Expand Down

0 comments on commit 1686b2e

Please sign in to comment.