File: errors_test.go

package info (click to toggle)
golang-github-mimuret-golang-iij-dpf 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,340 kB
  • sloc: makefile: 55
file content (303 lines) | stat: -rw-r--r-- 9,326 bytes parent folder | download
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
package api_test

import (
	"errors"
	"fmt"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/mimuret/golang-iij-dpf/pkg/api"
)

var _ = Describe("errors.go", func() {
	Context("IsStatusCode", func() {
		When("err is not BadResponse", func() {
			It("returns false", func() {
				Expect(api.IsStatusCode(errors.New(""), 400)).To(BeFalse())
			})
		})
		When("err not BadResponse, not match value", func() {
			It("returns false", func() {
				Expect(api.IsStatusCode(&api.BadResponse{StatusCode: 200}, 400)).To(BeFalse())
			})
		})
		When("err not BadResponse, match value", func() {
			It("returns true", func() {
				Expect(api.IsStatusCode(&api.BadResponse{StatusCode: 200}, 200)).To(BeTrue())
			})
		})
	})
	Context("IsErrType", func() {
		When("err is not BadResponse", func() {
			It("returns false", func() {
				Expect(api.IsErrType(errors.New(""), "hoge")).To(BeFalse())
			})
		})
		When("err not BadResponse, not match value", func() {
			It("returns false", func() {
				Expect(api.IsErrType(&api.BadResponse{ErrorType: "book"}, "hoge")).To(BeFalse())
			})
		})
		When("err not BadResponse, match value", func() {
			It("returns true", func() {
				Expect(api.IsErrType(&api.BadResponse{ErrorType: "hoge"}, "hoge")).To(BeTrue())
			})
		})
	})
	Context("IsErrMsg", func() {
		When("err is not BadResponse", func() {
			It("returns false", func() {
				Expect(api.IsErrMsg(errors.New(""), "hoge")).To(BeFalse())
			})
		})
		When("err not BadResponse, not match value", func() {
			It("returns false", func() {
				Expect(api.IsErrMsg(&api.BadResponse{ErrorMessage: "book"}, "hoge")).To(BeFalse())
			})
		})
		When("err not BadResponse, match value", func() {
			It("returns true", func() {
				Expect(api.IsErrMsg(&api.BadResponse{ErrorMessage: "hoge"}, "hoge")).To(BeTrue())
			})
		})
	})
	Context("IsErrorCode", func() {
		When("err is not BadResponse", func() {
			It("returns false", func() {
				Expect(api.IsErrorCode(errors.New(""), "hoge")).To(BeFalse())
			})
		})
		When("err not BadResponse, not match value", func() {
			It("returns false", func() {
				ok, _ := api.IsErrorCode(&api.BadResponse{ErrorDetails: api.ErrorDetails{{Code: "code", Attribute: "ruby"}}}, "hoge")
				Expect(ok).To(BeFalse())
			})
		})
		When("err not BadResponse, match value", func() {
			It("returns true", func() {
				ok, attr := api.IsErrorCode(&api.BadResponse{ErrorDetails: api.ErrorDetails{{Code: "code", Attribute: "ruby"}}}, "code")
				Expect(ok).To(BeTrue())
				Expect(attr).To(Equal("ruby"))
			})
		})
	})
	Context("IsErrorCodeAttribute", func() {
		When("err is not BadResponse", func() {
			It("returns false", func() {
				Expect(api.IsErrorCodeAttribute(errors.New(""), "code", "attribute")).To(BeFalse())
			})
		})
		When("err not BadResponse, not match details", func() {
			It("returns false", func() {
				Expect(api.IsErrorCodeAttribute(&api.BadResponse{ErrorDetails: api.ErrorDetails{{Code: "code", Attribute: "ruby"}}}, "code", "attribute")).To(BeFalse())
			})
		})
		When("err not BadResponse, include details", func() {
			It("returns true", func() {
				Expect(api.IsErrorCodeAttribute(&api.BadResponse{ErrorDetails: api.ErrorDetails{{Code: "book", Attribute: "one"}, {Code: "code", Attribute: "attribute"}}}, "code", "attribute")).To(BeTrue())
			})
		})
	})
	Context("Bad", func() {
		When("err is not BadResponse", func() {
			It("return false", func() {
				ok := api.IsBadResponse(fmt.Errorf("failed"), nil)
				Expect(ok).To(BeFalse())
			})
		})
		When("err is BadResponse, func not set", func() {
			It("return true", func() {
				ok := api.IsBadResponse(&api.BadResponse{}, nil)
				Expect(ok).To(BeTrue())
			})
		})
		When("err is BadResponse, func set", func() {
			It("return func result", func() {
				ok := api.IsBadResponse(&api.BadResponse{}, func(b *api.BadResponse) bool { return true })
				Expect(ok).To(BeTrue())
				ok = api.IsBadResponse(&api.BadResponse{}, func(b *api.BadResponse) bool { return false })
				Expect(ok).To(BeFalse())
			})
		})
	})
	Context("IsAuthError", func() {
		When("status code is 400, ErrType is ParameterError, ErrorDtail include invalid=access_token", func() {
			It("return true", func() {
				Expect(api.IsAuthError(&api.BadResponse{
					StatusCode:   400,
					ErrorType:    "ParameterError",
					ErrorDetails: api.ErrorDetails{{Code: "invalid", Attribute: "access_token"}},
				})).To(BeTrue())
			})
		})
		When("other", func() {
			It("return false", func() {
				Expect(api.IsAuthError(&api.BadResponse{
					StatusCode:   200,
					ErrorType:    "ParameterError",
					ErrorDetails: api.ErrorDetails{{Code: "invalid", Attribute: "access_token"}},
				})).To(BeFalse())
				Expect(api.IsAuthError(&api.BadResponse{
					StatusCode:   400,
					ErrorType:    "NotFound",
					ErrorDetails: api.ErrorDetails{{Code: "invalid", Attribute: "access_token"}},
				})).To(BeFalse())
				Expect(api.IsAuthError(&api.BadResponse{
					StatusCode:   400,
					ErrorType:    "ParameterError",
					ErrorDetails: api.ErrorDetails{{Code: "invalid", Attribute: "schema"}},
				})).To(BeFalse())
			})
		})
	})
	Context("IsRequestFormatError", func() {
		When("status code is 400, ErrType is ParameterError, ErrMsg `JSON parse error occurred`", func() {
			It("return true", func() {
				Expect(api.IsRequestFormatError(&api.BadResponse{
					StatusCode:   400,
					ErrorType:    "ParameterError",
					ErrorMessage: "JSON parse error occurred.",
				})).To(BeTrue())
			})
		})
		When("other", func() {
			It("return false", func() {
				Expect(api.IsRequestFormatError(&api.BadResponse{
					StatusCode:   200,
					ErrorType:    "ParameterError",
					ErrorMessage: "JSON parse error occurred.",
				})).To(BeFalse())
				Expect(api.IsRequestFormatError(&api.BadResponse{
					StatusCode:   400,
					ErrorType:    "NotFound",
					ErrorMessage: "JSON parse error occurred.",
				})).To(BeFalse())
				Expect(api.IsRequestFormatError(&api.BadResponse{
					StatusCode: 400,
					ErrorType:  "ParameterError",
				})).To(BeFalse())
			})
		})
	})
	Context("IsParameterError", func() {
		When("status code is 400, ErrType ParameterError, NotAuthError, NotRequestFormatError", func() {
			It("return true", func() {
				Expect(api.IsParameterError(&api.BadResponse{
					StatusCode: 400,
					ErrorType:  "ParameterError",
				})).To(BeTrue())
			})
		})
		When("other", func() {
			It("return false", func() {
				Expect(api.IsParameterError(&api.BadResponse{
					StatusCode: 200,
					ErrorType:  "ParameterError",
				})).To(BeFalse())
				Expect(api.IsParameterError(&api.BadResponse{
					StatusCode: 400,
					ErrorType:  "NotFound",
				})).To(BeFalse())
				Expect(api.IsParameterError(&api.BadResponse{
					StatusCode:   400,
					ErrorType:    "ParameterError",
					ErrorMessage: "JSON parse error occurred.",
				})).To(BeFalse())
				Expect(api.IsParameterError(&api.BadResponse{
					StatusCode:   400,
					ErrorType:    "ParameterError",
					ErrorDetails: api.ErrorDetails{{Code: "invalid", Attribute: "access_token"}},
				})).To(BeFalse())
			})
		})
	})
	Context("IsNotFound", func() {
		When("status code is 404", func() {
			It("return true", func() {
				Expect(api.IsNotFound(&api.BadResponse{
					StatusCode: 404,
				})).To(BeTrue())
			})
		})
		When("other", func() {
			It("return false", func() {
				Expect(api.IsNotFound(&api.BadResponse{
					StatusCode: 200,
				})).To(BeFalse())
			})
		})
	})
	Context("IsTooManyRequests", func() {
		When("status code is 429", func() {
			It("return true", func() {
				Expect(api.IsTooManyRequests(&api.BadResponse{
					StatusCode: 429,
				})).To(BeTrue())
			})
		})
		When("other", func() {
			It("return false", func() {
				Expect(api.IsTooManyRequests(&api.BadResponse{
					StatusCode: 200,
				})).To(BeFalse())
			})
		})
	})
	Context("IsSystemError", func() {
		When("status code is 500", func() {
			It("return true", func() {
				Expect(api.IsSystemError(&api.BadResponse{
					StatusCode: 500,
				})).To(BeTrue())
			})
		})
		When("other", func() {
			It("return false", func() {
				Expect(api.IsSystemError(&api.BadResponse{
					StatusCode: 200,
				})).To(BeFalse())
			})
		})
	})
	Context("IsGatewayTimeout", func() {
		When("status code is 504", func() {
			It("return true", func() {
				Expect(api.IsGatewayTimeout(&api.BadResponse{
					StatusCode: 504,
				})).To(BeTrue())
			})
		})
		When("other", func() {
			It("return false", func() {
				Expect(api.IsGatewayTimeout(&api.BadResponse{
					StatusCode: 200,
				})).To(BeFalse())
			})
		})
	})
	Context("IsInvalidSchema", func() {
		When("IsInvalidSchema and ErrorDtail include invalid=schema", func() {
			It("return true", func() {
				Expect(api.IsInvalidSchema(&api.BadResponse{
					StatusCode:   400,
					ErrorType:    "ParameterError",
					ErrorDetails: api.ErrorDetails{{Code: "invalid", Attribute: "schema"}},
				})).To(BeTrue())
			})
		})
		When("other", func() {
			It("return false", func() {
				Expect(api.IsInvalidSchema(&api.BadResponse{
					StatusCode: 400,
					ErrorType:  "ParameterError",
				})).To(BeFalse())
				Expect(api.IsInvalidSchema(&api.BadResponse{
					StatusCode:   404,
					ErrorType:    "ParameterError",
					ErrorDetails: api.ErrorDetails{{Code: "invalid", Attribute: "schema"}},
				})).To(BeFalse())
			})
		})
	})
})