Debian Codegraph @

tests/testthat/test_LayoutJson.R

raw
1 # LayoutJson --------------------------------------------------------------
2
3 test_that("LayoutJson works as expected", {
4 lo <- LayoutJson$new()
5
6 event <- LogEvent$new(
7 logger = Logger$new("dummy"),
8 level = 200L,
9 timestamp = as.POSIXct(1541175573.9308, origin = "1970-01-01", tz = "UTC"),
10 caller = NA_character_,
11 msg = "foo bar",
12 rawMsg = "foo raw",
13 customField = "hashbaz",
14 customField2 = "barfoo",
15 foo = "bar"
16 )
17
18 eres <- event$values
19 json <- lo$format_event(event)
20 tres <- jsonlite::fromJSON(json)
21
22 expected_values <- c("level", "timestamp", "logger", "caller", "msg", "foo", "customField", "customField2") # rawMsg is excluded by default
23
24 tres[sapply(tres, is.null)] <- NA_character_
25 expect_setequal(expected_values, names(tres))
26 expect_identical(tres[["level"]], eres[["level"]])
27 expect_identical(tres[["msg"]], eres[["msg"]])
28 expect_identical(tres[["caller"]], eres[["caller"]])
29 expect_identical(tres[["customField"]], eres[["customField"]])
30 expect_identical(tres[["customField"]], eres[["customField"]])
31 expect_equal(as.POSIXct(tres[["timestamp"]], tz = "UTC"), eres[["timestamp"]], tolerance = 1)
32 expect_equal(tres[["foo"]], "bar")
33 })
34
35
36 test_that("LayoutJson `excluded_fields` works as expected", {
37 lo <- LayoutJson$new(excluded_fields = c("customField", "customField2"))
38
39 event <- LogEvent$new(
40 logger = Logger$new("dummy"),
41 level = 200L,
42 timestamp = as.POSIXct(1541175573.9308, origin = "1970-01-01", tz = "UTC"),
43 caller = NA_character_,
44 msg = "foo bar",
45 rawMsg = "foo raw",
46 customField = "hashbaz",
47 customField2 = "barfoo",
48 foo = "bar"
49 )
50
51 eres <- event$values
52 json <- lo$format_event(event)
53 tres <- jsonlite::fromJSON(json)
54
55 expected_values <- c("level", "timestamp", "logger", "caller", "msg", "foo", "rawMsg")
56
57 tres[sapply(tres, is.null)] <- NA_character_
58 expect_setequal(expected_values, names(tres))
59 expect_identical(tres[["level"]], eres[["level"]])
60 expect_identical(tres[["msg"]], eres[["msg"]])
61 expect_identical(tres[["caller"]], eres[["caller"]])
62 expect_equal(as.POSIXct(tres[["timestamp"]], tz = "UTC"), eres[["timestamp"]], tolerance = 1)
63 expect_equal(tres[["foo"]], "bar")
64 expect_equal(tres[["rawMsg"]], "foo raw")
65 })
66
67
68 test_that("formatting timestamps with LayoutJson works", {
69 lo <- LayoutJson$new()
70
71 event <- LogEvent$new(
72 logger = Logger$new("dummy"),
73 level = 200L,
74 timestamp = as.POSIXct(1541175573.9308, origin = "1970-01-01", tz = "UTC"),
75 caller = NA_character_,
76 msg = "foo bar",
77 rawMsg = "foo raw",
78 customField = "hashbaz",
79 customField2 = "barfoo"
80 )
81
82 eres <- event$values
83 json <- lo$format_event(event)
84
85 lo$set_timestamp_fmt(function(event) "fmt timestamp with function")
86 expect_match(lo$format_event(event), "fmt timestamp with function")
87
88 lo$set_timestamp_fmt("%H:%M:%S..%OS")
89 expect_match(lo$format_event(event), format(event$timestamp, "%H:%M:%S..%OS"))
90 })
91
92
93 test_that("LayoutJson.format_event() - with field rename function - renames fields correctly", {
94
95 # Arrange
96 event <- LogEvent$new(
97 logger = Logger$new("dum/my"),
98 level = 200L,
99 timestamp = structure(1541175573.9308, class = c("POSIXct", "POSIXt")),
100 caller = NA_character_,
101 msg = "foo bar",
102 rawMsg = "foobar-raw"
103 )
104
105 lo <- LayoutJson$new(
106 transform_event_names = toupper,
107 excluded_fields = c("RAWMSG", "CALLER")
108 )
109
110 # Act
111 res <- jsonlite::fromJSON(lo$format_event(event))
112
113 # Assert
114 expect_setequal(
115 names(res),
116 c("LOGGER", "LEVEL", "TIMESTAMP", "MSG"))
117
118 expect_identical(res$MSG, "foo bar")
119 expect_identical(res$LEVEL, 200L)
120 expect_identical(res$LOGGER, "dum/my")
121 })