1library(testthat)
2library(recipes)
3library(dplyr)
4
5# ------------------------------------------------------------------------------
6
7iris_rec <- recipe( ~ ., data = iris)
8
9# ------------------------------------------------------------------------------
10
11test_that('basic usage', {
12  rec <-
13    iris_rec %>%
14    step_arrange(desc(Sepal.Length), 1/Petal.Length)
15
16  prepped <- prep(rec, training = iris %>% slice(1:75))
17
18  dplyr_train <-
19    iris %>%
20    as_tibble() %>%
21    slice(1:75) %>%
22    dplyr::arrange(desc(Sepal.Length), 1/Petal.Length)
23
24  rec_train <- juice(prepped)
25  expect_equal(dplyr_train, rec_train)
26
27  dplyr_test <-
28    iris %>%
29    as_tibble() %>%
30    slice(76:150) %>%
31    dplyr::arrange(desc(Sepal.Length), 1/Petal.Length)
32  rec_test <- bake(prepped, iris %>% slice(76:150))
33  expect_equal(dplyr_test, rec_test)
34})
35
36test_that('quasiquotation', {
37  sort_vars <- c("Sepal.Length", "Petal.Length")
38  sort_vars <- syms(sort_vars)
39  rec_1 <-
40    iris_rec %>%
41    step_arrange(!!!sort_vars)
42
43  prepped_1 <- prep(rec_1, training = iris %>% slice(1:75))
44
45  dplyr_train <-
46    iris %>%
47    as_tibble() %>%
48    slice(1:75) %>%
49    arrange(Sepal.Length, Petal.Length)
50
51  rec_1_train <- juice(prepped_1)
52  expect_equal(dplyr_train, rec_1_train)
53
54})
55
56test_that('no input', {
57  no_inputs <-
58    iris_rec %>%
59    step_arrange() %>%
60    prep(training = iris) %>%
61    juice(composition = "data.frame")
62  expect_equal(no_inputs, iris)
63})
64
65test_that('printing', {
66  rec <- iris_rec %>% step_arrange(Sepal.Length)
67  expect_output(print(rec))
68  expect_output(prep(rec, training = iris, verbose = TRUE))
69})
70
71