Skip to content

Commit

Permalink
expect_error -> expect_no_error (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
topepo authored Oct 23, 2024
1 parent 1184068 commit 2e10089
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 126 deletions.
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,22 @@
# check parameter finalization

Code
expect_error(p1 <- tune:::check_parameters(w1, data = mtcars, grid_names = character(
0)), regex = NA)
expect_no_error(p1 <- tune:::check_parameters(w1, data = mtcars, grid_names = character(
0)))
Message
i Creating pre-processing data to finalize unknown parameter: mtry

---

Code
expect_error(p2 <- tune:::check_parameters(w2, data = mtcars), regex = NA)
expect_no_error(p2 <- tune:::check_parameters(w2, data = mtcars))
Message
i Creating pre-processing data to finalize unknown parameter: mtry

---

Code
expect_error(p3_a <- tune:::check_parameters(w3, data = mtcars), regex = NA)
expect_no_error(p3_a <- tune:::check_parameters(w3, data = mtcars))
Message
i Creating pre-processing data to finalize unknown parameter: mtry

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-autoplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ test_that("coord_obs_pred", {

p2 <- p + coord_obs_pred()

expect_error(print(p2), regexp = NA)
expect_no_error(print(p2))

expect_true(inherits(p2$coordinates, "CoordObsPred"))
expect_equal(p2$coordinates$limits$x, rng)
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-bayes.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test_that("tune recipe only", {
expect_null(.get_tune_eval_time_target(res))

set.seed(1)
expect_error(
expect_no_error(
suppressMessages(
tune_bayes(
wflow,
Expand All @@ -67,8 +67,7 @@ test_that("tune recipe only", {
iter = iter2,
corr = list(type = "matern", nu = 3 / 2)
)
),
regexp = NA
)
)


Expand Down
88 changes: 40 additions & 48 deletions tests/testthat/test-checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_that("rsample objects", {
obj_loo <- rsample::loo_cv(mtcars)
obj_nst <- rsample::nested_cv(mtcars, obj_cv, inside = rsample::bootstraps())
obj_permut <- rsample::permutations(mtcars, hp)
expect_error(tune:::check_rset(obj_cv), regexp = NA)
expect_no_error(tune:::check_rset(obj_cv))
expect_snapshot(error = TRUE, tune:::check_rset(obj_loo))
expect_snapshot(error = TRUE, tune:::check_rset(obj_nst))
expect_snapshot(error = TRUE, tune:::check_rset(obj_permut))
Expand Down Expand Up @@ -69,7 +69,7 @@ test_that("grid objects", {
add_model(svm_mod) %>%
add_recipe(bare_rec)

expect_error(grid_2 <- tune:::check_grid(6, wflow_1), NA)
expect_no_error(grid_2 <- tune:::check_grid(6, wflow_1))
expect_equal(nrow(grid_2), 6)
expect_true(inherits(grid_2, "data.frame"))

Expand Down Expand Up @@ -226,17 +226,15 @@ test_that("workflow objects (will not tune, tidymodels/tune#548)", {
lr_glmnet_2 <- lr_lm_2 %>% parsnip::set_engine("glmnet")

# don't error when supplied tune args make sense given engine / steps
expect_error_na <- function(x) {testthat::expect_error(x, regexp = NA)}
expect_no_error(check_workflow(workflow(rec_bare, lr_lm_0)))
expect_no_error(check_workflow(workflow(rec_bare, lr_glmnet_0)))
expect_no_error(check_workflow(workflow(rec_bare, lr_glmnet_1)))
expect_no_error(check_workflow(workflow(rec_bare, lr_glmnet_2)))

expect_error_na(check_workflow(workflow(rec_bare, lr_lm_0)))
expect_error_na(check_workflow(workflow(rec_bare, lr_glmnet_0)))
expect_error_na(check_workflow(workflow(rec_bare, lr_glmnet_1)))
expect_error_na(check_workflow(workflow(rec_bare, lr_glmnet_2)))

expect_error_na(check_workflow(workflow(rec_tune, lr_lm_0)))
expect_error_na(check_workflow(workflow(rec_tune, lr_glmnet_0)))
expect_error_na(check_workflow(workflow(rec_tune, lr_glmnet_1)))
expect_error_na(check_workflow(workflow(rec_tune, lr_glmnet_2)))
expect_no_error(check_workflow(workflow(rec_tune, lr_lm_0)))
expect_no_error(check_workflow(workflow(rec_tune, lr_glmnet_0)))
expect_no_error(check_workflow(workflow(rec_tune, lr_glmnet_1)))
expect_no_error(check_workflow(workflow(rec_tune, lr_glmnet_2)))

# error when supplied tune args don't make sense given engine / steps
expect_error_nt <- function(x) {testthat::expect_error(x, class = "not_tunable_error")}
Expand Down Expand Up @@ -321,7 +319,7 @@ test_that("metrics must match the parsnip engine", {
# ------------------------------------------------------------------------------

test_that("grid control objects", {
expect_error(control_grid(), NA)
expect_no_error(control_grid())
expect_snapshot(error = TRUE, control_grid(tomato = 1))
expect_snapshot(error = TRUE, control_grid(verbose = 1))
expect_snapshot(error = TRUE, control_grid(verbose = rep(TRUE, 2)))
Expand All @@ -330,18 +328,18 @@ test_that("grid control objects", {
expect_snapshot(error = TRUE, control_grid(extract = Inf))
expect_snapshot(error = TRUE, control_grid(pkgs = Inf))

expect_error(control_grid(verbose = TRUE), NA)
expect_error(control_grid(allow_par = FALSE), NA)
expect_error(control_grid(save_pred = TRUE), NA)
expect_error(control_grid(extract = NULL), NA)
expect_error(control_grid(extract = I), NA)
expect_error(control_grid(pkgs = NULL), NA)
expect_error(control_grid(pkgs = letters), NA)
expect_no_error(control_grid(verbose = TRUE))
expect_no_error(control_grid(allow_par = FALSE))
expect_no_error(control_grid(save_pred = TRUE))
expect_no_error(control_grid(extract = NULL))
expect_no_error(control_grid(extract = I))
expect_no_error(control_grid(pkgs = NULL))
expect_no_error(control_grid(pkgs = letters))
expect_s3_class(control_grid(), c("control_grid", "control_resamples"))
})

test_that("Bayes control objects", {
expect_error(control_bayes(), NA)
expect_no_error(control_bayes())
expect_snapshot(error = TRUE, control_bayes(tomato = 1))
expect_snapshot(error = TRUE, control_bayes(verbose = 1))
expect_snapshot(error = TRUE, control_bayes(verbose = rep(TRUE, 2)))
Expand All @@ -357,15 +355,15 @@ test_that("Bayes control objects", {
tmp <- control_bayes(no_improve = 2, uncertain = 5)
)

expect_error(control_bayes(verbose = TRUE), NA)
expect_error(control_bayes(no_improve = 2), NA)
expect_error(control_bayes(uncertain = 2), NA)
expect_error(control_bayes(save_pred = TRUE), NA)
expect_error(control_bayes(extract = NULL), NA)
expect_error(control_bayes(extract = I), NA)
expect_error(control_bayes(pkgs = NULL), NA)
expect_error(control_bayes(pkgs = letters), NA)
expect_error(control_bayes(time_limit = 2), NA)
expect_no_error(control_bayes(verbose = TRUE))
expect_no_error(control_bayes(no_improve = 2))
expect_no_error(control_bayes(uncertain = 2))
expect_no_error(control_bayes(save_pred = TRUE))
expect_no_error(control_bayes(extract = NULL))
expect_no_error(control_bayes(extract = I))
expect_no_error(control_bayes(pkgs = NULL))
expect_no_error(control_bayes(pkgs = letters))
expect_no_error(control_bayes(time_limit = 2))
expect_s3_class(control_bayes(), "control_bayes")
})

Expand Down Expand Up @@ -448,16 +446,14 @@ test_that("check parameter finalization", {
add_model(rf1)

expect_snapshot(
expect_error(
p1 <- tune:::check_parameters(w1, data = mtcars, grid_names = character(0)),
regex = NA
expect_no_error(
p1 <- tune:::check_parameters(w1, data = mtcars, grid_names = character(0))
)
)
expect_false(any(dials::has_unknowns(p1$object)))

expect_error(
p1 <- tune:::check_parameters(w1, data = mtcars, grid_names = "mtry"),
regex = NA
expect_no_error(
p1 <- tune:::check_parameters(w1, data = mtcars, grid_names = "mtry")
)

w2 <-
Expand All @@ -466,9 +462,8 @@ test_that("check parameter finalization", {
add_model(rf1)

expect_snapshot(
expect_error(
p2 <- tune:::check_parameters(w2, data = mtcars),
regex = NA
expect_no_error(
p2 <- tune:::check_parameters(w2, data = mtcars)
)
)
expect_false(any(dials::has_unknowns(p2$object)))
Expand All @@ -480,9 +475,8 @@ test_that("check parameter finalization", {
p3 <- extract_parameter_set_dials(w3)

expect_snapshot(
expect_error(
p3_a <- tune:::check_parameters(w3, data = mtcars),
regex = NA
expect_no_error(
p3_a <- tune:::check_parameters(w3, data = mtcars)
)
)
expect_false(any(dials::has_unknowns(p3_a$object)))
Expand All @@ -500,9 +494,8 @@ test_that("check parameter finalization", {
extract_parameter_set_dials(w4) %>%
update(mtry = dials::mtry(c(1, 10)))

expect_error(
p4_b <- tune:::check_parameters(w4, p4_a, data = mtcars),
regex = NA
expect_no_error(
p4_b <- tune:::check_parameters(w4, p4_a, data = mtcars)
)
expect_true(inherits(p4_b, "parameters"))

Expand All @@ -511,9 +504,8 @@ test_that("check parameter finalization", {
add_recipe(rec_tune) %>%
add_model(lm1)

expect_error(
p5 <- tune:::check_parameters(w5, data = mtcars),
regex = NA
expect_no_error(
p5 <- tune:::check_parameters(w5, data = mtcars)
)
expect_true(inherits(p5, "parameters"))
})
16 changes: 10 additions & 6 deletions tests/testthat/test-conf-mat-resampled.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
test_that("appropriate return values", {
svm_results <- readRDS(test_path("data", "svm_results.rds"))

expect_error(
cm_1 <- conf_mat_resampled(svm_results, parameters = select_best(svm_results, metric = "accuracy")),
regex = NA
expect_no_error(
cm_1 <-
conf_mat_resampled(svm_results, parameters = select_best(svm_results, metric = "accuracy"))
)
expect_true(tibble::is_tibble(cm_1))

expect_error(
cm_2 <- conf_mat_resampled(svm_results, parameters = select_best(svm_results, metric = "accuracy"), tidy = FALSE),
regex = NA
expect_no_error(
cm_2 <-
conf_mat_resampled(
svm_results,
parameters = select_best(svm_results, metric = "accuracy"),
tidy = FALSE
)
)
expect_equal(class(cm_2), "conf_mat")

Expand Down
25 changes: 10 additions & 15 deletions tests/testthat/test-engine-parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,23 @@ test_that("tuning with engine parameters with dials objects", {
rs <- rsample::vfold_cv(mtcars)

set.seed(19828)
expect_error(
expect_no_error(
suppressMessages(
rf_tune <- rf_mod %>% tune_grid(mpg ~ ., resamples = rs, grid = 3)
),
regex = NA
)
)
expect_error(
p <- autoplot(rf_tune),
regex = NA
expect_no_error(
p <- autoplot(rf_tune)
)

set.seed(283)
expect_error(
expect_no_error(
suppressMessages(
rf_search <- rf_mod %>% tune_bayes(mpg ~ ., resamples = rs, initial = 3, iter = 2)
),
regex = NA
)
)
expect_error(
p <- autoplot(rf_search),
regex = NA
expect_no_error(
p <- autoplot(rf_search)
)
})

Expand Down Expand Up @@ -89,11 +85,10 @@ test_that("tuning with engine parameters without dials objects", {

## ---------------------------------------------------------------------------

expect_error(
expect_no_error(
suppressMessages(
rf_tune <- rf_mod %>% tune_grid(mpg ~ ., resamples = rs, grid = grid)
),
regex = NA
)
)
expect_snapshot(error = TRUE, {
p <- autoplot(rf_tune)
Expand Down
31 changes: 13 additions & 18 deletions tests/testthat/test-extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ test_that("tune recipe only", {
extract_recipe(x) %>% tidy(number = 2)
}
before_kind <- RNGkind()[[1]]
expect_error(
expect_no_error(
res_1_1 <-
workflow() %>%
add_recipe(helper_objects$rec_tune_1) %>%
add_model(helper_objects$lm_mod) %>%
tune_grid(resamples = mt_folds, control = control_grid(extract = extr_1_1)),
NA
tune_grid(resamples = mt_folds, control = control_grid(extract = extr_1_1))
)
after_kind <- RNGkind()[[1]]
expect_equal(before_kind, after_kind)
expect_error(extract_1_1 <- dplyr::bind_rows(res_1_1$.extracts), NA)
expect_no_error(extract_1_1 <- dplyr::bind_rows(res_1_1$.extracts))

expect_true(all(names(extract_1_1) == c("num_comp", ".extracts", ".config")))
expect_true(
Expand All @@ -42,7 +41,7 @@ test_that("tune model only", {
tibble(index = mod@alphaindex[[1]], estimate = mod@coef[[1]])
}

expect_error(
expect_no_error(
res_2_1 <-
workflow() %>%
add_recipe(helper_objects$rec_no_tune_1) %>%
Expand All @@ -51,10 +50,9 @@ test_that("tune model only", {
resamples = mt_folds,
grid = 2,
control = control_grid(extract = extr_2_1)
),
NA
)
)
expect_error(extract_2_1 <- dplyr::bind_rows(res_2_1$.extracts), NA)
expect_no_error(extract_2_1 <- dplyr::bind_rows(res_2_1$.extracts))

expect_true(all(names(extract_2_1) == c("cost", ".extracts", ".config")))
expect_true(
Expand All @@ -69,7 +67,7 @@ test_that("tune model only", {
}

# should not fail:
expect_error(
expect_no_error(
res_2_2 <-
workflow() %>%
add_recipe(helper_objects$rec_tune_1) %>%
Expand All @@ -78,15 +76,13 @@ test_that("tune model only", {
resamples = mt_folds,
grid = 2,
control = control_grid(extract = extr_2_2)
),
NA
)
)

expect_error(
expect_no_error(
extract_2_2 <-
dplyr::bind_rows(res_2_2$.extracts) %>%
tidyr::unnest(cols = c(.extracts)),
NA
tidyr::unnest(cols = c(.extracts))
)
expect_true(all(!extract_2_2$is_null_rec))
})
Expand Down Expand Up @@ -178,16 +174,15 @@ test_that("tune model and recipe", {
update(num_comp = dials::num_comp(c(2, 5))) %>%
dials::grid_space_filling(size = 4)

expect_error(
expect_no_error(
res_3_1 <- tune_grid(
wflow_3,
resamples = mt_folds,
grid = grid_3,
control = control_grid(extract = extr_3_1)
),
NA
)
)
expect_error(extract_3_1 <- dplyr::bind_rows(res_3_1$.extracts), NA)
expect_no_error(extract_3_1 <- dplyr::bind_rows(res_3_1$.extracts))

expect_true(all(names(extract_3_1) == c("num_comp", "cost", ".extracts", ".config")))
expect_true(
Expand Down
Loading

0 comments on commit 2e10089

Please sign in to comment.