Skip to content

Commit 453b6c1

Browse files
committed
add tests for check_outcome()
1 parent 57825cf commit 453b6c1

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

tests/testthat/_snaps/misc.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,27 @@
124124
Error in `set_engine()`:
125125
! `set_engine()` expected a model specification to be supplied to the `object` argument, but received a(n) `data.frame` object.
126126

127+
# check_outcome works as expected
128+
129+
Code
130+
check_outcome(factor(1:10), reg_spec)
131+
Condition
132+
Error in `check_outcome()`:
133+
! For a regression model, the outcome should be `numeric`, not a `factor`.
134+
135+
---
136+
137+
Code
138+
check_outcome(1:10, class_spec)
139+
Condition
140+
Error in `check_outcome()`:
141+
! For a classification model, the outcome should be a `factor`, not a `integer`.
142+
143+
---
144+
145+
Code
146+
check_outcome(1:10, cens_spec)
147+
Condition
148+
Error in `check_outcome()`:
149+
! For a censored regression model, the outcome should be a `Surv` object, not a `integer`.
150+

tests/testthat/test_misc.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,48 @@ test_that('set_engine works as a generic', {
181181
)
182182

183183
})
184+
185+
test_that('check_outcome works as expected', {
186+
reg_spec <- linear_reg()
187+
188+
expect_no_error(
189+
check_outcome(1:10, reg_spec)
190+
)
191+
192+
expect_no_error(
193+
check_outcome(mtcars, reg_spec)
194+
)
195+
196+
expect_snapshot(
197+
error = TRUE,
198+
check_outcome(factor(1:10), reg_spec)
199+
)
200+
201+
class_spec <- logistic_reg()
202+
203+
expect_no_error(
204+
check_outcome(factor(1:10), class_spec)
205+
)
206+
207+
expect_no_error(
208+
check_outcome(lapply(mtcars, as.factor), class_spec)
209+
)
210+
211+
expect_snapshot(
212+
error = TRUE,
213+
check_outcome(1:10, class_spec)
214+
)
215+
216+
# Fake specification to avoid having to load {censored}
217+
cens_spec <- logistic_reg()
218+
cens_spec$mode <- "censored regression"
219+
220+
expect_no_error(
221+
check_outcome(survival::Surv(1, 1), cens_spec)
222+
)
223+
224+
expect_snapshot(
225+
error = TRUE,
226+
check_outcome(1:10, cens_spec)
227+
)
228+
})

tests/testthat/test_nearest_neighbor_kknn.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test_that('kknn execution', {
2424
x = hpc[, num_pred],
2525
y = hpc$input_fields
2626
),
27-
regexp = "outcome should be a factor"
27+
regexp = "outcome should be a `factor`"
2828
)
2929

3030
# nominal

0 commit comments

Comments
 (0)