-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_test.go
More file actions
507 lines (461 loc) · 15.6 KB
/
array_test.go
File metadata and controls
507 lines (461 loc) · 15.6 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package schema
import (
"fmt"
"reflect"
"strings"
"testing"
)
// Test Array Schema Basic Validation
func TestArraySchema_Basic(t *testing.T) {
ctx := DefaultValidationContext()
schema := Array(String())
tests := []struct {
name string
value interface{}
expected bool
}{
{"valid string array", []string{"hello", "world"}, true},
{"valid interface array with strings", []interface{}{"hello", "world"}, true},
{"empty array", []string{}, true},
{"single item", []string{"hello"}, true},
{"invalid item type", []interface{}{"hello", 123}, false}, // 123 should fail String validation
{"not an array", "hello", false},
{"number", 123, false},
{"boolean", true, false},
{"nil", nil, false},
{"object", map[string]interface{}{"key": "value"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Array.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if len(result.Errors) > 0 {
t.Errorf("Error: %s", result.Errors[0].Message)
}
}
})
}
}
// Test Array Schema with Different Item Types
func TestArraySchema_ItemTypes(t *testing.T) {
ctx := DefaultValidationContext()
tests := []struct {
name string
schema *ArraySchema
value interface{}
expected bool
}{
{"int array valid", Array(Int()), []int{1, 2, 3}, true},
{"int array invalid", Array(Int()), []string{"1", "2", "3"}, false},
{"bool array valid", Array(Bool()), []bool{true, false, true}, true},
{"bool array invalid", Array(Bool()), []int{1, 0, 1}, false},
{"nested array valid", Array(Array(String())), [][]string{{"a", "b"}, {"c", "d"}}, true},
{"nested array invalid", Array(Array(String())), [][]interface{}{{"a", 123}, {"c", "d"}}, false},
{"object array valid", Array(Object().Property("name", String())), []map[string]interface{}{{"name": "John"}, {"name": "Jane"}}, true},
{"object array invalid", Array(Object().Property("name", String())), []map[string]interface{}{{"name": "John"}, {"age": 30}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Array.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if len(result.Errors) > 0 {
t.Errorf("Error: %s", result.Errors[0].Message)
}
}
})
}
}
// Test Array Length Constraints
func TestArraySchema_Length(t *testing.T) {
ctx := DefaultValidationContext()
tests := []struct {
name string
schema *ArraySchema
value interface{}
expected bool
}{
// MinItems tests
{"min items valid", Array(String()).MinItems(2), []string{"a", "b", "c"}, true},
{"min items exact", Array(String()).MinItems(2), []string{"a", "b"}, true},
{"min items invalid", Array(String()).MinItems(2), []string{"a"}, false},
{"min items empty", Array(String()).MinItems(1), []string{}, false},
// MaxItems tests
{"max items valid", Array(String()).MaxItems(3), []string{"a", "b"}, true},
{"max items exact", Array(String()).MaxItems(3), []string{"a", "b", "c"}, true},
{"max items invalid", Array(String()).MaxItems(3), []string{"a", "b", "c", "d"}, false},
{"max items empty", Array(String()).MaxItems(2), []string{}, true},
// Range tests
{"range valid", Array(String()).MinItems(2).MaxItems(4), []string{"a", "b", "c"}, true},
{"range min exact", Array(String()).MinItems(2).MaxItems(4), []string{"a", "b"}, true},
{"range max exact", Array(String()).MinItems(2).MaxItems(4), []string{"a", "b", "c", "d"}, true},
{"range too few", Array(String()).MinItems(2).MaxItems(4), []string{"a"}, false},
{"range too many", Array(String()).MinItems(2).MaxItems(4), []string{"a", "b", "c", "d", "e"}, false},
// Length tests (exact)
{"exact length valid", Array(String()).Length(3), []string{"a", "b", "c"}, true},
{"exact length invalid short", Array(String()).Length(3), []string{"a", "b"}, false},
{"exact length invalid long", Array(String()).Length(3), []string{"a", "b", "c", "d"}, false},
{"exact length zero", Array(String()).Length(0), []string{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Array.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if len(result.Errors) > 0 {
t.Errorf("Error: %s", result.Errors[0].Message)
}
}
})
}
}
// Test Array Unique Items
func TestArraySchema_UniqueItems(t *testing.T) {
ctx := DefaultValidationContext()
stringSchema := Array(String()).UniqueItems()
anySchema := Array(Any()).UniqueItems()
tests := []struct {
name string
schema *ArraySchema
value interface{}
expected bool
}{
{"unique strings", stringSchema, []string{"a", "b", "c"}, true},
{"duplicate strings", stringSchema, []string{"a", "b", "a"}, false},
{"empty array", stringSchema, []string{}, true},
{"single item", stringSchema, []string{"a"}, true},
{"case sensitive", stringSchema, []string{"Hello", "hello"}, true},
{"numbers unique", anySchema, []interface{}{1, 2, 3}, true},
{"numbers duplicate", anySchema, []interface{}{1, 2, 1}, false},
{"mixed types unique", anySchema, []interface{}{"hello", 42, true}, true},
{"mixed types duplicate", anySchema, []interface{}{"hello", 42, "hello"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Array.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if len(result.Errors) > 0 {
t.Errorf("Error: %s", result.Errors[0].Message)
}
}
})
}
}
// Test Array Required/Optional/Nullable
func TestArraySchema_RequiredOptionalNullable(t *testing.T) {
ctx := DefaultValidationContext()
tests := []struct {
name string
schema *ArraySchema
value interface{}
expected bool
}{
// Required tests (default)
{"required with array", Array(String()), []string{"a"}, true},
{"required with nil", Array(String()), nil, false},
// Optional tests
{"optional with array", Array(String()).Optional(), []string{"a"}, true},
{"optional with nil", Array(String()).Optional(), nil, true},
// Nullable tests
{"nullable with array", Array(String()).Nullable(), []string{"a"}, true},
{"nullable with nil", Array(String()).Nullable(), nil, true},
// Optional + Nullable
{"optional nullable with array", Array(String()).Optional().Nullable(), []string{"a"}, true},
{"optional nullable with nil", Array(String()).Optional().Nullable(), nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Array.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if len(result.Errors) > 0 {
t.Errorf("Error: %s", result.Errors[0].Message)
}
}
})
}
}
// Test Array Default Values
func TestArraySchema_DefaultValues(t *testing.T) {
ctx := DefaultValidationContext()
tests := []struct {
name string
schema *ArraySchema
value interface{}
expected bool
expectedValue interface{}
}{
{"default used for nil", Array(String()).Default([]string{"default"}), nil, true, []string{"default"}},
{"default not used for valid array", Array(String()).Default([]string{"default"}), []string{"actual"}, true, []string{"actual"}},
{"optional with default", Array(String()).Optional().Default([]string{"default"}), nil, true, []string{"default"}},
{"nullable with default keeps nil", Array(String()).Nullable().Default([]string{"default"}), nil, true, nil}, // nullable schemas return nil directly
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Array.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if len(result.Errors) > 0 {
t.Errorf("Error: %s", result.Errors[0].Message)
}
return
}
if result.Valid && tt.expectedValue != nil {
// Convert both values to strings for comparison to avoid reflection issues
expectedStr := fmt.Sprintf("%v", tt.expectedValue)
actualStr := fmt.Sprintf("%v", result.Value)
if expectedStr != actualStr {
t.Errorf("Array.Parse(%v) value = %v (%T), want %v (%T)", tt.value, result.Value, result.Value, tt.expectedValue, tt.expectedValue)
}
}
})
}
}
// Test Array Complex Combinations
func TestArraySchema_ComplexCombinations(t *testing.T) {
ctx := DefaultValidationContext()
tests := []struct {
name string
schema *ArraySchema
value interface{}
expected bool
}{
{
"string array with all constraints",
Array(String().MinLength(3)).MinItems(2).MaxItems(4).UniqueItems(),
[]string{"hello", "world", "test"},
true,
},
{
"string array too short item",
Array(String().MinLength(3)).MinItems(2).MaxItems(4).UniqueItems(),
[]string{"hello", "hi", "test"},
false,
},
{
"string array too few items",
Array(String().MinLength(3)).MinItems(2).MaxItems(4).UniqueItems(),
[]string{"hello"},
false,
},
{
"string array not unique",
Array(String().MinLength(3)).MinItems(2).MaxItems(4).UniqueItems(),
[]string{"hello", "world", "hello"},
false,
},
{
"integer array with constraints",
Array(Int().Min(0).Max(100)).Length(3),
[]int{10, 50, 90},
true,
},
{
"integer array out of range",
Array(Int().Min(0).Max(100)).Length(3),
[]int{10, 150, 90},
false,
},
{
"nested array validation",
Array(Array(String().MinLength(2)).MinItems(1)),
[][]string{{"hello", "world"}, {"test", "case"}},
true,
},
{
"nested array invalid inner",
Array(Array(String().MinLength(2)).MinItems(1)),
[][]string{{"hello", "world"}, {"a"}},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Array.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if len(result.Errors) > 0 {
t.Errorf("Error: %s", result.Errors[0].Message)
}
}
})
}
}
// Test Array JSON Schema Generation
func TestArraySchema_JSON(t *testing.T) {
tests := []struct {
name string
schema *ArraySchema
expectedFields map[string]interface{}
}{
{
"basic array",
Array(String()),
map[string]interface{}{
"type": "array",
"items": map[string]interface{}{"type": "string"},
},
},
{
"array with constraints",
Array(String()).MinItems(2).MaxItems(5).UniqueItems(),
map[string]interface{}{
"type": "array",
"items": map[string]interface{}{"type": "string"},
"minItems": 2,
"maxItems": 5,
"uniqueItems": true,
},
},
{
"nullable array",
Array(String()).Nullable(),
map[string]interface{}{
"type": []string{"array", "null"},
"items": map[string]interface{}{"type": "string"},
},
},
{
"array with title and description",
Array(String()).Title("Names").Description("List of names"),
map[string]interface{}{
"type": "array",
"title": "Names",
"description": "List of names",
"items": map[string]interface{}{"type": "string"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.schema.JSON()
for key, expected := range tt.expectedFields {
actual, exists := result[key]
if !exists {
t.Errorf("Expected field %s not found in JSON", key)
continue
}
// Special handling for slice comparisons
if expectedSlice, ok := expected.([]interface{}); ok {
if actualSlice, ok := actual.([]interface{}); ok {
if len(expectedSlice) != len(actualSlice) {
t.Errorf("Field %s length = %d, want %d", key, len(actualSlice), len(expectedSlice))
continue
}
allMatch := true
for i, expectedItem := range expectedSlice {
if actualSlice[i] != expectedItem {
allMatch = false
break
}
}
if !allMatch {
t.Errorf("Field %s = %v, want %v", key, actual, expected)
}
continue
} else {
t.Errorf("Field %s: expected slice but got %T: %v", key, actual, actual)
continue
}
}
// Handle []string comparisons
if expectedStringSlice, ok := expected.([]string); ok {
if actualStringSlice, ok := actual.([]string); ok {
if !reflect.DeepEqual(actualStringSlice, expectedStringSlice) {
t.Errorf("Field %s = %v, want %v", key, actual, expected)
}
continue
} else {
t.Errorf("Field %s: expected string slice but got %T: %v", key, actual, actual)
continue
}
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Field %s = %v, want %v", key, actual, expected)
}
}
})
}
}
// Test Array Edge Cases
func TestArraySchema_EdgeCases(t *testing.T) {
ctx := DefaultValidationContext()
tests := []struct {
name string
schema *ArraySchema
value interface{}
expected bool
}{
// Empty arrays
{"empty array with min items 0", Array(String()).MinItems(0), []string{}, true},
{"empty array with unique items", Array(String()).UniqueItems(), []string{}, true},
// Large arrays
{"large array", Array(String()), make([]string, 1000), true},
// Mixed type handling
{"interface slice with consistent types", Array(Any()), []interface{}{"string", 123, true, nil}, true},
// Nil item schemas edge case
{"array with nil items allowed", Array(Any()), []interface{}{"valid", nil, "items"}, true},
// Zero constraints
{"max items zero", Array(String()).MaxItems(0), []string{}, true},
{"max items zero with content", Array(String()).MaxItems(0), []string{"item"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create large array for the large array test
if tt.name == "large array" {
largeArray := make([]string, 1000)
for i := 0; i < 1000; i++ {
largeArray[i] = fmt.Sprintf("item%d", i)
}
tt.value = largeArray
}
result := tt.schema.Parse(tt.value, ctx)
if result.Valid != tt.expected {
t.Errorf("Array.Parse(%v) = %v, want %v", tt.value, result.Valid, tt.expected)
if len(result.Errors) > 0 {
t.Errorf("Error: %s", result.Errors[0].Message)
}
}
})
}
}
// Test Array Error Messages
func TestArraySchema_ErrorMessages(t *testing.T) {
ctx := DefaultValidationContext()
tests := []struct {
name string
schema *ArraySchema
value interface{}
expectedContain string
}{
{"type error", Array(String()), "not an array", "must be an array"},
{"min items error", Array(String()).MinItems(3), []string{"a", "b"}, "at least 3 items"},
{"max items error", Array(String()).MaxItems(2), []string{"a", "b", "c"}, "at most 2 items"},
{"unique items error", Array(String()).UniqueItems(), []string{"a", "b", "a"}, "unique items"},
{"item validation error", Array(String().MinLength(3)), []string{"hello", "hi"}, "invalid"},
{"required error", Array(String()), nil, "required"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.schema.Parse(tt.value, ctx)
if result.Valid {
t.Errorf("Expected validation to fail for %s", tt.name)
return
}
if len(result.Errors) == 0 {
t.Errorf("Expected error message for %s", tt.name)
return
}
errorMsg := result.Errors[0].Message
if !contains(errorMsg, tt.expectedContain) {
t.Errorf("Error message '%s' should contain '%s'", errorMsg, tt.expectedContain)
}
})
}
}
// Helper function to check if a string contains a substring
func contains(s, substr string) bool {
return strings.Contains(s, substr)
}