Debian Codegraph @

tests/test-cran/test-cache.R

raw
1 library(testit)
2
3 assert('cache_exec() with disk path caches and retrieves', {
4 d = tempfile(); dir.create(d)
5 path = paste0(d, '/')
6 r1 = cache_exec({Sys.time()}, path = path, id = 'disk-test')
7 Sys.sleep(0.01)
8 r2 = cache_exec({Sys.time()}, path = path, id = 'disk-test')
9 (r1 %==% r2)
10 unlink(d, recursive = TRUE)
11 })
12
13 assert('cache_exec() invalidates when hash changes', {
14 x = 1
15 r1 = cache_exec({x + 1}, path = ':memory:', id = 'hash-change-test')
16 (r1 %==% 2)
17 x = 10
18 r2 = cache_exec({x + 1}, path = ':memory:', id = 'hash-change-test')
19 (r2 %==% 11)
20 })
21
22 assert('cache_exec() with keep = FALSE always re-evaluates', {
23 r1 = cache_exec({runif(1)}, path = ':memory:', id = 'keep-false-test', keep = FALSE)
24 r2 = cache_exec({runif(1)}, path = ':memory:', id = 'keep-false-test', keep = FALSE)
25 (r1 != r2)
26 })
27
28 assert('cache_rds() with hash = "auto" detects global variables', {
29 d = tempfile(); dir.create(d)
30 x = 100
31 r1 = cache_rds(x * 2, file = 'auto-hash.rds', dir = paste0(d, '/'), hash = 'auto')
32 (r1 %==% 200)
33 r2 = cache_rds(x * 2, file = 'auto-hash.rds', dir = paste0(d, '/'), hash = 'auto')
34 (r2 %==% 200)
35 unlink(d, recursive = TRUE)
36 })
37
38 assert('cache_rds() with hash as list invalidates on change', {
39 d = tempfile(); dir.create(d)
40 r1 = cache_rds(1 + 1, file = 'hash-list.rds', dir = paste0(d, '/'), hash = list(a = 1))
41 (r1 %==% 2)
42 r2 = cache_rds(1 + 1, file = 'hash-list.rds', dir = paste0(d, '/'), hash = list(a = 2))
43 (r2 %==% 2)
44 # clean = TRUE removes old cache, so only one file exists
45 (length(list.files(d, pattern = '[.]rds$')) %==% 1L)
46 unlink(d, recursive = TRUE)
47 })
48
49 assert('cache_rds() errors on unsupported file extension', {
50 d = tempfile(); dir.create(d)
51 (has_error(cache_rds(1, file = 'test.xyz', dir = paste0(d, '/'))))
52 unlink(d, recursive = TRUE)
53 })
54
55 assert('cache_dir() returns a character string', {
56 d = cache_dir()
57 (is.character(d))
58 (nchar(d) > 0)
59 })
60
61 assert('find_locals() finds assigned variables', {
62 (find_locals('x = 1; y = 2') %==% c('x', 'y'))
63 (find_locals('for (i in 1:10) {}') %==% 'i')
64 (find_locals('x = 1 +') %==% character(0))
65 })
66
67 assert('find_globals() handles language input', {
68 e = new.env(parent = emptyenv())
69 e$z = 99
70 (find_globals(quote(y <- z + 1), e) %==% 'z')
71 })
72
73 assert('md5() returns consistent checksums', {
74 x1 = 1; x2 = 1:10; x3 = seq(1, 10)
75 m = md5(x1, x2, x3)
76 (length(m) %==% 3L)
77 (is.character(m))
78 (nchar(m) %==% c(32L, 32L, 32L))
79 # x2 and x3 are identical sequences
80 (m[2] %==% m[3])
81 # named arguments
82 mn = md5(a = x1, b = x2)
83 (names(mn) %==% c('a', 'b'))
84 })
85
86 assert('md5_one() produces a 32-character hex string', {
87 h = md5_one(42)
88 (is.character(h) && nchar(h) %==% 32L)
89 # same object same hash
90 (md5_one(42) %==% md5_one(42))
91 # different objects different hashes
92 (md5_one(42) != md5_one(43))
93 })
94
95 assert('cache_exec() with vars argument lazy-loads locals on cache hit', {
96 # first run: populates cache
97 r1 = cache_exec({
98 local_var = 123
99 local_var
100 }, path = ':memory:', id = 'vars-lazy-test', vars = 'local_var')
101 (r1 %==% 123)
102 # second run: hits cache and lazy-loads local_var into the calling env
103 r2 = cache_exec({
104 local_var = 123
105 local_var
106 }, path = ':memory:', id = 'vars-lazy-test', vars = 'local_var')
107 (r2 %==% 123)
108 # local_var should have been lazy-loaded into the current environment
109 (local_var %==% 123)
110 })
111
112 assert('cache_code() cleans up hash dict when path is NULL', {
113 # seed the dict with a fake hash for 'x', then call cache_code with path = NULL
114 assign('x', 'fakehash', envir = .cache_dict)
115 (exists('x', envir = .cache_dict, inherits = FALSE))
116 cache_code(quote(x <- 1), parent.frame(), config = list(path = NULL, vars = 'x'))
117 # the stale entry should have been removed
118 (!exists('x', envir = .cache_dict, inherits = FALSE))
119 })
120
121 assert('find_globals() returns character(0) when no globals exist', {
122 e = new.env(parent = emptyenv())
123 (find_globals('y = 1 + 1', e) %==% character(0))
124 })