-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patherror_test.go
399 lines (298 loc) · 10.1 KB
/
error_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2019 Aporeto Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package elemental
import (
"errors"
"fmt"
"net/http"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestError_NewError(t *testing.T) {
Convey("Given I create an Error", t, func() {
e := NewError("bad", "something bad", "containers", 42)
e.Trace = "xyz"
Convey("Then the Error should be correctly initialized", func() {
So(e.Code, ShouldEqual, 42)
So(e.Description, ShouldEqual, "something bad")
So(e.Subject, ShouldEqual, "containers")
So(e.Title, ShouldEqual, "bad")
So(e.Trace, ShouldEqual, "xyz")
})
Convey("Then the string representation should be correct", func() {
So(e.Error(), ShouldEqual, "error 42 (containers): bad: something bad [trace: xyz]")
})
})
Convey("Given I create an Error with data", t, func() {
e := NewErrorWithData("bad", "something bad", "containers", 42, map[string]string{"test": "coucou"})
Convey("Then the Error should be correctly initialized", func() {
So(e.Code, ShouldEqual, 42)
So(e.Description, ShouldEqual, "something bad")
So(e.Subject, ShouldEqual, "containers")
So(e.Title, ShouldEqual, "bad")
So(e.Data, ShouldResemble, map[string]string{"test": "coucou"})
})
Convey("Then the string representation should be correct", func() {
So(e.Error(), ShouldEqual, "error 42 (containers): bad: something bad")
})
})
}
func TestError_Error(t *testing.T) {
Convey("Given I create an Error", t, func() {
e := NewError("bad", "something bad", "containers", 42)
Convey("When I use the Error interface", func() {
s := e.Error()
Convey("Then string should be correct", func() {
So(s, ShouldEqual, "error 42 (containers): bad: something bad")
})
})
})
}
func TestError_NewErrors(t *testing.T) {
Convey("Given I create an elemental.Errors with some errors", t, func() {
e1 := NewError("bad", "something bad", "containers", 42)
e2 := NewError("bad1", "something bad1", "containers1", 43)
errs := NewErrors(e1, e2)
Convey("Then the Error should be correctly initialized", func() {
So(errs, ShouldResemble, Errors{e1, e2})
So(errs.Error(), ShouldResemble, "error 42 (containers): bad: something bad, error 43 (containers1): bad1: something bad1")
So(errs.Code(), ShouldEqual, 42)
})
})
Convey("Given I create an elemental.Errors without any error", t, func() {
errs := NewErrors()
Convey("Then the Error should be correctly initialized", func() {
So(errs, ShouldResemble, Errors{})
So(errs.Code(), ShouldEqual, -1)
})
})
}
func TestError_Append(t *testing.T) {
Convey("Given I append to an elemental.Errors some other errors", t, func() {
e0 := NewError("bad", "something bad", "containers", 42)
errs := NewErrors(e0)
e1 := NewError("bad", "something bad", "containers", 42)
e2 := NewError("bad", "something bad", "containers", 42)
errs2 := errs.Append(e1, e2, errs, fmt.Errorf("boom"))
Convey("Then out should be correct", func() {
So(errs2[0], ShouldResemble, e0)
So(errs2[1], ShouldResemble, e1)
So(errs2[2], ShouldResemble, e2)
So(errs2[3], ShouldResemble, e0)
So(errs2[4], ShouldResemble, NewError("Internal Server Error", "boom", "elemental", 500))
})
})
}
func TestError_SetTraceID(t *testing.T) {
Convey("Given I append to an elemental.Errors some other errors", t, func() {
e0 := NewError("bad", "something bad", "containers", 42)
e1 := NewError("bad", "something bad", "containers", 42)
e2 := NewError("bad", "something bad", "containers", 42)
errs := NewErrors(e0, e1, e2)
Convey("When I call setTraceID", func() {
errs = errs.Trace("trace")
Convey("Then the trace is set in all errors", func() {
So(errs[0].Trace, ShouldEqual, "trace")
So(errs[1].Trace, ShouldEqual, "trace")
So(errs[2].Trace, ShouldEqual, "trace")
})
})
})
}
func TestError_IsValidationError(t *testing.T) {
Convey("Given I have a list of one validation error", t, func() {
err := NewError("the title", "the description", "http.test", http.StatusUnprocessableEntity)
err.Data = map[string]any{"attribute": "theattr"}
errs := NewErrors(err)
Convey("When I call IsValidationError with expected title and attribute", func() {
ok := IsValidationError(errs, "the title", "theattr")
Convey("Then is should be ok", func() {
So(ok, ShouldBeTrue)
})
})
Convey("When I call IsValidationError with expected title and non expected attribute", func() {
ok := IsValidationError(errs, "the title", "not-theattr")
Convey("Then is should not be ok", func() {
So(ok, ShouldBeFalse)
})
})
Convey("When I call IsValidationError with non expected title and expected attribute", func() {
ok := IsValidationError(errs, "not the title", "theattr")
Convey("Then is should not be ok", func() {
So(ok, ShouldBeFalse)
})
})
})
Convey("Given I have a list of multiple validation errors", t, func() {
err := NewError("the title", "the description", "http.test", http.StatusUnprocessableEntity)
err.Data = map[string]any{"attribute": "theattr"}
errs := NewErrors(err, err)
Convey("When I call IsValidationError with expected title and attribute", func() {
ok := IsValidationError(errs, "the title", "theattr")
Convey("Then is should not be ok", func() {
So(ok, ShouldBeFalse)
})
})
})
Convey("Given I have a single validation error", t, func() {
err := NewError("the title", "the description", "http.test", http.StatusUnprocessableEntity)
err.Data = map[string]any{"attribute": "theattr"}
Convey("When I call IsValidationError with expected title and attribute", func() {
ok := IsValidationError(err, "the title", "theattr")
Convey("Then is should be ok", func() {
So(ok, ShouldBeTrue)
})
})
})
Convey("Given I have a classic error", t, func() {
err := errors.New("not elemental")
Convey("When I call IsValidationError", func() {
ok := IsValidationError(err, "the title", "theattr")
Convey("Then is should not be ok", func() {
So(ok, ShouldBeFalse)
})
})
})
Convey("Given I have a list of non validation elemental error", t, func() {
err := NewError("the title", "the description", "http.test", http.StatusNotFound)
err.Data = map[string]any{"attribute": "theattr"}
errs := NewErrors(err)
Convey("When I call IsValidationError with expected title and attribute", func() {
ok := IsValidationError(errs, "the title", "theattr")
Convey("Then is should not be ok", func() {
So(ok, ShouldBeFalse)
})
})
})
Convey("Given I have a non validation elemental error", t, func() {
err := NewError("the title", "the description", "http.test", http.StatusNotFound)
err.Data = map[string]any{"attribute": "theattr"}
Convey("When I call IsValidationError with expected title and attribute", func() {
ok := IsValidationError(err, "the title", "theattr")
Convey("Then is should not be ok", func() {
So(ok, ShouldBeFalse)
})
})
})
Convey("Given I have a validation elemental error with no data", t, func() {
err := NewError("the title", "the description", "http.test", http.StatusUnprocessableEntity)
Convey("When I call IsValidationError with expected title and attribute", func() {
ok := IsValidationError(err, "the title", "theattr")
Convey("Then is should not be ok", func() {
So(ok, ShouldBeFalse)
})
})
})
Convey("Given I have a validation elemental error with no bad data", t, func() {
err := NewError("the title", "the description", "http.test", http.StatusUnprocessableEntity)
err.Data = "zob"
Convey("When I call IsValidationError with expected title and attribute", func() {
ok := IsValidationError(err, "the title", "theattr")
Convey("Then is should not be ok", func() {
So(ok, ShouldBeFalse)
})
})
})
}
func TestError_DecodeError(t *testing.T) {
Convey("Given I have some valid json data", t, func() {
data := `[{"title":"t1","code":42,"description":"coucou"},{"title":"t1","code":42,"description":"coucou"}]`
Convey("When I call DecodeErrors", func() {
errs, err := DecodeErrors([]byte(data))
Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})
Convey("Then errs should be correct", func() {
So(len(errs), ShouldEqual, 2)
})
})
})
Convey("Given I have some invalid json data", t, func() {
data := `[{"title":"t1"]`
Convey("When I call DecodeErrors", func() {
errs, err := DecodeErrors([]byte(data))
Convey("Then err should be nil", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "invalid character ']' after object key:value pair")
})
Convey("Then errs should be correct", func() {
So(errs, ShouldBeNil)
})
})
})
}
func TestIsErrorWithCode(t *testing.T) {
type args struct {
err error
code int
}
tests := []struct {
name string
args args
want bool
}{
{
"positive test with elemental.Error",
args{
NewError("title", "description", "subject", 404),
404,
},
true,
},
{
"negative test with elemental.Error",
args{
NewError("title", "description", "subject", 404),
500,
},
false,
},
{
"positive test with elemental.Errors",
args{
NewErrors(NewError("title", "description", "subject", 404)),
404,
},
true,
},
{
"negative test with elemental.Errors",
args{
NewErrors(NewError("title", "description", "subject", 404)),
500,
},
false,
},
{
"test with classic error",
args{
fmt.Errorf("boom"),
500,
},
false,
},
{
"test with nil",
args{
nil,
500,
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsErrorWithCode(tt.args.err, tt.args.code); got != tt.want {
t.Errorf("IsErrorWithCode() = %v, want %v", got, tt.want)
}
})
}
}