Debian Codegraph @

tests/testthat/test-otel-shiny.R

raw
1 # Tests for otel-shiny.R functions
2
3 test_that("otel_tracer_name constant is correct", {
4 expect_equal(otel_tracer_name, "co.posit.r-package.shiny")
5 })
6
7
8 test_that("start_otel_span calls otel::start_span with correct parameters", {
9 mock_tracer <- create_mock_tracer()
10 mock_span <- create_mock_otel_span()
11 start_span_called <- FALSE
12
13 local_mocked_bindings(
14 start_span = function(name, ..., tracer = NULL) {
15 start_span_called <<- TRUE
16 expect_equal(name, "test_span")
17 expect_equal(tracer, mock_tracer)
18 mock_span
19 },
20 .package = "otel"
21 )
22 local_mocked_bindings(
23 shiny_otel_tracer = function() mock_tracer,
24 )
25
26 result <- start_otel_span("test_span", extra_param = "value")
27
28 expect_true(start_span_called)
29 expect_equal(result, mock_span)
30 })
31
32 test_that("is_otel_span correctly identifies otel spans", {
33 # Test with otel_span object
34 otel_span <- create_mock_otel_span()
35 expect_true(is_otel_span(otel_span))
36
37 # Test with non-otel objects
38 expect_false(is_otel_span("string"))
39 expect_false(is_otel_span(123))
40 expect_false(is_otel_span(list()))
41 expect_false(is_otel_span(NULL))
42
43 # Test with object that has different class
44 other_obj <- structure(list(), class = "other_class")
45 expect_false(is_otel_span(other_obj))
46 })
47
48 test_that("testthat__is_testing detects testing environment", {
49 # Test when TESTTHAT env var is set to "true"
50 withr::with_envvar(list(TESTTHAT = "true"), {
51 expect_true(testthat__is_testing())
52 })
53
54 # Test when TESTTHAT env var is not set
55 withr::with_envvar(list(TESTTHAT = NA), {
56 expect_false(testthat__is_testing())
57 })
58
59 # Test when TESTTHAT env var is set to other values
60 withr::with_envvar(list(TESTTHAT = "false"), {
61 expect_false(testthat__is_testing())
62 })
63
64 withr::with_envvar(list(TESTTHAT = ""), {
65 expect_false(testthat__is_testing())
66 })
67 })
68
69 test_that("otel_log calls otel::log with correct parameters", {
70 mock_logger <- create_mock_logger()
71 log_called <- FALSE
72
73 local_mocked_bindings(
74 log = function(msg, ..., severity = NULL, logger = NULL) {
75 log_called <<- TRUE
76 expect_equal(msg, "test message")
77 expect_equal(severity, "warn")
78 expect_equal(logger, mock_logger)
79 },
80 .package = "otel"
81 )
82 local_mocked_bindings(
83 shiny_otel_logger = function() mock_logger,
84 )
85
86 otel_log("test message", severity = "warn")
87 expect_true(log_called)
88 })
89
90 test_that("otel_log uses default severity and logger", {
91 mock_logger <- create_mock_logger()
92 log_called <- FALSE
93
94 local_mocked_bindings(
95 log = function(msg, ..., severity = NULL, logger = NULL) {
96 log_called <<- TRUE
97 expect_equal(msg, "default test")
98 expect_equal(severity, "info") # Default severity
99 expect_equal(logger, mock_logger) # Default logger
100 },
101 .package = "otel"
102 )
103 local_mocked_bindings(
104 shiny_otel_logger = function() mock_logger,
105 )
106
107 otel_log("default test")
108 expect_true(log_called)
109 })
110
111
112 test_that("integration test - start_otel_span with custom parameters", {
113 mock_tracer <- create_mock_tracer()
114 mock_span <- create_mock_otel_span()
115 start_span_params <- list()
116
117 local_mocked_bindings(
118 start_span = function(name, ..., tracer = NULL) {
119 start_span_params <<- list(
120 name = name,
121 tracer = tracer,
122 extra_args = list(...)
123 )
124 mock_span
125 },
126 .package = "otel"
127 )
128 local_mocked_bindings(
129 shiny_otel_tracer = function() mock_tracer,
130 )
131
132 result <- start_otel_span(
133 "custom_span",
134 attributes = list(key = "value"),
135 parent = "parent_span"
136 )
137
138 expect_equal(result, mock_span)
139 expect_equal(start_span_params$name, "custom_span")
140 expect_equal(start_span_params$tracer, mock_tracer)
141 expect_equal(start_span_params$extra_args$attributes, list(key = "value"))
142 expect_equal(start_span_params$extra_args$parent, "parent_span")
143 })