Skip to content

Commit

Permalink
expect_error(regex = NA) -> expect_snapshot()
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Sep 24, 2024
1 parent ec5e8f2 commit 3d2bc64
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 62 deletions.
25 changes: 10 additions & 15 deletions tests/testthat/test-collapse_cart.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_that("collapsing factors", {
data(ames, package = "modeldata")
ames$Sale_Price <- log10(ames$Sale_Price)

expect_error(
expect_no_error(
{
rec_1 <-
recipe(Sale_Price ~ ., data = ames) %>%
Expand All @@ -12,8 +12,7 @@ test_that("collapsing factors", {
outcome = vars(Sale_Price)
) %>%
prep()
},
regex = NA
}
)

expect_true(length(rec_1$steps[[1]]$results) == 1)
Expand All @@ -35,7 +34,7 @@ test_that("collapsing factors", {
)
)

expect_error(
expect_no_error(
{
rec_2 <-
recipe(Sale_Price ~ ., data = ames) %>%
Expand All @@ -44,8 +43,7 @@ test_that("collapsing factors", {
min_n = 100, cost_complexity = 0.1
) %>%
prep()
},
regex = NA
}
)

expect_true(
Expand All @@ -60,7 +58,7 @@ test_that("failed collapsing", {

# model fails
ames$Sale_Price2 <- Inf
expect_error(
expect_no_error(
{
rec_3 <-
recipe(Sale_Price2 ~ ., data = ames) %>%
Expand All @@ -69,14 +67,13 @@ test_that("failed collapsing", {
outcome = vars(Sale_Price2)
) %>%
prep()
},
regex = NA
}
)

expect_true(length(rec_3$steps[[1]]$results) == 0)

# too many splits
expect_error(
expect_no_error(
{
rec_4 <-
recipe(Sale_Price ~ ., data = ames) %>%
Expand All @@ -86,21 +83,19 @@ test_that("failed collapsing", {
cost_complexity = 0, min_n = 1
) %>%
prep()
},
regex = NA
}
)

expect_true(length(rec_4$steps[[1]]$results) == 0)

# too many splits
expect_error(
expect_no_error(
{
rec_5 <-
recipe(Sale_Price ~ ., data = ames) %>%
step_collapse_cart(Central_Air, outcome = vars(Sale_Price)) %>%
prep()
},
regex = NA
}
)

expect_true(length(rec_5$steps[[1]]$results) == 0)
Expand Down
20 changes: 8 additions & 12 deletions tests/testthat/test-collapse_stringdist.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ test_that("collapsing factors", {

data(ames, package = "modeldata")

expect_error(
expect_no_error(
{
rec_1 <-
recipe(Sale_Price ~ ., data = ames) %>%
step_collapse_stringdist(MS_SubClass, distance = 5) %>%
prep()
},
regex = NA
}
)

expect_true(length(rec_1$steps[[1]]$results) == 1)
Expand All @@ -33,14 +32,13 @@ test_that("collapsing factors", {
)
)

expect_error(
expect_no_error(
{
rec_2 <-
recipe(Sale_Price ~ ., data = ames) %>%
step_collapse_stringdist(MS_SubClass, Overall_Cond, distance = 10) %>%
prep()
},
regex = NA
}
)

expect_true(length(rec_2$steps[[1]]$results) == 2)
Expand Down Expand Up @@ -165,14 +163,13 @@ test_that("failed collapsing", {
data(ames, package = "modeldata")

# too many splits
expect_error(
expect_no_error(
{
rec_4 <-
recipe(Sale_Price ~ ., data = ames) %>%
step_collapse_stringdist(MS_SubClass, distance = 0) %>%
prep()
},
regex = NA
}
)

expect_equal(
Expand All @@ -181,14 +178,13 @@ test_that("failed collapsing", {
)

# too few splits
expect_error(
expect_no_error(
{
rec_5 <-
recipe(Sale_Price ~ ., data = ames) %>%
step_collapse_stringdist(MS_SubClass, distance = 10000) %>%
prep()
},
regex = NA
}
)

expect_equal(
Expand Down
20 changes: 8 additions & 12 deletions tests/testthat/test-discretize_cart.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod <- rpart(y ~ x, data = sim_tr_reg)
best_split <- unname(mod$splits[, "index"])

test_that("low-level binning for classification", {
expect_error(
expect_no_error(
splits <-
embed:::cart_binning(
sim_tr_cls$x,
Expand All @@ -26,8 +26,7 @@ test_that("low-level binning for classification", {
cost_complexity = 0.01,
tree_depth = 5,
min_n = 10
),
regexp = NA
)
)
expect_equal(splits, best_split)

Expand All @@ -47,7 +46,7 @@ test_that("low-level binning for classification", {
})

test_that("low-level binning for regression", {
expect_error(
expect_no_error(
splits <-
embed:::cart_binning(
sim_tr_reg$x,
Expand All @@ -56,8 +55,7 @@ test_that("low-level binning for regression", {
cost_complexity = 0.01,
tree_depth = 5,
min_n = 10
),
regexp = NA
)
)
expect_equal(splits, best_split)

Expand Down Expand Up @@ -87,9 +85,8 @@ test_that("step function for classification", {
expect_equal(names(cart_rec$steps[[1]]$rules), "x")
expect_equal(cart_rec$steps[[1]]$rules$x, best_split)

expect_error(
cart_pred <- bake(cart_rec, sim_tr_cls[, -3]),
regexp = NA
expect_no_error(
cart_pred <- bake(cart_rec, sim_tr_cls[, -3])
)

expect_true(is.factor(cart_pred$x))
Expand All @@ -108,9 +105,8 @@ test_that("step function for regression", {
expect_equal(names(cart_rec$steps[[1]]$rules), "x")
expect_equal(cart_rec$steps[[1]]$rules$x, best_split)

expect_error(
cart_pred <- bake(cart_rec, sim_tr_reg[, -3]),
regexp = NA
expect_no_error(
cart_pred <- bake(cart_rec, sim_tr_reg[, -3])
)

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

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

Expand Down
9 changes: 4 additions & 5 deletions tests/testthat/test-feature_hash.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test_that("basic usage", {
rec <- recipe(x1 ~ x3, data = ex_dat) %>%
step_feature_hash(x3)

expect_error(rec_tr <- prep(rec), regex = NA)
expect_no_error(rec_tr <- prep(rec))

res_tr <- bake(rec_tr, new_data = NULL, dplyr::starts_with("x3"))

Expand Down Expand Up @@ -64,7 +64,7 @@ test_that("basic usage - character strings", {
rec <- recipe(x1 ~ x3, data = ex_dat) %>%
step_feature_hash(x3)

expect_error(rec_tr <- prep(rec), regex = NA)
expect_no_error(rec_tr <- prep(rec))

res_tr <- bake(rec_tr, new_data = NULL, dplyr::starts_with("x3"))

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

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

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

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

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

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

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

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

Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-umap.R
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,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-woe.R
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,8 @@ test_that("keep_original_cols - can prep recipes with it missing", {
rec <- prep(rec)
)

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

Expand Down

0 comments on commit 3d2bc64

Please sign in to comment.