-
Notifications
You must be signed in to change notification settings - Fork 4
/
validator_test.go
366 lines (313 loc) · 12.2 KB
/
validator_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
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package bascule
import (
"context"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/suite"
)
type ValidatorsTestSuite struct {
TestSuite
expectedCtx context.Context
expectedSource int
inputToken Token
outputToken Token
expectedErr error
}
func (suite *ValidatorsTestSuite) SetupSuite() {
suite.expectedCtx = suite.testContext()
suite.expectedSource = 123
suite.inputToken = StubToken("input token")
suite.outputToken = StubToken("output token")
suite.expectedErr = errors.New("expected validator error")
}
// assertNoTransform verifies that the validator returns the same token as the input token.
func (suite *ValidatorsTestSuite) assertNoTransform(v Validator[int]) {
suite.Require().NotNil(v)
actualToken, actualErr := v.Validate(suite.expectedCtx, suite.expectedSource, suite.inputToken)
suite.Equal(suite.inputToken, actualToken)
suite.ErrorIs(suite.expectedErr, actualErr)
}
// assertTransform verifies a validator that returns a different token than the input token.
func (suite *ValidatorsTestSuite) assertTransform(v Validator[int]) {
suite.Require().NotNil(v)
actualToken, actualErr := v.Validate(suite.expectedCtx, suite.expectedSource, suite.inputToken)
suite.Equal(suite.outputToken, actualToken)
suite.ErrorIs(suite.expectedErr, actualErr)
}
// validateToken is a ValidatorFunc of the signature func(Token) error
func (suite *ValidatorsTestSuite) validateToken(actualToken Token) error {
suite.Equal(suite.inputToken, actualToken)
return suite.expectedErr
}
// validateSourceToken is a ValidatorFunc of the signature func(<source>, Token) error
func (suite *ValidatorsTestSuite) validateSourceToken(actualSource int, actualToken Token) error {
suite.Equal(suite.expectedSource, actualSource)
suite.Equal(suite.inputToken, actualToken)
return suite.expectedErr
}
// validateContextToken is a ValidatorFunc of the signature func(context.Context, Token) error
func (suite *ValidatorsTestSuite) validateContextToken(actualCtx context.Context, actualToken Token) error {
suite.Equal(suite.expectedCtx, actualCtx)
suite.Equal(suite.inputToken, actualToken)
return suite.expectedErr
}
// validateContextSourceToken is a ValidatorFunc of the signature func(context.Context, <source>, Token) error
func (suite *ValidatorsTestSuite) validateContextSourceToken(actualCtx context.Context, actualSource int, actualToken Token) error {
suite.Equal(suite.expectedCtx, actualCtx)
suite.Equal(suite.expectedSource, actualSource)
suite.Equal(suite.inputToken, actualToken)
return suite.expectedErr
}
// transformToken is a ValidatorFunc of the signature func(Token) (Token, error).
// This variant returns suite.outputToken.
func (suite *ValidatorsTestSuite) transformToken(actualToken Token) (Token, error) {
suite.Equal(suite.inputToken, actualToken)
return suite.outputToken, suite.expectedErr
}
// transformTokenToNil is a ValidatorFunc of the signature func(Token) (Token, error).
// This variant returns a nil Token, indicating that the original token is unchanged.
func (suite *ValidatorsTestSuite) transformTokenToNil(actualToken Token) (Token, error) {
suite.Equal(suite.inputToken, actualToken)
return nil, suite.expectedErr
}
// transformSourceToken is a ValidatorFunc of the signature func(<source>, Token) (Token, error)
// This variant returns suite.outputToken.
func (suite *ValidatorsTestSuite) transformSourceToken(actualSource int, actualToken Token) (Token, error) {
suite.Equal(suite.expectedSource, actualSource)
suite.Equal(suite.inputToken, actualToken)
return suite.outputToken, suite.expectedErr
}
// transformSourceTokenToNil is a ValidatorFunc of the signature func(<source>, Token) (Token, error)
// This variant returns a nil Token, indicating that the original token is unchanged.
func (suite *ValidatorsTestSuite) transformSourceTokenToNil(actualSource int, actualToken Token) (Token, error) {
suite.Equal(suite.expectedSource, actualSource)
suite.Equal(suite.inputToken, actualToken)
return nil, suite.expectedErr
}
// transformContextToken is a ValidatorFunc of the signature func(context.context, Token) (Token, error)
// This variant returns suite.outputToken.
func (suite *ValidatorsTestSuite) transformContextToken(actualCtx context.Context, actualToken Token) (Token, error) {
suite.Equal(suite.expectedCtx, actualCtx)
suite.Equal(suite.inputToken, actualToken)
return suite.outputToken, suite.expectedErr
}
// transformContextTokenToNil is a ValidatorFunc of the signature func(context.context, Token) (Token, error)
// This variant returns a nil Token, indicating that the original token is unchanged.
func (suite *ValidatorsTestSuite) transformContextTokenToNil(actualCtx context.Context, actualToken Token) (Token, error) {
suite.Equal(suite.expectedCtx, actualCtx)
suite.Equal(suite.inputToken, actualToken)
return nil, suite.expectedErr
}
// transformContextSourceToken is a ValidatorFunc of the signature func(context.Context, <source>, Token) (Token, error)
// This variant returns suite.outputToken.
func (suite *ValidatorsTestSuite) transformContextSourceToken(actualCtx context.Context, actualSource int, actualToken Token) (Token, error) {
suite.Equal(suite.expectedCtx, actualCtx)
suite.Equal(suite.expectedSource, actualSource)
suite.Equal(suite.inputToken, actualToken)
return suite.outputToken, suite.expectedErr
}
// transformContextSourceToken is a ValidatorFunc of the signature func(context.Context, <source>, Token) (Token, error)
// This variant returns a nil Token, indicating that the original token is unchanged.
func (suite *ValidatorsTestSuite) transformContextSourceTokenToNil(actualCtx context.Context, actualSource int, actualToken Token) (Token, error) {
suite.Equal(suite.expectedCtx, actualCtx)
suite.Equal(suite.expectedSource, actualSource)
suite.Equal(suite.inputToken, actualToken)
return nil, suite.expectedErr
}
func (suite *ValidatorsTestSuite) testAsValidatorToken() {
suite.Run("ReturnError", func() {
suite.Run("Simple", func() {
v := AsValidator[int](suite.validateToken)
suite.assertNoTransform(v)
})
suite.Run("CustomType", func() {
type Custom func(Token) error
f := Custom(suite.validateToken)
v := AsValidator[int](f)
suite.assertNoTransform(v)
})
})
suite.Run("ReturnTokenError", func() {
suite.Run("Simple", func() {
v := AsValidator[int](suite.transformToken)
suite.assertTransform(v)
})
suite.Run("NilOutputToken", func() {
v := AsValidator[int](suite.transformTokenToNil)
suite.assertNoTransform(v)
})
suite.Run("CustomType", func() {
type Custom func(Token) (Token, error)
f := Custom(suite.transformToken)
v := AsValidator[int](f)
suite.assertTransform(v)
})
})
}
func (suite *ValidatorsTestSuite) testAsValidatorSourceToken() {
suite.Run("ReturnError", func() {
suite.Run("Simple", func() {
v := AsValidator[int](suite.validateSourceToken)
suite.assertNoTransform(v)
})
suite.Run("CustomType", func() {
type Custom func(int, Token) error
f := Custom(suite.validateSourceToken)
v := AsValidator[int](f)
suite.assertNoTransform(v)
})
})
suite.Run("ReturnTokenError", func() {
suite.Run("Simple", func() {
v := AsValidator[int](suite.transformSourceToken)
suite.assertTransform(v)
})
suite.Run("NilOutputToken", func() {
v := AsValidator[int](suite.transformSourceTokenToNil)
suite.assertNoTransform(v)
})
suite.Run("CustomType", func() {
type Custom func(int, Token) (Token, error)
f := Custom(suite.transformSourceToken)
v := AsValidator[int](f)
suite.assertTransform(v)
})
})
}
func (suite *ValidatorsTestSuite) testAsValidatorContextToken() {
suite.Run("ReturnError", func() {
suite.Run("Simple", func() {
v := AsValidator[int](suite.validateContextToken)
suite.assertNoTransform(v)
})
suite.Run("CustomType", func() {
type Custom func(context.Context, Token) error
f := Custom(suite.validateContextToken)
v := AsValidator[int](f)
suite.assertNoTransform(v)
})
})
suite.Run("ReturnTokenError", func() {
suite.Run("Simple", func() {
v := AsValidator[int](suite.transformContextToken)
suite.assertTransform(v)
})
suite.Run("NilOutputToken", func() {
v := AsValidator[int](suite.transformContextTokenToNil)
suite.assertNoTransform(v)
})
suite.Run("CustomType", func() {
type Custom func(context.Context, Token) (Token, error)
f := Custom(suite.transformContextToken)
v := AsValidator[int](f)
suite.assertTransform(v)
})
})
}
func (suite *ValidatorsTestSuite) testAsValidatorContextSourceToken() {
suite.Run("ReturnError", func() {
suite.Run("Simple", func() {
v := AsValidator[int](suite.validateContextSourceToken)
suite.assertNoTransform(v)
})
suite.Run("CustomType", func() {
type Custom func(context.Context, int, Token) error
f := Custom(suite.validateContextSourceToken)
v := AsValidator[int](f)
suite.assertNoTransform(v)
})
})
suite.Run("ReturnTokenError", func() {
suite.Run("Simple", func() {
v := AsValidator[int](suite.transformContextSourceToken)
suite.assertTransform(v)
})
suite.Run("NilOutputToken", func() {
v := AsValidator[int](suite.transformContextSourceTokenToNil)
suite.assertNoTransform(v)
})
suite.Run("CustomType", func() {
type Custom func(context.Context, int, Token) (Token, error)
f := Custom(suite.transformContextSourceToken)
v := AsValidator[int](f)
suite.assertTransform(v)
})
})
}
func (suite *ValidatorsTestSuite) TestAsValidator() {
suite.Run("Token", suite.testAsValidatorToken)
suite.Run("SourceToken", suite.testAsValidatorSourceToken)
suite.Run("ContextToken", suite.testAsValidatorContextToken)
suite.Run("ContextSourceToken", suite.testAsValidatorContextSourceToken)
}
// newValidators constructs an array of validators that can only be called once
// and which successfully validate the suite's input token.
func (suite *ValidatorsTestSuite) newValidators(count int) (vs []Validator[int]) {
vs = make([]Validator[int], 0, count)
for len(vs) < cap(vs) {
v := new(mockValidator[int])
v.ExpectValidate(suite.expectedCtx, suite.expectedSource, suite.inputToken).
Return(nil, nil).Once()
vs = append(vs, v)
}
return
}
func (suite *ValidatorsTestSuite) TestValidate() {
suite.Run("NoValidators", func() {
outputToken, err := Validate[int](suite.expectedCtx, suite.expectedSource, suite.inputToken)
suite.Equal(suite.inputToken, outputToken)
suite.NoError(err)
})
suite.Run("NilOutputToken", func() {
for _, count := range []int{1, 2, 5} {
suite.Run(fmt.Sprintf("count=%d", count), func() {
vs := suite.newValidators(count)
actualToken, actualErr := Validate(suite.expectedCtx, suite.expectedSource, suite.inputToken, vs...)
suite.Equal(suite.inputToken, actualToken)
suite.NoError(actualErr)
assertValidators(suite.T(), vs...)
})
}
})
}
func (suite *ValidatorsTestSuite) TestCompositeValidators() {
suite.Run("Empty", func() {
var vs Validators[int]
outputToken, err := vs.Validate(suite.expectedCtx, suite.expectedSource, suite.inputToken)
suite.Equal(suite.inputToken, outputToken)
suite.NoError(err)
})
suite.Run("NotEmpty", func() {
suite.Run("len=1", func() {
v := new(mockValidator[int])
v.ExpectValidate(suite.expectedCtx, suite.expectedSource, suite.inputToken).
Return(suite.outputToken, nil).Once()
var vs Validators[int]
vs = vs.Append(v)
actualToken, actualErr := vs.Validate(suite.expectedCtx, suite.expectedSource, suite.inputToken)
suite.Equal(suite.outputToken, actualToken)
suite.NoError(actualErr)
assertValidators(suite.T(), v)
})
suite.Run("len=2", func() {
v1 := new(mockValidator[int])
v1.ExpectValidate(suite.expectedCtx, suite.expectedSource, suite.inputToken).
Return(nil, nil).Once()
v2 := new(mockValidator[int])
v2.ExpectValidate(suite.expectedCtx, suite.expectedSource, suite.inputToken).
Return(nil, nil).Once()
var vs Validators[int]
vs = vs.Append(v1, v2)
actualToken, actualErr := vs.Validate(suite.expectedCtx, suite.expectedSource, suite.inputToken)
suite.Equal(suite.inputToken, actualToken) // the token should be unchanged
suite.NoError(actualErr)
assertValidators(suite.T(), v1, v2)
})
})
}
func TestValidators(t *testing.T) {
suite.Run(t, new(ValidatorsTestSuite))
}