diff --git a/R/cache.R b/R/cache.R index c0b6e3df..5e0fa711 100644 --- a/R/cache.R +++ b/R/cache.R @@ -249,7 +249,11 @@ cache_epidata_call <- function(epidata_call, fetch_args = fetch_args_list()) { ) } if (!is.key_missing(cached)) { - cli::cli_warn("loading from the cache at {cache_environ$epidatr_cache$info()$dir}; see {cache_environ$epidatr_cache$info()$logfile} for more details.", + cli::cli_warn( + c( + "loading from the cache at {cache_environ$epidatr_cache$info()$dir}; ", + "see {cache_environ$epidatr_cache$info()$logfile} for more details." + ), .frequency = "regularly", .frequency_id = "using the cache", class = "cache_access" diff --git a/tests/testthat/test-cache.R b/tests/testthat/test-cache.R index 0bacb691..c0e18031 100644 --- a/tests/testthat/test-cache.R +++ b/tests/testthat/test-cache.R @@ -1,8 +1,8 @@ test_that("basic cache setup", { expect_true(is.null(cache_environ$epidatr_cache)) }) -# clear_cache(disable = TRUE) tmp_base_dir <- Sys.getenv("TMPDIR") +new_temp_dir <- NULL if (tmp_base_dir == "") { new_temp_dir <- md5(paste(Sys.time(), "I am the very model of a modern major general")) } else { @@ -17,7 +17,14 @@ test_set_cache <- function(cache_dir = new_temp_dir, logfile = "logfile.txt", prune_rate = 3L, confirm = FALSE) { - set_cache(cache_dir = cache_dir, days = days, max_size = max_size, logfile = logfile, prune_rate = prune_rate, confirm = confirm) + set_cache( + cache_dir = cache_dir, + days = days, + max_size = max_size, + logfile = logfile, + prune_rate = prune_rate, + confirm = confirm + ) } test_that("cache set as expected", { @@ -70,7 +77,9 @@ test_that("cache saves & loads", { rlang::reset_warning_verbosity("using the cache") expect_equal(first_call, cache_call) # and compare directly with the file saved - # the request url hashed here is "https://api.delphi.cmu.edu/epidata/covidcast/?data_source=jhu-csse&signals=confirmed_7dav_incidence_prop&geo_type=state&time_type=day&geo_values=ca%2Cfl&time_values=20200601-20200801&as_of=20220101" + # the request url hashed here is "https://api.delphi.cmu.edu/epidata/covidcast/?data_source=jhu-csse&signals= + # confirmed_7dav_incidence_prop&geo_type=state&time_type=day&geo_values=ca%2Cfl&time_values=20200601-20200801 + # &as_of=20220101" request_hash <- "01479468989102176d7cb70374f18f1f.rds" direct_from_cache <- readRDS(file.path(new_temp_dir, request_hash)) expect_equal(first_call, direct_from_cache[[1]]) @@ -81,10 +90,34 @@ test_that("check_is_recent", { expect_true(check_is_recent(format(Sys.Date(), format = "%Y%m%d"), 7)) expect_true(check_is_recent(format(Sys.Date() + 12, format = "%Y%m%d"), 7)) expect_false(check_is_recent(format(Sys.Date() - 12, format = "%Y%m%d"), 7)) - expect_false(check_is_recent(epirange(format(Sys.Date() - 12, format = "%Y%m%d"), format(Sys.Date() - 9, format = "%Y%m%d")), 7)) - expect_true(check_is_recent(epirange(format(Sys.Date() - 12, format = "%Y%m%d"), format(Sys.Date(), format = "%Y%m%d")), 7)) - expect_true(check_is_recent(epirange(format(Sys.Date() - 2, format = "%Y%m%d"), format(Sys.Date(), format = "%Y%m%d")), 7)) - expect_true(check_is_recent(epirange(format(Sys.Date(), format = "%Y%m%d"), format(Sys.Date() + 5, format = "%Y%m%d")), 7)) + expect_false(check_is_recent( + epirange( + format(Sys.Date() - 12, format = "%Y%m%d"), + format(Sys.Date() - 9, format = "%Y%m%d") + ), + 7 + )) + expect_true(check_is_recent( + epirange( + format(Sys.Date() - 12, format = "%Y%m%d"), + format(Sys.Date() - 2, format = "%Y%m%d") + ), + 7 + )) + expect_true(check_is_recent( + epirange( + format(Sys.Date() - 2, format = "%Y%m%d"), + format(Sys.Date(), format = "%Y%m%d") + ), + 7 + )) + expect_true(check_is_recent( + epirange( + format(Sys.Date(), format = "%Y%m%d"), + format(Sys.Date() + 5, format = "%Y%m%d") + ), + 7 + )) }) test_that("check_is_cachable", { @@ -114,15 +147,43 @@ test_that("check_is_cachable", { check_fun(issues = "*", expected_result = FALSE) # invalid issues # any odd fetch args mean don't use the cache - check_fun(as_of = "2020-01-01", fetch_args = fetch_args_list(fields = c("time_value", "value")), expected_result = FALSE) - check_fun(as_of = "2020-01-01", fetch_args = fetch_args_list(disable_date_parsing = TRUE), expected_result = FALSE) - check_fun(as_of = "2020-01-01", fetch_args = fetch_args_list(disable_data_frame_parsing = TRUE), expected_result = FALSE) + check_fun( + as_of = "2020-01-01", + fetch_args = fetch_args_list(fields = c("time_value", "value")), + expected_result = FALSE + ) + check_fun( + as_of = "2020-01-01", + fetch_args = fetch_args_list(disable_date_parsing = TRUE), + expected_result = FALSE + ) + check_fun( + as_of = "2020-01-01", + fetch_args = fetch_args_list(disable_data_frame_parsing = TRUE), + expected_result = FALSE + ) # return_empty is fine # timeout_seconds is fine - check_fun(as_of = "2020-01-01", fetch_args = fetch_args_list(base_url = "foo.bar"), expected_result = FALSE) - check_fun(as_of = "2020-01-01", fetch_args = fetch_args_list(dry_run = TRUE), expected_result = FALSE) - check_fun(as_of = "2020-01-01", fetch_args = fetch_args_list(debug = TRUE), expected_result = FALSE) - check_fun(as_of = "2020-01-01", fetch_args = fetch_args_list(format_type = "csv"), expected_result = FALSE) + check_fun( + as_of = "2020-01-01", + fetch_args = fetch_args_list(base_url = "foo.bar"), + expected_result = FALSE + ) + check_fun( + as_of = "2020-01-01", + fetch_args = fetch_args_list(dry_run = TRUE), + expected_result = FALSE + ) + check_fun( + as_of = "2020-01-01", + fetch_args = fetch_args_list(debug = TRUE), + expected_result = FALSE + ) + check_fun( + as_of = "2020-01-01", + fetch_args = fetch_args_list(format_type = "csv"), + expected_result = FALSE + ) # cases where the cache isn't active disable_cache()