/
test-mtcars-example.R
103 lines (93 loc) · 3.48 KB
/
test-mtcars-example.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
drake_context("mtcars example")
test_with_dir("mtcars example works", {
scenario <- get_testing_scenario()
e <- eval(parse(text = scenario$envir))
jobs <- scenario$jobs
parallelism <- scenario$parallelism
caching <- scenario$caching
load_mtcars_example(envir = e)
my_plan <- e$my_plan
config <- drake_config(my_plan, envir = e,
jobs = jobs, parallelism = parallelism,
verbose = FALSE, caching = caching)
expect_false(file.exists("Makefile"))
dats <- c("small", "large")
config$targets <- dats
con <- testrun(config)
expect_true(is.list(dependency_profile(
target = "small", config = con)))
expect_equal(parallelism == "Makefile", file.exists("Makefile"))
expect_equal(sort(justbuilt(con)), sort(dats))
remove_these <- intersect(dats, ls(config$envir))
rm(list = remove_these, envir = config$envir)
config$targets <- config$plan$target
con <- testrun(config)
jb <- justbuilt(con)
expect_true("report" %in% jb)
expect_false(any(dats %in% jb))
# Check that file is not rehashed.
# Code coverage should cover every line of file_hash().
expect_true(is.character(file_hash(
target = "\"report.Rmd\"", config = con, size_cutoff = -1)))
config <- drake_config(
my_plan, envir = e, jobs = jobs, parallelism = parallelism,
verbose = FALSE)
expect_equal(outdated(config), character(0))
e$reg2 <- function(d) {
d$x3 <- d$x ^ 3
lm(y ~ x3, data = d)
}
config <- drake_config(
my_plan, envir = e, jobs = jobs, parallelism = parallelism,
verbose = FALSE)
expect_equal(
sort(outdated(config = config)),
sort(c("report", "coef_regression2_large",
"coef_regression2_small", "regression2_large", "regression2_small",
"summ_regression2_large", "summ_regression2_small")))
testrun(config)
config <- drake_config(
my_plan, envir = e, jobs = jobs, parallelism = parallelism,
verbose = FALSE)
expect_equal(sort(outdated(config = config)),
character(0))
# Take this opportunity to test tidyselect API. Saves test time that way.
# loadd() # nolint
e <- new.env(parent = globalenv())
coefs <- sort(c("coef_regression1_large", "coef_regression1_small",
"coef_regression2_large", "coef_regression2_small"))
loadd(starts_with("coef"), envir = e)
expect_equal(sort(ls(envir = e)), coefs)
# build_times() # nolint
skip_if_not_installed("lubridate")
all_times <- build_times()
expect_true(nrow(all_times) >= nrow(config$plan))
some_times <- build_times(starts_with("coef"))
expect_equal(sort(some_times$item), coefs)
# clean() # nolint
x <- sort(cached())
expect_true(all(coefs %in% x))
clean(starts_with("coef"))
expect_equal(sort(cached()), setdiff(x, coefs))
# knitr file deps
# Included here instead of test-knitr.R because report.md already exists.
# Saves time that way.
# But we can remove it all when we deprecate the single-quoted stuff
# and users know to rely on knitr_input().
x <- drake_plan(
a = knitr::knit(knitr_in("report.Rmd")), # nolint
b = knitr::knit(knitr_in("report.md")), # nolint
c = knitr::knit("nonfile"),
d = rmarkdown::render("report.Rmd"), # nolint
e = rmarkdown::render("report.md"), # nolint
f = rmarkdown::render("nonfile"),
strings_in_dots = "literals"
)
suppressWarnings(con <- drake_config(plan = x))
for (target in c("a")){
expect_true("small" %in% dependencies(targets = target, config = con))
}
for (target in c("b", "c")){
expect_false("small" %in% dependencies(targets = target, config = con))
}
})