Skip to content

Commit

Permalink
expect_error() -> expect_snapshot(error = TRUE)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Sep 18, 2024
1 parent a2ee89a commit 6c90981
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 17 deletions.
24 changes: 24 additions & 0 deletions tests/testthat/_snaps/classdist.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@

# case weights

Code
recipes:::get_center(mtcars, wts = wts, mfun = median)
Condition
Error in `recipes:::get_center()`:
! The centering function requested cannot be used with case weights.

---

Code
recipes:::get_both(mtcars, wts = wts, mfun = median)
Condition
Error in `recipes:::get_both()`:
! The centering function requested cannot be used with case weights.

---

Code
recipes:::get_both(mtcars, wts = wts, cfun = mad)
Condition
Error in `recipes:::get_both()`:
! The variance function requested cannot be used with case weights.

---

Code
rec_prep
Message
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/_snaps/colcheck.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@
x The following required columns are missing from `new_data`: `mpg`.
i These columns have one of the following roles, which are required at `bake()` time: `predictor`.

# non-standard roles during bake/predict

Code
predict(role_fit, Chicago %>% select(-date))
Condition
Error in `validate_column_names()`:
! The following required columns are missing: 'date'.

---

Code
predict(role_wts_fit, head(Chicago) %>% select(-date))
Condition
Error in `validate_column_names()`:
! The following required columns are missing: 'date'.

---

Code
predict(rm_fit, Chicago %>% select(-date))
Condition
Error in `validate_column_names()`:
! The following required columns are missing: 'date'.

---

Code
predict(rm_fit, Chicago %>% select(-date))
Condition
Error in `validate_column_names()`:
! The following required columns are missing: 'date'.

# empty printing

Code
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/mutate_at.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# no input

Code
iris_rec %>% step_mutate_at() %>% prep(training = iris) %>% bake(new_data = NULL,
composition = "data.frame")
Condition
Error in `step_mutate_at()`:
! argument "fn" is missing, with no default

# empty printing

Code
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/rename_at.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
Caused by error in `dplyr::rename_at()`:
! `.funs` must contain one renaming function, not 2.

# no input

Code
iris_rec %>% step_rename_at() %>% prep(training = iris) %>% bake(new_data = NULL,
composition = "data.frame")
Condition
Error in `step_rename_at()`:
! argument "fn" is missing, with no default

# empty printing

Code
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/update-role-requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
Warning:
Column `x` returned NaN, because variance cannot be calculated and scaling cannot be used. Consider avoiding `Inf` or `-Inf` values and/or setting `na_rm = TRUE` before normalizing.

---

Code
bake(rec, df)
Condition
Error in `step_scale()`:
! The following required column is missing from `new_data`: x.

# can update `bake` requirements after prepping

Code
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/window.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
Error in `step_window()`:
! `size` must be a number, not `NA`.

---

Code
rec %>% step_window(y1, size = NULL)
Condition
Error in `step_window()`:
! `size` must be a number, not `NULL`.

---

Code
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-classdist.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ test_that("case weights", {

expect_equal(means_wts, means_wts_exp)
expect_equal(means_no, means_exp)
expect_error(
recipes:::get_center(mtcars, wts = wts, mfun = median),
"The centering function requested cannot be used with case weights"
expect_snapshot(
error = TRUE,
recipes:::get_center(mtcars, wts = wts, mfun = median)
)

# ------------------------------------------------------------------------------
Expand All @@ -128,13 +128,13 @@ test_that("case weights", {
expect_equal(cov_no$scale, cov_exp)
expect_equal(cov_wts$center, means_wts_exp)
expect_equal(cov_no$center, means_exp)
expect_error(
recipes:::get_both(mtcars, wts = wts, mfun = median),
"The centering function requested cannot be used with case weights"
expect_snapshot(
error = TRUE,
recipes:::get_both(mtcars, wts = wts, mfun = median)
)
expect_error(
recipes:::get_both(mtcars, wts = wts, cfun = mad),
"The variance function requested cannot be used with case weights"
expect_snapshot(
error = TRUE,
recipes:::get_both(mtcars, wts = wts, cfun = mad)
)

# ------------------------------------------------------------------------------
Expand Down
20 changes: 16 additions & 4 deletions tests/testthat/test-colcheck.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ test_that("non-standard roles during bake/predict", {

# This should require 'date' to predict.
# The error comes from hardhat, so we don't snapshot it because we don't own it.
expect_error(predict(role_fit, Chicago %>% select(-date)))
expect_snapshot(
error = TRUE,
predict(role_fit, Chicago %>% select(-date))
)

# ----------------------------------------------------------------------------
# non-standard role used in a step and case weights
Expand All @@ -115,7 +118,10 @@ test_that("non-standard roles during bake/predict", {
# This should require 'date' but not 'wts' to predict
expect_no_error(predict(role_wts_fit, head(Chicago)))
expect_no_error(predict(role_wts_fit, head(Chicago) %>% select(-wts)))
expect_error(predict(role_wts_fit, head(Chicago) %>% select(-date)))
expect_snapshot(
error = TRUE,
predict(role_wts_fit, head(Chicago) %>% select(-date))
)

# ----------------------------------------------------------------------------
# Removing variable after use
Expand All @@ -131,7 +137,10 @@ test_that("non-standard roles during bake/predict", {
rm_fit <- fit(rm_wflow, data = Chicago)

# This should require 'date' to predict
expect_error(predict(rm_fit, Chicago %>% select(-date)))
expect_snapshot(
error = TRUE,
predict(rm_fit, Chicago %>% select(-date))
)

# ----------------------------------------------------------------------------
# Removing variable after use, with case weights
Expand All @@ -149,7 +158,10 @@ test_that("non-standard roles during bake/predict", {

# This should require 'date' but not 'wts' to predict
expect_no_error(predict(rm_fit, Chicago %>% select(-wts)))
expect_error(predict(rm_fit, Chicago %>% select(-date)))
expect_snapshot(
error = TRUE,
predict(rm_fit, Chicago %>% select(-date))
)
})

# Infrastructure ---------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-mutate_at.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ test_that("mulitple functions", {

test_that("no input", {
# Wait for call pass through
expect_error(
expect_snapshot(
error = TRUE,
iris_rec %>%
step_mutate_at() %>%
prep(training = iris) %>%
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-rename_at.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ test_that("mulitple functions", {

test_that("no input", {
# Wait for call pass through
expect_error(
expect_snapshot(
error = TRUE,
iris_rec %>%
step_rename_at() %>%
prep(training = iris) %>%
Expand Down
5 changes: 4 additions & 1 deletion tests/testthat/test-update-role-requirements.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ test_that("will still error if a step actually used a role that set `bake = FALS
df$x <- NULL

# Error is specific to details of `step_scale()`
expect_error(bake(rec, df))
expect_snapshot(
error = TRUE,
bake(rec, df)
)
})

test_that("can `bake()` without roles that set `bake = FALSE`", {
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-window.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ test_that("error checks", {
rec %>% step_window(y1, size = NA)
)
# Wait for call pass through
expect_error(
expect_snapshot(
error = TRUE,
rec %>% step_window(y1, size = NULL)
)
expect_snapshot(error = TRUE,
Expand Down

0 comments on commit 6c90981

Please sign in to comment.