-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
234 lines (219 loc) · 6 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
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
package validation
import (
"errors"
"testing"
)
func TestNewValidationError(t *testing.T) {
t.Run("should fail", func(t *testing.T) {
t.Run("empty message", func(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("expected to panic")
}
}()
_ = NewValidationError("")
})
})
t.Run("should pass", func(t *testing.T) {
err := NewValidationError("test_err_1")
if !IsValidationError(err) {
t.Error("expected to return true from IsValidationError()")
}
})
}
func TestIsValidationError(t *testing.T) {
t.Run("errValidation", func(t *testing.T) {
if !IsValidationError(errValidation) {
t.Fatal("expected to return true for errValidation")
}
})
t.Run("errBadRecord", func(t *testing.T) {
if !IsValidationError(errBadRecord) {
t.Fatal("expected to return true for errBadRecord")
}
})
t.Run("errBadRequest", func(t *testing.T) {
if !IsValidationError(errBadRequest) {
t.Fatal("expected to return true for errBadRequest")
}
})
}
func TestIsBadRecordError(t *testing.T) {
t.Run("should fail", func(t *testing.T) {
if IsBadRecordError(errors.New("some random error")) {
t.Error("should fail for random error")
}
t.Run("errBadRequest", func(t *testing.T) {
if IsBadRecordError(errBadRequest) {
t.Error("should be false")
}
})
})
t.Run("should pass for", func(t *testing.T) {
t.Run("errBadRecord", func(t *testing.T) {
if !IsBadRecordError(errBadRecord) {
t.Error("should be true")
}
})
t.Run("NewErrRecordIsMissingRequiredField", func(t *testing.T) {
if !IsBadRecordError(NewErrRecordIsMissingRequiredField("field1")) {
t.Error("should be true")
}
})
t.Run("NewErrBadRecordFieldValue", func(t *testing.T) {
if !IsBadRecordError(NewErrBadRecordFieldValue("field1", "msg1")) {
t.Error("should be true")
}
})
})
}
func TestIsBadRequestError(t *testing.T) {
t.Run("should fail", func(t *testing.T) {
if IsBadRequestError(errors.New("some random error")) {
t.Error("should fail for random error")
}
t.Run("errBadRecord", func(t *testing.T) {
if IsBadRequestError(errBadRecord) {
t.Error("should be false")
}
})
})
t.Run("should pass for", func(t *testing.T) {
t.Run("errBadRequest", func(t *testing.T) {
if !IsBadRequestError(errBadRequest) {
t.Error("should be true")
}
})
t.Run("NewErrRequestIsMissingRequiredField", func(t *testing.T) {
if !IsBadRequestError(NewErrRequestIsMissingRequiredField("field1")) {
t.Error("should be true")
}
})
t.Run("NewErrBadRequestFieldValue", func(t *testing.T) {
if !IsBadRequestError(NewErrBadRequestFieldValue("field1", "msg1")) {
t.Error("should be true")
}
})
})
}
func TestNewErrBadRecordFieldValue(t *testing.T) {
t.Run("should panic", func(t *testing.T) {
t.Run("empty name", func(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("expected to panic")
}
}()
_ = NewErrBadRecordFieldValue("", "bad_field_123")
})
t.Run("empty message", func(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("expected to panic")
}
}()
_ = NewErrBadRecordFieldValue("field1", "")
})
})
t.Run("should pass", func(t *testing.T) {
err := NewErrBadRecordFieldValue("field1", "bad_field_123")
mustBeValidationError(t, err)
mustBeBadFieldError(t, err)
//
mustBeBadRecordError(t, err)
})
}
func TestNewErrBadRequestFieldValue(t *testing.T) {
t.Run("should panic", func(t *testing.T) {
t.Run("empty name", func(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("expected to panic")
}
}()
_ = NewErrBadRequestFieldValue("", "bad_field_123")
})
t.Run("empty message", func(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("expected to panic")
}
}()
_ = NewErrBadRequestFieldValue("field1", "")
})
})
t.Run("should pass", func(t *testing.T) {
err := NewErrBadRequestFieldValue("field1", "bad_field_123")
mustBeValidationError(t, err)
mustBeBadFieldError(t, err)
//
mustBeBadRequestError(t, err)
})
}
func mustBeValidationError(t *testing.T, err error) {
t.Helper()
if !IsValidationError(err) {
t.Errorf("expected to be IsValidationError, got %T: %v", err, err)
}
}
func mustBeBadRecordError(t *testing.T, err error) {
t.Helper()
if !IsBadRecordError(err) {
t.Errorf("expected to be IsBadRecordError, got %T: %v", err, err)
}
}
func mustBeBadRequestError(t *testing.T, err error) {
t.Helper()
if !IsBadRequestError(err) {
t.Errorf("expected to be IsBadRequestError, got %T: %v", err, err)
}
}
func mustBeBadFieldError(t *testing.T, err error) {
t.Helper()
if !IsBadFieldValueError(err) {
t.Errorf("expected to be IsBadFieldValueError, got %T: %v", err, err)
}
}
type TestMock struct {
helper int
errorf []struct {
format string
args []interface{}
}
}
func (v *TestMock) Helper() {
v.helper += 1
}
func (v *TestMock) Errorf(format string, args ...interface{}) {
if format == "" {
panic("format is a required parameter")
}
v.errorf = append(v.errorf, struct {
format string
args []interface{}
}{format: format, args: args})
}
func testHelperShouldBeCalledOnce(t *testing.T, testMock *TestMock) {
t.Helper()
if testMock.helper != 1 {
t.Errorf("t.Helper() should be called once, got called %v times", testMock.helper)
}
}
func TestMustBeFieldError(t *testing.T) {
t.Run("should write a single error", func(t *testing.T) {
testMock := new(TestMock)
MustBeFieldError(testMock, errors.New("some error"), "field1")
testHelperShouldBeCalledOnce(t, testMock)
if len(testMock.errorf) != 1 {
t.Errorf("t.Errorf() should be called once, got called %v times", len(testMock.errorf))
}
})
t.Run("should not write any errors", func(t *testing.T) {
testMock := new(TestMock)
MustBeFieldError(testMock, NewErrRecordIsMissingRequiredField("field1"), "field1")
testHelperShouldBeCalledOnce(t, testMock)
if len(testMock.errorf) != 0 {
t.Errorf("t.Errorf() should NOT be called, got called %v times", len(testMock.errorf))
}
})
}