Skip to content

Commit

Permalink
Add extra test for custom property, use purrr::chuck()
Browse files Browse the repository at this point in the history
use purrr::chuck() so we throw an error when the indexed object doesn't exist instead of passing the test
  • Loading branch information
PietrH committed Jul 10, 2024
1 parent 0c11bda commit 049b767
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions tests/testthat/test-write_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,26 @@ test_that("write_package() encodes null and NA as null", {
data = data.frame("col_1" = c(1, 2), "col_2" = c("a", "b")),
title = NA
)
expect_null(p$image)
expect_equal(p$resources[[4]]$title, NA)
# Set a custom property as NA, to see if it gets written as `null` in JSON
p$my_property <- NA

# Image is set as `NULL` in the JSON and that should still be the case here.
expect_null(purrr::chuck(p, "image"))

## Use purrr::chuck() so an error is returned if image doesn't exist
expect_null(purrr::chuck(p, "image"))
expect_identical(p$resources[[4]]$title, NA)
expect_identical(p$my_property, NA)

dir <- file.path(tempdir(), "package")
on.exit(unlink(dir, recursive = TRUE))
write_package(p, dir)

p_loaded <- read_package(file.path(dir, "datapackage.json"))
expect_null(p_loaded$image)
expect_null(p_loaded$resources[[4]]$title)
# Use purrr::chuck() so an error is returned if the requested index doesn't
# exist
expect_null(purrr::chuck(p_loaded, "image"))
expect_null(purrr::chuck(p_loaded, "resources", 4, "title"))
## my_property was set to NA, when writing this becomes NULL
expect_null(purrr::chuck(p_loaded, "my_property"))
})

0 comments on commit 049b767

Please sign in to comment.