From b09eb2c473da0e72870f1558c3bd14198f0d37ba Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 11 Apr 2026 09:26:04 +0100 Subject: [PATCH] Add regression tests for script helpers --- tests/testthat/test-script-helpers.R | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/testthat/test-script-helpers.R diff --git a/tests/testthat/test-script-helpers.R b/tests/testthat/test-script-helpers.R new file mode 100644 index 0000000..e56e559 --- /dev/null +++ b/tests/testthat/test-script-helpers.R @@ -0,0 +1,44 @@ +test_that("sourced getStationData handles request failure without attached httr", { + helper_env <- new.env(parent = globalenv()) + sys.source(testthat::test_path("..", "..", "R", "getStationData.R"), envir = helper_env) + + helper_env$request_api <- function(...) NULL + + expect_null( + helper_env$getStationData( + start_date = "2024-01-01", + end_date = "2024-01-02", + station_id = "12345678", + headers = list(), + timesleep = 0 + ) + ) +}) + + +test_that("getAllFromCoord returns NULL when every station fetch fails", { + helper_env <- new.env(parent = globalenv()) + sys.source(testthat::test_path("..", "..", "R", "getAllFromCoord.R"), envir = helper_env) + + helper_env$getIdFromCoords <- function(...) { + data.frame( + Id_station = c("1001", "1002"), + Nom_usuel = c("Station A", "Station B"), + stringsAsFactors = FALSE + ) + } + + helper_env$getStationData <- function(...) NULL + + expect_null( + helper_env$getAllFromCoord( + coord = c(44.85, 5.84), + start_date = "2024-01-01", + end_date = "2024-01-02", + allstations = data.frame(), + headers = list(), + btw_station_sleep = 0, + within_statio_sleep = 0 + ) + ) +})