Skip to content

Commit

Permalink
tests: use more specialised testthat functions
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke committed Dec 14, 2024
1 parent 6be8889 commit 9d1314a
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion inst/testthat/helper_expectations.R
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ expect_measure = function(m) {
testthat::expect_output(print(m), "Measure")

if ("requires_no_prediction" %in% m$properties) {
testthat::expect_true(is.null(m$predict_sets))
testthat::expect_null(m$predict_sets)
}

expect_id(m$id)
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test_DataBackendRbind.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ test_that("DataBackendRbind", {


# all col-hashes are mutually disjoint
expect_true(length(intersect(b1$col_hashes, b2$col_hashes)) == 0)
expect_true(length(intersect(b$col_hashes, b1$col_hashes)) == 0)
expect_true(length(intersect(b$col_hashes, b2$col_hashes)) == 0)
expect_length(intersect(b1$col_hashes, b2$col_hashes), 0)
expect_length(intersect(b$col_hashes, b1$col_hashes), 0)
expect_length(intersect(b$col_hashes, b2$col_hashes), 0)

})

Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test_Learner.R
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ test_that("validation task's backend is removed", {
task = tsk("mtcars")
task$internal_valid_task = 1:10
learner$train(task)
expect_true(is.null(learner$state$train_task$internal_valid_task$backend))
expect_null(learner$state$train_task$internal_valid_task$backend)
})

test_that("manual $train() stores validation hash and validation ids", {
Expand All @@ -348,7 +348,7 @@ test_that("manual $train() stores validation hash and validation ids", {
# nothing is stored for learners that don't do it
l2 = lrn("classif.featureless")
l2$train(task)
expect_true(is.null(l2$state$internal_valid_task_hash))
expect_null(l2$state$internal_valid_task_hash)
})

test_that("error when training a learner that sets valiadte to 'predefined' on a task without a validation task", {
Expand Down Expand Up @@ -421,15 +421,15 @@ test_that("internal_valid_task is created correctly", {
task$internal_valid_task = partition(task)$test
learner$train(task)
learner$validate = NULL
expect_true(is.null(learner$internal_valid_scores))
expect_true(is.null(learner$task$internal_valid_task))
expect_null(learner$internal_valid_scores)
expect_null(learner$task$internal_valid_task)

# validate = NULL (but task has none)
learner1 = LearnerClassifTest$new()
task1 = tsk("iris")
learner1$train(task1)
expect_true(is.null(learner1$internal_valid_scores))
expect_true(is.null(learner1$task$internal_valid_task))
expect_null(learner1$internal_valid_scores)
expect_null(learner1$task$internal_valid_task)

# validate = "test"
LearnerClassifTest2 = R6Class("LearnerClassifTest2", inherit = LearnerClassifDebug,
Expand Down Expand Up @@ -477,7 +477,7 @@ test_that("internal_valid_task is created correctly", {
learner4 = lrn("classif.debug", validate = 0.2)
task = tsk("iris")
learner4$train(task)
expect_true(is.null(task$internal_valid_task))
expect_null(task$internal_valid_task)
})

test_that("compatability check on validation task", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_Measure.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ test_that("primary iters are respected", {

test_that("no predict_sets required (#1094)", {
m = msr("internal_valid_score")
expect_equal(m$predict_sets, NULL)
expect_null(m$predict_sets)
rr = resample(tsk("iris"), lrn("classif.debug", validate = 0.3, predict_sets = NULL), rsmp("holdout"))
expect_double(rr$aggregate(m))
expect_warning(rr$aggregate(msr("classif.ce")), "needs predict sets")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_Task.R
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ test_that("internal_valid_task is printed", {
task = tsk("iris")
task$internal_valid_task = c(1:10, 51:60, 101:110)
out = capture_output(print(task))
expect_true(grepl(pattern = "* Validation Task: (30x5)", fixed = TRUE, x = out))
expect_match(out, "* Validation Task: (30x5)", fixed = TRUE)
})

test_that("task hashes during resample", {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_as_learner.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ test_that("as_learner conversion", {
test_that("discard_state", {
learner = lrn("classif.rpart")$train(tsk("iris"))
learner2 = as_learner(learner, clone = TRUE, discard_state = TRUE)
expect_true(is.null(learner2$state))
expect_null(learner2$state)
expect_false(is.null(learner$state))

learner3 = lrn("classif.rpart")
as_learner(learner3, clone = FALSE, discard_state = TRUE)
expect_true(is.null(learner3$state))
expect_null(learner3$state)
})
2 changes: 1 addition & 1 deletion tests/testthat/test_encapsulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test_that("evaluate / single step", {
expect_data_table(log, nrows = 2L, ncols = 3L, any.missing = FALSE)
expect_factor(log$class)
expect_set_equal(as.character(log$class), c("output", "warning"))
expect_true(all(grepl("->train()", log$msg, fixed = TRUE)))
expect_match(log$msg, "->train()", fixed = TRUE)
expect_true("output" %in% log$class)
expect_true("warning" %in% log$class)
expect_false("error" %in% log$class)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_lgr.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ test_that("log to text file", {
lines = readLines(f)
expect_true(any(startsWith(lines, "INFO")))
expect_true(any(startsWith(lines, "DEBUG")))
expect_true(any(grepl("'iris'", lines, fixed = TRUE)))
expect_true(any(grepl("'classif.featureless'", lines, fixed = TRUE)))
expect_match(lines, "'iris'", fixed = TRUE, all = FALSE)
expect_match(lines, "'classif.featureless'", lines, fixed = TRUE, all = FALSE)
})
2 changes: 1 addition & 1 deletion tests/testthat/test_marshal.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test_that("marshal count works for LearnerClassifDebug", {

learner2 = lrn("classif.debug", count_marshaling = FALSE)
learner2$train(task)
expect_true(is.null(learner2$model$marshal_count))
expect_null(learner2$model$marshal_count)
model1 = learner2$model
model2 = learner2$marshal()$unmarshal()$model
expect_equal(model1, model2)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_resample.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ test_that("marshaling works when store_models is FALSE", {
resample(task, learner, resampling, store_models = FALSE, unmarshal = TRUE)
})
expect_resample_result(rr)
expect_true(is.null(rr$learners[[1]]$model))
expect_null(rr$learners[[1]]$model)

rr1 = with_future(future::sequential, {
resample(task, learner, resampling, store_models = FALSE, unmarshal = TRUE)
})
expect_resample_result(rr1)
expect_true(is.null(rr1$learners[[1]]$model))
expect_null(rr1$learners[[1]]$model)
})


Expand Down

0 comments on commit 9d1314a

Please sign in to comment.