From 3702d5ce20c8f3930f681e162b851ea7554106b9 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Tue, 9 Apr 2024 17:10:22 -0700 Subject: [PATCH 1/6] add tests for check_args --- tests/testthat/_snaps/c5.md | 42 ++++++++++++++++++++++ tests/testthat/_snaps/cubist.md | 64 +++++++++++++++++++++++++++++++++ tests/testthat/test-c5.R | 45 +++++++++++++++++++++++ tests/testthat/test-cubist.R | 64 +++++++++++++++++++++++++++++++++ 4 files changed, 215 insertions(+) create mode 100644 tests/testthat/_snaps/c5.md create mode 100644 tests/testthat/_snaps/cubist.md diff --git a/tests/testthat/_snaps/c5.md b/tests/testthat/_snaps/c5.md new file mode 100644 index 0000000..8817fbd --- /dev/null +++ b/tests/testthat/_snaps/c5.md @@ -0,0 +1,42 @@ +# check_args() works + + Code + spec <- C5_rules(trees = c(1, 2, 3)) %>% set_engine("C5.0") %>% set_mode( + "classification") + fit(spec, Class ~ ., data = ad_data$ad_mod) + Condition + Error in `fit()`: + ! Only a single value of `trees` should be passed, not 3. + +--- + + Code + spec <- C5_rules(trees = 0) %>% set_engine("C5.0") %>% set_mode( + "classification") + res <- fit(spec, Class ~ ., data = ad_data$ad_mod) + Condition + Warning: + The number of trees should be `>= 1` and `<= 100` + Truncating to 1. + +--- + + Code + spec <- C5_rules(trees = 1000) %>% set_engine("C5.0") %>% set_mode( + "classification") + res <- fit(spec, Class ~ ., data = ad_data$ad_mod) + Condition + Warning: + The number of trees should be `>= 1` and `<= 100` + Truncating to 100. + +--- + + Code + spec <- C5_rules(min_n = c(1, 2, 3)) %>% set_engine("C5.0") %>% set_mode( + "classification") + fit(spec, Class ~ ., data = ad_data$ad_mod) + Condition + Error in `fit()`: + ! Only a single value of `min_n` should be passed, not 3. + diff --git a/tests/testthat/_snaps/cubist.md b/tests/testthat/_snaps/cubist.md new file mode 100644 index 0000000..7af2d1e --- /dev/null +++ b/tests/testthat/_snaps/cubist.md @@ -0,0 +1,64 @@ +# check_args() works + + Code + spec <- cubist_rules(committees = c(1, 2, 3)) %>% set_engine("Cubist") %>% + set_mode("regression") + fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Error in `fit()`: + ! Only a single value of `committees` should be passed, not 3. + +--- + + Code + spec <- cubist_rules(committees = 0) %>% set_engine("Cubist") %>% set_mode( + "regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Warning: + The number of committees should be `>= 1` and `<= 100`. + Truncating to 100. + +--- + + Code + spec <- cubist_rules(committees = 1000) %>% set_engine("Cubist") %>% set_mode( + "regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Warning: + The number of committees should be `>= 1` and `<= 100`. + Truncating to 100. + +--- + + Code + spec <- cubist_rules(neighbors = c(1, 2, 3)) %>% set_engine("Cubist") %>% + set_mode("regression") + fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Error in `fit()`: + ! Only a single value of `neighbors` should be passed, not 3. + +--- + + Code + spec <- cubist_rules(neighbors = -1) %>% set_engine("Cubist") %>% set_mode( + "regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Warning: + The number of neighbors should be `>= 0` and `<= 9`. + Truncating to 0. + +--- + + Code + spec <- cubist_rules(neighbors = 1000) %>% set_engine("Cubist") %>% set_mode( + "regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Warning: + The number of neighbors should be `>= 0` and `<= 9`. + Truncating to 9. + diff --git a/tests/testthat/test-c5.R b/tests/testthat/test-c5.R index 208744b..51f4d43 100644 --- a/tests/testthat/test-c5.R +++ b/tests/testthat/test-c5.R @@ -412,3 +412,48 @@ test_that("tidy method", { expect_equal(nrow(tidy_2), term_nodes_2) }) + +test_that('check_args() works', { + skip_on_cran() + skip_if_not_installed("C50") + skip_if_not_installed("parsnip", "1.2.1.9001") + ad_data <- make_ad_data() + + expect_snapshot( + error = TRUE, + { + spec <- C5_rules(trees = c(1, 2, 3)) %>% + set_engine("C5.0") %>% + set_mode("classification") + fit(spec, Class ~ ., data = ad_data$ad_mod) + } + ) + + expect_snapshot( + { + spec <- C5_rules(trees = 0) %>% + set_engine("C5.0") %>% + set_mode("classification") + res <- fit(spec, Class ~ ., data = ad_data$ad_mod) + } + ) + + expect_snapshot( + { + spec <- C5_rules(trees = 1000) %>% + set_engine("C5.0") %>% + set_mode("classification") + res <- fit(spec, Class ~ ., data = ad_data$ad_mod) + } + ) + + expect_snapshot( + error = TRUE, + { + spec <- C5_rules(min_n = c(1, 2, 3)) %>% + set_engine("C5.0") %>% + set_mode("classification") + fit(spec, Class ~ ., data = ad_data$ad_mod) + } + ) +}) \ No newline at end of file diff --git a/tests/testthat/test-cubist.R b/tests/testthat/test-cubist.R index d90406d..a4af024 100644 --- a/tests/testthat/test-cubist.R +++ b/tests/testthat/test-cubist.R @@ -705,3 +705,67 @@ test_that("mode specific package dependencies", { list(c("Cubist", "rules")) ) }) + + +test_that('check_args() works', { + skip_on_cran() + skip_if_not_installed("Cubist") + skip_if_not_installed("parsnip", "1.2.1.9001") + chi_data <- make_chi_data() + + expect_snapshot( + error = TRUE, + { + spec <- cubist_rules(committees = c(1, 2, 3)) %>% + set_engine("Cubist") %>% + set_mode("regression") + fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) + + expect_snapshot( + { + spec <- cubist_rules(committees = 0) %>% + set_engine("Cubist") %>% + set_mode("regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) + + expect_snapshot( + { + spec <- cubist_rules(committees = 1000) %>% + set_engine("Cubist") %>% + set_mode("regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) + + expect_snapshot( + error = TRUE, + { + spec <- cubist_rules(neighbors = c(1, 2, 3)) %>% + set_engine("Cubist") %>% + set_mode("regression") + fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) + + expect_snapshot( + { + spec <- cubist_rules(neighbors = -1) %>% + set_engine("Cubist") %>% + set_mode("regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) + + expect_snapshot( + { + spec <- cubist_rules(neighbors = 1000) %>% + set_engine("Cubist") %>% + set_mode("regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) +}) \ No newline at end of file From 1584b8e7428acf857166b60e1a196ff5c1441ad7 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Tue, 9 Apr 2024 17:53:03 -0700 Subject: [PATCH 2/6] add tests for check_args.rule_fit --- tests/testthat/test-rule_fit.R | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 tests/testthat/test-rule_fit.R diff --git a/tests/testthat/test-rule_fit.R b/tests/testthat/test-rule_fit.R new file mode 100644 index 0000000..245f18f --- /dev/null +++ b/tests/testthat/test-rule_fit.R @@ -0,0 +1,4 @@ +test_that("check_args() works", { + # Here for completeness, no checking is done + expect_true(TRUE) +}) \ No newline at end of file From 746977f1ba91abfdc56624e6dfe9e397a2156b59 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Tue, 9 Apr 2024 18:50:38 -0700 Subject: [PATCH 3/6] add missing skip_if_not_installed() --- tests/testthat/test-rule_fit.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat/test-rule_fit.R b/tests/testthat/test-rule_fit.R index 245f18f..b1b9948 100644 --- a/tests/testthat/test-rule_fit.R +++ b/tests/testthat/test-rule_fit.R @@ -1,4 +1,6 @@ test_that("check_args() works", { + skip_if_not_installed("parsnip", "1.2.1.9001") + # Here for completeness, no checking is done expect_true(TRUE) }) \ No newline at end of file From a94ef7e499afd1f88d5409b4e8cd95a59780924b Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Wed, 10 Apr 2024 09:47:01 -0700 Subject: [PATCH 4/6] update snapshot --- tests/testthat/_snaps/cubist.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/_snaps/cubist.md b/tests/testthat/_snaps/cubist.md index 7af2d1e..99dbfdd 100644 --- a/tests/testthat/_snaps/cubist.md +++ b/tests/testthat/_snaps/cubist.md @@ -17,7 +17,7 @@ Condition Warning: The number of committees should be `>= 1` and `<= 100`. - Truncating to 100. + Truncating to 1. --- From 42b5e64e02c628fb9f0b283d2afb927d02846c8d Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Wed, 10 Apr 2024 10:13:39 -0700 Subject: [PATCH 5/6] update snapshots --- tests/testthat/_snaps/c5.md | 4 ++-- tests/testthat/_snaps/cubist.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/_snaps/c5.md b/tests/testthat/_snaps/c5.md index 8817fbd..26d6a98 100644 --- a/tests/testthat/_snaps/c5.md +++ b/tests/testthat/_snaps/c5.md @@ -6,7 +6,7 @@ fit(spec, Class ~ ., data = ad_data$ad_mod) Condition Error in `fit()`: - ! Only a single value of `trees` should be passed, not 3. + ! `tree` must be a whole number or `NULL`, not a double vector. --- @@ -38,5 +38,5 @@ fit(spec, Class ~ ., data = ad_data$ad_mod) Condition Error in `fit()`: - ! Only a single value of `min_n` should be passed, not 3. + ! `min_n` must be a whole number or `NULL`, not a double vector. diff --git a/tests/testthat/_snaps/cubist.md b/tests/testthat/_snaps/cubist.md index 99dbfdd..09f5b4f 100644 --- a/tests/testthat/_snaps/cubist.md +++ b/tests/testthat/_snaps/cubist.md @@ -6,7 +6,7 @@ fit(spec, ridership ~ ., data = chi_data$chi_mod) Condition Error in `fit()`: - ! Only a single value of `committees` should be passed, not 3. + ! `committees` must be a whole number or `NULL`, not a double vector. --- @@ -38,7 +38,7 @@ fit(spec, ridership ~ ., data = chi_data$chi_mod) Condition Error in `fit()`: - ! Only a single value of `neighbors` should be passed, not 3. + ! `neighbors` must be a whole number or `NULL`, not a double vector. --- From d4680dcb03479038bf780918e9a0fee3bb2fd2bf Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Wed, 10 Apr 2024 10:54:35 -0700 Subject: [PATCH 6/6] add end of line in files --- tests/testthat/test-c5.R | 2 +- tests/testthat/test-cubist.R | 2 +- tests/testthat/test-rule_fit.R | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-c5.R b/tests/testthat/test-c5.R index 51f4d43..6bbe82a 100644 --- a/tests/testthat/test-c5.R +++ b/tests/testthat/test-c5.R @@ -456,4 +456,4 @@ test_that('check_args() works', { fit(spec, Class ~ ., data = ad_data$ad_mod) } ) -}) \ No newline at end of file +}) diff --git a/tests/testthat/test-cubist.R b/tests/testthat/test-cubist.R index a4af024..8144678 100644 --- a/tests/testthat/test-cubist.R +++ b/tests/testthat/test-cubist.R @@ -768,4 +768,4 @@ test_that('check_args() works', { res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) } ) -}) \ No newline at end of file +}) diff --git a/tests/testthat/test-rule_fit.R b/tests/testthat/test-rule_fit.R index b1b9948..8666054 100644 --- a/tests/testthat/test-rule_fit.R +++ b/tests/testthat/test-rule_fit.R @@ -3,4 +3,4 @@ test_that("check_args() works", { # Here for completeness, no checking is done expect_true(TRUE) -}) \ No newline at end of file +})