Skip to content

Commit 6a70274

Browse files
committed
tests: fix a few tests
1 parent 6b7944e commit 6a70274

File tree

2 files changed

+78
-49
lines changed

2 files changed

+78
-49
lines changed

R/slide.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ epi_slide <- function(x, f, ..., before = NULL, after = NULL, ref_time_values =
126126
if (inherits(before, "difftime")) {
127127
after <- as.difftime(0, units = units(before))
128128
} else {
129-
if (before == Inf && time_type %in% c("day", "week")) {
129+
if (identical(before, Inf) && time_type %in% c("day", "week")) {
130130
after <- as.difftime(0, units = glue::glue("{time_type}s"))
131131
} else {
132132
after <- 0
@@ -435,7 +435,7 @@ epi_slide <- function(x, f, ..., before = NULL, after = NULL, ref_time_values =
435435
#' ungroup()
436436
epi_slide_opt <- function(x, col_names, f, ..., before = NULL, after = NULL, ref_time_values = NULL,
437437
new_col_name = NULL, all_rows = FALSE,
438-
as_list_col = deprecated(), names_sep = deprecated()) {
438+
as_list_col = deprecated(), names_sep = NULL) {
439439
assert_class(x, "epi_df")
440440

441441
if (nrow(x) == 0L) {
@@ -540,7 +540,7 @@ epi_slide_opt <- function(x, col_names, f, ..., before = NULL, after = NULL, ref
540540
if (inherits(before, "difftime")) {
541541
after <- as.difftime(0, units = units(before))
542542
} else {
543-
if (before == Inf && time_type %in% c("day", "week")) {
543+
if (identical(before, Inf) && time_type %in% c("day", "week")) {
544544
after <- as.difftime(0, units = glue::glue("{time_type}s"))
545545
} else {
546546
after <- 0
@@ -736,7 +736,7 @@ epi_slide_opt <- function(x, col_names, f, ..., before = NULL, after = NULL, ref
736736
#' ungroup()
737737
epi_slide_mean <- function(x, col_names, ..., before = NULL, after = NULL, ref_time_values = NULL,
738738
new_col_name = NULL, all_rows = FALSE,
739-
as_list_col = deprecated(), names_sep = deprecated()) {
739+
as_list_col = deprecated(), names_sep = NULL) {
740740
epi_slide_opt(
741741
x = x,
742742
col_names = {{ col_names }},
@@ -783,7 +783,7 @@ epi_slide_sum <- function(x, col_names, ..., before = NULL, after = NULL, ref_ti
783783
new_col_name = NULL,
784784
all_rows = FALSE,
785785
as_list_col = deprecated(),
786-
names_sep = deprecated()) {
786+
names_sep = NULL) {
787787
epi_slide_opt(
788788
x = x,
789789
col_names = {{ col_names }},

tests/testthat/test-epi_slide.R

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
library(cli)
2+
13
# Create an epi_df and a function to test epi_slide with
24
test_date <- as.Date("2020-01-01")
35
days_dt <- as.difftime(1, units = "days")
@@ -53,19 +55,22 @@ bad_values <- list(
5355
"a", 0.5, -1L, -1.5, 1.5, NA, c(0, 1)
5456
)
5557
purrr::map(bad_values, function(bad_value) {
56-
test_that("`before` and `after` in epi_slide fail on {x}", {
57-
expect_error(
58-
epi_slide(grouped, before = bad_value, ref_time_values = test_date + 2),
59-
class = "epiprocess__validate_slide_window_arg"
60-
)
61-
expect_error(
62-
epi_slide(grouped, after = bad_value, ref_time_values = test_date + 2),
63-
class = "epiprocess__validate_slide_window_arg"
64-
)
65-
})
58+
test_that(
59+
format_inline("`before` and `after` in epi_slide fail on {bad_value}"),
60+
{
61+
expect_error(
62+
epi_slide(grouped, before = bad_value, ref_time_values = test_date + 2),
63+
class = "epiprocess__validate_slide_window_arg"
64+
)
65+
expect_error(
66+
epi_slide(grouped, after = bad_value, ref_time_values = test_date + 2),
67+
class = "epiprocess__validate_slide_window_arg"
68+
)
69+
}
70+
)
6671
})
6772
purrr::map(bad_values, function(bad_value) {
68-
test_that("`before` and `after` in epi_slide_mean fail on {x}", {
73+
test_that(format_inline("`before` and `after` in epi_slide_mean fail on {bad_value}"), {
6974
expect_error(
7075
epi_slide_mean(grouped, col_names = value, before = bad_value, ref_time_values = test_date + 2),
7176
class = "epiprocess__validate_slide_window_arg"
@@ -79,7 +84,7 @@ purrr::map(bad_values, function(bad_value) {
7984

8085
bad_values <- c(min(grouped$time_value) - 1, max(grouped$time_value) + 1)
8186
purrr::map(bad_values, function(bad_value) {
82-
test_that("epi_slide or epi_slide_mean: `ref_time_values` out of range for all groups generate an error", {
87+
test_that(format_inline("epi_slide[_mean]: `ref_time_values` out of range for all groups {bad_value}"), {
8388
expect_error(
8489
epi_slide(grouped, f, before = 2 * days_dt, ref_time_values = bad_value),
8590
class = "epi_slide__invalid_ref_time_values"
@@ -142,38 +147,62 @@ test_that("epi_slide alerts if the provided f doesn't take enough args", {
142147
})
143148

144149

145-
# Computation tests
146-
test_that("epi_slide outputs list columns when desired, and unpacks unnamed computations", {
147-
# See `toy_edf` and `basic_sum_result` definitions at top of file.
148-
expect_equal(
149-
toy_edf %>% epi_slide(before = 6 * days_dt, ~ sum(.x$value)),
150-
basic_sum_result
151-
)
152-
expect_equal(
153-
toy_edf %>% epi_slide(before = 6 * days_dt, ~ list(rep(sum(.x$value), 2L))),
154-
basic_sum_result %>% mutate(slide_value = lapply(slide_value, rep, 2L))
155-
)
156-
expect_equal(
157-
toy_edf %>% epi_slide(before = 6 * days_dt, ~ data.frame(slide_value = sum(.x$value))),
158-
basic_sum_result
159-
)
160-
expect_equal(
161-
toy_edf %>% epi_slide(before = 6 * days_dt, ~ list(data.frame(slide_value = sum(.x$value)))),
162-
basic_sum_result %>%
163-
mutate(slide_value = purrr::map(slide_value, ~ data.frame(slide_value = .x)))
164-
)
165-
expect_identical(
166-
toy_edf %>% epi_slide(before = 6L, ~ tibble(slide_value = list(sum(.x$value)))),
167-
basic_sum_result %>% mutate(slide_value = as.list(slide_value))
168-
)
169-
# unnamed data-masking expression producing data frame:
170-
expect_identical(
171-
# unfortunately, we can't pass this directly as `f` and need an extra comma
172-
toy_edf %>% epi_slide(before = 6L, , data.frame(slide_value = sum(.x$value))),
173-
basic_sum_result
174-
)
175-
})
150+
# Common example tests
151+
for (rtv in list(NULL, test_date + 1, c(test_date + 1, test_date + 3))) {
152+
test_that(format_inline("epi_slide works with formulas, lists, and data.frame outputs with ref_time_value {rtv}"), {
153+
expect_equal(
154+
toy_edf %>% epi_slide(before = 6 * days_dt, ~ sum(.x$value), ref_time_values = rtv),
155+
basic_sum_result %>%
156+
{
157+
if (!is.null(rtv)) dplyr::filter(., time_value %in% rtv) else .
158+
}
159+
)
160+
expect_equal(
161+
toy_edf %>% epi_slide(before = 6 * days_dt, ~ list(rep(sum(.x$value), 2L)), ref_time_values = rtv),
162+
basic_sum_result %>% mutate(slide_value = lapply(slide_value, rep, 2L)) %>%
163+
{
164+
if (!is.null(rtv)) dplyr::filter(., time_value %in% rtv) else .
165+
}
166+
)
167+
expect_equal(
168+
toy_edf %>% epi_slide(before = 6 * days_dt, ~ data.frame(slide_value = sum(.x$value)), ref_time_values = rtv),
169+
basic_sum_result %>%
170+
{
171+
if (!is.null(rtv)) dplyr::filter(., time_value %in% rtv) else .
172+
}
173+
)
174+
expect_equal(
175+
toy_edf %>% epi_slide(
176+
before = 6 * days_dt, ~ list(data.frame(slide_value = sum(.x$value))),
177+
ref_time_values = rtv
178+
),
179+
basic_sum_result %>%
180+
mutate(slide_value = purrr::map(slide_value, ~ data.frame(slide_value = .x))) %>%
181+
{
182+
if (!is.null(rtv)) dplyr::filter(., time_value %in% rtv) else .
183+
}
184+
)
185+
expect_identical(
186+
toy_edf %>% epi_slide(before = 6L, ~ tibble(slide_value = list(sum(.x$value))), ref_time_values = rtv),
187+
basic_sum_result %>% mutate(slide_value = as.list(slide_value)) %>%
188+
{
189+
if (!is.null(rtv)) dplyr::filter(., time_value %in% rtv) else .
190+
}
191+
)
192+
# unnamed data-masking expression producing data frame:
193+
expect_identical(
194+
# unfortunately, we can't pass this directly as `f` and need an extra comma
195+
toy_edf %>% epi_slide(before = 6L, , data.frame(slide_value = sum(.x$value)), ref_time_values = rtv),
196+
basic_sum_result %>%
197+
{
198+
if (!is.null(rtv)) dplyr::filter(., time_value %in% rtv) else .
199+
}
200+
)
201+
})
202+
}
203+
176204

205+
# Edge example tests
177206
test_that("epi_slide can use sequential data masking expressions including NULL", {
178207
edf_a <- tibble::tibble(
179208
geo_value = 1,
@@ -837,7 +866,7 @@ test_that("epi_slide gets correct ref_time_value when groups have non-overlappin
837866

838867
time_types <- c("days", "weeks", "yearmonths", "integers")
839868
for (time_type in time_types) {
840-
test_that("epi_slide and epi_slide_mean: different before/after match for {time_type}", {
869+
test_that(format_inline("epi_slide and epi_slide_mean: different before/after match for {time_type}"), {
841870
set.seed(0)
842871
n <- 16
843872
epi_data_no_missing <- rbind(

0 commit comments

Comments
 (0)