Skip to content

Commit a920a76

Browse files
authored
Merge pull request #259 from cmu-delphi/ndefries/caching-asof-date
If `as_of` sent to `check_is_recent` is a `Date`, compare to a `Date` threshold.
2 parents 693c04b + 739eebd commit a920a76

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: epidatr
22
Type: Package
33
Title: Client for Delphi's 'Epidata' API
4-
Version: 1.1.0
4+
Version: 1.1.1
55
Authors@R:
66
c(
77
person("Logan", "Brooks", email = "[email protected]", role = c("aut")),

NEWS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# epidatr 1.1.1
2+
3+
## Changes
4+
5+
## Features
6+
7+
## Patches
8+
- Fixed failure when passing `as_of` values in `Date` format to
9+
`pub_covidcast` while caching is enabled (#259)
10+
111
# epidatr 1.1.0
212

313
## Changes

R/utils.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ format_list <- function(values) {
2222
#'
2323
#' @keywords internal
2424
check_is_recent <- function(dates, max_age) {
25-
(!is.null(dates) && any(dates >= format(Sys.Date() - max_age, format = "%Y%m%d")))
25+
if (inherits(dates, "Date")) {
26+
threshold <- Sys.Date() - max_age
27+
} else {
28+
threshold <- format(Sys.Date() - max_age, format = "%Y%m%d")
29+
}
30+
(!is.null(dates) && any(dates >= threshold))
2631
}
2732

2833
#' helper that checks whether a call is actually cachable

tests/testthat/test-utils.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,33 @@ test_that("check_is_cachable can handle both str and date inputs of various leng
6565
expect_no_error(check_is_cachable(epidata_call, fetch_args))
6666
})
6767

68+
test_that("check_is_recent can handle both str and date inputs of various lengths", {
69+
# NULL
70+
as_of <- NULL
71+
expect_no_error(result <- check_is_recent(as_of, 10))
72+
expect_identical(result, FALSE)
73+
74+
# as_of single string
75+
as_of <- "2022-01-01"
76+
expect_no_error(result <- check_is_recent(as_of, 10))
77+
expect_identical(result, FALSE)
78+
79+
# as_of string vector
80+
as_of <- c("2022-01-01", "3000-01-02", "3000-01-03")
81+
expect_no_error(result <- check_is_recent(as_of, 10))
82+
expect_identical(result, TRUE)
83+
84+
# as_of single date
85+
as_of <- as.Date("2022-01-01")
86+
expect_no_error(result <- check_is_recent(as_of, 10))
87+
expect_identical(result, FALSE)
88+
89+
# as_of date vector
90+
as_of <- as.Date(c("2022-01-01", "3000-01-02", "3000-01-03"))
91+
expect_no_error(result <- check_is_recent(as_of, 10))
92+
expect_identical(result, TRUE)
93+
})
94+
6895
test_that("get_wildcard_equivalent_dates works in basic cases", {
6996
# Week date
7097
result <- get_wildcard_equivalent_dates(epirange(202002, 202013), "week")

0 commit comments

Comments
 (0)