Skip to content

Commit

Permalink
update checks and snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
topepo committed Oct 29, 2024
1 parent 5f36f5b commit 04c3762
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions R/corr.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ step_corr_new <-
prep.step_corr <- function(x, training, info = NULL, ...) {
col_names <- recipes_eval_select(x$terms, training, info)
check_type(training[, col_names], types = c("double", "integer"))
check_number_decimal(x$threshold, min = 0, max = 1, arg = "threshold")
use <- x$use
rlang::arg_match(use, c("all.obs", "complete.obs", "pairwise.complete.obs",
"everything", "na.or.complete"))
method <- x$method
rlang::arg_match(method, c("pearson", "kendall", "spearman"))

wts <- get_case_weights(info, training)
were_weights_used <- are_weights_used(wts, unsupervised = TRUE)
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/_snaps/corr.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,31 @@
-- Operations
* Correlation filter on: V6 and V1 | Trained

# bad args

Code
recipe(mpg ~ ., mtcars) %>% step_corr(all_predictors(), threshold = 2) %>% prep()
Condition
Error in `step_corr()`:
Caused by error in `prep()`:
! `threshold` must be a number between 0 and 1, not the number 2.

---

Code
recipe(mpg ~ ., mtcars) %>% step_corr(all_predictors(), use = "this") %>% prep()
Condition
Error in `step_corr()`:
Caused by error in `prep()`:
! `use` must be one of "all.obs", "complete.obs", "pairwise.complete.obs", "everything", or "na.or.complete", not "this".

---

Code
recipe(mpg ~ ., mtcars) %>% step_corr(all_predictors(), method = "my dissertation") %>%
prep()
Condition
Error in `step_corr()`:
Caused by error in `prep()`:
! `method` must be one of "pearson", "kendall", or "spearman", not "my dissertation".

22 changes: 22 additions & 0 deletions tests/testthat/test-corr.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,25 @@ test_that("tunable is setup to work with extract_parameter_set_dials", {
expect_s3_class(params, "parameters")
expect_identical(nrow(params), 1L)
})

test_that("bad args", {
expect_snapshot(
recipe(mpg ~ ., mtcars) %>%
step_corr(all_predictors(), threshold = 2) %>%
prep(),
error = TRUE
)
expect_snapshot(
recipe(mpg ~ ., mtcars) %>%
step_corr(all_predictors(), use = "this") %>%
prep(),
error = TRUE
)
expect_snapshot(
recipe(mpg ~ ., mtcars) %>%
step_corr(all_predictors(), method = "my dissertation") %>%
prep(),
error = TRUE
)
})

0 comments on commit 04c3762

Please sign in to comment.