Debian Codegraph @

tests/testthat/test-otel-with.R

raw
1 test_that("withOtelCollect sets collection level temporarily", {
2 # Save original option
3 original <- getOption("shiny.otel.collect")
4 on.exit(options(shiny.otel.collect = original), add = TRUE)
5
6 # Set a baseline option
7 options(shiny.otel.collect = "all")
8
9 # Test that withOtelCollect temporarily changes the option
10 result <- withOtelCollect("none", {
11 getOption("shiny.otel.collect")
12 })
13
14 expect_equal(result, "none")
15
16 # Verify option is restored after expression
17 expect_equal(getOption("shiny.otel.collect"), "all")
18 })
19
20 test_that("withOtelCollect returns value of expr", {
21 result <- withOtelCollect("none", {
22 42
23 })
24
25 expect_equal(result, 42)
26
27 # Test with more complex return value
28 result <- withOtelCollect("reactivity", {
29 list(a = 1, b = "test")
30 })
31
32 expect_equal(result, list(a = 1, b = "test"))
33 })
34
35 test_that("withOtelCollect validates collect level", {
36 expect_error(
37 withOtelCollect("invalid", { 1 }),
38 "'arg' should be one of"
39 )
40
41 expect_error(
42 withOtelCollect(123, { 1 }),
43 "`collect` must be a character vector"
44 )
45
46 expect_error(
47 withOtelCollect(c("all", "none"), { 1 }),
48 "'arg' must be of length 1"
49 )
50 })
51
52 test_that("withOtelCollect rejects session and reactive_update levels", {
53 expect_error(
54 withOtelCollect("session", { 1 }),
55 "'arg' should be one of"
56 )
57
58 expect_error(
59 withOtelCollect("reactive_update", { 1 }),
60 "'arg' should be one of"
61 )
62 })
63
64 test_that("withOtelCollect works with all valid collect levels", {
65 for (level in c("none", "reactivity", "all")) {
66 result <- withOtelCollect(level, {
67 getOption("shiny.otel.collect")
68 })
69 expect_equal(result, level)
70 }
71 })
72
73 test_that("withOtelCollect nests correctly", {
74 original <- getOption("shiny.otel.collect")
75 on.exit(options(shiny.otel.collect = original), add = TRUE)
76
77 options(shiny.otel.collect = "all")
78
79 result <- withOtelCollect("reactivity", {
80 outer <- getOption("shiny.otel.collect")
81 inner <- withOtelCollect("none", {
82 getOption("shiny.otel.collect")
83 })
84 restored <- getOption("shiny.otel.collect")
85
86 list(outer = outer, inner = inner, restored = restored)
87 })
88
89 expect_equal(result$outer, "reactivity")
90 expect_equal(result$inner, "none")
91 expect_equal(result$restored, "reactivity")
92 expect_equal(getOption("shiny.otel.collect"), "all")
93 })
94
95 test_that("withOtelCollect restores option even on error", {
96 original <- getOption("shiny.otel.collect")
97 on.exit(options(shiny.otel.collect = original), add = TRUE)
98
99 options(shiny.otel.collect = "all")
100
101 expect_error(
102 withOtelCollect("none", {
103 stop("test error")
104 }),
105 "test error"
106 )
107
108 # Option should still be restored
109 expect_equal(getOption("shiny.otel.collect"), "all")
110 })
111
112 test_that("localOtelCollect sets collection level in function scope", {
113 original <- getOption("shiny.otel.collect")
114 on.exit(options(shiny.otel.collect = original), add = TRUE)
115
116 options(shiny.otel.collect = "all")
117
118 test_func <- function() {
119 localOtelCollect("none")
120 getOption("shiny.otel.collect")
121 }
122
123 result <- test_func()
124 expect_equal(result, "none")
125
126 # Option should be restored after function exits
127 expect_equal(getOption("shiny.otel.collect"), "all")
128 })
129
130 test_that("localOtelCollect returns previous collect value invisibly", {
131 original <- getOption("shiny.otel.collect")
132 on.exit(options(shiny.otel.collect = original), add = TRUE)
133
134 options(shiny.otel.collect = "all")
135
136 result <- withVisible(localOtelCollect("none"))
137
138 # Should return a list with the old option value
139 expect_type(result$value, "list")
140 expect_equal(result$value$shiny.otel.collect, "all")
141 expect_false(result$visible)
142 })
143
144 test_that("localOtelCollect validates collect level", {
145 expect_error(
146 localOtelCollect("invalid"),
147 "'arg' should be one of"
148 )
149
150 expect_error(
151 localOtelCollect(NULL),
152 "`collect` must be a character vector"
153 )
154
155 expect_error(
156 localOtelCollect(c("all", "none")),
157 "'arg' must be of length 1"
158 )
159 })
160
161 test_that("localOtelCollect rejects session and reactive_update levels", {
162 expect_error(
163 localOtelCollect("session"),
164 "'arg' should be one of"
165 )
166
167 expect_error(
168 localOtelCollect("reactive_update"),
169 "'arg' should be one of"
170 )
171 })
172
173 test_that("localOtelCollect works with all valid collect levels", {
174 for (level in c("none", "reactivity", "all")) {
175 test_func <- function() {
176 localOtelCollect(level)
177 getOption("shiny.otel.collect")
178 }
179 result <- test_func()
180 expect_equal(result, level)
181 }
182 })
183
184 test_that("localOtelCollect respects envir parameter", {
185 original <- getOption("shiny.otel.collect")
186 on.exit(options(shiny.otel.collect = original), add = TRUE)
187
188 options(shiny.otel.collect = "all")
189
190 outer_func <- function() {
191 env <- environment()
192
193 inner_func <- function() {
194 localOtelCollect("none", envir = env)
195 }
196
197 inner_func()
198 getOption("shiny.otel.collect")
199 }
200
201 result <- outer_func()
202 expect_equal(result, "none")
203 expect_equal(getOption("shiny.otel.collect"), "all")
204 })
205
206 test_that("localOtelCollect scope is limited to function", {
207 original <- getOption("shiny.otel.collect")
208 on.exit(options(shiny.otel.collect = original), add = TRUE)
209
210 options(shiny.otel.collect = "all")
211
212 func1 <- function() {
213 localOtelCollect("reactivity")
214 getOption("shiny.otel.collect")
215 }
216
217 func2 <- function() {
218 localOtelCollect("none")
219 getOption("shiny.otel.collect")
220 }
221
222 result1 <- func1()
223 result2 <- func2()
224
225 expect_equal(result1, "reactivity")
226 expect_equal(result2, "none")
227 expect_equal(getOption("shiny.otel.collect"), "all")
228 })
229
230 test_that("withOtelCollect and localOtelCollect work together", {
231 original <- getOption("shiny.otel.collect")
232 on.exit(options(shiny.otel.collect = original), add = TRUE)
233
234 options(shiny.otel.collect = "all")
235
236 result <- withOtelCollect("reactivity", {
237 outer <- getOption("shiny.otel.collect")
238
239 test_func <- function() {
240 localOtelCollect("none")
241 getOption("shiny.otel.collect")
242 }
243
244 inner <- test_func()
245 restored <- getOption("shiny.otel.collect")
246
247 list(outer = outer, inner = inner, restored = restored)
248 })
249
250 expect_equal(result$outer, "reactivity")
251 expect_equal(result$inner, "none")
252 expect_equal(result$restored, "reactivity")
253 expect_equal(getOption("shiny.otel.collect"), "all")
254 })
255
256 test_that("withOtelCollect affects otel_collect_is_enabled", {
257 # This tests integration with the otel collection system
258 original <- getOption("shiny.otel.collect")
259 on.exit(options(shiny.otel.collect = original), add = TRUE)
260
261 options(shiny.otel.collect = "all")
262
263 # With "none", nothing except "none" should be enabled
264 result <- withOtelCollect("none", {
265 list(
266 none = otel_collect_is_enabled("none"),
267 session = otel_collect_is_enabled("session"),
268 reactivity = otel_collect_is_enabled("reactivity")
269 )
270 })
271
272 expect_true(result$none)
273 expect_false(result$session)
274 expect_false(result$reactivity)
275
276 # With "reactivity", reactivity and below should be enabled, but not "all"
277 result <- withOtelCollect("reactivity", {
278 list(
279 none = otel_collect_is_enabled("none"),
280 session = otel_collect_is_enabled("session"),
281 reactive_update = otel_collect_is_enabled("reactive_update"),
282 reactivity = otel_collect_is_enabled("reactivity"),
283 all = otel_collect_is_enabled("all")
284 )
285 })
286
287 expect_true(result$none)
288 expect_true(result$session)
289 expect_true(result$reactive_update)
290 expect_true(result$reactivity)
291 expect_false(result$all)
292 })
293
294 test_that("localOtelCollect affects otel_collect_is_enabled", {
295 original <- getOption("shiny.otel.collect")
296 on.exit(options(shiny.otel.collect = original), add = TRUE)
297
298 options(shiny.otel.collect = "all")
299
300 test_func <- function() {
301 localOtelCollect("reactivity")
302 list(
303 session = otel_collect_is_enabled("session"),
304 reactive_update = otel_collect_is_enabled("reactive_update"),
305 reactivity = otel_collect_is_enabled("reactivity"),
306 all = otel_collect_is_enabled("all")
307 )
308 }
309
310 result <- test_func()
311
312 expect_true(result$session)
313 expect_true(result$reactive_update)
314 expect_true(result$reactivity)
315 expect_false(result$all)
316 })