-
Notifications
You must be signed in to change notification settings - Fork 5
/
formatter_test.go
103 lines (85 loc) · 3.79 KB
/
formatter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package log
import (
"encoding/json"
"reflect"
"testing"
)
func TestFixFieldsConflict(t *testing.T) {
m := map[string]interface{}{
"request_id": "request_id",
"time": "time",
"field.time": "field.time",
"level": "level",
"field.level": "field.level",
"field.level.2": "field.level.2",
}
fixFieldsConflict(m, []string{"request_id", "field.level", "field.level.3"})
want := map[string]interface{}{
"field.request_id": "request_id",
"field.time.2": "time",
"field.time": "field.time",
"field.field.level.3": "level",
"field.field.level": "field.level",
"field.level.2": "field.level.2",
}
if !reflect.DeepEqual(m, want) {
t.Errorf("\nhave:%v\nwant:%v", m, want)
return
}
}
func TestFixFieldsConflictAndHandleErrorFields(t *testing.T) {
var nilErr error
fields := map[string]interface{}{
"user_id": 123456, // integer
"name": "jack", // string
"nil_error": nilErr, // nil error
"error": testError{}, // error
"context_error1": testContextError1{}, // error with ErrorContext
"context_error2": testContextError2{}, // error with ErrorContextJSON
"context_error3": testContextError3{}, // error with ErrorContext and ErrorContextJSON
"context_not_error": testContextWithoutError{X: "test"}, // not error with ErrorContext and ErrorContextJSON
"context_error1_context": "context_error1_context_value", // conflict with context_error1.context
}
fixFieldsConflictAndHandleErrorFields(fields)
want := map[string]interface{}{
"user_id": 123456,
"name": "jack",
"nil_error": nil,
"error": "test_error_123456789",
"context_error1": "context_error1_error_123456789",
"context_error1_context": "context_error1_context_123456789",
"context_error2": "context_error2_error_123456789",
"context_error2_context": json.RawMessage(`{"key":"context_error2_context_json_123456789"}`),
"context_error3": "context_error3_error_123456789",
"context_error3_context": json.RawMessage(`{"key":"context_error3_context_json_123456789"}`),
"context_not_error": testContextWithoutError{X: "test"},
"field.context_error1_context": "context_error1_context_value",
}
if !reflect.DeepEqual(fields, want) {
t.Errorf("\nhave:%v\nwant:%v", fields, want)
return
}
}
type testError struct{}
func (testError) Error() string { return "test_error_123456789" }
type testContextError1 struct{}
func (testContextError1) Error() string { return "context_error1_error_123456789" }
func (testContextError1) ErrorContext() string { return "context_error1_context_123456789" }
type testContextError2 struct{}
func (testContextError2) Error() string { return "context_error2_error_123456789" }
func (testContextError2) ErrorContextJSON() json.RawMessage {
return []byte(`{"key":"context_error2_context_json_123456789"}`)
}
type testContextError3 struct{}
func (testContextError3) Error() string { return "context_error3_error_123456789" }
func (testContextError3) ErrorContext() string { return "context_error3_context_123456789" }
func (testContextError3) ErrorContextJSON() json.RawMessage {
return []byte(`{"key":"context_error3_context_json_123456789"}`)
}
type testContextWithoutError struct {
X string `json:"x"`
}
func (testContextWithoutError) ErrorContext() string { return "context_not_error_context_123456789" }
func (testContextWithoutError) ErrorContextJSON() json.RawMessage {
return []byte(`{"key":"context_not_error_context_json_123456789"}`)
}