context("utils-validation tests")
wd <- getwd()
setwd(dir = tempdir())
test_that('isEmpty returns true if value input is both null and na (test with null)',{
val1 = NULL
test_result <- repgen:::isEmpty(val1)
expect_true(test_result)
})
test_that('isEmpty returns true if value input is both null and na (test with na)',{
val1 = NA
test_result <- repgen:::isEmpty(val1)
expect_true(test_result)
})
test_that('isEmpty returns true if value input is both null and na (test with na and null)',{
val1 <- c(NULL,NA)
test_result <- repgen:::isEmpty(val1)
expect_true(test_result)
})
test_that('isEmpty returns false if value input is not both null and na (test with na)',{
val1 <- c(1:3,NA)
test_result <- repgen:::isEmpty(val1)
expect_false(test_result)
})
test_that('isEmpty returns false if value input is not both null and na (test with null)',{
val1 <- c(1:3,NULL)
test_result <- repgen:::isEmpty(val1)
expect_false(test_result)
})
test_that('isEmpty returns false if value input is not both null and na (test with non-null and non-na)',{
val1 <- c(1:3,6:4)
test_result <- repgen:::isEmpty(val1)
expect_false(test_result)
})
test_that('isEmptyOrBlank returns true if value input is null and empty string and na (test with null)',{
val1 = NULL
test_result <- repgen:::isEmptyOrBlank(val1)
expect_true(test_result)
})
test_that('isEmptyOrBlank returns true if value input is null and empty string and na (test with empty string)',{
val1 = ""
test_result <- repgen:::isEmptyOrBlank(val1)
expect_true(test_result)
})
test_that('isEmptyOrBlank returns true if value input is null and empty string and na (test with na)',{
val1 = NA
test_result <- repgen:::isEmptyOrBlank(val1)
expect_true(test_result)
})
test_that('isEmptyOrBlank returns true if value input is null and empty string and na (test with empty string and null)',{
val1 <- c(NULL,"")
test_result <- repgen:::isEmptyOrBlank(val1)
expect_true(test_result)
})
test_that('isEmptyOrBlank returns true if value input is null and empty string and na (test with empty string and na)',{
val1 <- c(NA,"")
test_result <- repgen:::isEmptyOrBlank(val1)
expect_true(test_result)
})
test_that('isEmptyOrBlank returns true if value input is null and empty string and na (test with NULL and na)',{
val1 <- c(NA,NULL)
test_result <- repgen:::isEmptyOrBlank(val1)
expect_true(test_result)
})
test_that('isEmptyOrBlank returns false if value input is not all null and empty string and na (test with empty string)',{
val1 <- c(1:3,"")
test_result <- repgen:::isEmptyOrBlank(val1)
expect_false(test_result)
})
test_that('isEmptyOrBlank returns false if value input is not all null and empty string and na (test with null)',{
val1 <- c(1:3,NULL)
test_result <- repgen:::isEmptyOrBlank(val1)
expect_false(test_result)
})
test_that('isEmptyOrBlank returns false if value input is not all null and empty string and na (test with na)',{
val1 <- c(1:3,NA)
test_result <- repgen:::isEmptyOrBlank(val1)
expect_false(test_result)
})
test_that('isEmptyOrBlank returns false if value input is not all null and empty string and na (test with na and null)',{
val1 <- c(1:3,NA,NULL)
test_result <- repgen:::isEmptyOrBlank(val1)
expect_false(test_result)
})
test_that('isEmptyOrBlank returns false if value input is not all null and empty string and na (test with na and null), with null first',{
val1 <- c(NULL,1:3,NA)
test_result <- repgen:::isEmptyOrBlank(val1)
expect_false(test_result)
})
test_that('isEmptyOrBlank returns false if value input is not all null and empty string and na (test with na and empty string)',{
val1 <- c(1:3,NA,"")
test_result <- repgen:::isEmptyOrBlank(val1)
expect_false(test_result)
})
test_that('isEmptyOrBlank returns false if value input is not all null and empty string and na (test with null and empty string)',{
val1 <- c(1:3,NULL,"")
test_result <- repgen:::isEmptyOrBlank(val1)
expect_false(test_result)
})
test_that('isEmptyOrBlank returns false if value input is not all null and empty string and na (test with non-null and non-empty string and non-na)',{
val1 <- c(1:3,6:4,8:2)
test_result <- repgen:::isEmptyOrBlank(val1)
expect_false(test_result)
})
test_that('isEmptyOrBlank returns true if value exists in the environment or false if it does not',{
a <- 1
c <- ""
d <- NULL
e <- NA
expect_false(repgen:::isEmptyOrBlank(listObjects = ls(), objectName = "a")) # should be FALSE
expect_true(repgen:::isEmptyOrBlank(listObjects = ls(), objectName = "b")) # should be TRUE
expect_true(repgen:::isEmptyOrBlank(val = c)) # should be TRUE
expect_true(repgen:::isEmptyOrBlank(val = d)) # should be TRUE
expect_true(repgen:::isEmptyOrBlank(val = e)) # should be TRUE
expect_false(repgen:::isEmptyOrBlank(val = a)) # should be FALSE
})
test_that('validParam returns a string value if it is not NULL', {
val1 <- "xyz"
expect_equal(repgen:::validParam(val1, "testParam"), "xyz")
})
test_that('validParam returns a numeric value if it is not NULL', {
val1 <- 23141
expect_equal(repgen:::validParam(val1, "testParam"), 23141)
})
test_that('validParam returns an error if NULL and required', {
val1 <- NULL
expect_error(repgen:::validParam(val1, "testParam", TRUE), 'required value testParam missing.')
})
test_that('validParam returns as.numeric(NA) if NULL and not required and as.numeric', {
val1 <- NULL
expect_equal(repgen:::validParam(val1, "testParam", FALSE, TRUE), as.numeric(NA))
})
test_that('validParam returns empty string if NULL and not required and not as.numeric', {
val1 <- NULL
expect_equal(repgen:::validParam(val1, "testParam", FALSE, FALSE), "")
})
test_that('anyDataExist works for lists', {
expect_false(repgen:::anyDataExist(list()))
expect_false(repgen:::anyDataExist(list(one=numeric(), two=numeric())))
expect_true(repgen:::anyDataExist(list(one=c(1:3), two="one")))
expect_true(repgen:::anyDataExist(list(one=c(), two="one")))
})
test_that('anyDataExist works for vectors', {
expect_false(repgen:::anyDataExist(c()))
expect_true(repgen:::anyDataExist(1:4))
expect_true(repgen:::anyDataExist(c("one", "two")))
})
test_that('anyDataExist works for data.frames', {
expect_false(repgen:::anyDataExist(data.frame()))
expect_false(repgen:::anyDataExist(data.frame(one=numeric(), two=numeric())))
expect_true(repgen:::anyDataExist(data.frame(one=c(1:3))))
expect_true(repgen:::anyDataExist(data.frame(one=c(1:2), two=c("row1", "row2"))))
})
test_that('checkRequiredFields properly checks fields', {
library(jsonlite)
testJSON <- fromJSON('{
"req1": "data",
"req2": "",
"opt1": "data",
"opt2": ""
}')
testArray <- fromJSON('{
"array": [
{
"req1": "1",
"req2": "",
"req3": "3",
"opt1": "1",
"opt2": ""
},
{
"req1": "1",
"req2": "",
"opt1": "1"
}
]
}')
requiredFields1 <- c("req1", "req2")
requiredFields2 <- c("req1", "req2", "req3")
valid1 <- repgen:::checkRequiredFields(testJSON, requiredFields1)
valid2 <- repgen:::checkRequiredFields(testArray[['array']], requiredFields1)
invalid1 <- repgen:::checkRequiredFields(testJSON, requiredFields2)
invalid2 <- repgen:::checkRequiredFields(testArray[['array']], requiredFields2)
expect_is(valid1, 'NULL')
expect_is(valid2, 'NULL')
expect_is(invalid1, 'character')
expect_is(invalid2, 'character')
expect_equal(valid1, NULL)
expect_equal(valid2, NULL)
expect_equal(invalid1, c("req3"))
expect_equal(invalid2, c("req3"))
})
test_that('validateFetchedData properly validates data', {
testJSON <- fromJSON('{
"object":{
"req1": "data",
"req2": "",
"opt1": "data",
"opt2": ""
},
"empty": {},
"array": [
{
"req1": "1",
"req2": "",
"req3": "3",
"opt1": "1",
"opt2": ""
},
{
"req1": "1",
"req2": "",
"opt1": "1"
}
]
}')
requiredFields1 <- c("req1", "req2")
requiredFields2 <- c("req1", "req2", "req3")
valid1 <- repgen:::validateFetchedData(testJSON[['object']], "object", requiredFields1)
valid2 <- repgen:::validateFetchedData(testJSON[['array']], "array", requiredFields1)
expect_warning(expect_false(repgen:::validateFetchedData(testJSON[['missing']], "missing", requiredFields1, stopNull=FALSE)))
expect_error(repgen:::validateFetchedData(testJSON[['missing']], "missing", requiredFields1))
expect_warning(expect_false(repgen:::validateFetchedData(testJSON[['array']], "array", requiredFields2, stopMissing=FALSE)))
expect_error(repgen:::validateFetchedData(testJSON[['array']], "array", requiredFields2))
expect_warning(expect_false(repgen:::validateFetchedData(testJSON[['empty']], "empty", requiredFields1, stopEmpty=FALSE)))
expect_error(repgen:::validateFetchedData(testJSON[['empty']], "empty", requiredFields1))
})
test_that('fieldExists works as expected', {
data <- list(field1=list(), field2=list())
expect_true(repgen:::fieldExists(data, "field1"))
expect_true(repgen:::fieldExists(data, "field2"))
expect_false(repgen:::fieldExists(data, "field3"))
})
test_that('isEmptyOrBlankVectors handles single values', {
singleValue <- "temp"
emptyValue <- ""
naValue <- NA
nullValue <- NULL
expect_false(repgen:::isEmptyOrBlankVectors(singleValue))
expect_true(repgen:::isEmptyOrBlankVectors(emptyValue))
expect_true(repgen:::isEmptyOrBlankVectors(naValue))
expect_true(repgen:::isEmptyOrBlankVectors(nullValue))
})
test_that('isEmptyOrBlankVectors handles lists and returns a response for each item in list', {
coupleOfValues <- c("a","b","c")
response <- repgen:::isEmptyOrBlankVectors(coupleOfValues)
expect_equal(response, c(FALSE,FALSE,FALSE))
expect_length(response, 3)
coupleOfDifferentValues <- c("a","","c")
response <- repgen:::isEmptyOrBlankVectors(coupleOfDifferentValues)
expect_equal(response, c(FALSE,TRUE,FALSE))
expect_length(response, 3)
otherValues <- c(NA,"a","B")
response <- repgen:::isEmptyOrBlankVectors(otherValues)
expect_equal(response, c(TRUE,FALSE,FALSE))
expect_length(response, 3)
})
test_that('isEmptyOrBlankVectors handles dataframe data', {
exampleDf <- data.frame(
numbers = c(1:5,""),
words = c("Approved","In Review","","Working",NA,"Approved"),
dates = as.Date(c("2012-01-01", "2012-02-01", "2012-03-01", "2012-04-01","2012-05-01",NA)),
stringsAsFactors = FALSE
)
response <- repgen:::isEmptyOrBlankVectors(exampleDf)
expect_equal(response[,1],as.logical(c(FALSE,FALSE,FALSE,FALSE,FALSE,TRUE)))
expect_equal(response[,2],as.logical(c(FALSE,FALSE,TRUE,FALSE,TRUE,FALSE)))
expect_equal(response[,3],as.logical(c(FALSE,FALSE,FALSE,FALSE,FALSE,TRUE)))
expect_s3_class(response,"data.frame")
expect_equal(dim(exampleDf),dim(response))
})
test_that('isEmptyOrBlankVectors handles bug case found and reported in AQCU-1642', {
val1 <- c(NA,1:3)
test_result <- repgen:::isEmptyOrBlankVectors(val1)
expect_equal(test_result,as.logical(c(TRUE,FALSE,FALSE,FALSE)))
})
setwd(dir = wd)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.