From 138b138d55a1b862031604bb89012842ae79c479 Mon Sep 17 00:00:00 2001 From: Bart Date: Wed, 20 Aug 2025 11:24:52 +0200 Subject: [PATCH 1/2] Skip test if radar has not recently been updated --- tests/testthat/test-get_pvol_se.R | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/testthat/test-get_pvol_se.R b/tests/testthat/test-get_pvol_se.R index 814a545b..f5d3eae2 100644 --- a/tests/testthat/test-get_pvol_se.R +++ b/tests/testthat/test-get_pvol_se.R @@ -1,6 +1,12 @@ test_that("Pvol for Sweden can be downloaded", { skip_if_offline("opendata-download-radar.smhi.se") time <- as.POSIXct(Sys.time(), tz = "Europe/Helsinki") - lubridate::hours(10) + skip_if_not( + httr2::request("https://opendata-download-radar.smhi.se/api/version/latest/area/") |> + httr2::req_perform() |> httr2::resp_body_json() |> purrr::chuck("areas") |> + purrr::map(~ as.data.frame(.x)) |> dplyr::bind_rows() |> dplyr::filter(key == "atvidaberg") |> + purrr::chuck("updated") |> lubridate::as_datetime() > time + ) expect_s3_class(pvol <- get_pvol("seatv", time, param = "all"), "pvol") expect_true(bioRad::is.pvol(pvol)) expect_identical( @@ -12,6 +18,13 @@ test_that("Pvol for Sweden can be downloaded", { test_that("Pvol for Sweden fails out of time range", { skip_if_offline("opendata-download-radar.smhi.se") time <- Sys.time() - lubridate::hours(40) + skip_if_not( + httr2::request("https://opendata-download-radar.smhi.se/api/version/latest/area/") |> + httr2::req_perform() |> httr2::resp_body_json() |> purrr::chuck("areas") |> + purrr::map(~ as.data.frame(.x)) |> dplyr::bind_rows() |> dplyr::filter(key == "hudiksvall") |> + purrr::chuck("updated") |> lubridate::as_datetime() > time + ) + expect_error(get_pvol("sehuv", time), class = "getRad_error_get_pvol_se_data_not_found") time <- Sys.time() + lubridate::hours(1) expect_error(get_pvol("sehuv", time), class = "getRad_error_get_pvol_se_data_not_found") From c8f51a1cd7729305c395277cefcdf0d9d76e7ea3 Mon Sep 17 00:00:00 2001 From: Bart Date: Wed, 20 Aug 2025 14:11:42 +0200 Subject: [PATCH 2/2] Generalize to function --- tests/testthat/test-get_pvol_se.R | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tests/testthat/test-get_pvol_se.R b/tests/testthat/test-get_pvol_se.R index f5d3eae2..83f1fca5 100644 --- a/tests/testthat/test-get_pvol_se.R +++ b/tests/testthat/test-get_pvol_se.R @@ -1,12 +1,19 @@ +skip_if_se_not_updated <- function(radar, time) { + if ( + httr2::request("https://opendata-download-radar.smhi.se/api/version/latest/area/") |> + httr2::req_perform() |> httr2::resp_body_json() |> purrr::chuck("areas") |> + purrr::map(~ as.data.frame(.x)) |> dplyr::bind_rows() |> dplyr::filter(key == radar) |> + purrr::chuck("updated") |> lubridate::as_datetime() < time + ) { + testthat::skip(glue::glue("Most recent data for {radar} is older then {time}")) + } else { + invisible() + } +} test_that("Pvol for Sweden can be downloaded", { skip_if_offline("opendata-download-radar.smhi.se") time <- as.POSIXct(Sys.time(), tz = "Europe/Helsinki") - lubridate::hours(10) - skip_if_not( - httr2::request("https://opendata-download-radar.smhi.se/api/version/latest/area/") |> - httr2::req_perform() |> httr2::resp_body_json() |> purrr::chuck("areas") |> - purrr::map(~ as.data.frame(.x)) |> dplyr::bind_rows() |> dplyr::filter(key == "atvidaberg") |> - purrr::chuck("updated") |> lubridate::as_datetime() > time - ) + skip_if_se_not_updated("atvidaberg", time) expect_s3_class(pvol <- get_pvol("seatv", time, param = "all"), "pvol") expect_true(bioRad::is.pvol(pvol)) expect_identical( @@ -18,12 +25,7 @@ test_that("Pvol for Sweden can be downloaded", { test_that("Pvol for Sweden fails out of time range", { skip_if_offline("opendata-download-radar.smhi.se") time <- Sys.time() - lubridate::hours(40) - skip_if_not( - httr2::request("https://opendata-download-radar.smhi.se/api/version/latest/area/") |> - httr2::req_perform() |> httr2::resp_body_json() |> purrr::chuck("areas") |> - purrr::map(~ as.data.frame(.x)) |> dplyr::bind_rows() |> dplyr::filter(key == "hudiksvall") |> - purrr::chuck("updated") |> lubridate::as_datetime() > time - ) + skip_if_se_not_updated("hudiksvall", time) expect_error(get_pvol("sehuv", time), class = "getRad_error_get_pvol_se_data_not_found") time <- Sys.time() + lubridate::hours(1)