-
Notifications
You must be signed in to change notification settings - Fork 53
/
values_test.go
417 lines (349 loc) · 13.9 KB
/
values_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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2015 go-swagger maintainers
//
// 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 validate
import (
"context"
"math"
"testing"
"github.com/go-openapi/errors"
"github.com/go-openapi/strfmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestValues_ValidateIntEnum(t *testing.T) {
enumValues := []interface{}{1, 2, 3}
require.Error(t, Enum("test", "body", int64(5), enumValues))
require.Nil(t, Enum("test", "body", int64(1), enumValues))
}
func TestValues_ValidateEnum(t *testing.T) {
enumValues := []string{"aa", "bb", "cc"}
require.Error(t, Enum("test", "body", "a", enumValues))
require.Nil(t, Enum("test", "body", "bb", enumValues))
type CustomString string
require.Error(t, Enum("test", "body", CustomString("a"), enumValues))
require.Nil(t, Enum("test", "body", CustomString("bb"), enumValues))
}
func TestValues_ValidateNilEnum(t *testing.T) {
enumValues := []string{"aa", "bb", "cc"}
require.Error(t, Enum("test", "body", nil, enumValues))
}
// Check edge cases in Enum
func TestValues_Enum_EdgeCases(t *testing.T) {
enumValues := "aa, bb, cc"
// No validation occurs: enumValues is not a slice
require.Nil(t, Enum("test", "body", int64(1), enumValues))
// TODO(TEST): edge case: value is not a concrete type
// It's really a go internals challenge
// to figure a test case to demonstrate
// this case must be checked (!!)
}
func TestValues_ValidateEnumCaseInsensitive(t *testing.T) {
enumValues := []string{"aa", "bb", "cc"}
require.Error(t, EnumCase("test", "body", "a", enumValues, true))
require.Nil(t, EnumCase("test", "body", "bb", enumValues, true))
require.Error(t, EnumCase("test", "body", "BB", enumValues, true))
require.Error(t, EnumCase("test", "body", "a", enumValues, false))
require.Nil(t, EnumCase("test", "body", "bb", enumValues, false))
require.Nil(t, EnumCase("test", "body", "BB", enumValues, false))
require.Error(t, EnumCase("test", "body", int64(1), enumValues, false))
}
func TestValues_ValidateUniqueItems(t *testing.T) {
itemsNonUnique := []interface{}{
[]int32{1, 2, 3, 4, 4, 5},
[]string{"aa", "bb", "cc", "cc", "dd"},
}
for _, v := range itemsNonUnique {
require.Error(t, UniqueItems("test", "body", v))
}
itemsUnique := []interface{}{
[]int32{1, 2, 3},
"I'm a string",
map[string]int{
"aaa": 1111,
"b": 2,
"ccc": 333,
},
nil,
}
for _, v := range itemsUnique {
require.Nil(t, UniqueItems("test", "body", v))
}
}
func TestValues_ValidateMinLength(t *testing.T) {
const minLength = int64(5)
require.Error(t, MinLength("test", "body", "aa", minLength))
require.Nil(t, MinLength("test", "body", "aaaaa", minLength))
}
func TestValues_ValidateMaxLength(t *testing.T) {
const maxLength = int64(5)
require.Error(t, MaxLength("test", "body", "bbbbbb", maxLength))
require.Nil(t, MaxLength("test", "body", "aa", maxLength))
}
func TestValues_ReadOnly(t *testing.T) {
const (
path = "test"
in = "body"
)
ReadOnlySuccess := []interface{}{
"",
0,
nil,
}
// fail only when operation type is request
ReadOnlyFail := []interface{}{
" ",
"bla-bla-bla",
2,
[]interface{}{21, []int{}, "testString"},
}
t.Run("No operation context", func(t *testing.T) {
// readonly should not have any effect
ctx := context.Background()
for _, v := range ReadOnlySuccess {
require.Nil(t, ReadOnly(ctx, path, in, v))
}
for _, v := range ReadOnlyFail {
require.Nil(t, ReadOnly(ctx, path, in, v))
}
})
t.Run("operationType request", func(t *testing.T) {
ctx := WithOperationRequest(context.Background())
for _, v := range ReadOnlySuccess {
require.Nil(t, ReadOnly(ctx, path, in, v))
}
for _, v := range ReadOnlyFail {
require.Error(t, ReadOnly(ctx, path, in, v))
}
})
t.Run("operationType response", func(t *testing.T) {
ctx := WithOperationResponse(context.Background())
for _, v := range ReadOnlySuccess {
require.Nil(t, ReadOnly(ctx, path, in, v))
}
for _, v := range ReadOnlyFail {
require.Nil(t, ReadOnly(ctx, path, in, v))
}
})
}
func TestValues_ValidateRequired(t *testing.T) {
const (
path = "test"
in = "body"
)
RequiredFail := []interface{}{
"",
0,
nil,
}
for _, v := range RequiredFail {
require.Error(t, Required(path, in, v))
}
RequiredSuccess := []interface{}{
" ",
"bla-bla-bla",
2,
[]interface{}{21, []int{}, "testString"},
}
for _, v := range RequiredSuccess {
require.Nil(t, Required(path, in, v))
}
}
func TestValues_ValidateRequiredNumber(t *testing.T) {
require.Error(t, RequiredNumber("test", "body", 0))
require.Nil(t, RequiredNumber("test", "body", 1))
}
func TestValuMultipleOf(t *testing.T) {
// positive
require.Nil(t, MultipleOf("test", "body", 9, 3))
require.Nil(t, MultipleOf("test", "body", 9.3, 3.1))
require.Nil(t, MultipleOf("test", "body", 9.1, 0.1))
require.Nil(t, MultipleOf("test", "body", 3, 0.3))
require.Nil(t, MultipleOf("test", "body", 6, 0.3))
require.Nil(t, MultipleOf("test", "body", 1, 0.25))
require.Nil(t, MultipleOf("test", "body", 8, 0.2))
// zero
require.Error(t, MultipleOf("test", "body", 9, 0))
require.Error(t, MultipleOf("test", "body", 9.1, 0))
// negative
require.Error(t, MultipleOf("test", "body", 3, 0.4))
require.Error(t, MultipleOf("test", "body", 9.1, 0.2))
require.Error(t, MultipleOf("test", "body", 9.34, 0.1))
// error on negative factor
require.Error(t, MultipleOf("test", "body", 9.34, -0.1))
}
// Test edge case for Pattern (in regular spec, no invalid regexp should reach there)
func TestValues_Pattern_Edgecases(t *testing.T) {
require.Nil(t, Pattern("path", "in", "pick-a-boo", `.*-[a-z]-.*`))
t.Run("with invalid regexp", func(t *testing.T) {
err := Pattern("path", "in", "pick-a-boo", `.*-[a(-z]-^).*`)
require.Error(t, err)
assert.Equal(t, int(err.Code()), int(errors.PatternFailCode))
assert.Contains(t, err.Error(), "pattern is invalid")
})
t.Run("with valid regexp, invalid pattern", func(t *testing.T) {
err := Pattern("path", "in", "pick-8-boo", `.*-[a-z]-.*`)
require.Error(t, err)
assert.Equal(t, int(err.Code()), int(errors.PatternFailCode))
assert.NotContains(t, err.Error(), "pattern is invalid")
assert.Contains(t, err.Error(), "should match")
})
}
// Test edge cases in FormatOf
// not easily tested with full specs
func TestValues_FormatOf_EdgeCases(t *testing.T) {
var err *errors.Validation
err = FormatOf("path", "in", "bugz", "", nil)
require.Error(t, err)
assert.Equal(t, int(err.Code()), int(errors.InvalidTypeCode))
assert.Contains(t, err.Error(), "bugz is an invalid type name")
err = FormatOf("path", "in", "bugz", "", strfmt.Default)
require.Error(t, err)
assert.Equal(t, int(err.Code()), int(errors.InvalidTypeCode))
assert.Contains(t, err.Error(), "bugz is an invalid type name")
}
// Test edge cases in MaximumNativeType
// not easily exercised with full specs
func TestValues_MaximumNative(t *testing.T) {
require.Nil(t, MaximumNativeType("path", "in", int(5), 10, false))
require.Nil(t, MaximumNativeType("path", "in", uint(5), 10, true))
require.Nil(t, MaximumNativeType("path", "in", int8(5), 10, true))
require.Nil(t, MaximumNativeType("path", "in", uint8(5), 10, true))
require.Nil(t, MaximumNativeType("path", "in", int16(5), 10, true))
require.Nil(t, MaximumNativeType("path", "in", uint16(5), 10, true))
require.Nil(t, MaximumNativeType("path", "in", int32(5), 10, true))
require.Nil(t, MaximumNativeType("path", "in", uint32(5), 10, true))
require.Nil(t, MaximumNativeType("path", "in", int64(5), 10, true))
require.Nil(t, MaximumNativeType("path", "in", uint64(5), 10, true))
require.Nil(t, MaximumNativeType("path", "in", float32(5.5), 10, true))
require.Nil(t, MaximumNativeType("path", "in", float64(5.5), 10, true))
var err *errors.Validation
err = MaximumNativeType("path", "in", int32(10), 10, true)
require.Error(t, err)
code := int(err.Code())
assert.Equal(t, errors.MaxFailCode, code)
err = MaximumNativeType("path", "in", uint(10), 10, true)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, errors.MaxFailCode, code)
err = MaximumNativeType("path", "in", int64(12), 10, false)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, errors.MaxFailCode, code)
err = MaximumNativeType("path", "in", float32(12.6), 10, false)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, int(errors.MaxFailCode), code)
err = MaximumNativeType("path", "in", float64(12.6), 10, false)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, int(errors.MaxFailCode), code)
err = MaximumNativeType("path", "in", uint(5), -10, true)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, int(errors.MaxFailCode), code)
}
// Test edge cases in MinimumNativeType
// not easily exercised with full specs
func TestValues_MinimumNative(t *testing.T) {
require.Nil(t, MinimumNativeType("path", "in", int(5), 0, false))
require.Nil(t, MinimumNativeType("path", "in", uint(5), 0, true))
require.Nil(t, MinimumNativeType("path", "in", int8(5), 0, true))
require.Nil(t, MinimumNativeType("path", "in", uint8(5), 0, true))
require.Nil(t, MinimumNativeType("path", "in", int16(5), 0, true))
require.Nil(t, MinimumNativeType("path", "in", uint16(5), 0, true))
require.Nil(t, MinimumNativeType("path", "in", int32(5), 0, true))
require.Nil(t, MinimumNativeType("path", "in", uint32(5), 0, true))
require.Nil(t, MinimumNativeType("path", "in", int64(5), 0, true))
require.Nil(t, MinimumNativeType("path", "in", uint64(5), 0, true))
require.Nil(t, MinimumNativeType("path", "in", float32(5.5), 0, true))
require.Nil(t, MinimumNativeType("path", "in", float64(5.5), 0, true))
var err *errors.Validation
err = MinimumNativeType("path", "in", uint(10), 10, true)
require.Error(t, err)
code := int(err.Code())
assert.Equal(t, int(errors.MinFailCode), code)
err = MinimumNativeType("path", "in", uint(10), 10, true)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, int(errors.MinFailCode), code)
err = MinimumNativeType("path", "in", int64(8), 10, false)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, int(errors.MinFailCode), code)
err = MinimumNativeType("path", "in", float32(12.6), 20, false)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, int(errors.MinFailCode), code)
err = MinimumNativeType("path", "in", float64(12.6), 20, false)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, int(errors.MinFailCode), code)
require.Nil(t, MinimumNativeType("path", "in", uint(5), -10, true))
}
// Test edge cases in MaximumNativeType
// not easily exercised with full specs
func TestValues_MultipleOfNative(t *testing.T) {
require.Nil(t, MultipleOfNativeType("path", "in", int(5), 1))
require.Nil(t, MultipleOfNativeType("path", "in", uint(5), 1))
require.Nil(t, MultipleOfNativeType("path", "in", int8(5), 1))
require.Nil(t, MultipleOfNativeType("path", "in", uint8(5), 1))
require.Nil(t, MultipleOfNativeType("path", "in", int16(5), 1))
require.Nil(t, MultipleOfNativeType("path", "in", uint16(5), 1))
require.Nil(t, MultipleOfNativeType("path", "in", int32(5), 1))
require.Nil(t, MultipleOfNativeType("path", "in", uint32(5), 1))
require.Nil(t, MultipleOfNativeType("path", "in", int64(5), 1))
require.Nil(t, MultipleOfNativeType("path", "in", uint64(5), 1))
var err *errors.Validation
err = MultipleOfNativeType("path", "in", int64(5), 0)
require.Error(t, err)
code := int(err.Code())
assert.Equal(t, int(errors.MultipleOfMustBePositiveCode), code)
err = MultipleOfNativeType("path", "in", uint64(5), 0)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, int(errors.MultipleOfMustBePositiveCode), code)
err = MultipleOfNativeType("path", "in", int64(5), -1)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, int(errors.MultipleOfMustBePositiveCode), code)
err = MultipleOfNativeType("path", "in", int64(11), 5)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, int(errors.MultipleOfFailCode), code)
err = MultipleOfNativeType("path", "in", uint64(11), 5)
require.Error(t, err)
code = int(err.Code())
assert.Equal(t, int(errors.MultipleOfFailCode), code)
}
// Test edge cases in IsValueValidAgainstRange
// not easily exercised with full specs: we did not simulate these formats in full specs
func TestValues_IsValueValidAgainstRange(t *testing.T) {
require.NoError(t, IsValueValidAgainstRange(float32(123.45), "number", "float32", "prefix", "path"))
require.NoError(t, IsValueValidAgainstRange(float64(123.45), "number", "float32", "prefix", "path"))
require.NoError(t, IsValueValidAgainstRange(int64(123), "number", "float", "prefix", "path"))
require.NoError(t, IsValueValidAgainstRange(int64(123), "integer", "", "prefix", "path"))
require.NoError(t, IsValueValidAgainstRange(int64(123), "integer", "int64", "prefix", "path"))
require.NoError(t, IsValueValidAgainstRange(int64(123), "integer", "uint64", "prefix", "path"))
require.NoError(t, IsValueValidAgainstRange(int64(2147483647), "integer", "int32", "prefix", "path"))
require.NoError(t, IsValueValidAgainstRange(int64(2147483647), "integer", "uint32", "prefix", "path"))
var err error
// Error case (do not occur in normal course of a validation)
err = IsValueValidAgainstRange(float64(math.MaxFloat64), "integer", "", "prefix", "path")
require.Error(t, err)
assert.Contains(t, err.Error(), "must be of type integer (default format)")
// Checking a few limits
err = IsValueValidAgainstRange("123", "number", "", "prefix", "path")
require.Error(t, err)
assert.Contains(t, err.Error(), "called with invalid (non numeric) val type")
}