Skip to content

Commit

Permalink
Add test for no data upon filter
Browse files Browse the repository at this point in the history
  • Loading branch information
asadow committed Jun 27, 2024
1 parent d4601c6 commit f0d1e4e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
17 changes: 12 additions & 5 deletions R/pull.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() |>
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-get.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/trade/trade-cd86e0.R
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit f0d1e4e

Please sign in to comment.