Skip to content

Commit 4dfcf71

Browse files
committed
update tests for new excludedFields
1 parent a04f8cf commit 4dfcf71

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

tests/testthat/test_Layout.R

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ tevent <- LogEvent$new(
77
timestamp = as.POSIXct(1541175573.9308, origin = "1970-01-01", tz = "UTC"),
88
caller = NA_character_,
99
msg = "foo bar",
10-
rawMsg = "foo raw"
10+
rawMsg = "foo raw",
11+
customField = "hashbaz",
12+
customField2 = "barfoo"
1113
)
1214

1315

@@ -31,6 +33,26 @@ test_that("LayoutFormat works as expected", {
3133
})
3234

3335

36+
test_that("LayoutFormat works as expected with custom fields", {
37+
lo <- LayoutFormat$new("%r %j")
38+
expect_identical(lo$format_event(tevent), "foo raw {\"customField\":\"hashbaz\",\"customField2\":\"barfoo\"}")
39+
40+
lo$set_timestamp_fmt("%Y-%m-%d")
41+
lo$set_fmt("[%t]")
42+
expect_identical(lo$format_event(tevent), "[2018-11-02]")
43+
})
44+
45+
46+
test_that("LayoutFormat works as expected with custom fields", {
47+
lo <- LayoutFormat$new("%r %j", excluded_fields = "customField2")
48+
expect_identical(lo$format_event(tevent), "foo raw {\"customField\":\"hashbaz\"}")
49+
50+
lo$set_timestamp_fmt("%Y-%m-%d")
51+
lo$set_fmt("[%t]")
52+
expect_identical(lo$format_event(tevent), "[2018-11-02]")
53+
})
54+
55+
3456

3557
test_that("LayoutGlue works as expected", {
3658
# basic formatting works
@@ -68,20 +90,22 @@ test_that("LayoutJson works as expected", {
6890
json <- lo$format_event(x)
6991
tres <- jsonlite::fromJSON(json)
7092

71-
expected_values <- c("level", "timestamp", "logger", "caller", "msg", "foo") # rawMsg is excluded by default
93+
expected_values <- c("level", "timestamp", "logger", "caller", "msg", "foo", "customField", "customField2" ) # rawMsg is excluded by default
7294

7395
tres[sapply(tres, is.null)] <- NA_character_
7496
expect_setequal(expected_values, names(tres))
7597
expect_identical(tres[["level"]], eres[["level"]])
7698
expect_identical(tres[["msg"]], eres[["msg"]])
7799
expect_identical(tres[["caller"]], eres[["caller"]])
100+
expect_identical(tres[["customField"]], eres[["customField"]])
101+
expect_identical(tres[["customField"]], eres[["customField"]])
78102
expect_equal(as.POSIXct(tres[["timestamp"]], tz = "UTC"), eres[["timestamp"]], tolerance = 1)
79103
expect_equal(tres[["foo"]], "bar")
80104
})
81105

82106

83107
test_that("LayoutJson `excluded_fields` works as expected", {
84-
lo <- LayoutJson$new(excluded_fields = NULL)
108+
lo <- LayoutJson$new(excluded_fields = c("customField", "customField2"))
85109

86110
x <- tevent$clone()
87111
x$foo <- "bar"

0 commit comments

Comments
 (0)