Skip to content

Commit

Permalink
fix: happy linter
Browse files Browse the repository at this point in the history
  • Loading branch information
dsweber2 committed Sep 5, 2023
1 parent 6c0e8fa commit 5e17a36
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
6 changes: 5 additions & 1 deletion R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
89 changes: 75 additions & 14 deletions tests/testthat/test-cache.R
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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", {
Expand Down Expand Up @@ -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]])
Expand All @@ -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", {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5e17a36

Please sign in to comment.