Skip to content

Commit de586be

Browse files
committed
tests: more snapshot tests
1 parent 434262a commit de586be

27 files changed

+135
-141
lines changed

NAMESPACE

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,18 @@ import(distributional)
214214
import(epiprocess)
215215
import(parsnip)
216216
import(recipes)
217+
importFrom(checkmate,assert)
218+
importFrom(checkmate,assert_character)
217219
importFrom(checkmate,assert_class)
220+
importFrom(checkmate,assert_date)
221+
importFrom(checkmate,assert_function)
222+
importFrom(checkmate,assert_int)
223+
importFrom(checkmate,assert_integer)
224+
importFrom(checkmate,assert_integerish)
225+
importFrom(checkmate,assert_logical)
226+
importFrom(checkmate,assert_number)
218227
importFrom(checkmate,assert_numeric)
219-
importFrom(checkmate,test_character)
220-
importFrom(checkmate,test_date)
221-
importFrom(checkmate,test_function)
222-
importFrom(checkmate,test_integerish)
223-
importFrom(checkmate,test_logical)
224-
importFrom(checkmate,test_numeric)
225-
importFrom(checkmate,test_scalar)
228+
importFrom(checkmate,assert_scalar)
226229
importFrom(cli,cli_abort)
227230
importFrom(cli,cli_warn)
228231
importFrom(dplyr,across)
@@ -273,24 +276,12 @@ importFrom(rlang,caller_arg)
273276
importFrom(rlang,caller_env)
274277
importFrom(rlang,enquo)
275278
importFrom(rlang,enquos)
276-
importFrom(rlang,env_get_list)
277279
importFrom(rlang,expr)
278280
importFrom(rlang,global_env)
279281
importFrom(rlang,inject)
280-
importFrom(rlang,is_call)
281-
importFrom(rlang,is_character)
282-
importFrom(rlang,is_closure)
283-
importFrom(rlang,is_environment)
284-
importFrom(rlang,is_formula)
285-
importFrom(rlang,is_function)
286-
importFrom(rlang,is_list)
287282
importFrom(rlang,is_logical)
288-
importFrom(rlang,is_missing)
289283
importFrom(rlang,is_null)
290-
importFrom(rlang,is_string)
291-
importFrom(rlang,is_symbol)
292284
importFrom(rlang,is_true)
293-
importFrom(rlang,is_vector)
294285
importFrom(rlang,set_names)
295286
importFrom(rlang,sym)
296287
importFrom(stats,as.formula)

man/autoplot-epipred.Rd

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

tests/testthat/test-arx_args_list.R

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
test_that("arx_args checks inputs", {
22
expect_s3_class(arx_args_list(), c("arx_fcast", "alist"))
3-
expect_error(arx_args_list(ahead = c(0, 4)))
4-
expect_error(arx_args_list(n_training = c(28, 65)))
3+
expect_snapshot(error = TRUE, arx_args_list(ahead = c(0, 4)))
4+
expect_snapshot(error = TRUE, arx_args_list(n_training = c(28, 65)))
55

6-
expect_error(arx_args_list(ahead = -1))
7-
expect_error(arx_args_list(ahead = 1.5))
8-
expect_error(arx_args_list(n_training = -1))
9-
expect_error(arx_args_list(n_training = 1.5))
10-
expect_error(arx_args_list(lags = c(-1, 0)))
11-
expect_error(arx_args_list(lags = list(c(1:5, 6.5), 2:8)))
6+
expect_snapshot(error = TRUE, arx_args_list(ahead = -1))
7+
expect_snapshot(error = TRUE, arx_args_list(ahead = 1.5))
8+
expect_snapshot(error = TRUE, arx_args_list(n_training = -1))
9+
expect_snapshot(error = TRUE, arx_args_list(n_training = 1.5))
10+
expect_snapshot(error = TRUE, arx_args_list(lags = c(-1, 0)))
11+
expect_snapshot(error = TRUE, arx_args_list(lags = list(c(1:5, 6.5), 2:8)))
1212

13-
expect_error(arx_args_list(symmetrize = 4))
14-
expect_error(arx_args_list(nonneg = 4))
13+
expect_snapshot(error = TRUE, arx_args_list(symmetrize = 4))
14+
expect_snapshot(error = TRUE, arx_args_list(nonneg = 4))
1515

16-
expect_error(arx_args_list(quantile_levels = -.1))
17-
expect_error(arx_args_list(quantile_levels = 1.1))
16+
expect_snapshot(error = TRUE, arx_args_list(quantile_levels = -.1))
17+
expect_snapshot(error = TRUE, arx_args_list(quantile_levels = 1.1))
1818
expect_type(arx_args_list(quantile_levels = NULL), "list")
1919

20-
expect_error(arx_args_list(target_date = "2022-01-01"))
20+
expect_snapshot(error = TRUE, arx_args_list(target_date = "2022-01-01"))
2121
expect_identical(
2222
arx_args_list(target_date = as.Date("2022-01-01"))$target_date,
2323
as.Date("2022-01-01")
2424
)
2525

26-
expect_error(arx_args_list(n_training_min = "de"))
27-
expect_error(arx_args_list(epi_keys = 1))
26+
expect_snapshot(error = TRUE, arx_args_list(n_training_min = "de"))
27+
expect_snapshot(error = TRUE, arx_args_list(epi_keys = 1))
2828

2929
expect_warning(arx_args_list(
3030
forecast_date = as.Date("2022-01-01"),
@@ -58,7 +58,7 @@ test_that("arx forecaster disambiguates quantiles", {
5858
sort(unique(tlist))
5959
)
6060
alist <- c(.1, .3, .5, .7, .9) # neither default, and different,
61-
expect_error(compare_quantile_args(alist, tlist))
61+
expect_snapshot(error = TRUE, compare_quantile_args(alist, tlist))
6262
})
6363

6464
test_that("arx_lags_validator handles named & unnamed lists as expected", {
@@ -94,7 +94,7 @@ test_that("arx_lags_validator handles named & unnamed lists as expected", {
9494
)
9595

9696
# More lags than predictors - Error
97-
expect_error(arx_lags_validator(pred_vec, lags_finit_fn_switch2))
97+
expect_snapshot(error = TRUE, arx_lags_validator(pred_vec, lags_finit_fn_switch2))
9898

9999
# Unnamed list of lags
100100
lags_init_un <- list(c(0, 7, 14), c(0, 1, 2, 3, 7, 14))
@@ -115,5 +115,5 @@ test_that("arx_lags_validator handles named & unnamed lists as expected", {
115115
# Try use a name not in predictors - Error
116116
lags_init_other_name <- list(death_rate = c(0, 7, 14), test_var = c(0, 1, 2, 3, 7, 14))
117117

118-
expect_error(arx_lags_validator(pred_vec, lags_init_other_name))
118+
expect_snapshot(error = TRUE, arx_lags_validator(pred_vec, lags_init_other_name))
119119
})

tests/testthat/test-arx_cargs_list.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
test_that("arx_class_args checks inputs", {
22
expect_s3_class(arx_class_args_list(), c("arx_class", "alist"))
3-
expect_error(arx_class_args_list(ahead = c(0, 4)))
4-
expect_error(arx_class_args_list(n_training = c(28, 65)))
3+
expect_snapshot(error = TRUE, arx_class_args_list(ahead = c(0, 4)))
4+
expect_snapshot(error = TRUE, arx_class_args_list(n_training = c(28, 65)))
55

6-
expect_error(arx_class_args_list(ahead = -1))
7-
expect_error(arx_class_args_list(ahead = 1.5))
8-
expect_error(arx_class_args_list(n_training = -1))
9-
expect_error(arx_class_args_list(n_training = 1.5))
10-
expect_error(arx_class_args_list(lags = c(-1, 0)))
11-
expect_error(arx_class_args_list(lags = list(c(1:5, 6.5), 2:8)))
6+
expect_snapshot(error = TRUE, arx_class_args_list(ahead = -1))
7+
expect_snapshot(error = TRUE, arx_class_args_list(ahead = 1.5))
8+
expect_snapshot(error = TRUE, arx_class_args_list(n_training = -1))
9+
expect_snapshot(error = TRUE, arx_class_args_list(n_training = 1.5))
10+
expect_snapshot(error = TRUE, arx_class_args_list(lags = c(-1, 0)))
11+
expect_snapshot(error = TRUE, arx_class_args_list(lags = list(c(1:5, 6.5), 2:8)))
1212

1313

14-
expect_error(arx_class_args_list(target_date = "2022-01-01"))
14+
expect_snapshot(error = TRUE, arx_class_args_list(target_date = "2022-01-01"))
1515
expect_identical(
1616
arx_class_args_list(target_date = as.Date("2022-01-01"))$target_date,
1717
as.Date("2022-01-01")
1818
)
1919

20-
expect_error(arx_class_args_list(n_training_min = "de"))
21-
expect_error(arx_class_args_list(epi_keys = 1))
20+
expect_snapshot(error = TRUE, arx_class_args_list(n_training_min = "de"))
21+
expect_snapshot(error = TRUE, arx_class_args_list(epi_keys = 1))
2222

2323
expect_warning(arx_class_args_list(
2424
forecast_date = as.Date("2022-01-01"),

tests/testthat/test-bake-method.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ test_that("bake method works in all cases", {
2525
expect_s3_class(bake(prep(r, edf), NULL, composition = "tibble"), "tbl_df")
2626
expect_s3_class(bake(prep(r, edf), NULL, composition = "data.frame"), "data.frame")
2727
# can't be a matrix because time_value/geo_value aren't numeric
28-
expect_error(bake(prep(r, edf), NULL, composition = "matrix"))
28+
expect_snapshot(error = TRUE, bake(prep(r, edf), NULL, composition = "matrix"))
2929
})

tests/testthat/test-check-training-set.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test_that("training set validation works", {
77
expect_silent(validate_meta_match(template, template, "time_type", "blah"))
88
attr(t1, "metadata")$geo_type <- "county"
99
expect_warning(validate_meta_match(t1, template, "geo_type"), "county")
10-
expect_error(validate_meta_match(t1, template, "geo_type", "abort"), "county")
10+
expect_snapshot(error = TRUE, validate_meta_match(t1, template, "geo_type", "abort"), "county")
1111

1212

1313
expect_identical(template, epi_check_training_set(template, rec))
@@ -25,5 +25,5 @@ test_that("training set validation works", {
2525
expect_warning(t4 <- epi_check_training_set(t3, rec))
2626
expect_identical(rec$template, t4)
2727
attr(rec$template, "metadata")$other_keys <- "missing_col"
28-
expect_error(epi_check_training_set(t4, rec), "missing_col")
28+
expect_snapshot(error = TRUE, epi_check_training_set(t4, rec), "missing_col")
2929
})

tests/testthat/test-check_enough_train_data.R

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ test_that("check_enough_train_data works on pooled data", {
2323
bake(new_data = NULL)
2424
)
2525
# Check both column don't have enough data
26-
expect_error(
26+
expect_snapshot(
27+
error = TRUE,
2728
epi_recipe(toy_epi_df) %>%
2829
check_enough_train_data(x, y, n = 2 * n + 1, drop_na = FALSE) %>%
2930
prep(toy_epi_df) %>%
30-
bake(new_data = NULL),
31-
regexp = "The following columns don't have enough data"
31+
bake(new_data = NULL)
3232
)
3333
# Check drop_na works
34-
expect_error(
34+
expect_snapshot(
35+
error = TRUE,
3536
epi_recipe(toy_epi_df) %>%
3637
check_enough_train_data(x, y, n = 2 * n - 1, drop_na = TRUE) %>%
3738
prep(toy_epi_df) %>%
@@ -48,15 +49,16 @@ test_that("check_enough_train_data works on unpooled data", {
4849
bake(new_data = NULL)
4950
)
5051
# Check one column don't have enough data
51-
expect_error(
52+
expect_snapshot(
53+
error = TRUE,
5254
epi_recipe(toy_epi_df) %>%
5355
check_enough_train_data(x, y, n = n + 1, epi_keys = "geo_value", drop_na = FALSE) %>%
5456
prep(toy_epi_df) %>%
55-
bake(new_data = NULL),
56-
regexp = "The following columns don't have enough data"
57+
bake(new_data = NULL)
5758
)
5859
# Check drop_na works
59-
expect_error(
60+
expect_snapshot(
61+
error = TRUE,
6062
epi_recipe(toy_epi_df) %>%
6163
check_enough_train_data(x, y, n = 2 * n - 3, epi_keys = "geo_value", drop_na = TRUE) %>%
6264
prep(toy_epi_df) %>%
@@ -114,7 +116,8 @@ test_that("check_enough_train_data works with all_predictors() downstream of con
114116
prep(toy_epi_df) %>%
115117
bake(new_data = NULL)
116118
)
117-
expect_error(
119+
expect_snapshot(
120+
error = TRUE,
118121
epi_recipe(toy_epi_df) %>%
119122
step_epi_lag(x, lag = c(1, 2)) %>%
120123
check_enough_train_data(all_predictors(), y, n = 2 * n - 5) %>%

tests/testthat/test-dist_quantiles.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
library(distributional)
22

33
test_that("constructor returns reasonable quantiles", {
4-
expect_error(new_quantiles(rnorm(5), rnorm(5)))
4+
expect_snapshot(error = TRUE, new_quantiles(rnorm(5), rnorm(5)))
55
expect_silent(new_quantiles(sort(rnorm(5)), sort(runif(5))))
6-
expect_error(new_quantiles(sort(rnorm(5)), sort(runif(2))))
6+
expect_snapshot(error = TRUE, new_quantiles(sort(rnorm(5)), sort(runif(2))))
77
expect_silent(new_quantiles(1:5, 1:5 / 10))
8-
expect_error(new_quantiles(c(2, 1, 3, 4, 5), c(.1, .1, .2, .5, .8)))
9-
expect_error(new_quantiles(c(2, 1, 3, 4, 5), c(.1, .15, .2, .5, .8)))
10-
expect_error(new_quantiles(c(1, 2, 3), c(.1, .2, 3)))
8+
expect_snapshot(error = TRUE, new_quantiles(c(2, 1, 3, 4, 5), c(.1, .1, .2, .5, .8)))
9+
expect_snapshot(error = TRUE, new_quantiles(c(2, 1, 3, 4, 5), c(.1, .15, .2, .5, .8)))
10+
expect_snapshot(error = TRUE, new_quantiles(c(1, 2, 3), c(.1, .2, 3)))
1111
})
1212

1313

@@ -106,6 +106,6 @@ test_that("arithmetic works on quantiles", {
106106
expect_identical(dstn / 4, dstn2)
107107
expect_identical((1 / 4) * dstn, dstn2)
108108

109-
expect_error(sum(dstn))
110-
expect_error(suppressWarnings(dstn + distributional::dist_normal()))
109+
expect_snapshot(error = TRUE, sum(dstn))
110+
expect_snapshot(error = TRUE, suppressWarnings(dstn + distributional::dist_normal()))
111111
})

tests/testthat/test-enframer.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
test_that("enframer errors/works as needed", {
22
template1 <- data.frame(aa = 1:5, a = NA, b = NA, c = NA)
33
template2 <- data.frame(aa = 1:5, a = 2:6, b = 2:6, c = 2:6)
4-
expect_error(enframer(1:5, letters[1]))
5-
expect_error(enframer(data.frame(a = 1:5), 1:3))
6-
expect_error(enframer(data.frame(a = 1:5), letters[1:3]))
4+
expect_snapshot(error = TRUE, enframer(1:5, letters[1]))
5+
expect_snapshot(error = TRUE, enframer(data.frame(a = 1:5), 1:3))
6+
expect_snapshot(error = TRUE, enframer(data.frame(a = 1:5), letters[1:3]))
77
expect_identical(enframer(data.frame(aa = 1:5), letters[1:3]), template1)
8-
expect_error(enframer(data.frame(aa = 1:5), letters[1:2], fill = 1:4))
8+
expect_snapshot(error = TRUE, enframer(data.frame(aa = 1:5), letters[1:2], fill = 1:4))
99
expect_identical(
1010
enframer(data.frame(aa = 1:5), letters[1:3], fill = 2:6),
1111
template2

tests/testthat/test-epi_recipe.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,6 @@ test_that("add/update/adjust/remove epi_recipe works as intended", {
155155

156156

157157
wf <- remove_epi_recipe(wf)
158-
expect_error(workflows::extract_preprocessor(wf)$steps)
158+
expect_snapshot(error = TRUE, workflows::extract_preprocessor(wf)$steps)
159159
expect_equal(wf$pre$actions$recipe$recipe, NULL)
160160
})

tests/testthat/test-epi_workflow.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ test_that("model can be added/updated/removed from epi_workflow", {
5959
expect_equal(class(model_spec2), c("linear_reg", "model_spec"))
6060

6161
wf <- remove_model(wf)
62-
expect_error(extract_spec_parsnip(wf))
62+
expect_snapshot(error = TRUE, extract_spec_parsnip(wf))
6363
expect_equal(wf$fit$actions$model$spec, NULL)
6464
})
6565

@@ -103,7 +103,7 @@ test_that("forecast method errors when workflow not fit", {
103103
step_epi_naomit()
104104
wf <- epi_workflow(r, parsnip::linear_reg())
105105

106-
expect_error(forecast(wf))
106+
expect_snapshot(error = TRUE, forecast(wf))
107107
})
108108

109109
test_that("fit method does not silently drop the class", {

tests/testthat/test-extract_argument.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ test_that("layer argument extractor works", {
44
layer_residual_quantiles(quantile_levels = c(0.0275, 0.975), symmetrize = FALSE) %>%
55
layer_naomit(.pred)
66

7-
expect_error(extract_argument(f$layers[[1]], "uhoh", "bubble"))
8-
expect_error(extract_argument(f$layers[[1]], "layer_predict", "bubble"))
7+
expect_snapshot(error = TRUE, extract_argument(f$layers[[1]], "uhoh", "bubble"))
8+
expect_snapshot(error = TRUE, extract_argument(f$layers[[1]], "layer_predict", "bubble"))
99
expect_identical(
1010
extract_argument(f$layers[[2]], "layer_residual_quantiles", "quantile_levels"),
1111
c(0.0275, 0.9750)
1212
)
1313

14-
expect_error(extract_argument(f, "layer_thresh", "quantile_levels"))
14+
expect_snapshot(error = TRUE, extract_argument(f, "layer_thresh", "quantile_levels"))
1515
expect_identical(
1616
extract_argument(f, "layer_residual_quantiles", "quantile_levels"),
1717
c(0.0275, 0.9750)
1818
)
1919

2020
wf <- epi_workflow(postprocessor = f)
21-
expect_error(extract_argument(epi_workflow(), "layer_residual_quantiles", "quantile_levels"))
21+
expect_snapshot(error = TRUE, extract_argument(epi_workflow(), "layer_residual_quantiles", "quantile_levels"))
2222
expect_identical(
2323
extract_argument(wf, "layer_residual_quantiles", "quantile_levels"),
2424
c(0.0275, 0.9750)
2525
)
2626

27-
expect_error(extract_argument(wf, "layer_predict", c("type", "opts")))
27+
expect_snapshot(error = TRUE, extract_argument(wf, "layer_predict", c("type", "opts")))
2828
})
2929

3030
test_that("recipe argument extractor works", {
@@ -41,19 +41,19 @@ test_that("recipe argument extractor works", {
4141
step_naomit(all_outcomes(), skip = TRUE)
4242

4343

44-
expect_error(extract_argument(r$steps[[1]], "uhoh", "bubble"))
45-
expect_error(extract_argument(r$steps[[1]], "step_epi_lag", "bubble"))
44+
expect_snapshot(error = TRUE, extract_argument(r$steps[[1]], "uhoh", "bubble"))
45+
expect_snapshot(error = TRUE, extract_argument(r$steps[[1]], "step_epi_lag", "bubble"))
4646
expect_identical(extract_argument(r$steps[[2]], "step_epi_ahead", "ahead"), 7L)
4747

4848

49-
expect_error(extract_argument(r, "step_lightly", "quantile_levels"))
49+
expect_snapshot(error = TRUE, extract_argument(r, "step_lightly", "quantile_levels"))
5050
expect_identical(
5151
extract_argument(r, "step_epi_lag", "lag"),
5252
list(c(0L, 7L, 14L), c(0L, 7L, 14L))
5353
)
5454

5555
wf <- epi_workflow(preprocessor = r)
56-
expect_error(extract_argument(epi_workflow(), "step_epi_lag", "lag"))
56+
expect_snapshot(error = TRUE, extract_argument(epi_workflow(), "step_epi_lag", "lag"))
5757
expect_identical(
5858
extract_argument(wf, "step_epi_lag", "lag"),
5959
list(c(0L, 7L, 14L), c(0L, 7L, 14L))

0 commit comments

Comments
 (0)