From f0d1e4e89dfdd105af755b16af9fa3e66fdd02b7 Mon Sep 17 00:00:00 2001 From: Adam Sadowski Date: Thu, 27 Jun 2024 10:26:43 -0400 Subject: [PATCH] Add test for no data upon filter --- R/pull.R | 17 ++++++++++++----- tests/testthat/test-get.R | 10 ++++++++++ tests/testthat/trade/trade-cd86e0.R | 22 ++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 tests/testthat/trade/trade-cd86e0.R diff --git a/R/pull.R b/R/pull.R index 11a3882..fa6eebc 100644 --- a/R/pull.R +++ b/R/pull.R @@ -33,16 +33,12 @@ mm_data <- function(endpoint, ..., allfields = TRUE) { successes <- responses |> httr2::resps_successes() pages <- successes |> purrr::map(mm_resp_extract) - timestamps <- successes |> - purrr::map(\(x) x |> purrr::pluck("headers", "Date") |> parse_header_date()) - pages <- purrr::map2(pages, timestamps, \(x, y) x |> dplyr::mutate(ts = !!y)) - no_data_i <- pages |> purrr::map_lgl(is.null) |> which() if (errors_occured || any(no_data_i)) { cli::cli_alert_info( c("NB: {length(errors) + length(no_data_i)}/{length(responses)}", - " requests returned errors or no data.") + " request{?s} returned errors or no data.") ) for (error in errors) { url <- error$request$url @@ -60,6 +56,17 @@ mm_data <- function(endpoint, ..., allfields = TRUE) { } } + ## Add ts timestamp column + timestamps <- successes |> + purrr::map(\(x) x |> purrr::pluck("headers", "Date") |> parse_header_date()) + pages <- purrr::map2( + pages, + timestamps, + \(x, y) if(!is.null(x)) { + x |> dplyr::mutate(ts = !!y) + } else x + ) + data <- pages |> mm_pagebind() data |> remove_api_urls() |> diff --git a/tests/testthat/test-get.R b/tests/testthat/test-get.R index ec9b4cf..eda6c12 100644 --- a/tests/testthat/test-get.R +++ b/tests/testthat/test-get.R @@ -25,6 +25,16 @@ with_mock_dir("statuses", { }) }) +with_mock_dir("trade", { + test_that("mm_data() returns informative error on no data", { + skip_on_cran() + expect_message( + mm_data("trade", department_code = "f"), + regexp = "returned errors or no data." + ) + }) +}) + with_mock_dir("status_col_info", { test_that("mm_names() returns column names and info", { skip_on_cran() diff --git a/tests/testthat/trade/trade-cd86e0.R b/tests/testthat/trade/trade-cd86e0.R new file mode 100644 index 0000000..2407dbe --- /dev/null +++ b/tests/testthat/trade/trade-cd86e0.R @@ -0,0 +1,22 @@ +structure(list(method = "GET", url = "trade?DEPARTMENT_CODE=f&ALLFIELDS=1", + status_code = 200L, headers = structure(list(`Cache-Control` = "REDACTED", + `Content-Type` = "application/hal+json", Server = "REDACTED", + `X-Powered-By` = "REDACTED", `Access-Control-Allow-Origin` = "REDACTED", + `WWW-Authenticate` = "REDACTED", `X-Powered-By` = "REDACTED", + `X-Robots-Tag` = "REDACTED", Date = "Thu, 27 Jun 2024 14:22:57 GMT", + `Content-Length` = "REDACTED", `Set-Cookie` = "REDACTED", + `Strict-Transport-Security` = "REDACTED"), class = "httr2_headers"), + body = as.raw(c(0x7b, 0x22, 0x5f, 0x6c, 0x69, 0x6e, 0x6b, + 0x73, 0x22, 0x3a, 0x7b, 0x22, 0x73, 0x65, 0x6c, 0x66, 0x22, + 0x3a, 0x7b, 0x22, 0x68, 0x72, 0x65, 0x66, 0x22, 0x3a, 0x22, + 0x74, 0x72, 0x61, 0x64, 0x65, 0x3f, 0x44, 0x45, 0x50, 0x41, + 0x52, 0x54, 0x4d, 0x45, 0x4e, 0x54, 0x5f, 0x43, 0x4f, 0x44, + 0x45, 0x3d, 0x66, 0x26, 0x41, 0x4c, 0x4c, 0x46, 0x49, 0x45, + 0x4c, 0x44, 0x53, 0x3d, 0x31, 0x22, 0x7d, 0x2c, 0x22, 0x61, + 0x70, 0x69, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x50, 0x6f, 0x69, + 0x6e, 0x74, 0x22, 0x3a, 0x7b, 0x22, 0x68, 0x72, 0x65, 0x66, + 0x22, 0x3a, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, + 0x2f, 0x61, 0x70, 0x69, 0x2e, 0x6d, 0x65, 0x67, 0x61, 0x6d, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x75, 0x6f, 0x67, 0x2f, 0x64, 0x6c, 0x22, 0x7d, 0x7d, 0x7d + )), cache = new.env(parent = emptyenv())), class = "httr2_response")