Skip to content

Commit

Permalink
expect_error(..., NA) -> expect_no_error()
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Sep 18, 2024
1 parent e809513 commit a2ee89a
Show file tree
Hide file tree
Showing 41 changed files with 122 additions and 159 deletions.
2 changes: 1 addition & 1 deletion tests/testthat/test-YeoJohnson.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ test_that("missing data", {
rec_true <- recipe(~., data = ex_dat) %>%
step_YeoJohnson(x1, x2, x3, x4)

expect_error(prep(rec_true, training = ex_dat, verbose = FALSE), NA)
expect_no_error(prep(rec_true, training = ex_dat, verbose = FALSE))

rec_false <- recipe(~., data = ex_dat) %>%
step_YeoJohnson(x1, x2, x3, x4, na_rm = FALSE)
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-bs.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = mtcars),
NA
expect_no_error(
bake(rec, new_data = mtcars)
)
})

Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class(x3) <- c(class(x3), "Julian")
x_newdata_2 <- tibble(x1 = x1, x2 = x3)

test_that("bake_check_class helper function gives expected output", {
expect_error(bake_check_class_core(x1, "numeric", "x1"), NA)
expect_error(bake_check_class_core(x2, c("POSIXct", "POSIXt"), "x1"), NA)
expect_no_error(bake_check_class_core(x1, "numeric", "x1"))
expect_no_error(bake_check_class_core(x2, c("POSIXct", "POSIXt"), "x1"))
expect_snapshot(error = TRUE,
bake_check_class_core(x1, "character", "x1")
)
expect_snapshot(error = TRUE,
bake_check_class_core(x2, c("POSIXct", "Julian"), "x2")
)
expect_error(bake_check_class_core(x2, "POSIXct", "x2", TRUE), NA)
expect_no_error(bake_check_class_core(x2, "POSIXct", "x2", TRUE))
expect_snapshot(error = TRUE,
bake_check_class_core(x2, "POSIXct", "x2")
)
Expand All @@ -33,7 +33,7 @@ test_that("check_class works when class is learned", {
check_class(all_predictors()) %>%
prep()

expect_error(bake(rec1, x), NA)
expect_no_error(bake(rec1, x))
expect_equal(bake(rec1, x), x)
expect_snapshot(error = TRUE,
bake(rec1, x_newdata)
Expand All @@ -48,7 +48,7 @@ test_that("check_class works when class is provided", {
check_class(x1, class_nm = "numeric") %>%
prep()

expect_error(bake(rec2, x), NA)
expect_no_error(bake(rec2, x))
expect_equal(bake(rec2, x), x)
expect_snapshot(error = TRUE,
bake(rec2, x_newdata)
Expand All @@ -58,7 +58,7 @@ test_that("check_class works when class is provided", {
check_class(x2, class_nm = c("POSIXct", "POSIXt")) %>%
prep()

expect_error(bake(rec3, x), NA)
expect_no_error(bake(rec3, x))
expect_equal(bake(rec3, x), x)
expect_snapshot(error = TRUE,
bake(rec3, x_newdata_2)
Expand All @@ -70,7 +70,7 @@ test_that("check_class works when class is provided", {
allow_additional = TRUE
) %>%
prep()
expect_error(bake(rec4, x_newdata_2), NA)
expect_no_error(bake(rec4, x_newdata_2))
})

# recipes has internal coercion to character >> factor
Expand All @@ -79,13 +79,13 @@ test_that("characters are handled correctly", {
check_class(all_predictors()) %>%
prep(Sacramento[1:10, ], strings_as_factors = FALSE)

expect_error(bake(rec5_NULL, Sacramento[11:20, ]), NA)
expect_no_error(bake(rec5_NULL, Sacramento[11:20, ]))

rec5_man <- recipe(Sacramento[1:10, ], sqft ~ .) %>%
check_class(city, zip) %>%
prep(Sacramento[1:10, ], strings_as_factors = FALSE)

expect_error(bake(rec5_man, Sacramento[11:20, ]), NA)
expect_no_error(bake(rec5_man, Sacramento[11:20, ]))

sacr_fac <-
dplyr::mutate(
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-classdist.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = iris),
NA
expect_no_error(
bake(rec, new_data = iris)
)
})

Expand Down
35 changes: 20 additions & 15 deletions tests/testthat/test-colcheck.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,37 @@ rp1 <- recipe(mtcars, cyl ~ .)
rp2 <- recipe(mtcars, cyl ~ mpg + drat)

test_that("check_col works in the prep stage", {
expect_error(rp1 %>% check_cols(all_predictors()) %>% prep(), NA)
expect_error(rp2 %>% check_cols(all_predictors()) %>% prep(), NA)
expect_error(rp2 %>% check_cols(cyl, mpg, drat) %>% prep(), NA)
expect_error(rp2 %>% check_cols(cyl, mpg) %>% prep(), NA)
expect_no_error(rp1 %>% check_cols(all_predictors()) %>% prep())
expect_no_error(rp2 %>% check_cols(all_predictors()) %>% prep())
expect_no_error(rp2 %>% check_cols(cyl, mpg, drat) %>% prep())
expect_no_error(rp2 %>% check_cols(cyl, mpg) %>% prep())
})


test_that("check_col works in the bake stage", {

expect_error(rp1 %>% check_cols(all_predictors()) %>% prep() %>% bake(mtcars),
NA)
expect_no_error(
rp1 %>% check_cols(all_predictors()) %>% prep() %>% bake(mtcars)
)
expect_equal(rp1 %>% check_cols(all_predictors()) %>% prep() %>% bake(mtcars),
tibble(mtcars[ ,c(1, 3:11, 2)]))
expect_error(rp2 %>% check_cols(cyl, mpg, drat) %>% prep %>% bake(mtcars), NA)

expect_no_error(
rp2 %>% check_cols(cyl, mpg, drat) %>% prep %>% bake(mtcars)
)
expect_equal(rp2 %>% check_cols(cyl, mpg, drat) %>% prep %>% bake(mtcars),
tibble(mtcars[ ,c(1, 5, 2)]))

expect_error(
rp1 %>% check_cols(all_predictors()) %>% prep() %>% bake(mtcars),
NA
expect_no_error(
rp1 %>% check_cols(all_predictors()) %>% prep() %>% bake(mtcars)
)
expect_equal(
rp1 %>% check_cols(all_predictors()) %>% prep() %>% bake(mtcars),
tibble(mtcars[, c(1, 3:11, 2)])
)
expect_error(rp2 %>% check_cols(cyl, mpg, drat) %>% prep() %>% bake(mtcars), NA)
expect_no_error(
rp2 %>% check_cols(cyl, mpg, drat) %>% prep() %>% bake(mtcars)
)
expect_equal(
rp2 %>% check_cols(cyl, mpg, drat) %>% prep() %>% bake(mtcars),
tibble(mtcars[, c(1, 5, 2)])
Expand Down Expand Up @@ -86,7 +91,7 @@ test_that("non-standard roles during bake/predict", {

role_fit <- fit(role_wflow, data = Chicago)

expect_error(predict(role_fit, head(Chicago)), NA)
expect_no_error(predict(role_fit, head(Chicago)))

# This should require 'date' to predict.
# The error comes from hardhat, so we don't snapshot it because we don't own it.
Expand All @@ -108,8 +113,8 @@ test_that("non-standard roles during bake/predict", {
role_wts_fit <- fit(role_wts_wflow, data = Chicago)

# This should require 'date' but not 'wts' to predict
expect_error(predict(role_wts_fit, head(Chicago)), NA)
expect_error(predict(role_wts_fit, head(Chicago) %>% select(-wts)), NA)
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)))

# ----------------------------------------------------------------------------
Expand Down Expand Up @@ -143,7 +148,7 @@ test_that("non-standard roles during bake/predict", {
rm_wts_fit <- fit(rm_wts_wflow, data = Chicago)

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

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-count.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = covers),
NA
expect_no_error(
bake(rec, new_data = covers)
)
})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-cut.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("step_cut throws error on non-numerics", {
x <- tibble(num_var = 1:3, cat_var = c("1", "2", "3"))
expect_error(recipe(x) %>% step_cut(num_var, breaks = 2) %>% prep(), NA)
expect_no_error(recipe(x) %>% step_cut(num_var, breaks = 2) %>% prep())
expect_snapshot(error = TRUE,
recipe(x) %>% step_cut(cat_var, breaks = 2) %>% prep()
)
Expand Down
10 changes: 4 additions & 6 deletions tests/testthat/test-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,8 @@ test_that("can bake and recipes with no locale", {

date_rec$steps[[1]]$locale <- NULL

expect_error(
date_res <- bake(date_rec, new_data = examples, all_predictors()),
NA
expect_no_error(
date_res <- bake(date_rec, new_data = examples, all_predictors())
)
})

Expand Down Expand Up @@ -273,9 +272,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = examples),
NA
expect_no_error(
bake(rec, new_data = examples)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-depth.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = iris),
NA
expect_no_error(
bake(rec, new_data = iris)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-dummy.R
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = iris),
NA
expect_no_error(
bake(rec, new_data = iris)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-dummy_extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = color_examples),
NA
expect_no_error(
bake(rec, new_data = color_examples)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-dummy_multi_choice.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = languages),
NA
expect_no_error(
bake(rec, new_data = languages)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ test_that("quasiquotation", {

prepped_2 <- prep(rec_2, training = iris %>% slice(1:75))

expect_error(
prepped_2 <- prep(rec_2, training = iris %>% slice(1:75)),
regexp = NA
expect_no_error(
prepped_2 <- prep(rec_2, training = iris %>% slice(1:75))
)
rec_2_train <- bake(prepped_2, new_data = NULL)
expect_equal(dplyr_train, rec_2_train)
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-geodist.R
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = rand_data),
NA
expect_no_error(
bake(rec, new_data = rand_data)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-harmonic.R
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = mtcars),
NA
expect_no_error(
bake(rec, new_data = mtcars)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-holiday.R
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = test_data),
NA
expect_no_error(
bake(rec, new_data = test_data)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-ica.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = mtcars),
NA
expect_no_error(
bake(rec, new_data = mtcars)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-indicate_na.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = mtcars),
NA
expect_no_error(
bake(rec, new_data = mtcars)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-interact.R
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = dat_tr),
NA
expect_no_error(
bake(rec, new_data = dat_tr)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-isomap.R
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
transform = scrub_timestamp
)

expect_error(
bake(rec, new_data = dat1),
NA
expect_no_error(
bake(rec, new_data = dat1)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-kpca.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = mtcars),
NA
expect_no_error(
bake(rec, new_data = mtcars)
)
})

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-kpca_poly.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

expect_error(
bake(rec, new_data = tr_dat),
NA
expect_no_error(
bake(rec, new_data = tr_dat)
)
})

Expand Down
Loading

0 comments on commit a2ee89a

Please sign in to comment.