-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler_test.go
198 lines (182 loc) · 4.92 KB
/
handler_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package slogsentry
import (
"context"
"fmt"
"log/slog"
"testing"
"github.com/getsentry/sentry-go"
)
func TestSentryHandler_Enabled(t *testing.T) {
tests := map[string]struct {
handlerLevel slog.Level
checkLevel slog.Level
expected bool
}{
"LevelDebug, CheckDebug": {
handlerLevel: slog.LevelDebug,
checkLevel: slog.LevelDebug,
expected: true,
},
"LevelInfo, CheckDebug": {
handlerLevel: slog.LevelInfo,
checkLevel: slog.LevelDebug,
expected: false,
},
"LevelError, CheckWarn": {
handlerLevel: slog.LevelError,
checkLevel: slog.LevelWarn,
expected: false,
},
"LevelWarn, CheckError": {
handlerLevel: slog.LevelWarn,
checkLevel: slog.LevelError,
expected: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
h := SentryHandler{option: Option{Level: tt.handlerLevel}}
if got := h.Enabled(context.Background(), tt.checkLevel); got != tt.expected {
t.Errorf("Enabled() = %v, want %v", got, tt.expected)
}
})
}
}
func TestSentryHandler_WithAttrs(t *testing.T) {
tests := map[string]struct {
initialAttrs []slog.Attr
newAttrs []slog.Attr
expected []slog.Attr
}{
"Empty initial attrs": {
initialAttrs: []slog.Attr{},
newAttrs: []slog.Attr{slog.String("key", "value")},
expected: []slog.Attr{slog.String("key", "value")},
},
"Non-empty initial attrs": {
initialAttrs: []slog.Attr{slog.String("existing", "attr")},
newAttrs: []slog.Attr{slog.String("key", "value")},
expected: []slog.Attr{slog.String("existing", "attr"), slog.String("key", "value")},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
h := SentryHandler{attrs: tt.initialAttrs}
newHandler := h.WithAttrs(tt.newAttrs)
if !equalAttrs(newHandler.(*SentryHandler).attrs, tt.expected) {
t.Errorf("WithAttrs() = %+v, want %+v", newHandler.(*SentryHandler).attrs, tt.expected)
}
})
}
}
func TestSentryHandler_WithGroup(t *testing.T) {
tests := map[string]struct {
initialGroups []string
newGroup string
expected []string
}{
"Empty initial groups": {
initialGroups: []string{},
newGroup: "group1",
expected: []string{"group1"},
},
"Non-empty initial groups": {
initialGroups: []string{"existingGroup"},
newGroup: "newGroup",
expected: []string{"existingGroup", "newGroup"},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
h := SentryHandler{groups: tt.initialGroups}
newHandler := h.WithGroup(tt.newGroup)
if !equalStrings(newHandler.(*SentryHandler).groups, tt.expected) {
t.Errorf("WithGroup() = %+v, want %+v", newHandler.(*SentryHandler).groups, tt.expected)
}
})
}
}
func TestOption_NewSentryHandler(t *testing.T) {
tests := map[string]struct {
option Option
expected slog.Handler
}{
"Default options": {
option: Option{},
expected: &SentryHandler{option: Option{Level: slog.LevelDebug, Converter: DefaultConverter, AttrFromContext: []func(ctx context.Context) []slog.Attr{}}},
},
"Custom options": {
option: Option{
Level: slog.LevelInfo,
Converter: CustomConverter,
AttrFromContext: []func(ctx context.Context) []slog.Attr{customAttrFromContext},
},
expected: &SentryHandler{
option: Option{
Level: slog.LevelInfo,
Converter: CustomConverter,
AttrFromContext: []func(ctx context.Context) []slog.Attr{customAttrFromContext},
},
attrs: []slog.Attr{},
groups: []string{},
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := tt.option.NewSentryHandler()
if !compareHandlers(got, tt.expected) {
t.Errorf("NewSentryHandler() = %+v, want %+v", got, tt.expected)
}
})
}
}
func equalAttrs(a, b []slog.Attr) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i].Key != b[i].Key || a[i].String() != b[i].String() {
return false
}
}
return true
}
func equalStrings(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func compareHandlers(h1, h2 slog.Handler) bool {
sh1, ok1 := h1.(*SentryHandler)
sh2, ok2 := h2.(*SentryHandler)
if !ok1 || !ok2 {
return false
}
return sh1.option.Level == sh2.option.Level &&
equalFuncs(sh1.option.AttrFromContext, sh2.option.AttrFromContext)
}
func equalFuncs(a, b []func(ctx context.Context) []slog.Attr) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if fmt.Sprintf("%p", a[i]) != fmt.Sprintf("%p", b[i]) {
return false
}
}
return true
}
// Mock functions for custom converter and custom attr from context.
func CustomConverter(bool, func([]string, slog.Attr) slog.Attr, []slog.Attr, []string, *slog.Record, *sentry.Hub) *sentry.Event {
return sentry.NewEvent()
}
func customAttrFromContext(context.Context) []slog.Attr {
return []slog.Attr{slog.String("custom", "attr")}
}