Skip to content

Commit

Permalink
add snapshots for remaining errors
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Oct 25, 2024
1 parent f8af7d9 commit 99208a5
Show file tree
Hide file tree
Showing 16 changed files with 335 additions and 29 deletions.
2 changes: 1 addition & 1 deletion R/kpca_rbf.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ prep.step_kpca_rbf <- function(x, training, info = NULL, ...) {
cli::cli_abort(c(
x = "Failed with error:",
i = as.character(kprc)
), .internal = TRUE)
))
}
} else {
kprc <- NULL
Expand Down
5 changes: 2 additions & 3 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ get_rhs_vars <- function(formula, data, no_lhs = FALSE) {
#' dummy_names("x", substring(after_mm, 2), ordinal = TRUE)
#' @export
names0 <- function(num, prefix = "x") {
if (num < 1) {
cli::cli_abort("{.arg num} should be > 0.")
}
check_number_whole(num, min = 1)

ind <- format(seq_len(num))
ind <- gsub(" ", "0", ind)
paste0(prefix, ind)
Expand Down
18 changes: 3 additions & 15 deletions R/recipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,15 @@ recipe.data.frame <-

cli::cli_abort(c(
x = "{.arg vars} must have unique values.",
i = "The following values were duplicated:",
"*" = "{.and {offenders}}"
i = "The following values were duplicated: {.and {.field {offenders}}}."
))
}
if (any(!(vars %in% colnames(x)))) {
offenders <- vars[!(vars %in% colnames(x))]

cli::cli_abort(c(
x = "The following elements of {.arg vars} are not found in {.arg x}:",
"*" = "{.and {offenders}}"
"*" = "{.and {.field {offenders}}}."
))
}

Expand Down Expand Up @@ -684,7 +683,7 @@ bake.recipe <- function(object, new_data, ..., composition = "tibble") {
if (!any(composition == formats)) {
cli::cli_abort(c(
"x" = "{.arg composition} cannot be {.val {composition}}.",
"i" = "Allowed values are {.or {formats}}."
"i" = "Allowed values are {.or {.val {formats}}}."
))
}

Expand All @@ -693,17 +692,6 @@ bake.recipe <- function(object, new_data, ..., composition = "tibble") {
terms <- quos(everything())
}

# In case someone used the deprecated `newdata`:
if (is.null(new_data) || is.null(ncol(new_data))) {
if (any(names(terms) == "newdata")) {
cli::cli_abort(
"Please use {.arg new_data} instead of {.arg newdata} with {.fun bake}."
)
} else {
cli::cli_abort("Please pass a data set to {.arg new_data}.")
}
}

if (is_sparse_matrix(new_data)) {
new_data <- sparsevctrs::coerce_to_sparse_tibble(
new_data,
Expand Down
8 changes: 0 additions & 8 deletions R/regex.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,6 @@ prep.step_regex <- function(x, training, info = NULL, ...) {
col_name <- recipes_eval_select(x$terms, training, info)
check_type(training[, col_name], types = c("string", "factor", "ordered"))

if (length(col_name) > 1) {
cli::cli_abort(c(
x = "The selector should select at most a single variable.",
i = "The following {length(col_name)} were selected: \\
{.and {.var {col_name}}}."
))
}

step_regex_new(
terms = x$terms,
role = x$role,
Expand Down
88 changes: 88 additions & 0 deletions tests/testthat/_snaps/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,91 @@
Error in `as.data.frame.default()`:
! cannot coerce class '"function"' to a data.frame

# bake() error on wrong composition

Code
recipe(~., data = mtcars) %>% prep() %>% bake(mtcars, composition = "wrong")
Condition
Error in `bake()`:
x `composition` cannot be "wrong".
i Allowed values are "tibble", "dgCMatrix", "matrix", or "data.frame".

# juice() error on wrong composition

Code
recipe(~., data = mtcars) %>% prep() %>% juice(composition = "wrong")
Condition
Error in `juice()`:
x `composition` cannot be "wrong".
i Allowed values are "tibble", "dgCMatrix", "matrix", or "data.frame".

# juice() error if prep(retain = FALSE)

Code
recipe(~., data = mtcars) %>% prep(retain = FALSE) %>% juice()
Condition
Error in `juice()`:
! Use `retain = TRUE` in `prep()` to be able to extract the training set.

# recipe() error with minus in formula

Code
recipe(~ . - 1, data = mtcars)
Condition
Error in `recipe()`:
x `-` is not allowed in a recipe formula.
i Use `step_rm()` (`?recipes::step_rm()`) instead.

# recipe() error if vars and roles have different lengths

Code
recipe(mtcars, vars = c("mpg", "disp"), roles = c("predictor"))
Condition
Error in `recipe()`:
x `vars` and `roles` must have same length.
* `vars` has length 2
* `roles` has length 1

# recipe() error if vars not in data

Code
recipe(mtcars, vars = c("wrong", "disp-wrong"))
Condition
Error in `recipe()`:
x The following elements of `vars` are not found in `x`:
* wrong and disp-wrong.

# recipe() error if vars contains duplicates

Code
recipe(mtcars, vars = c("mpg", "mpg"))
Condition
Error in `recipe()`:
x `vars` must have unique values.
i The following values were duplicated: mpg.

# recipe() error if vars and roles are used with formula

Code
recipe(mtcars, ~., vars = c("mpg"))
Condition
Error in `recipe()`:
! The `vars` argument will be ignored when a formula is used.

---

Code
recipe(mtcars, ~., roles = c("mpg"))
Condition
Error in `recipe()`:
! The `roles` argument will be ignored when a formula is used.

# recipe() error for unsupported data types

Code
recipe(list())
Condition
Error in `recipe()`:
x `x` should be a data frame, matrix, formula, or tibble.
i `x` is an empty list.

12 changes: 12 additions & 0 deletions tests/testthat/_snaps/extract_parameter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# rethrows error correctly from implementation

Code
params <- extract_parameter_set_dials(rec)
Condition
Error in `mutate()`:
i In argument: `object = purrr::map(call_info, eval_call_info)`.
Caused by error in `purrr::map()`:
i In index: 1.
Caused by error in `.f()`:
! Error when calling `num_comp()`: Error in dials::num_comp(range = c(1L, 4L)) : mocked error

10 changes: 10 additions & 0 deletions tests/testthat/_snaps/kpca_rbf.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
! Name collision occurred. The following variable names already exist:
* `kPC1`

# rethrows error correctly from implementation

Code
recipe(~., data = mtcars) %>% step_kpca_rbf(all_predictors()) %>% prep()
Condition
Error in `step_kpca_rbf()`:
Caused by error in `prep()`:
x Failed with error:
i Error in kernlab::kpca(x = as.matrix(training[, col_names]), features = 5, : mocked error

# bake method errors when needed non-standard role columns are missing

Code
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/_snaps/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,28 @@
Error in `recipes:::spline_msg()`:
! craaazzyy {{}}{}{}

# names0() error on non-positive number

Code
names0(0)
Condition
Error in `names0()`:
! `num` must be a whole number larger than or equal to 1, not the number 0.

# ellipse_check() errors on empty selection

Code
ellipse_check()
Condition
Error in `ellipse_check()`:
! Please supply at least one variable specification.
i See ?selections (`?recipes::selections()`) for more information.

---

Code
uses_dim_red(x)
Condition
Error in `uses_dim_red()`:
! Recipes version >= 0.1.17 represents the estimates using a different format. Please recreate this recipe or use version 0.1.16 or less. See issue #823 (<https://github.com/tidymodels/recipes/issues/823>).

23 changes: 23 additions & 0 deletions tests/testthat/_snaps/tune_args.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# tune_args() errors on multiple tune()s in same arg

Code
recipe(~., data = mtcars) %>% step_pca(all_predictors(), num_comp = ~ tune() +
tune()) %>% tune_args()
Condition
Error in `purrr::map()`:
i In index: 1.
Caused by error in `purrr::map_chr()`:
i In index: 1.
i With name: num_comp.
Caused by error in `find_tune_id()`:
! Only one tunable value is currently allowed per argument. The current argument has: tune() + tune().

# tune_tbl() errors on duplicate ids

Code
tune_tbl(name = c("a", "b"), tunable = c(TRUE, TRUE), id = c("a", "a"), source = c(
"a", "b"), component = c("a", "b"), component_id = c("a", "b"), full = TRUE)
Condition
Error in `tune_tbl()`:
! There are duplicate id values listed in `tune()`: "a".

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@
Error in `validate_not_trained()`:
! To update `step_stp()`, it must not be trained.

# update() errors on duplicate assignments

Code
update(step, x = 5, x = 6)
Condition
Error in `validate_has_unique_names()`:
! All of the changes supplied in `...` must be uniquely named.

73 changes: 73 additions & 0 deletions tests/testthat/test-basics.R
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,76 @@ test_that("data argument is checked in recipe.formula() (#1325)", {
recipe(~ ., data = data)
)
})

test_that("bake() error on wrong composition", {
expect_snapshot(
error = TRUE,
recipe(~., data = mtcars) %>%
prep() %>%
bake(mtcars, composition = "wrong")
)
})

test_that("juice() error on wrong composition", {
expect_snapshot(
error = TRUE,
recipe(~., data = mtcars) %>%
prep() %>%
juice(composition = "wrong")
)
})

test_that("juice() error if prep(retain = FALSE)", {
expect_snapshot(
error = TRUE,
recipe(~., data = mtcars) %>%
prep(retain = FALSE) %>%
juice()
)
})

test_that("recipe() error with minus in formula", {
expect_snapshot(
error = TRUE,
recipe(~ . - 1, data = mtcars)
)
})

test_that("recipe() error if vars and roles have different lengths", {
expect_snapshot(
error = TRUE,
recipe(mtcars, vars = c("mpg", "disp"), roles = c("predictor"))
)
})

test_that("recipe() error if vars not in data", {
expect_snapshot(
error = TRUE,
recipe(mtcars, vars = c("wrong", "disp-wrong"))
)
})

test_that("recipe() error if vars contains duplicates", {
expect_snapshot(
error = TRUE,
recipe(mtcars, vars = c("mpg", "mpg"))
)
})

test_that("recipe() error if vars and roles are used with formula", {
expect_snapshot(
error = TRUE,
recipe(mtcars, ~., vars = c("mpg"))
)
expect_snapshot(
error = TRUE,
recipe(mtcars, ~., roles = c("mpg"))
)
})

test_that("recipe() error for unsupported data types", {
expect_snapshot(
error = TRUE,
recipe(list())
)
})
17 changes: 17 additions & 0 deletions tests/testthat/test-extract_parameter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test_that("rethrows error correctly from implementation", {
skip_if_not_installed("dials")
rec <- recipe(~., data = mtcars) %>%
step_pca(all_predictors(), num_comp = hardhat::tune())

local_mocked_bindings(
.package = "dials",
num_comp = function(...) {
cli::cli_abort("mocked error")
}
)

expect_snapshot(
error = TRUE,
params <- extract_parameter_set_dials(rec)
)
})
17 changes: 17 additions & 0 deletions tests/testthat/test-kpca_rbf.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ test_that("Do nothing for num_comps = 0 and keep_original_cols = FALSE (#1152)",
expect_identical(res, tibble::as_tibble(mtcars))
})

test_that("rethrows error correctly from implementation", {
skip_if_not_installed("kernlab")

local_mocked_bindings(
.package = "kernlab",
kpca = function(...) {
cli::cli_abort("mocked error")
}
)
expect_snapshot(
error = TRUE,
recipe(~ ., data = mtcars) %>%
step_kpca_rbf(all_predictors()) %>%
prep()
)
})

# Infrastructure ---------------------------------------------------------------

test_that("bake method errors when needed non-standard role columns are missing", {
Expand Down
Loading

0 comments on commit 99208a5

Please sign in to comment.