Skip to content

Commit 88cebdc

Browse files
committed
Switch covidcast_meta to request CSVs as well
1 parent 7c5abc6 commit 88cebdc

File tree

4 files changed

+16
-44
lines changed

4 files changed

+16
-44
lines changed

R-packages/covidcast/R/covidcast.R

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,17 +429,15 @@ covidcast_signals <- function(data_source, signal,
429429
#'
430430
#' @export
431431
covidcast_meta <- function() {
432-
meta <- jsonlite::fromJSON(.request(
432+
meta <- .request(
433433
list(source = "covidcast_meta",
434-
cached = "true")))
434+
format = "csv"))
435435

436-
if (meta$message != "success") {
437-
abort(paste0("Failed to obtain metadata: ", meta$message, "."),
438-
err_msg = meta$message,
439-
class = "covidcast_meta_fetch_failed")
436+
if (nchar(meta) == 0) {
437+
abort("Failed to obtain metadata", class = "covidcast_meta_fetch_failed")
440438
}
441439

442-
meta <- meta$epidata %>%
440+
meta <- read.csv(textConnection(meta), stringsAsFactors = FALSE) %>%
443441
dplyr::mutate(min_time = api_to_date(.data$min_time),
444442
max_time = api_to_date(.data$max_time),
445443
max_issue = api_to_date(.data$max_issue))

R-packages/covidcast/tests/testthat/api.covidcast.cmu.edu/epidata/api.php-d2e163.json

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data_source,signal,time_type,geo_type,min_time,max_time,min_value,max_value,max_issue
2+
foo,bar,day,county,20200101,20200102,0,10,20200404
3+
foo,bar,day,county,20201002,20201003,0,10,20201101

R-packages/covidcast/tests/testthat/test-covidcast.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ library(dplyr)
1313
# types of errors.
1414
#
1515
# 2. Once you've written a test, it can be difficult to find the file storing
16-
# the JSON needed for that test. We hence store the filename in comments
17-
# adjacent to every call.
16+
# the JSON or CSV needed for that test. We hence store the filename in comments
17+
# adjacent to every call. (Note that we request CSVs from the API for
18+
# covidcast_signal, and httptest suggests filenames ending in .json by default.
19+
# You can use .csv instead and httptest will correctly locate those files.)
1820
#
1921
# 3. covidcast_signal() calls covidcast_meta() unconditionally. We hence need a
2022
# single meta file that suffices for all tests that call covidcast_signal().
@@ -38,29 +40,28 @@ library(dplyr)
3840

3941
with_mock_api({
4042
test_that("covidcast_meta formats result correctly", {
41-
# api.php-d2e163.json
43+
# api.php-dd024f.csv
4244
expect_equal(covidcast_meta(),
4345
structure(
4446
data.frame(
4547
data_source = "foo",
4648
signal = c("bar", "bar2"),
4749
min_time = as.Date(c("2020-01-01", "2020-10-02")),
4850
max_time = as.Date(c("2020-01-02", "2020-10-03")),
49-
max_issue = as.Date(c("2020-04-04", "2020-11-01")),
5051
min_value = 0,
5152
max_value = 10,
5253
num_locations = 100,
5354
time_type = "day",
54-
geo_type = "county"
55+
geo_type = "county",
56+
max_issue = as.Date(c("2020-04-04", "2020-11-01"))
5557
),
5658
class = c("covidcast_meta", "data.frame")
5759
))
5860
})
5961
})
6062

6163
test_that("covidcast_meta raises error when API signals one", {
62-
stub(covidcast_meta, ".request",
63-
"{\"message\": \"argle-bargle\"}")
64+
stub(covidcast_meta, ".request", "")
6465

6566
expect_error(covidcast_meta(),
6667
class = "covidcast_meta_fetch_failed")

0 commit comments

Comments
 (0)