-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BSON roundtripping tweaks and unit tests
- Loading branch information
Showing
4 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "tests/testthat/specifications"] | ||
path = tests/testthat/specifications | ||
url = https://github.com/mongodb/specifications |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule specifications
added at
1b4159
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
context("specifications") | ||
|
||
roundtrip_json <- function(testdata){ | ||
extjson <- na.omit(testdata$valid$extjson) | ||
m <- mongo(verbose = FALSE) | ||
on.exit(try(m$drop(), silent = TRUE)) | ||
m$insert(extjson) | ||
out <- m$find() | ||
jsonlite::toJSON(out, raw = "mongo", collapse = FALSE, always_decimal = TRUE, digits = NA) | ||
} | ||
|
||
roundtrip_test <- function(testdata){ | ||
json <- roundtrip_json(testdata) | ||
for(i in seq_along(json)){ | ||
x <- jsonlite::fromJSON(testdata$valid$extjson[i], simplifyVector = FALSE) | ||
y <- jsonlite::fromJSON(json[i], simplifyVector = FALSE) | ||
expect_equal(x, y) | ||
} | ||
} | ||
|
||
test_that("roundtrip array", { | ||
testdata <- jsonlite::fromJSON("specifications/source/bson-corpus/tests/array.json") | ||
roundtrip_test(testdata) | ||
}) | ||
|
||
test_that("roundtrip binary", { | ||
testdata <- jsonlite::fromJSON("specifications/source/bson-corpus/tests/binary.json") | ||
roundtrip_test(testdata) | ||
}) | ||
|
||
test_that("roundtrip boolean", { | ||
testdata <- jsonlite::fromJSON("specifications/source/bson-corpus/tests/boolean.json") | ||
roundtrip_test(testdata) | ||
}) | ||
|
||
test_that("roundtrip datetime", { | ||
testdata <- jsonlite::fromJSON("specifications/source/bson-corpus/tests/datetime.json") | ||
roundtrip_test(testdata) | ||
}) | ||
|
||
test_that("roundtrip document", { | ||
testdata <- jsonlite::fromJSON("specifications/source/bson-corpus/tests/document.json") | ||
# Remove test with empty column name. | ||
testdata$valid = testdata$valid[-2,] | ||
roundtrip_test(testdata) | ||
}) | ||
|
||
test_that("roundtrip double", { | ||
testdata <- jsonlite::fromJSON("specifications/source/bson-corpus/tests/double.json") | ||
roundtrip_test(testdata) | ||
}) | ||
|
||
test_that("roundtrip int32", { | ||
testdata <- jsonlite::fromJSON("specifications/source/bson-corpus/tests/int32.json") | ||
roundtrip_test(testdata) | ||
}) | ||
|
||
test_that("roundtrip string", { | ||
testdata <- jsonlite::fromJSON("specifications/source/bson-corpus/tests/string.json") | ||
roundtrip_test(testdata) | ||
}) | ||
|
||
test_that("roundtrip timestamp", { | ||
testdata <- jsonlite::fromJSON("specifications/source/bson-corpus/tests/timestamp.json") | ||
a <- jsonlite::fromJSON(testdata$valid$extjson)$a[["$timestamp"]] | ||
b <- jsonlite::fromJSON(roundtrip_json(testdata))$a | ||
expect_equal(a, b) | ||
}) |