Skip to content

Commit

Permalink
Working predict hooks and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertzk committed Aug 2, 2015
1 parent 8a175b0 commit 1adb4ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/tundraContainer-predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ predict <- function(dataframe, predict_args = list(), verbose = FALSE, munge = T

private$run_hooks("predict_pre_munge")
if (isTRUE(munge) && length(self$.munge_procedure) > 0) {
initial_nrow <- NROW(datafram)
initial_nrow <- NROW(dataframe)
dataframe <- munge(dataframe, self$.munge_procedure, verbose)
if (NROW(dataframe) != initial_nrow) {
warning("Some rows were removed during data preparation. ",
Expand All @@ -38,7 +38,7 @@ predict <- function(dataframe, predict_args = list(), verbose = FALSE, munge = T
}
private$run_hooks("predict_post_munge")

if (length(formals(self$.predict_function) < 2 || missing(predict_args))) {
if (length(formals(self$.predict_function)) < 2 || missing(predict_args)) {
args <- list(dataframe)
} else {
args <- list(dataframe, predict_args)
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,25 @@ test_that("it runs the train_finalize hook in the correct order", {
expect_equal(env$effect, c("train", "hook"))
})

test_that("it runs the predict pre-munge hook in the correct order", {
env <- list2env(list(effect = character(0)))
hook <- function() { env$effect <- c(env$effect, "hook") }
container <- container_with_munge_side_effect(env)
container$add_hook("predict_pre_munge", hook)
container$train(iris, munge = FALSE)
expect_equal(env$effect, character(0))
container$predict(iris)
expect_equal(env$effect, c("hook", "mungebit"))
})

test_that("it runs the predict post-munge hook in the correct order", {
env <- list2env(list(effect = character(0)))
hook <- function() { env$effect <- c(env$effect, "hook") }
container <- container_with_munge_side_effect(env)
container$add_hook("predict_post_munge", hook)
container$train(iris, munge = FALSE)
expect_equal(env$effect, character(0))
container$predict(iris)
expect_equal(env$effect, c("mungebit", "hook"))
})

0 comments on commit 1adb4ab

Please sign in to comment.