test_that("Test offline", { skip_on_cran() skip_if_siane_offline() local_mocked_bindings(is_online_fun = function(...) { FALSE }) url <- paste0( "https://github.com/rOpenSpain/mapSpain/raw/sianedata/dist/", "se89_3_urban_capimuni_p_y.gpkg" ) cdir <- file.path(tempdir(), "testthat_ex") if (dir.exists(cdir)) { unlink(cdir, recursive = TRUE, force = TRUE) } expect_snapshot( fend <- download_url( url, cache_dir = cdir, subdir = "fixme", update_cache = FALSE, verbose = FALSE ) ) expect_null(fend) expect_length(list.files(cdir, recursive = TRUE), 0) unlink(cdir, recursive = TRUE, force = TRUE) local_mocked_bindings(is_online_fun = function(...) { httr2::is_online() }) }) test_that("Test 404", { skip_on_cran() skip_if_siane_offline() cdir <- file.path(tempdir(), "testthat_ex") if (dir.exists(cdir)) { unlink(cdir, recursive = TRUE, force = TRUE) } local_mocked_bindings(is_404 = function(...) { TRUE }) url <- paste0( "https://github.com/rOpenSpain/mapSpain/raw/sianedata/dist/", "se89_3_urban_capimuni_p_y.gpkg" ) expect_message( s <- download_url( url, verbose = FALSE, cache_dir = cdir, update_cache = TRUE ), "Error " ) expect_null(s) local_mocked_bindings(is_404 = function(...) { FALSE }) # Otherwise work expect_silent( s <- download_url( url, verbose = FALSE, cache_dir = cdir, update_cache = TRUE ) ) expect_length(s, 1) expect_true(is.character(s)) if (dir.exists(cdir)) { unlink(cdir, recursive = TRUE, force = TRUE) } }) test_that("Caching tests", { skip_on_cran() skip_if_siane_offline() url <- paste0( "https://github.com/rOpenSpain/mapSpain/raw/sianedata/dist/", "se89_3_urban_capimuni_p_y.gpkg" ) cdir <- file.path(tempdir(), "testthat_ex") if (dir.exists(cdir)) { unlink(cdir, recursive = TRUE, force = TRUE) } expect_message( fend <- download_url( url, cache_dir = cdir, subdir = "fixme", update_cache = FALSE, verbose = TRUE ), "Cache dir is" ) expect_length(list.files(cdir, recursive = TRUE), 1) expect_message( fend <- download_url( url, cache_dir = cdir, subdir = "fixme", update_cache = FALSE, verbose = TRUE ), "File already" ) expect_message( fend <- download_url( url, cache_dir = cdir, subdir = "fixme", update_cache = TRUE, verbose = TRUE ), "Updating cached" ) unlink(cdir, recursive = TRUE, force = TRUE) }) test_that("Caching errors", { skip_on_cran() skip_if_siane_offline() url <- paste0( "https://github.com/rOpenSpain/mapSpain/raw/sianedata/dist/", "fake-file.txt" ) cdir <- file.path(tempdir(), "testthat_ex") if (dir.exists(cdir)) { unlink(cdir, recursive = TRUE, force = TRUE) } expect_message( fend <- download_url( url, cache_dir = cdir, subdir = "fixme", update_cache = FALSE, verbose = FALSE ), "Error" ) expect_null(fend) # Warn if size of download is huge url <- paste0( "https://gisco-services.ec.europa.eu/distribution/v2/", "lau/gpkg/LAU_RG_01M_2024_4326.gpkg" ) expect_message( download_url( url, cache_dir = cdir, subdir = "fixme", update_cache = FALSE, verbose = FALSE ), "The file to be downloaded has size" ) unlink(cdir, recursive = TRUE, force = TRUE) }) test_that("Test import jsonlite", { skip_on_cran() skip_if_siane_offline() expect_silent(p <- for_import_jsonlite()) expect_null(for_import_jsonlite()) }) test_that("Test timeout", { skip_on_cran() skip_if_siane_offline() cdir <- file.path(tempdir(), "testthat_timeout") if (dir.exists(cdir)) { unlink(cdir, recursive = TRUE, force = TRUE) } url <- paste0( "https://github.com/rOpenSpain/mapSpain/raw/sianedata/dist/", "se89_3_admin_muni_a_x.gpkg" ) withr::local_options(mapspain_timeout = 0.01) expect_error( download_url(url = url, verbose = FALSE, cache_dir = cdir), "Failed to perform HTTP request(.*)Timeout(.*)after(.*)milliseconds" ) withr::local_options(mapspain_timeout = 300L) expect_silent( ff <- download_url(url = url, verbose = FALSE, cache_dir = cdir) ) expect_true(file.exists(ff)) unlink(cdir, recursive = TRUE, force = TRUE) expect_false(file.exists(ff)) })