Skip to content

Commit ea7392a

Browse files
authored
snapshot test the interactive logger for finetune (#214)
1 parent c068150 commit ea7392a

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# interactive logger works (finetune integration, error)
2+
3+
Code
4+
res_anova <- tune_race_anova(parsnip::nearest_neighbor("regression", "kknn",
5+
neighbors = tune()), Sale_Price ~ ., rsample::vfold_cv(modeldata::ames[, c(72,
6+
40:45)], 5), control = control_race(extract = function(x) {
7+
raise_warning()
8+
raise_error()
9+
}))
10+
Message
11+
> A | warning: ope! yikes.
12+
> B | error: AHHhH
13+
14+
---
15+
16+
Code
17+
catalog_summary_test
18+
Output
19+
A: x5 B: x5
20+
21+
---
22+
23+
Code
24+
res_sa <- tune_sim_anneal(parsnip::nearest_neighbor("regression", "kknn",
25+
neighbors = tune()), Sale_Price ~ ., rsample::vfold_cv(modeldata::ames[, c(72,
26+
40:45)], 5), initial = res_anova, iter = 15, control = control_sim_anneal(
27+
verbose_iter = FALSE, extract = function(x) {
28+
raise_warning()
29+
raise_error()
30+
}))
31+
Message
32+
> A | warning: ope! yikes.
33+
> B | error: AHHhH
34+
35+
---
36+
37+
Code
38+
catalog_summary_test
39+
Output
40+
A: x75 B: x75
41+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# copied from https://github.com/tidymodels/tune/blob/main/tests/testthat/helper-tune-package.R
2+
catalog_lines <- function(lines) {
3+
lines[grepl("^>", lines)]
4+
}
5+
6+
# Make a new binding to prevent infinite recursion when the original is mocked.
7+
initialize_catalog_ <- tune:::initialize_catalog
8+
9+
# Sets a new exit handler on `initialize_catalog()` that stores the summary
10+
# of issues before it's cleared along with the progress bar. Together with
11+
# the above, we can test the full catalog output.
12+
redefer_initialize_catalog <- function(test_env) {
13+
local({
14+
function(control, env = rlang::caller_env()) {
15+
initialize_catalog_(control, env)
16+
17+
withr::defer(
18+
assign(
19+
"catalog_summary_test",
20+
tune:::tune_env$progress_env$catalog_summary,
21+
test_env
22+
),
23+
envir = env,
24+
priority = "first"
25+
)
26+
27+
NULL
28+
}
29+
})
30+
}
31+
32+
test_that("interactive logger works (finetune integration, error)", {
33+
skip_if(tune:::allow_parallelism(FALSE), "Will not catalog: parallelism is enabled")
34+
local_mocked_bindings(
35+
is_testing = function() {FALSE},
36+
initialize_catalog = redefer_initialize_catalog(rlang::current_env()),
37+
.package = "tune"
38+
)
39+
library(finetune)
40+
41+
raise_error <- function(x) {stop("AHHhH")}
42+
raise_warning <- function(x) {warning("ope! yikes.")}
43+
44+
set.seed(1)
45+
expect_snapshot(
46+
{res_anova <-
47+
tune_race_anova(
48+
parsnip::nearest_neighbor("regression", "kknn", neighbors = tune()),
49+
Sale_Price ~ .,
50+
rsample::vfold_cv(modeldata::ames[, c(72, 40:45)], 5),
51+
control = control_race(extract = function(x) {raise_warning(); raise_error()})
52+
)},
53+
transform = catalog_lines
54+
)
55+
56+
# `catalog_summary_test` written to this env via `redefer_initialize_catalog()`
57+
expect_snapshot(catalog_summary_test)
58+
59+
set.seed(1)
60+
expect_snapshot(
61+
{res_sa <-
62+
tune_sim_anneal(
63+
parsnip::nearest_neighbor("regression", "kknn", neighbors = tune()),
64+
Sale_Price ~ .,
65+
rsample::vfold_cv(modeldata::ames[, c(72, 40:45)], 5),
66+
initial = res_anova,
67+
iter = 15,
68+
control = control_sim_anneal(verbose_iter = FALSE,
69+
extract = function(x) {raise_warning(); raise_error()})
70+
)},
71+
transform = catalog_lines
72+
)
73+
74+
# `catalog_summary_test` written to this env via `redefer_initialize_catalog()`
75+
expect_snapshot(catalog_summary_test)
76+
})

0 commit comments

Comments
 (0)