-
Notifications
You must be signed in to change notification settings - Fork 5
refactor(epidatacall): remove fetch_csv
and use testthat
mocking
#115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,6 @@ RoxygenNote: 7.2.3 | |
Suggests: | ||
dplyr, | ||
knitr, | ||
mockery, | ||
mockr, | ||
rmarkdown, | ||
testthat (>= 3.1.5), | ||
withr | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
test_that("fetch_tbl", { | ||
test_that("fetch and fetch_tbl", { | ||
epidata_call <- covidcast( | ||
data_source = "jhu-csse", | ||
signals = "confirmed_7dav_incidence_prop", | ||
|
@@ -7,32 +7,21 @@ test_that("fetch_tbl", { | |
time_values = epirange("2020-06-01", "2020-08-01"), | ||
geo_values = "ca,fl" | ||
) | ||
# Generated with | ||
# epidata_call %>% | ||
# fetch_debug(format_type = "classic") %>% | ||
# readr::write_rds(testthat::test_path("data/test-classic.rds")) | ||
mockery::stub(fetch_classic, "httr::content", readRDS(testthat::test_path("data/test-classic.rds"))) | ||
mockery::stub(fetch_tbl, "fetch_classic", fetch_classic) | ||
# Generated with | ||
# epidata_call %>% | ||
# fetch_debug(format_type = "csv") %>% | ||
# readr::write_rds(testthat::test_path("data/test-csv.rds")) | ||
mockery::stub(fetch_csv, "httr::content", readRDS(testthat::test_path("data/test-csv.rds"))) | ||
|
||
# This test compares the output of a tibble using fetch_tbl and fetch_csv. | ||
# | ||
# 1) fetch_tbl calls fetch_classic, which requests the default (classic | ||
# format) from the API, uses jsonlite::fromJSON to convert the underlying data | ||
# to a data.frame, and finally applies parse_data_frame is used to do the data | ||
# type coersion specified by the epidata_call metadata. | ||
# 2) fetch_csv requests the csv format from the API, then uses readr::read_csv | ||
# to get a data.frame, and has its own methods to enforce data types. | ||
tbl_out <- epidata_call %>% fetch_tbl() | ||
csv_out <- epidata_call %>% fetch_csv() | ||
expect_identical(tbl_out, csv_out) | ||
# This test compares the output of a tibble using fetch and fetch_tbl. | ||
with_mocked_bindings( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I run this locally with no internet connection, its hanging. This makes me think content isn't being mocked and the test is just calling the server. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for pointing this out. the problem was that I was mocking httr::content, which parse the response, but i didnt mock epidatr::request_impl, which was still making a request (even though the request wasn't being used). |
||
{ | ||
tbl_out <- epidata_call %>% fetch_tbl() | ||
out <- epidata_call %>% fetch() | ||
}, | ||
# RDS file generated with | ||
# epidata_call %>% | ||
# fetch_debug(format_type = "classic") %>% | ||
# readr::write_rds(testthat::test_path("data/test-classic.rds")) | ||
content = function(...) readRDS(testthat::test_path("data/test-classic.rds")), | ||
.package = "httr" | ||
dshemetov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
# # This test compares fetch_tbl with the output of fetch, which should be identical. | ||
out <- epidata_call %>% fetch() | ||
expect_identical(out, tbl_out) | ||
}) | ||
|
||
|
@@ -50,11 +39,15 @@ test_that("fetch_tbl warns on non-success", { | |
# see generation code above | ||
readRDS(testthat::test_path("data/test-classic.rds")) %>% | ||
jsonlite::fromJSON() %>% | ||
`[[<-`("message", "* This is a warning with a leading asterisk and {braces} to make sure we don't have bulleting/glue bugs.") | ||
mockery::stub(fetch_classic, "jsonlite::fromJSON", debug_response_content_triplet) | ||
mockery::stub(fetch_tbl, "fetch_classic", fetch_classic) | ||
expect_warning(epidata_call %>% fetch_tbl(), | ||
regexp = paste0("epidata warning: ", artificial_warning), | ||
fixed = TRUE | ||
`[[<-`("message", artificial_warning) | ||
with_mocked_bindings( | ||
{ | ||
expect_warning(epidata_call %>% fetch_tbl(), | ||
regexp = paste0("epidata warning: ", artificial_warning), | ||
fixed = TRUE | ||
) | ||
}, | ||
fromJSON = function(...) debug_response_content_triplet, | ||
.package = "jsonlite" | ||
) | ||
}) |
Uh oh!
There was an error while loading. Please reload this page.