Debian Codegraph @

tests/testthat/test_LogEvent.R

raw
1 context("LogEvent")
2
3
4 test_that("LogEvent can have custom fields", {
5 l <- Logger$new("l")
6
7 expect_output(l$log(100, "blubb", user_agent = "007"))
8 expect_identical(l$last_event$values$user_agent, "007")
9
10 expect_output(
11 l$info("blubb %s -", "blah", user_agent = "008"),
12 "blubb blah"
13 )
14
15 expect_identical(l$last_event$values$user_agent, "008")
16 expect_identical(l$last_event$values$msg, "blubb blah -")
17 })
18
19
20
21
22 test_that("LogEvents preserves field order", {
23 l <- Logger$new("l", propagate = FALSE)
24
25 l$fatal("test", c = "1", a = "2", b = "3")
26
27 # Order depends on the internal implementation of environments I guess...
28 # let's see if this will break one day.
29 expect_identical(names(l$last_event)[1:3], c("c", "a", "b"))
30 })
31
32
33
34
35
36
37 # as.data.frame/table/tibble ---------------------------------------------
38
39 as_funs <- list(
40 as.data.frame = as.data.frame.LogEvent,
41 as.data.table = as.data.table.LogEvent,
42 as_tibble = as_tibble.LogEvent
43 )
44
45 # nm <- "as.data.table"
46
47 for (nm in names(as_funs)){
48
49 l <- Logger$new("l", propagate = FALSE)
50
51 test_that(paste0(nm, "() works as expected"), {
52 l$fatal("test", df = iris, root_logger = lgr)
53 res <- as_funs[[nm]](l$last_event)
54
55 expect_identical(nrow(res), 1L)
56 expect_true(is.data.frame(res$df[[1]]))
57 expect_identical(res$root_logger[[1]], lgr)
58 expect_identical(nrow(res$df[[1]]), 150L)
59 })
60
61
62 test_that(paste0(nm, "() vectorizes over msg"), {
63 l <- Logger$new("l", propagate = FALSE)
64 l$fatal(c("test", "test2"), letters = letters, df = iris, root_logger = lgr)
65
66 res <- as_funs[[nm]](l$last_event)
67
68 expect_identical(nrow(res), 2L)
69 expect_true(is.data.frame(res$df[[1]]))
70 expect_true(is.data.frame(res$df[[2]]))
71 expect_true(is.character(res$letters[[1]]))
72 expect_true(is.character(res$letters[[2]]))
73 expect_identical(nrow(res$df[[1]]), 150L)
74 expect_identical(nrow(res$df[[2]]), 150L)
75 })
76 }
77
78
79
80
81
82 test_that("as_LogEvent.list and as_LogEvent.data.frame work", {
83
84 ts <- Sys.time()
85
86 l <- list(
87 level = 100,
88 msg = "blah",
89 timestamp = ts,
90 caller = NA,
91 foo = "bar"
92 )
93
94 Sys.sleep(1)
95 expect_identical(as_LogEvent(l)$timestamp, l$timestamp)
96 expect_s3_class(as_LogEvent(l), "LogEvent")
97 expect_s3_class(as_LogEvent(as.data.frame(l)), "LogEvent")
98
99
100 l <- list(
101 level = 100,
102 msg = "blah",
103 `@timestamp` = ts,
104 caller = NA,
105 foo = "bar"
106 )
107
108 expect_identical(as_LogEvent(l)[["timestamp"]], l[["@timestamp"]])
109 expect_s3_class(as_LogEvent(l), "LogEvent")
110 expect_s3_class(as_LogEvent(as.data.frame(l)), "LogEvent")
111 })