Skip to content

Commit

Permalink
Update testthat (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbutuseless committed Jul 24, 2023
1 parent f3ceb99 commit 2d527d9
Show file tree
Hide file tree
Showing 29 changed files with 272 additions and 272 deletions.
6 changes: 3 additions & 3 deletions tests/testthat/flatten.R
@@ -1,9 +1,9 @@
context("flatten")


test_that("flattening", {
x <- list(test = data.frame(foo=1:3))
x$test$bar <- data.frame(x=5:3, y=7:9)
expect_that(x, equals(fromJSON(toJSON(x), flatten = FALSE)));
expect_that(names(fromJSON(toJSON(x), flatten = TRUE)$test), equals(c("foo", "bar.x", "bar.y")))
expect_equal(x, fromJSON(toJSON(x), flatten = FALSE));
expect_equal(names(fromJSON(toJSON(x), flatten = TRUE)$test), c("foo", "bar.x", "bar.y"));
});

12 changes: 6 additions & 6 deletions tests/testthat/test-fromJSON-NA-values.R
@@ -1,7 +1,7 @@
context("fromJSON NA values")


test_that("fromJSON NA values", {

objects <- list(
numbers = c(1,2, NA, NaN, Inf, -Inf, 3.14),
logical = c(TRUE, FALSE, NA),
Expand All @@ -13,12 +13,12 @@ test_that("fromJSON NA values", {
boolNA = as.logical(NA),
df = data.frame(foo=c(1,NA))
)

#test all but list
lapply(objects, function(object){
expect_that(fromJSON(toJSON(object)), equals(object))
expect_equal(fromJSON(toJSON(object)), object);
});

#test all in list
expect_that(fromJSON(toJSON(objects)), equals(objects))
expect_equal(fromJSON(toJSON(objects)), objects);
});
30 changes: 15 additions & 15 deletions tests/testthat/test-fromJSON-array.R
@@ -1,53 +1,53 @@
context("fromJSON Array")


test_that("fromJSON Array, row major", {

# test high dimensional arrays
lapply(2:5, function(n){
object <- array(1:prod(n), dim=1:n)
newobject <- fromJSON(toJSON(object));
expect_that(object, equals(newobject));
expect_equal(object, newobject);
});

# adding some flat dimensions
lapply(1:5, function(n){
object <- array(1:prod(n), dim=c(1:n, 1))
newobject <- fromJSON(toJSON(object));
expect_that(object, equals(newobject));
});
expect_equal(object, newobject);
});
});

test_that("fromJSON Array, column major", {

# test high dimensional arrays
lapply(2:5, function(n){
object <- array(1:prod(n), dim=1:n)
newobject <- fromJSON(toJSON(object, matrix="columnmajor"), columnmajor=TRUE);
expect_that(object, equals(newobject));
expect_equal(object, newobject);
});

# adding some flat dimensions
lapply(1:5, function(n){
object <- array(1:prod(n), dim=c(1:n, 1))
newobject <- fromJSON(toJSON(object, matrix="columnmajor"), columnmajor=TRUE);
expect_that(object, equals(newobject));
});
expect_equal(object, newobject);
});
});


test_that("fromJSON Array, character strings", {

# test high dimensional arrays
lapply(2:5, function(n){
object <- array(paste("cell", 1:prod(n)), dim=1:n)
newobject <- fromJSON(toJSON(object, matrix="columnmajor"), columnmajor=TRUE);
expect_that(object, equals(newobject));
expect_equal(object, newobject);
});

# adding some flat dimensions
lapply(1:5, function(n){
object <- array(paste("cell", 1:prod(n)), dim=c(1:n, 1))
newobject <- fromJSON(toJSON(object, matrix="columnmajor"), columnmajor=TRUE);
expect_that(object, equals(newobject));
});
expect_equal(object, newobject);
});
});
24 changes: 12 additions & 12 deletions tests/testthat/test-fromJSON-dataframe.R
@@ -1,4 +1,4 @@
context("fromJSON dataframes")


options(stringsAsFactors=FALSE);

Expand All @@ -16,13 +16,13 @@ test_that("recover nested data frames", {

#test all but list
lapply(objects, function(object){
expect_that(fromJSON(toJSON(object)), equals(object))
expect_that(fromJSON(toJSON(object, na="null")), equals(object))
expect_that(names(fromJSON(toJSON(object), flatten = TRUE)), equals(names(unlist(object[1,,drop=FALSE]))))
expect_equal(fromJSON(toJSON(object)), object);
expect_equal(fromJSON(toJSON(object, na="null")), object);
expect_equal(names(fromJSON(toJSON(object), flatten = TRUE)), names(unlist(object[1,,drop=FALSE])));
});

#test all in list
expect_that(fromJSON(toJSON(objects)), equals(objects))
expect_equal(fromJSON(toJSON(objects)), objects);
});

test_that("recover lists in data frames", {
Expand All @@ -39,10 +39,10 @@ test_that("recover lists in data frames", {
z <- list(x=x, y=y);
zz <- list(x,y);

expect_that(fromJSON(toJSON(x)), equals(x))
expect_that(fromJSON(toJSON(y)), equals(y))
expect_that(fromJSON(toJSON(z)), equals(z))
expect_that(fromJSON(toJSON(zz)), equals(zz))
expect_equal(fromJSON(toJSON(x)), x);
expect_equal(fromJSON(toJSON(y)), y);
expect_equal(fromJSON(toJSON(z)), z);
expect_equal(fromJSON(toJSON(zz)), zz);
});

#note: nested matrix does not perfectly restore
Expand All @@ -53,7 +53,7 @@ test_that("nested matrix in data frame", {
expect_true(validate(toJSON(x)))

y <- fromJSON(toJSON(x))
expect_that(y, is_a("data.frame"))
expect_that(names(x), equals(names(y)))
expect_that(length(y[[1,"bar"]]), equals(3))
expect_s3_class(y, "data.frame");
expect_equal(names(x), names(y));
expect_equal(length(y[[1,"bar"]]), 3);
});
12 changes: 6 additions & 6 deletions tests/testthat/test-fromJSON-datasets.R
@@ -1,18 +1,18 @@
context("fromJSON datasets")


# Note about numeric precision
# In the unit tests we use digits=10. Lowever values will result in problems for some datasets
test_that("fromJSON datasets", {
objects <- Filter(is.data.frame, lapply(ls("package:datasets"), get));

#data frames are never identical because:
# - attributes
# - attributes
# - factors, times, dates turn into strings
# - integers turn into numeric
lapply(objects, function(object){
newobject <- fromJSON(toJSON(object))
expect_that(newobject, is_a("data.frame"));
expect_that(names(object), is_identical_to(names(newobject)));
expect_that(nrow(object), is_identical_to(nrow(newobject)))
expect_s3_class(newobject, "data.frame");
expect_identical(names(object), names(newobject));
expect_identical(nrow(object), nrow(newobject));
});
});
22 changes: 11 additions & 11 deletions tests/testthat/test-fromJSON-date.R
@@ -1,19 +1,19 @@
context("fromJSON date objects")


test_that("fromJSON date objects", {

x <- Sys.time() + c(1, 2, NA, 3)
mydf <- data.frame(x=x)
expect_that(fromJSON(toJSON(x, POSIXt="mongo")), is_a("POSIXct"))
expect_that(fromJSON(toJSON(x, POSIXt="mongo")), equals(x))
#expect_that(fromJSON(toJSON(x, POSIXt="mongo", na="string")), is_a("POSIXct"))
expect_that(fromJSON(toJSON(x, POSIXt="mongo", na="null")), is_a("POSIXct"))

expect_that(fromJSON(toJSON(mydf, POSIXt="mongo")), is_a("data.frame"))
expect_that(fromJSON(toJSON(mydf, POSIXt="mongo"))$x, is_a("POSIXct"))
#expect_that(fromJSON(toJSON(mydf, POSIXt="mongo", na="string"))$x, is_a("POSIXct"))
expect_that(fromJSON(toJSON(mydf, POSIXt="mongo", na="null"))$x, is_a("POSIXct"))
expect_that(fromJSON(toJSON(mydf, POSIXt="mongo"))$x, equals(x))
expect_s3_class(fromJSON(toJSON(x, POSIXt="mongo")), "POSIXct");
expect_equal(fromJSON(toJSON(x, POSIXt="mongo")), x);
#expect_s3_class(fromJSON(toJSON(x, POSIXt="mongo", na="string")), "POSIXct");
expect_s3_class(fromJSON(toJSON(x, POSIXt="mongo", na="null")), "POSIXct");

expect_s3_class(fromJSON(toJSON(mydf, POSIXt="mongo")), "data.frame");
expect_s3_class(fromJSON(toJSON(mydf, POSIXt="mongo"))$x, "POSIXct");
#expect_s3_class(fromJSON(toJSON(mydf, POSIXt="mongo", na="string"))$x, "POSIXct");
expect_s3_class(fromJSON(toJSON(mydf, POSIXt="mongo", na="null"))$x, "POSIXct");
expect_equal(fromJSON(toJSON(mydf, POSIXt="mongo"))$x, x);

xct <- as.POSIXct(x)
xlt <- as.POSIXlt(x)
Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test-fromJSON-matrix.R
@@ -1,4 +1,4 @@
context("fromJSON Matrix")


# Note about numeric precision
# In the unit tests we use digits=10. Lowever values will result in problems for some datasets
Expand All @@ -15,18 +15,18 @@ test_that("fromJSON Matrix", {

lapply(objects, function(object){
newobject <- fromJSON(toJSON(object));
expect_that(newobject, is_a("matrix"));
expect_that(object, equals(newobject));
expect_true(inherits(newobject, "matrix"));
expect_equal(object, newobject);
});

expect_that(fromJSON(toJSON(objects)), equals(objects));
expect_equal(fromJSON(toJSON(objects)), objects);
});

test_that("fromJSON Matrix with simplifyMatrix=FALSE", {
expect_that(fromJSON(toJSON(matrix(1)), simplifyMatrix=FALSE), equals(list(1)));
expect_that(fromJSON(toJSON(matrix(1)), simplifyVector=FALSE), equals(list(list((1)))));
expect_that(fromJSON(toJSON(matrix(NA)), simplifyMatrix=FALSE), equals(list(NA)));
expect_that(fromJSON(toJSON(matrix(NA)), simplifyVector=FALSE), equals(list(list((NULL)))));
expect_equal(fromJSON(toJSON(matrix(1)), simplifyMatrix=FALSE), list(1));
expect_equal(fromJSON(toJSON(matrix(1)), simplifyVector=FALSE), list(list((1))));
expect_equal(fromJSON(toJSON(matrix(NA)), simplifyMatrix=FALSE), list(NA));
expect_equal(fromJSON(toJSON(matrix(NA)), simplifyVector=FALSE), list(list((NULL))));
});


Expand All @@ -36,12 +36,12 @@ test_that("fromJSON Matrix datasets", {
lapply(objects, function(object){
class(object) <- "matrix";
newobject <- fromJSON(toJSON(object, digits=4))
expect_that(newobject, is_a("matrix"));
expect_that(dim(newobject), equals(dim(object)));
expect_true(inherits(newobject, "matrix"));
expect_equal(dim(newobject), dim(object));
attributes(newobject) <- attributes(object);

# R has changed rounding algo in 4.0 and no longer matches printf
#expect_that(newobject, equals(round(object,4)))
#expect_equal(newobject, round(object,4));
expect_equal(newobject, object, tolerance = 1e-4)
});
});
8 changes: 4 additions & 4 deletions tests/testthat/test-libjson-escaping.R
@@ -1,4 +1,4 @@
context("libjson Escaping")


test_that("escaping and parsing of special characters", {

Expand All @@ -11,11 +11,11 @@ test_that("escaping and parsing of special characters", {
#generate 1000 random strings
for(i in 1:200){
x <- createstring(i);
expect_that(x, equals(fromJSON(toJSON(x))));
expect_that(x, equals(fromJSON(toJSON(x, pretty=TRUE))));
expect_equal(x, fromJSON(toJSON(x)));
expect_equal(x, fromJSON(toJSON(x, pretty=TRUE)));

y <- setNames(list(123), x)
expect_that(x, equals(fromJSON(toJSON(x, pretty=TRUE))));
expect_equal(x, fromJSON(toJSON(x, pretty=TRUE)));
}

});
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-libjson-large.R
@@ -1,17 +1,17 @@
context("libjson Large strings")


test_that("escaping and parsing of special characters", {

#create random strings
mychars <- c('a', 'b', " ", '"', "\\", "\t", "\n", "'", "/", "#", "$");
createstring <- function(length){
paste(mychars[ceiling(runif(length, 0, length(mychars)))], collapse="")
}
}

#try some very long strings
for(i in 1:10){
zz <- list(foo=createstring(1e5))
expect_that(zz, equals(fromJSON(toJSON(zz))));
expect_equal(zz, fromJSON(toJSON(zz)));
}

});
16 changes: 8 additions & 8 deletions tests/testthat/test-libjson-utf8.R
Expand Up @@ -23,26 +23,26 @@ test_that("test that non ascii characters are ok", {
#Encoding(x) <- "UTF-8"
myjson <- toJSON(x, pretty=TRUE);
expect_true(validate(myjson));
expect_that(fromJSON(myjson), equals(x));
expect_equal(fromJSON(myjson), x);

#prettify needs to parse + output
prettyjson <- prettify(myjson);
expect_true(validate(prettyjson));
expect_that(fromJSON(prettyjson), equals(x));
expect_equal(fromJSON(prettyjson), x);

#test encoding is preserved when roundtripping to disk
tmp <- tempfile()
write_json(x, tmp)
expect_that(read_json(tmp, simplifyVector = TRUE), equals(x));
expect_equal(read_json(tmp, simplifyVector = TRUE), x);
unlink(tmp)
});

#Test escaped unicode characters
expect_that(fromJSON('["Z\\u00FCrich"]'), equals("Z\u00fcrich"));
expect_that(fromJSON(prettify('["Z\\u00FCrich"]')), equals("Z\u00fcrich"));
expect_equal(fromJSON('["Z\\u00FCrich"]'), "Z\u00fcrich");
expect_equal(fromJSON(prettify('["Z\\u00FCrich"]')), "Z\u00fcrich");

expect_that(length(unique(fromJSON('["Z\\u00FCrich", "Z\u00fcrich"]'))), equals(1L))
expect_that(fromJSON('["\\u586B"]'), equals("\u586b"));
expect_that(fromJSON(prettify('["\\u586B"]')), equals("\u586B"));
expect_equal(length(unique(fromJSON('["Z\\u00FCrich", "Z\u00fcrich"]'))), 1L);
expect_equal(fromJSON('["\\u586B"]'), "\u586b");
expect_equal(fromJSON(prettify('["\\u586B"]')), "\u586B");

});
2 changes: 1 addition & 1 deletion tests/testthat/test-libjson-validator.R
@@ -1,4 +1,4 @@
context("libjson Validator")


test_that("test that the validator properly deals with escaped characters", {

Expand Down

0 comments on commit 2d527d9

Please sign in to comment.