1library(testthat) 2library(recipes) 3library(dplyr) 4 5 6set_with_na <- tibble( 7 a = c(1, 2, NA), 8 b = c(1, 2, NA_real_), 9 d = as.integer(c(1, 2, NA_integer_)), 10 e = c(1, 2, NA_character_) 11) 12 13tst <- function(...) { 14 cols <- quos(...) 15 recipe(set_with_na) %>% check_missing(!!!cols) %>% 16 prep() %>% bake(set_with_na) 17} 18 19test_that("check_missing passes silently when no NA", { 20 no_na_rp <- recipe(mtcars) %>% 21 check_missing(all_numeric()) %>% 22 prep() 23 expect_error(bake(no_na_rp, mtcars), NA) 24 expect_equal(bake(no_na_rp, mtcars), tibble(mtcars)) 25}) 26 27test_that("check_missing throws error on all types", { 28 expect_error(tst(a), 29 "The following columns contain missing values: `a`.") 30 expect_error(tst(b), 31 "The following columns contain missing values: `b`.") 32 expect_error(tst(d), 33 "The following columns contain missing values: `d`.") 34 expect_error(tst(e), 35 "The following columns contain missing values: `e`.") 36}) 37 38test_that("check_missing works on multiple columns simultaneously" ,{ 39 expect_error(tst(a, e), 40 "The following columns contain missing values: `a`, `e`.") 41 expect_error(tst(everything()), 42 paste0("The following columns contain missing values: ", 43 "`a`, `b`, `d`, `e`.")) 44}) 45 46test_that("check_missing on a new set", { 47 no_na <- tibble(a = 1:3) 48 na <- tibble(a = c(1, NA)) 49 rp <- recipe(no_na) %>% check_missing(a) %>% prep(no_na) 50 expect_error(bake(rp, na), 51 "The following columns contain missing values: `a`.") 52}) 53