Skip to content

Commit

Permalink
Add tests for the download_JSON method
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardMN authored and Bisaloo committed Feb 4, 2022
1 parent c9e9ca8 commit e48e358
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
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 @@ -74,6 +75,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
43 changes: 43 additions & 0 deletions R/test-DataClass.R
Expand Up @@ -121,6 +121,49 @@ 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_s3_class(data, "data.frame")
testthat::expect_true(nrow(data) > 0)
testthat::expect_true(ncol(data) >= 2
|| typeof(data[[1]]) == "list")
})
}
)
DataClass_obj$data$raw <- map(vn$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.

0 comments on commit e48e358

Please sign in to comment.