diff --git a/R/classDS.R b/R/classDS.R index 16720b80..a33e49a3 100644 --- a/R/classDS.R +++ b/R/classDS.R @@ -5,18 +5,13 @@ #' @param x a string character, the name of an object #' @return the class of the input object #' @author Stuart Wheater, for DataSHIELD Development Team +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export #' classDS <- function(x){ - - x.val <- eval(parse(text=x), envir = parent.frame()) - - # find the class of the input object + x.val <- .loadServersideObject(x) out <- class(x.val) - - # return the class return(out) - } #AGGREGATE FUNCTION # classDS diff --git a/R/completeCasesDS.R b/R/completeCasesDS.R index 6e1837f6..25e6e1b5 100644 --- a/R/completeCasesDS.R +++ b/R/completeCasesDS.R @@ -31,6 +31,7 @@ #' without problems no studysideMessage will have been saved and ds.message("newobj") #' will return the message: "ALL OK: there are no studysideMessage(s) on this datasource". #' @author Paul Burton for DataSHIELD Development Team +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export #' completeCasesDS <- function(x1.transmit){ @@ -111,10 +112,9 @@ completeCasesDS <- function(x1.transmit){ } #Activate target object - #x1.transmit is the name of a serverside data.frame, matrix or vector - x1.use <- eval(parse(text=x1.transmit), envir = parent.frame()) + x1.use <- .loadServersideObject(x1.transmit) complete.rows <- stats::complete.cases(x1.use) - + if(is.matrix(x1.use) || is.data.frame(x1.use)){ output.object <- x1.use[complete.rows,] }else if(is.atomic(x1.use) || is.factor(x1.use)){ diff --git a/R/dimDS.R b/R/dimDS.R index 3b51ed49..c27db5b5 100644 --- a/R/dimDS.R +++ b/R/dimDS.R @@ -3,20 +3,16 @@ #' @description This function is similar to R function \code{dim}. #' @details The function returns the dimension of the input dataframe or matrix #' @param x a string character, the name of a dataframe or matrix -#' @return the dimension of the input object +#' @return a list with two elements: \code{dim} (the dimension of the input object) +#' and \code{class} (the class of the input object, for client-side consistency checking) #' @author Demetris Avraam, for DataSHIELD Development Team +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export #' dimDS <- function(x){ - - x.var <- eval(parse(text=x), envir = parent.frame()) - - # find the dim of the input dataframe or matrix - out <- dim(x.var) - - # return the dimension - return(out) - + x.val <- .loadServersideObject(x) + .checkClass(obj = x.val, obj_name = x, permitted_classes = c("data.frame", "matrix")) + list(dim = dim(x.val), class = class(x.val)) } #AGGREGATE FUNCTION # dimDS diff --git a/R/isNaDS.R b/R/isNaDS.R index 917c420b..3c73f019 100644 --- a/R/isNaDS.R +++ b/R/isNaDS.R @@ -1,19 +1,20 @@ -#' -#' @title Checks if a vector is empty -#' @description this function is similar to R function \code{is.na} but instead of a vector +#' +#' @title Checks if a vector is empty +#' @description this function is similar to R function \code{is.na} but instead of a vector #' of booleans it returns just one boolean to tell if all the element are missing values. -#' @param xvect a numerical or character vector -#' @return the integer '1' if the vector contains on NAs and '0' otherwise +#' @param x a character string, the name of a server-side vector +#' @return a list with two elements: \code{is.na} (TRUE if the vector contains +#' only NAs, FALSE otherwise) and \code{class} (the class of the input object, +#' for client-side consistency checking) #' @author Gaye, A. +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export #' -isNaDS <- function(xvect){ - +isNaDS <- function(x){ + xvect <- .loadServersideObject(x) + .checkClass(obj = xvect, obj_name = x, permitted_classes = c("character", "factor", "integer", "logical", "numeric", "data.frame", "matrix")) out <- is.na(xvect) total <- sum(out, na.rm=TRUE) - if(total==(1*length(out))){ - return(TRUE) - }else{ - return(FALSE) - } + is_na <- total == (1 * length(out)) + list(is.na = is_na, class = class(xvect)) } diff --git a/R/lengthDS.R b/R/lengthDS.R index 7e4b8997..1c793aa0 100644 --- a/R/lengthDS.R +++ b/R/lengthDS.R @@ -3,20 +3,17 @@ #' @description This function is similar to R function \code{length}. #' @details The function returns the length of the input vector or list. #' @param x a string character, the name of a vector or list -#' @return a numeric, the number of elements of the input vector or list. +#' @return a list with two elements: \code{length} (the number of elements of the input +#' vector or list) and \code{class} (the class of the input object, for client-side +#' consistency checking) #' @author Demetris Avraam, for DataSHIELD Development Team +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export #' lengthDS <- function(x){ - - x.var <- eval(parse(text=x), envir = parent.frame()) - - # find the length of the input vector or list - out <- length(x.var) - - # return output length - return(out) - + x.val <- .loadServersideObject(x) + .checkClass(obj = x.val, obj_name = x, permitted_classes = c("character", "factor", "integer", "logical", "numeric", "list", "data.frame")) + list(length = length(x.val), class = class(x.val)) } #AGGREGATE FUNCTION # lengthDS diff --git a/R/levelsDS.R b/R/levelsDS.R index bdb374d5..33c33ec6 100644 --- a/R/levelsDS.R +++ b/R/levelsDS.R @@ -3,42 +3,36 @@ #' @description This function is similar to R function \code{levels}. #' @details The function returns the levels of the input vector or list. #' @param x a factor vector -#' @return a list, the factor levels present in the vector +#' @return a list with one element: \code{Levels} (the factor levels present +#' in the vector) #' @author Alex Westerberg, for DataSHIELD Development Team +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export #' levelsDS <- function(x){ - + + x.val <- .loadServersideObject(x) + .checkClass(obj = x.val, obj_name = x, permitted_classes = "factor") + # Check Permissive Privacy Control Level. dsBase::checkPermissivePrivacyControlLevel(c('permissive', 'banana', 'carrot')) - + ################################################################## #MODULE 1: CAPTURE THE nfilter SETTINGS # thr <- dsBase::listDisclosureSettingsDS() # - #nfilter.tab <- as.numeric(thr$nfilter.tab) # - #nfilter.glm <- as.numeric(thr$nfilter.glm) # - #nfilter.subset <- as.numeric(thr$nfilter.subset) # - #nfilter.string <- as.numeric(thr$nfilter.string) # - #nfilter.stringShort <- as.numeric(thr$nfilter.stringShort) # - #nfilter.kNN <- as.numeric(thr$nfilter.kNN) # - #nfilter.noise <- as.numeric(thr$nfilter.noise) # nfilter.levels.density <- as.numeric(thr$nfilter.levels.density) # - #nfilter.levels.max <- as.numeric(thr$nfilter.levels.max) # ################################################################## - + # find the levels of the input vector - out <- levels(x) - input.length <- length(x) + out <- levels(x.val) + input.length <- length(x.val) output.length <- length(out) - studysideMessage <- "VALID ANALYSIS" if((input.length * nfilter.levels.density) < output.length) { - out <- NA - studysideMessage <- "FAILED: Result length less than nfilter.levels.density of input length." - stop(studysideMessage, call. = FALSE) + stop("FAILED: Result length less than nfilter.levels.density of input length.", call. = FALSE) } - - out.obj <- list(Levels=out,ValidityMessage=studysideMessage) + + out.obj <- list(Levels=out) return(out.obj) } #AGGREGATE FUNCTION diff --git a/R/namesDS.R b/R/namesDS.R index 144c7270..a32aa916 100644 --- a/R/namesDS.R +++ b/R/namesDS.R @@ -16,6 +16,7 @@ #' @return \code{namesDS} returns to the client-side the names #' of a list object stored on the server-side. #' @author Amadou Gaye, updated by Paul Burton 25/06/2020 +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export #' namesDS <- function(xname.transmit){ @@ -50,14 +51,14 @@ nfilter.stringShort<-as.numeric(thr$nfilter.stringShort) # stop(studysideMessage, call. = FALSE) } - list.obj<-eval(parse(text=xname.transmit), envir = parent.frame()) - - trace.message<-class(list.obj) - + list.obj <- .loadServersideObject(xname.transmit) if(!is.list(list.obj)){ - error.message <- "The input object is not of class " - stop(paste0(error.message,trace.message), call. = FALSE) + stop( + "The input object is not of class . '", xname.transmit, "' is type ", + paste(class(list.obj), collapse = ", "), + call. = FALSE + ) } diff --git a/R/numNaDS.R b/R/numNaDS.R index 5f369b90..95011e25 100644 --- a/R/numNaDS.R +++ b/R/numNaDS.R @@ -1,15 +1,17 @@ -#' +#' #' @title Counts the number of missing values -#' @description this function just counts the number of missing entries -#' in a vector. -#' @param xvect a vector -#' @return an integer, the number of missing values +#' @description this function just counts the number of missing entries +#' in a vector. +#' @param x a character string, the name of a server-side vector +#' @return a list with two elements: \code{numNA} (an integer, the number of +#' missing values) and \code{class} (the class of the input object, for +#' client-side consistency checking) #' @author Gaye, A. +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export #' -numNaDS <- function(xvect){ - +numNaDS <- function(x){ + xvect <- .loadServersideObject(x) out <- length(which(is.na(xvect))) - return (out) - + list(numNA = out, class = class(xvect)) } diff --git a/R/uniqueDS.R b/R/uniqueDS.R index 6834ff8a..23290d3b 100644 --- a/R/uniqueDS.R +++ b/R/uniqueDS.R @@ -6,26 +6,12 @@ #' @return the object specified by the \code{newobj} argument #' which is written to the server-side. #' @author Stuart Wheater for DataSHIELD Development Team +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export #' uniqueDS <- function(x.name.transmit = NULL){ - # Check 'x.name.transmit' contains a name - if (is.null(x.name.transmit)) - stop("Variable's name can't be NULL", call. = FALSE) - - if ((! is.character(x.name.transmit)) || (length(x.name.transmit) != 1)) - stop("Variable's name isn't a single character vector", call. = FALSE) - - # Check object exists - x.value <- eval(parse(text=x.name.transmit), envir = parent.frame()) - - if (is.null(x.value)) - stop("Variable can't be NULL", call. = FALSE) - - # Compute the unique's value + x.value <- .loadServersideObject(x.name.transmit) out <- base::unique(x.value) - - # assign the outcome to the data servers return(out) } # ASSIGN FUNCTION diff --git a/man/classDS.Rd b/man/classDS.Rd index c1a51f83..030958cf 100644 --- a/man/classDS.Rd +++ b/man/classDS.Rd @@ -20,4 +20,6 @@ The function returns the class of an object } \author{ Stuart Wheater, for DataSHIELD Development Team + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/completeCasesDS.Rd b/man/completeCasesDS.Rd index 792c73a0..8bc0ed08 100644 --- a/man/completeCasesDS.Rd +++ b/man/completeCasesDS.Rd @@ -47,4 +47,6 @@ under help("complete.cases") in native R. } \author{ Paul Burton for DataSHIELD Development Team + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/dimDS.Rd b/man/dimDS.Rd index c14d82af..f7119f68 100644 --- a/man/dimDS.Rd +++ b/man/dimDS.Rd @@ -10,7 +10,8 @@ dimDS(x) \item{x}{a string character, the name of a dataframe or matrix} } \value{ -the dimension of the input object +a list with two elements: \code{dim} (the dimension of the input object) + and \code{class} (the class of the input object, for client-side consistency checking) } \description{ This function is similar to R function \code{dim}. @@ -20,4 +21,6 @@ The function returns the dimension of the input dataframe or matrix } \author{ Demetris Avraam, for DataSHIELD Development Team + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/isNaDS.Rd b/man/isNaDS.Rd index b4954850..faae7cfb 100644 --- a/man/isNaDS.Rd +++ b/man/isNaDS.Rd @@ -4,18 +4,22 @@ \alias{isNaDS} \title{Checks if a vector is empty} \usage{ -isNaDS(xvect) +isNaDS(x) } \arguments{ -\item{xvect}{a numerical or character vector} +\item{x}{a character string, the name of a server-side vector} } \value{ -the integer '1' if the vector contains on NAs and '0' otherwise +a list with two elements: \code{is.na} (TRUE if the vector contains + only NAs, FALSE otherwise) and \code{class} (the class of the input object, + for client-side consistency checking) } \description{ -this function is similar to R function \code{is.na} but instead of a vector +this function is similar to R function \code{is.na} but instead of a vector of booleans it returns just one boolean to tell if all the element are missing values. } \author{ Gaye, A. + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/lengthDS.Rd b/man/lengthDS.Rd index 75498994..18a6a32e 100644 --- a/man/lengthDS.Rd +++ b/man/lengthDS.Rd @@ -10,7 +10,9 @@ lengthDS(x) \item{x}{a string character, the name of a vector or list} } \value{ -a numeric, the number of elements of the input vector or list. +a list with two elements: \code{length} (the number of elements of the input + vector or list) and \code{class} (the class of the input object, for client-side + consistency checking) } \description{ This function is similar to R function \code{length}. @@ -20,4 +22,6 @@ The function returns the length of the input vector or list. } \author{ Demetris Avraam, for DataSHIELD Development Team + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/levelsDS.Rd b/man/levelsDS.Rd index 7046a117..c54b7d13 100644 --- a/man/levelsDS.Rd +++ b/man/levelsDS.Rd @@ -10,7 +10,9 @@ levelsDS(x) \item{x}{a factor vector} } \value{ -a list, the factor levels present in the vector +a list with two elements: \code{Levels} (the factor levels present + in the vector) and \code{class} (the class of the input object, for + client-side consistency checking) } \description{ This function is similar to R function \code{levels}. @@ -20,4 +22,6 @@ The function returns the levels of the input vector or list. } \author{ Alex Westerberg, for DataSHIELD Development Team + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/namesDS.Rd b/man/namesDS.Rd index 951bfdd0..8eb4ad0c 100644 --- a/man/namesDS.Rd +++ b/man/namesDS.Rd @@ -31,4 +31,6 @@ is formally of double class "glm" and "ls" but responds TRUE to is.list(), } \author{ Amadou Gaye, updated by Paul Burton 25/06/2020 + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/numNaDS.Rd b/man/numNaDS.Rd index 0162a630..572507fa 100644 --- a/man/numNaDS.Rd +++ b/man/numNaDS.Rd @@ -4,18 +4,22 @@ \alias{numNaDS} \title{Counts the number of missing values} \usage{ -numNaDS(xvect) +numNaDS(x) } \arguments{ -\item{xvect}{a vector} +\item{x}{a character string, the name of a server-side vector} } \value{ -an integer, the number of missing values +a list with two elements: \code{numNA} (an integer, the number of + missing values) and \code{class} (the class of the input object, for + client-side consistency checking) } \description{ -this function just counts the number of missing entries +this function just counts the number of missing entries in a vector. } \author{ Gaye, A. + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/uniqueDS.Rd b/man/uniqueDS.Rd index 4168fd1b..4efedf80 100644 --- a/man/uniqueDS.Rd +++ b/man/uniqueDS.Rd @@ -21,4 +21,6 @@ The function computes the uniques values of a variable. } \author{ Stuart Wheater for DataSHIELD Development Team + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/tests/testthat/test-arg-uniqueDS.R b/tests/testthat/test-arg-uniqueDS.R index 48d6bd48..bff02d5d 100644 --- a/tests/testthat/test-arg-uniqueDS.R +++ b/tests/testthat/test-arg-uniqueDS.R @@ -19,25 +19,19 @@ # Tests # -# context("uniqueDS::arg::simple null argument") -test_that("simple uniqueDS for NULL", { - expect_error(uniqueDS(NULL), "Variable's name can't be NULL", fixed = TRUE) -}) - -# context("uniqueDS::arg::null value") -test_that("simple uniqueDS for NULL", { - input <- NULL - expect_error(uniqueDS("input"), "Variable can't be NULL", fixed = TRUE) +# context("uniqueDS::arg::null argument") +test_that("uniqueDS errors for NULL argument", { + expect_error(uniqueDS(NULL), "must be a single character string", fixed = TRUE) }) # context("uniqueDS::arg::not character value") -test_that("simple uniqueDS for NULL", { - expect_error(uniqueDS(17), "Variable's name isn't a single character vector", fixed = TRUE) +test_that("uniqueDS errors for non-character argument", { + expect_error(uniqueDS(17), "must be a single character string", fixed = TRUE) }) # context("uniqueDS::arg::missing value") -test_that("simple uniqueDS for NULL", { - expect_error(uniqueDS("input"), "object 'input' not found", fixed = TRUE) +test_that("uniqueDS errors for nonexistent object", { + expect_error(uniqueDS("nonexistent_object"), "does not exist") }) # diff --git a/tests/testthat/test-smk-classDS.R b/tests/testthat/test-smk-classDS.R index d2efcf40..a3eb79d3 100644 --- a/tests/testthat/test-smk-classDS.R +++ b/tests/testthat/test-smk-classDS.R @@ -230,6 +230,13 @@ test_that("special classDS, NULL", { expect_equal(res, "NULL") }) +test_that("classDS throws error when object does not exist", { + expect_error( + classDS("nonexistent_object"), + regexp = "does not exist" + ) +}) + # # Done # diff --git a/tests/testthat/test-smk-completeCasesDS.R b/tests/testthat/test-smk-completeCasesDS.R index 2ba7b913..81ca9e29 100644 --- a/tests/testthat/test-smk-completeCasesDS.R +++ b/tests/testthat/test-smk-completeCasesDS.R @@ -190,6 +190,13 @@ test_that("simple completeCasesDS, data.matrix, with NAs", { expect_equal(res.colnames[2], "v2") }) +test_that("completeCasesDS throws error when object does not exist", { + expect_error( + completeCasesDS("nonexistent_object"), + regexp = "does not exist" + ) +}) + # # Done # diff --git a/tests/testthat/test-smk-dimDS.R b/tests/testthat/test-smk-dimDS.R index 7915e9a1..c45d07fb 100644 --- a/tests/testthat/test-smk-dimDS.R +++ b/tests/testthat/test-smk-dimDS.R @@ -25,10 +25,10 @@ test_that("numeric dimDS", { res <- dimDS("input") - expect_length(res, 2) - expect_equal(class(res), "integer") - expect_equal(res[1], 5) - expect_equal(res[2], 2) + expect_equal(class(res), "list") + expect_equal(res$dim[1], 5) + expect_equal(res$dim[2], 2) + expect_equal(res$class, "data.frame") }) # context("dimDS::smk::character") @@ -37,10 +37,35 @@ test_that("character dimDS", { res <- dimDS("input") - expect_length(res, 2) - expect_equal(class(res), "integer") - expect_equal(res[1], 5) - expect_equal(res[2], 2) + expect_equal(class(res), "list") + expect_equal(res$dim[1], 5) + expect_equal(res$dim[2], 2) + expect_equal(res$class, "data.frame") +}) + +test_that("dimDS with matrix", { + input <- matrix(1:6, nrow = 2, ncol = 3) + + res <- dimDS("input") + + expect_equal(res$dim[1], 2) + expect_equal(res$dim[2], 3) + expect_true("matrix" %in% res$class) +}) + +test_that("dimDS throws error when object does not exist", { + expect_error( + dimDS("nonexistent_object"), + regexp = "does not exist" + ) +}) + +test_that("dimDS throws error when object is not data.frame or matrix", { + bad_input <- c(1, 2, 3) + expect_error( + dimDS("bad_input"), + regexp = "must be of type data.frame or matrix" + ) }) # diff --git a/tests/testthat/test-smk-isNaDS.R b/tests/testthat/test-smk-isNaDS.R index 766d513c..33012766 100644 --- a/tests/testthat/test-smk-isNaDS.R +++ b/tests/testthat/test-smk-isNaDS.R @@ -23,62 +23,69 @@ test_that("numeric vector isNaDS", { input <- c(0.1, 1.1, 2.1, 3.1, 4.1) - res <- isNaDS(input) + res <- isNaDS("input") - expect_length(res, 1) - expect_equal(class(res), "logical") - expect_equal(res, FALSE) + expect_length(res$is.na, 1) + expect_equal(class(res$is.na), "logical") + expect_equal(res$is.na, FALSE) }) test_that("numeric vector isNaDS - with NA single", { input <- c(0.1, NA, 2.1, 3.1, 4.1) - res <- isNaDS(input) + res <- isNaDS("input") - expect_length(res, 1) - expect_equal(class(res), "logical") - expect_equal(res, FALSE) + expect_length(res$is.na, 1) + expect_equal(class(res$is.na), "logical") + expect_equal(res$is.na, FALSE) }) test_that("numeric vector isNaDS - with NA all", { input <- c(NA, NA, NA, NA, NA) - res <- isNaDS(input) + res <- isNaDS("input") - expect_length(res, 1) - expect_equal(class(res), "logical") - expect_equal(res, TRUE) + expect_length(res$is.na, 1) + expect_equal(class(res$is.na), "logical") + expect_equal(res$is.na, TRUE) }) # context("isNaDS::smk::character vector") test_that("character vector isNaDS", { input <- c("101", "202", "303", "404", "505") - res <- isNaDS(input) + res <- isNaDS("input") - expect_length(res, 1) - expect_equal(class(res), "logical") - expect_equal(res, FALSE) + expect_length(res$is.na, 1) + expect_equal(class(res$is.na), "logical") + expect_equal(res$is.na, FALSE) }) test_that("character vector isNaDS - with NA single", { input <- c("101", NA, "303", "404", "505") - res <- isNaDS(input) + res <- isNaDS("input") - expect_length(res, 1) - expect_equal(class(res), "logical") - expect_equal(res, FALSE) + expect_length(res$is.na, 1) + expect_equal(class(res$is.na), "logical") + expect_equal(res$is.na, FALSE) }) test_that("character vector isNaDS - with NA all", { input <- c(NA, NA, NA, NA, NA) - res <- isNaDS(input) + res <- isNaDS("input") - expect_length(res, 1) - expect_equal(class(res), "logical") - expect_equal(res, TRUE) + expect_length(res$is.na, 1) + expect_equal(class(res$is.na), "logical") + expect_equal(res$is.na, TRUE) +}) + +test_that("isNaDS throws error when object does not exist", { + expect_error( + isNaDS("nonexistent_object"), + regexp = "does not exist" + ) }) # diff --git a/tests/testthat/test-smk-lengthDS.R b/tests/testthat/test-smk-lengthDS.R index b5fad0e7..b410915d 100644 --- a/tests/testthat/test-smk-lengthDS.R +++ b/tests/testthat/test-smk-lengthDS.R @@ -19,42 +19,61 @@ # Tests # -# context("lengthDS::smk::data.frame") -test_that("simple lengthDS, numeric data.frame", { - input <- data.frame(v1 = c(0.0, 1.0, 2.0, 3.0, 4.0), v2 = c(4.0, 3.0, 2.0, 1.0, 0.0)) +# context("lengthDS::smk::vector") +test_that("simple lengthDS, numeric vector", { + input <- c(0.0, 1.0, 2.0, 3.0, 4.0) res <- lengthDS("input") - expect_equal(class(res), "integer") - expect_equal(res, 2) + expect_equal(class(res), "list") + expect_equal(res$length, 5) + expect_equal(res$class, "numeric") }) -test_that("simple lengthDS, character data.frame", { - input <- data.frame(v1 = c("0.0", "1.0", "2.0", "3.0", "4.0"), v2 = c("4.0", "3.0", "2.0", "1.0", "0.0"), stringsAsFactors = FALSE) +test_that("simple lengthDS, character vector", { + input <- c("0.0", "1.0", "2.0", "3.0", "4.0") res <- lengthDS("input") - expect_equal(class(res), "integer") - expect_equal(res, 2) + expect_equal(class(res), "list") + expect_equal(res$length, 5) + expect_equal(res$class, "character") }) -# context("lengthDS::smk::vector") -test_that("simple lengthDS, numeric vector", { - input <- c(0.0, 1.0, 2.0, 3.0, 4.0) +test_that("simple lengthDS, list", { + input <- list(a = 1, b = 2, c = 3) res <- lengthDS("input") - expect_equal(class(res), "integer") - expect_equal(res, 5) + expect_equal(res$length, 3) + expect_equal(res$class, "list") }) -test_that("simple lengthDS, character vector", { - input <- c("0.0", "1.0", "2.0", "3.0", "4.0") +test_that("lengthDS throws error when object does not exist", { + expect_error( + lengthDS("nonexistent_object"), + regexp = "does not exist" + ) +}) + +test_that("simple lengthDS, numeric data.frame", { + input <- data.frame(v1 = c(0.0, 1.0, 2.0, 3.0, 4.0), v2 = c(4.0, 3.0, 2.0, 1.0, 0.0)) + + res <- lengthDS("input") + + expect_equal(class(res), "list") + expect_equal(res$length, 2) + expect_equal(res$class, "data.frame") +}) + +test_that("simple lengthDS, character data.frame", { + input <- data.frame(v1 = c("0.0", "1.0", "2.0", "3.0", "4.0"), v2 = c("4.0", "3.0", "2.0", "1.0", "0.0"), stringsAsFactors = FALSE) res <- lengthDS("input") - expect_equal(class(res), "integer") - expect_equal(res, 5) + expect_equal(class(res), "list") + expect_equal(res$length, 2) + expect_equal(res$class, "data.frame") }) # diff --git a/tests/testthat/test-smk-levelsDS.R b/tests/testthat/test-smk-levelsDS.R index 5ba10980..2c313e1a 100644 --- a/tests/testthat/test-smk-levelsDS.R +++ b/tests/testthat/test-smk-levelsDS.R @@ -25,9 +25,9 @@ set.standard.disclosure.settings() test_that("numeric vector levelsDS", { input <- as.factor(c(0, 1, 2, 1, 2, 3, 1, 2, 1, 0, 1, 2, 0)) - res <- levelsDS(input) + res <- levelsDS("input") - expect_length(res, 2) + expect_length(res, 1) expect_equal(class(res), "list") expect_equal(class(res$Levels), "character") expect_length(res$Levels, 4) @@ -35,8 +35,21 @@ test_that("numeric vector levelsDS", { expect_equal(res$Levels[2], "1") expect_equal(res$Levels[3], "2") expect_equal(res$Levels[4], "3") - expect_equal(class(res$ValidityMessage), "character") - expect_equal(res$ValidityMessage, "VALID ANALYSIS") +}) + +test_that("levelsDS throws error when object does not exist", { + expect_error( + levelsDS("nonexistent_object"), + regexp = "does not exist" + ) +}) + +test_that("levelsDS throws error when object is not a factor", { + bad_input <- c(1, 2, 3) + expect_error( + levelsDS("bad_input"), + regexp = "must be of type factor" + ) }) # diff --git a/tests/testthat/test-smk-namesDS.R b/tests/testthat/test-smk-namesDS.R index dbc5f3b1..fe1134d5 100644 --- a/tests/testthat/test-smk-namesDS.R +++ b/tests/testthat/test-smk-namesDS.R @@ -45,6 +45,21 @@ test_that("simple namesDS, data.matrix", { expect_true("v2" %in% res) }) +test_that("namesDS throws error when object does not exist", { + expect_error( + namesDS("nonexistent_object"), + regexp = "does not exist" + ) +}) + +test_that("namesDS throws error when object is not a list", { + bad_input <- c(1, 2, 3) + expect_error( + namesDS("bad_input"), + regexp = "not of class " + ) +}) + # # Done # diff --git a/tests/testthat/test-smk-numNaDS.R b/tests/testthat/test-smk-numNaDS.R index c77db4ed..5040c94c 100644 --- a/tests/testthat/test-smk-numNaDS.R +++ b/tests/testthat/test-smk-numNaDS.R @@ -23,21 +23,28 @@ test_that("simple numNaDS", { input <- c(NA, 1, NA, 2, NA) - res <- numNaDS(input) + res <- numNaDS("input") - expect_equal(class(res), "integer") - expect_length(res, 1) - expect_equal(res, 3) + expect_equal(class(res$numNA), "integer") + expect_length(res$numNA, 1) + expect_equal(res$numNA, 3) }) -test_that("simple numNaDS", { +test_that("simple numNaDS, single NA", { input <- NA - res <- numNaDS(input) + res <- numNaDS("input") + + expect_equal(class(res$numNA), "integer") + expect_length(res$numNA, 1) + expect_equal(res$numNA, 1) +}) - expect_equal(class(res), "integer") - expect_length(res, 1) - expect_equal(res, 1) +test_that("numNaDS throws error when object does not exist", { + expect_error( + numNaDS("nonexistent_object"), + regexp = "does not exist" + ) }) #