Skip to content

Commit

Permalink
Merge pull request #259 from cmu-delphi/ndefries/caching-asof-date
Browse files Browse the repository at this point in the history
If `as_of` sent to `check_is_recent` is a `Date`, compare to a `Date` threshold.
  • Loading branch information
nmdefries authored Feb 29, 2024
2 parents 693c04b + 739eebd commit a920a76
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: epidatr
Type: Package
Title: Client for Delphi's 'Epidata' API
Version: 1.1.0
Version: 1.1.1
Authors@R:
c(
person("Logan", "Brooks", email = "[email protected]", role = c("aut")),
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# epidatr 1.1.1

## Changes

## Features

## Patches
- Fixed failure when passing `as_of` values in `Date` format to
`pub_covidcast` while caching is enabled (#259)

# epidatr 1.1.0

## Changes
Expand Down
7 changes: 6 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ format_list <- function(values) {
#'
#' @keywords internal
check_is_recent <- function(dates, max_age) {
(!is.null(dates) && any(dates >= format(Sys.Date() - max_age, format = "%Y%m%d")))
if (inherits(dates, "Date")) {
threshold <- Sys.Date() - max_age
} else {
threshold <- format(Sys.Date() - max_age, format = "%Y%m%d")
}
(!is.null(dates) && any(dates >= threshold))
}

#' helper that checks whether a call is actually cachable
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ test_that("check_is_cachable can handle both str and date inputs of various leng
expect_no_error(check_is_cachable(epidata_call, fetch_args))
})

test_that("check_is_recent can handle both str and date inputs of various lengths", {
# NULL
as_of <- NULL
expect_no_error(result <- check_is_recent(as_of, 10))
expect_identical(result, FALSE)

# as_of single string
as_of <- "2022-01-01"
expect_no_error(result <- check_is_recent(as_of, 10))
expect_identical(result, FALSE)

# as_of string vector
as_of <- c("2022-01-01", "3000-01-02", "3000-01-03")
expect_no_error(result <- check_is_recent(as_of, 10))
expect_identical(result, TRUE)

# as_of single date
as_of <- as.Date("2022-01-01")
expect_no_error(result <- check_is_recent(as_of, 10))
expect_identical(result, FALSE)

# as_of date vector
as_of <- as.Date(c("2022-01-01", "3000-01-02", "3000-01-03"))
expect_no_error(result <- check_is_recent(as_of, 10))
expect_identical(result, TRUE)
})

test_that("get_wildcard_equivalent_dates works in basic cases", {
# Week date
result <- get_wildcard_equivalent_dates(epirange(202002, 202013), "week")
Expand Down

0 comments on commit a920a76

Please sign in to comment.