From 04c3762c053c16af44d7325c9cb1151efb6ddcdf Mon Sep 17 00:00:00 2001 From: topepo Date: Tue, 29 Oct 2024 10:54:49 -0400 Subject: [PATCH] update checks and snapshots --- R/corr.R | 6 ++++++ tests/testthat/_snaps/corr.md | 28 ++++++++++++++++++++++++++++ tests/testthat/test-corr.R | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/R/corr.R b/R/corr.R index 76f219f48..52180e690 100644 --- a/R/corr.R +++ b/R/corr.R @@ -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) diff --git a/tests/testthat/_snaps/corr.md b/tests/testthat/_snaps/corr.md index 25541ab76..fb0d27e37 100644 --- a/tests/testthat/_snaps/corr.md +++ b/tests/testthat/_snaps/corr.md @@ -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". + diff --git a/tests/testthat/test-corr.R b/tests/testthat/test-corr.R index ab891cb4b..e16036c68 100644 --- a/tests/testthat/test-corr.R +++ b/tests/testthat/test-corr.R @@ -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 + ) +}) +