forked from getsentry/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal_test.go
203 lines (194 loc) · 4.39 KB
/
marshal_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
199
200
201
202
203
package sentry
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
var (
goReleaseDate = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
utcMinusTwo = time.FixedZone("UTC-2", -2*60*60)
)
func TestMarshalJSON(t *testing.T) {
tests := []struct {
in interface{}
out string
}{
// TODO: eliminate empty struct fields from serialization of empty event.
// Only *Event implements json.Marshaler.
// {Event{}, `{"sdk":{},"user":{}}`},
{&Event{}, `{"sdk":{},"user":{}}`},
// Only *Breadcrumb implements json.Marshaler.
// {Breadcrumb{}, `{}`},
{&Breadcrumb{}, `{}`},
}
for _, tt := range tests {
tt := tt
t.Run("", func(t *testing.T) {
want := tt.out
b, err := json.Marshal(tt.in)
if err != nil {
t.Fatal(err)
}
got := string(b)
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("JSON serialization mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestErrorEventMarshalJSON(t *testing.T) {
tests := []*Event{
{
Message: "test",
Timestamp: goReleaseDate,
},
{
Message: "test",
Timestamp: goReleaseDate.In(utcMinusTwo),
},
{
Message: "test",
},
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
for i, tt := range tests {
i, tt := i, tt
t.Run("", func(t *testing.T) {
defer buf.Reset()
err := enc.Encode(tt)
if err != nil {
t.Fatal(err)
}
path := filepath.Join("testdata", "json", "event", fmt.Sprintf("%03d.json", i))
if *update {
WriteGoldenFile(t, path, buf.Bytes())
}
got := buf.String()
want := ReadOrGenerateGoldenFile(t, path, buf.Bytes())
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("JSON mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestTransactionEventMarshalJSON(t *testing.T) {
tests := []*Event{
{
Type: transactionType,
StartTime: goReleaseDate.Add(-time.Minute),
Timestamp: goReleaseDate,
},
{
Type: transactionType,
StartTime: goReleaseDate.Add(-time.Minute).In(utcMinusTwo),
Timestamp: goReleaseDate.In(utcMinusTwo),
},
{
Type: transactionType,
},
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
for i, tt := range tests {
i, tt := i, tt
t.Run("", func(t *testing.T) {
defer buf.Reset()
err := enc.Encode(tt)
if err != nil {
t.Fatal(err)
}
path := filepath.Join("testdata", "json", "transaction", fmt.Sprintf("%03d.json", i))
if *update {
WriteGoldenFile(t, path, buf.Bytes())
}
got := buf.String()
want := ReadOrGenerateGoldenFile(t, path, buf.Bytes())
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("JSON mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestBreadcrumbMarshalJSON(t *testing.T) {
tests := []*Breadcrumb{
// complete
{
Type: "default",
Category: "sentryhttp",
Message: "breadcrumb message",
Data: map[string]interface{}{
"key": "value",
},
Level: LevelInfo,
Timestamp: goReleaseDate,
},
// timestamp not in UTC
{
Data: map[string]interface{}{
"key": "value",
},
Timestamp: goReleaseDate.In(utcMinusTwo),
},
// missing timestamp
{
Message: "breadcrumb message",
},
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
for i, tt := range tests {
i, tt := i, tt
t.Run("", func(t *testing.T) {
defer buf.Reset()
err := enc.Encode(tt)
if err != nil {
t.Fatal(err)
}
path := filepath.Join("testdata", "json", "breadcrumb", fmt.Sprintf("%03d.json", i))
if *update {
WriteGoldenFile(t, path, buf.Bytes())
}
got := buf.String()
want := ReadOrGenerateGoldenFile(t, path, buf.Bytes())
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("JSON mismatch (-want +got):\n%s", diff)
}
})
}
}
func WriteGoldenFile(t *testing.T, path string, bytes []byte) {
t.Helper()
err := os.MkdirAll(filepath.Dir(path), 0777)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(path, bytes, 0666)
if err != nil {
t.Fatal(err)
}
}
func ReadOrGenerateGoldenFile(t *testing.T, path string, bytes []byte) string {
t.Helper()
b, err := os.ReadFile(path)
switch {
case errors.Is(err, os.ErrNotExist):
if *generate {
WriteGoldenFile(t, path, bytes)
return string(bytes)
}
t.Fatalf("Missing golden file. Run `go test -args -gen` to generate it.")
case err != nil:
t.Fatal(err)
}
return string(b)
}