|
| 1 | +test_that("tidy.tailor works", { |
| 2 | + library(tibble) |
| 3 | + |
| 4 | + set.seed(1) |
| 5 | + d_calibration <- tibble(y = rnorm(100), y_pred = y/2 + rnorm(100)) |
| 6 | + d_test <- tibble(y = rnorm(100), y_pred = y/2 + rnorm(100)) |
| 7 | + |
| 8 | + # TODO: reintroduce custom predictions when #61 is resolved |
| 9 | + tlr <- |
| 10 | + tailor() %>% |
| 11 | + adjust_numeric_calibration() %>% |
| 12 | + adjust_numeric_range(lower_limit = 2) #%>% |
| 13 | + #adjust_predictions_custom(squared = y_pred^2) |
| 14 | + |
| 15 | + tidy_tlr <- tidy(tlr) |
| 16 | + |
| 17 | + expect_s3_class(tidy_tlr, "tbl_df") |
| 18 | + expect_equal(nrow(tidy_tlr), length(tlr$adjustments)) |
| 19 | + expect_named( |
| 20 | + tidy_tlr, |
| 21 | + c("number", "name", "input", "output_numeric", "output_prob", |
| 22 | + "output_class", "output_all", "trained", "requires_training") |
| 23 | + ) |
| 24 | + expect_equal(tidy_tlr$number, seq_len(length(tlr$adjustments))) |
| 25 | + expect_false(any(tidy_tlr$trained)) |
| 26 | + expect_true(any(tidy_tlr$requires_training)) |
| 27 | + |
| 28 | + tidy_tlr_1 <- tidy(tlr, 1) |
| 29 | + tidy_tlr_2 <- tidy(tlr, 2) |
| 30 | + |
| 31 | + expect_equal(tidy_tlr[1,], tidy_tlr_1) |
| 32 | + expect_equal(tidy_tlr[2,], tidy_tlr_2) |
| 33 | + |
| 34 | + tlr_fit <- fit(tlr, d_calibration, outcome = y, estimate = y_pred) |
| 35 | + |
| 36 | + tidy_tlr_fit <- tidy(tlr_fit) |
| 37 | + |
| 38 | + expect_identical( |
| 39 | + tidy_tlr[names(tidy_tlr) != "trained"], |
| 40 | + tidy_tlr_fit[names(tidy_tlr_fit) != "trained"] |
| 41 | + ) |
| 42 | + expect_true(all(tidy_tlr_fit$trained)) |
| 43 | +}) |
| 44 | + |
| 45 | +test_that("tidy.tailor errors informatively with bad arguments", { |
| 46 | + tlr <- |
| 47 | + tailor() %>% |
| 48 | + adjust_numeric_calibration() %>% |
| 49 | + adjust_numeric_range(lower_limit = 2) |
| 50 | + |
| 51 | + expect_error(tidy(tlr, silly = "head"), class = "rlib_error_dots_nonempty") |
| 52 | + expect_snapshot(error = TRUE, tidy(tlr, number = 4)) |
| 53 | +}) |
| 54 | + |
| 55 | +test_that("tidying a tailor with no adjustments", { |
| 56 | + tidy_tlr <- tidy(tailor()) |
| 57 | + |
| 58 | + expect_equal(nrow(tidy_tlr), 0) |
| 59 | + expect_equal( |
| 60 | + ncol(tidy_tlr), |
| 61 | + tailor() %>% |
| 62 | + adjust_numeric_calibration() %>% |
| 63 | + tidy() %>% |
| 64 | + ncol()) |
| 65 | +}) |
0 commit comments