tests/testthat/helper.R

skip_if_gisco_offline <- function() {
  if (gisco_check_access()) {
    return(invisible(TRUE))
  }

  testthat::skip("GISCO API not reachable")

  invisible()
}

mock_connection_failure <- function(...) {
  cli::cli_abort("Mock connection failure.", class = "httr2_failure")
}

local_test_cache_dir <- function(
  pattern = "gisco-test-",
  .local_envir = parent.frame()
) {
  path <- withr::local_tempdir(pattern = pattern, .local_envir = .local_envir)
  withr::defer(
    unlink(path, recursive = TRUE, force = TRUE),
    envir = .local_envir
  )
  path
}

local_test_cached_db <- function(pattern = "gisco-db-") {
  local_envir <- parent.frame()
  cache_dir <- local_test_cache_dir(pattern, .local_envir = local_envir)
  withr::local_envvar(GISCO_CACHE_DIR = cache_dir, .local_envir = local_envir)

  saveRDS(giscoR::gisco_db, cached_db_file(cache_dir))
  invisible(cache_dir)
}

Try the giscoR package in your browser

Any scripts or data that you put into this service are public.

giscoR documentation built on Oct. 1, 2026, 5:13 p.m.