Skip to content

Commit 02a6882

Browse files
authored
Merge pull request #101 from cmu-delphi/lcb/allow-timeset-star-again
Allow "*" as a timeset, update roxygen, tests
2 parents 0bdcdc2 + 121249d commit 02a6882

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

R/model.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ epirange <- function(from, to) {
5555
#' YYYYMMDD,
5656
#' - Epiweeks: integer-like values or integer-like strings that take the form YYYYMM,
5757
#' - EpiRanges: a list of [`epirange`] instances.
58+
#' - Wildcard: "*" (requests all available values for this dimension)
5859
#'
59-
#' Please refer to the specific endpoint documentation for guidance on using dates vs weeks. Most
60-
#' endpoints support only one or the other.
60+
#' Please refer to the specific endpoint documentation for guidance on using
61+
#' dates vs weeks. Most endpoints support only one or the other. Some (less
62+
#' commonly used) endpoints may not accept the `"*"` wildcard, but this can be
63+
#' simulated with a large [`epirange`].
6164
#'
6265
#' @name timeset
6366
NULL
@@ -176,7 +179,9 @@ parse_timeset_input <- function(value) {
176179
stop(paste0("Invalid timeset input: ", value))
177180
}
178181
} else if (test_character(value)) {
179-
if (all(nchar(value) %in% c(6, 8))) {
182+
if (identical(value, "*")) {
183+
return(value)
184+
} else if (all(nchar(value) %in% c(6, 8))) {
180185
return(value)
181186
} else if (all(nchar(value) == 10)) {
182187
value <- as.Date(value, format = "%Y-%m-%d")

man/timeset.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-check.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ test_that("assert_date_param", {
3232
})
3333

3434
test_that("assert_timeset_param", {
35+
# Make sure to keep in sync with test-model.R parse_timeset_input checks
3536
expect_no_error(assert_timeset_param("name", "*"))
3637
expect_no_error(assert_timeset_param("name", "2020-01-01", len = 1))
3738
expect_error(assert_timeset_param("name", c("2020-01-01", "2021-01-02"), len = 1))
@@ -43,4 +44,7 @@ test_that("assert_timeset_param", {
4344
expect_error(assert_timeset_param("name", NULL))
4445
expect_no_error(assert_timeset_param("name", NULL, required = FALSE))
4546
expect_error(assert_timeset_param("name", list(epirange(20200101, 20200102), epirange(20200101, 20200102))))
47+
# Non-EpiRange-class epiranges are no longer allowed:
48+
expect_error(assert_timeset_param("name", list(from = "2020-01-01", to = "2021-01-02")))
49+
expect_error(assert_timeset_param("name", c(from = "2020-01-01", to = "2021-01-02")))
4650
})

tests/testthat/test-epirange.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ test_that("epirange", {
88
expect_error(epirange(1, 2))
99
expect_error(epirange("1", "2"))
1010
expect_error(epirange(201501, 20160101))
11+
expect_error(epirange(epirange(201501, 201502), epirange(201601, 201602)))
1112
})

tests/testthat/test-model.R

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
test_that("`parse_timeset_input` on valid inputs", {
2+
# Date-like: we can get out Date vector, or character or integerish YYYYmmdd
3+
# format only
4+
expect_identical(
5+
parse_timeset_input(as.Date("2018-01-01")),
6+
as.Date("2018-01-01")
7+
)
8+
expect_identical(
9+
parse_timeset_input(as.Date(c("2018-01-01", "2018-01-02"))),
10+
as.Date(c("2018-01-01", "2018-01-02"))
11+
)
12+
expect_identical(parse_timeset_input("2018-01-01"), "20180101")
13+
expect_identical(
14+
parse_timeset_input(c("2018-01-01", "2018-01-02")),
15+
c("20180101", "20180102")
16+
)
17+
expect_identical(parse_timeset_input("20180101"), "20180101")
18+
expect_identical(
19+
parse_timeset_input(c("20180101", "20180102")),
20+
c("20180101", "20180102")
21+
)
22+
expect_identical(parse_timeset_input(20180101), 20180101)
23+
expect_identical(
24+
parse_timeset_input(c(20180101, 20180102)),
25+
c(20180101, 20180102)
26+
)
27+
# Epiweeks: we can get out character or integerish
28+
expect_identical(parse_timeset_input("201801"), "201801")
29+
expect_identical(
30+
parse_timeset_input(c("201801", "201802")),
31+
c("201801", "201802")
32+
)
33+
expect_identical(parse_timeset_input(201801), 201801)
34+
expect_identical(
35+
parse_timeset_input(c(201801, 201802)),
36+
c(201801, 201802)
37+
)
38+
# EpiRanges: aren't changed
39+
expect_identical(
40+
epirange(as.Date("2018-01-01"), as.Date("2018-01-05")),
41+
epirange(as.Date("2018-01-01"), as.Date("2018-01-05"))
42+
)
43+
expect_identical(epirange(201801, 201805), epirange(201801, 201805))
44+
# Wildcard:
45+
expect_identical(parse_timeset_input("*"), "*")
46+
# NULL: allow this as a missing argument marker
47+
expect_identical(parse_timeset_input(NULL), NULL)
48+
})

0 commit comments

Comments
 (0)