Skip to content

Commit

Permalink
Merge pull request epiforecasts#451 from epiforecasts/RichardMN-test-…
Browse files Browse the repository at this point in the history
…json-2

Add tests for json_reader function and sample json data
  • Loading branch information
seabbs authored Feb 5, 2022
2 parents 6c43144 + 4cf4da6 commit e4b57dd
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export(start_using_memoise)
export(stop_using_memoise)
export(test_cleaning)
export(test_download)
export(test_download_JSON)
export(test_processing)
export(test_return)
importFrom(R6,R6Class)
Expand Down Expand Up @@ -75,6 +76,7 @@ importFrom(dplyr,pull)
importFrom(dplyr,recode)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(dplyr,slice_head)
importFrom(dplyr,slice_tail)
importFrom(dplyr,starts_with)
importFrom(dplyr,summarise)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This release is currently under development
* Change the data source for Switzerland to draw data from the Swiss Federal Office of Public Health (FOPH)
* Updated the package logo to include the newly supported data sets.
* Reduced the number of package dependencies (@bisaloo and @RichardMN)
* Added tests for JSON download code (@RichardMN).

## Bug fixes

Expand Down
41 changes: 41 additions & 0 deletions R/test-DataClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,47 @@ test_download <- function(DataClass_obj, download, snapshot_path) {
}
}


#' Test download method for JSON files works correctly
#' @description Test data can be downloaded if `download = TRUE`, or a requested
#' snapshot file is not found, and store a snap shot in the `snapshot_dir`. If
#' an existing snapshot file is found then load this data to use in future tests
#' @param DataClass_obj The R6Class object to perform checks on.
#' Must be a `DataClass` or `DataClass` child object.
#' @param download Logical check to download or use a snapshot of the data
#' @param snapshot_path character_array the path to save the downloaded
#' snapshot to.
#' @importFrom purrr map walk
#' @importFrom dplyr slice_head
#' @family tests
#' @concept tests
#' @export
test_download_JSON <- function(DataClass_obj, download, snapshot_path) {
if (!file.exists(snapshot_path)) {
download <- TRUE
}
if (download) {
testthat::test_that(
paste0(DataClass_obj$data_name, " downloads sucessfully"),
{
DataClass_obj$download_JSON()
walk(DataClass_obj$data$raw, function(data) {
testthat::expect_true(length(data) > 0)
testthat::expect_false(is.null(data))
})
}
)
DataClass_obj$data$raw <- map(DataClass_obj$data$raw,
slice_head,
n = 2
)

saveRDS(DataClass_obj$data$raw, snapshot_path)
} else {
DataClass_obj$data$raw <- readRDS(snapshot_path)
}
}

#' Test clean method works correctly
#' @description Test data can be cleaned properly. The clean method is invoked
#' to generate clean data. This data is checked to ensure it is a data.frame,
Expand Down
1 change: 1 addition & 0 deletions man/expect_clean_cols.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/expect_columns_contain_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/expect_processed_cols.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/test_cleaning.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/test_download.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions man/test_download_JSON.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/test_processing.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/test_return.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/custom_data/mtcars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.62,"qsec":16.46,"vs":0,"am":1,"gear":4,"carb":4},{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.875,"qsec":17.02,"vs":0,"am":1,"gear":4,"carb":4},{"mpg":22.8,"cyl":4,"disp":108,"hp":93,"drat":3.85,"wt":2.32,"qsec":18.61,"vs":1,"am":1,"gear":4,"carb":1},{"mpg":21.4,"cyl":6,"disp":258,"hp":110,"drat":3.08,"wt":3.215,"qsec":19.44,"vs":1,"am":0,"gear":3,"carb":1},{"mpg":18.7,"cyl":8,"disp":360,"hp":175,"drat":3.15,"wt":3.44,"qsec":17.02,"vs":0,"am":0,"gear":3,"carb":2},{"mpg":18.1,"cyl":6,"disp":225,"hp":105,"drat":2.76,"wt":3.46,"qsec":20.22,"vs":1,"am":0,"gear":3,"carb":1}]
36 changes: 36 additions & 0 deletions tests/testthat/test-json_reader.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
test_path <- "custom_data/mtcars.json"
target <- tibble::as_tibble(head(mtcars))

test_that("json_reader can read in a simple dataset", {
test <- json_reader(test_path)
expect_s3_class(test, "tbl_df")
expect_equal(
colnames(test),
colnames(target)
)
attributes(test)$spec <- NULL
attributes(test)$problems <- NULL
expect_equal(
test,
target
)
})

test_that("json_reader verbosity is controlled as expected", {
expect_gte(
length(capture.output(tmp <- json_reader(test_path, verbose = TRUE),
type = "message"
)),
1
)
expect_equal(
length(capture.output(tmp <- json_reader(test_path, verbose = FALSE),
type = "message"
)),
0
)
})

test_that("json_reader fails as expected when given a file that doesn't exist", {
expect_error(json_reader("nonsense.json", verbose = FALSE))
})

0 comments on commit e4b57dd

Please sign in to comment.