beaumontmeteo/tests/testthat/test-script-helpers.R

58 lines
1.6 KiB
R

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("DPClim period bounds include the full end day", {
helper_env <- new.env(parent = globalenv())
sys.source(testthat::test_path("..", "..", "R", "getStationData.R"), envir = helper_env)
bounds <- helper_env$get_dpclim_period_bounds(
start_date = "2024-08-09",
end_date = "2024-09-07"
)
expect_identical(bounds$start, "2024-08-09T00:00:00Z")
expect_identical(bounds$end, "2024-09-08T00:00:00Z")
})
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
)
)
})