-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
100 lines (87 loc) · 2.47 KB
/
errors_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
package auth
import (
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func TestErrors_New(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
err *Error
expectedCode int
}{
{
name: "base error",
err: NewError("base error"),
expectedCode: 0,
}, {
name: "bad request",
err: NewError("bad request").SetStatus(http.StatusBadRequest),
expectedCode: http.StatusBadRequest,
}, {
name: "too many requests",
err: NewError("too many requests").SetStatus(http.StatusTooManyRequests),
expectedCode: http.StatusTooManyRequests,
}, {
name: "bad gateway",
err: NewError("bad gateway").SetStatus(http.StatusBadGateway),
expectedCode: http.StatusBadGateway,
},
}
for _, testCase := range testCases {
test := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
require.NotNil(t, test.err, "error should not be nil")
require.Equal(t, test.expectedCode, test.err.Code, "expected error code did not match")
require.Equal(t, test.name, test.err.Message, "error messages did not match")
})
}
}
func TestErrors_Is(t *testing.T) {
t.Parallel()
baseError := NewError("base error")
testCases := []struct {
name string
inputErr error
baseErr *Error
boolExpectation require.BoolAssertionFunc
}{
{
name: "input nil",
inputErr: nil,
baseErr: baseError,
boolExpectation: require.False,
}, {
name: "base nil",
inputErr: nil,
baseErr: nil,
boolExpectation: require.False,
}, {
name: "base different",
inputErr: errors.New("different error"),
baseErr: nil,
boolExpectation: require.False,
}, {
name: "base vs too many requests",
inputErr: &Error{Message: "input", Code: http.StatusTooManyRequests},
baseErr: baseError,
boolExpectation: require.False,
}, {
name: "too many requests",
inputErr: &Error{Message: "input", Code: http.StatusTooManyRequests},
baseErr: &Error{Message: "base", Code: http.StatusTooManyRequests},
boolExpectation: require.True,
},
}
for _, testCase := range testCases {
test := testCase
t.Run(test.name, func(t *testing.T) {
t.Parallel()
result := test.baseErr.Is(test.inputErr)
test.boolExpectation(t, result, "error is value expectation failed.")
})
}
}