-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkyte_test.go
573 lines (501 loc) · 15 KB
/
kyte_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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package kyte
import (
"errors"
"testing"
)
type TestTodo struct {
ID string `bson:"id"`
Name string `bson:"name"`
Message string `bson:"message"`
}
type TestArrayStruct struct {
UserID string `bson:"user_id"`
Username string `bson:"username"`
Type string `bson:"type"`
Todos []TestTodo `bson:"todos"`
}
type TestArrayStructWithPointer struct {
UserID string `bson:"user_id"`
Username string `bson:"username"`
Type string `bson:"type"`
Todos *[]TestTodo `bson:"todos"`
}
type TestStructWithPointerArray struct {
UserID string `bson:"user_id"`
Username string `bson:"username"`
Type string `bson:"type"`
Todos []*TestTodo `bson:"todos"`
}
type TestWithNestedStruct struct {
UserID string `bson:"user_id"`
Username string `bson:"username"`
Type string `bson:"type"`
Todo TestTodo `bson:"todo"`
}
type TestWithNestedStructWithPointer struct {
UserID string `bson:"user_id"`
Username string `bson:"username"`
Type string `bson:"type"`
Todo *TestTodo `bson:"todo"`
}
func testKyteFieldAndSource(t *testing.T, kyte *kyte, fields map[any]string, fieldCount int) {
if len(kyte.fields) != fieldCount {
t.Errorf("kyte.fields should be %v but got %v", fieldCount, len(kyte.fields))
}
if len(kyte.fieldNames) != fieldCount {
t.Errorf("kyte.fieldNames should be %v but got %v", fieldCount, len(kyte.fieldNames))
}
for ptr, field := range fields {
if _, ok := kyte.fields[ptr]; !ok {
t.Errorf("kyte.fields should have %v field", field)
}
}
}
func TestNewKyte(t *testing.T) {
t.Parallel()
t.Run("with source nil should return empty kyte", func(t *testing.T) {
kyte := newKyte(nil, true)
if kyte.source != nil {
t.Errorf("kyte.source should be nil but got %v", kyte.source)
}
testKyteFieldAndSource(t, kyte, nil, 0)
})
t.Run("with source not nil should return kyte with fields", func(t *testing.T) {
t.Run("zero value", func(t *testing.T) {
source := &TestArrayStruct{}
fields := map[any]string{
&source.UserID: "user_id",
&source.Username: "username",
&source.Type: "type",
&source.Todos: "todos",
}
kyte := newKyte(source, true)
if kyte.source == nil {
t.Errorf("kyte.source should not be nil")
}
testKyteFieldAndSource(t, kyte, fields, 7)
})
t.Run("non zero value", func(t *testing.T) {
source := &TestArrayStruct{
UserID: "user_id",
Username: "username",
Type: "type",
Todos: []TestTodo{},
}
fields := map[any]string{
&source.UserID: "user_id",
&source.Username: "username",
&source.Type: "type",
&source.Todos: "todos",
}
kyte := newKyte(source, true)
if kyte.source == nil {
t.Errorf("kyte.source should not be nil")
}
testKyteFieldAndSource(t, kyte, fields, 7)
})
})
t.Run("with source with pointer of struct should return kyte with fields", func(t *testing.T) {
t.Run("zero value", func(t *testing.T) {
source := &TestArrayStructWithPointer{}
fields := map[any]string{
&source.UserID: "user_id",
&source.Username: "username",
&source.Type: "type",
&source.Todos: "todos",
}
kyte := newKyte(source, true)
if kyte.source == nil {
t.Errorf("kyte.source should not be nil")
}
testKyteFieldAndSource(t, kyte, fields, 7)
})
t.Run("non zero value", func(t *testing.T) {
source := &TestArrayStructWithPointer{Todos: &[]TestTodo{}}
fields := map[any]string{
&source.UserID: "user_id",
&source.Username: "username",
&source.Type: "type",
&source.Todos: "todos",
}
kyte := newKyte(source, true)
if kyte.source == nil {
t.Errorf("kyte.source should not be nil")
}
testKyteFieldAndSource(t, kyte, fields, 7)
})
})
t.Run("with source with array of pointer of struct should return kyte with fields", func(t *testing.T) {
t.Run("zero value", func(t *testing.T) {
source := &TestStructWithPointerArray{}
fields := map[any]string{
&source.UserID: "user_id",
&source.Todos: "todos",
&source.Type: "type",
}
kyte := newKyte(source, true)
if kyte.source == nil {
t.Errorf("kyte.source should not be nil")
}
testKyteFieldAndSource(t, kyte, fields, 7)
})
t.Run("non zero value", func(t *testing.T) {
source := &TestStructWithPointerArray{Todos: []*TestTodo{}}
fields := map[any]string{
&source.UserID: "user_id",
&source.Todos: "todos",
&source.Type: "type",
}
kyte := newKyte(source, true)
if kyte.source == nil {
t.Errorf("kyte.source should not be nil")
}
testKyteFieldAndSource(t, kyte, fields, 7)
})
})
t.Run("with source with nested struct should return kyte with fields", func(t *testing.T) {
t.Run("zero value", func(t *testing.T) {
source := &TestWithNestedStruct{}
fields := map[any]string{
&source.UserID: "user_id",
&source.Username: "username",
&source.Type: "type",
&source.Todo: "todo",
&source.Todo.ID: "todo.id",
&source.Todo.Name: "todo.name",
&source.Todo.Message: "todo.message",
}
kyte := newKyte(source, true)
if kyte.source == nil {
t.Errorf("kyte.source should not be nil")
}
testKyteFieldAndSource(t, kyte, fields, 7)
})
t.Run("non zero value", func(t *testing.T) {
source := &TestWithNestedStruct{Todo: TestTodo{}}
fields := map[any]string{
&source.UserID: "user_id",
&source.Username: "username",
&source.Type: "type",
&source.Todo: "todo",
&source.Todo.ID: "todo.id",
&source.Todo.Name: "todo.name",
&source.Todo.Message: "todo.message",
}
kyte := newKyte(source, true)
if kyte.source == nil {
t.Errorf("kyte.source should not be nil")
}
testKyteFieldAndSource(t, kyte, fields, 7)
})
})
t.Run("with source with nested struct with pointer should return kyte with fields", func(t *testing.T) {
source := &TestWithNestedStructWithPointer{Todo: &TestTodo{}}
fields := map[any]string{
&source.UserID: "user_id",
&source.Username: "username",
&source.Type: "type",
&source.Todo: "todo",
&source.Todo.ID: "todo.id",
&source.Todo.Name: "todo.name",
&source.Todo.Message: "todo.message",
}
kyte := newKyte(source, true)
testKyteFieldAndSource(t, kyte, fields, 7)
})
t.Run("not pointer source should return kyte with error", func(t *testing.T) {
source := TestArrayStruct{}
kyte := newKyte(source, true)
if !kyte.hasErrors() {
t.Errorf("kyte should have errors")
}
if kyte.err != ErrNotPtrSource {
t.Errorf("kyte should have error %v but got %v", ErrNotPtrSource, kyte.err)
}
})
t.Run("source unexported field should not be added to kyte", func(t *testing.T) {
type TestAnonymousStruct struct {
Name string `bson:"name"`
}
source := &TestAnonymousStruct{}
fields := map[any]string{
&source.Name: "name",
}
kyte := newKyte(source, true)
testKyteFieldAndSource(t, kyte, fields, 1)
})
t.Run("array of pointer of struct non zero value ", func(t *testing.T) {
type TestAnonymousWithSlice struct {
Name string `bson:"name"`
Todos []*TestTodo `bson:"todos"`
}
todo := TestTodo{}
source := &TestAnonymousWithSlice{Todos: []*TestTodo{
&todo,
}}
fields := map[any]string{
&source.Name: "name",
&source.Todos: "todos",
&todo.ID: "todos.id",
&todo.Name: "todos.name",
&todo.Message: "todos.message",
}
kyte := newKyte(source, true)
testKyteFieldAndSource(t, kyte, fields, 5)
})
t.Run("array of pointer of struct zero value ", func(t *testing.T) {
type TestAnonymousWithSlice struct {
Name string `bson:"name"`
Todos []*TestTodo `bson:"todos"`
}
source := &TestAnonymousWithSlice{}
fields := map[any]string{
&source.Name: "name",
&source.Todos: "todos",
}
kyte := newKyte(source, true)
testKyteFieldAndSource(t, kyte, fields, 5)
})
t.Run("pointer array of pointer of struct non zero value ", func(t *testing.T) {
type TestAnonymousWithSlicePointer struct {
Name string `bson:"name"`
Todos *[]*TestTodo `bson:"todos"`
}
todo := TestTodo{}
source := &TestAnonymousWithSlicePointer{Todos: &[]*TestTodo{
&todo,
}}
fields := map[any]string{
&source.Name: "name",
&source.Todos: "todos",
&todo.ID: "todos.id",
&todo.Name: "todos.name",
&todo.Message: "todos.message",
}
kyte := newKyte(source, true)
testKyteFieldAndSource(t, kyte, fields, 5)
})
t.Run("pointer array of pointer of struct zero value ", func(t *testing.T) {
type TestAnonymousWithSlicePointer struct {
Name string `bson:"name"`
Todos *[]*TestTodo `bson:"todos"`
}
source := &TestAnonymousWithSlicePointer{}
fields := map[any]string{
&source.Name: "name",
&source.Todos: "todos",
}
kyte := newKyte(source, true)
testKyteFieldAndSource(t, kyte, fields, 5)
})
t.Run("ignore field if does not have bson tag", func(t *testing.T) {
type TestAnonymousWithSlicePointer struct {
Name string `bson:"name"`
Todo *TestTodo `bson:"todo"`
Age int `bson:"-"`
Time string
}
todo := TestTodo{}
source := &TestAnonymousWithSlicePointer{
Todo: &todo,
}
fields := map[any]string{
&source.Name: "name",
&source.Todo: "todo",
&todo.ID: "todo.id",
&todo.Name: "todo.name",
}
kyte := newKyte(source, true)
testKyteFieldAndSource(t, kyte, fields, 5)
})
}
func TestKyteErros(t *testing.T) {
t.Parallel()
t.Run("not nil errors", func(t *testing.T) {
kyte := newKyte("str", true)
if kyte.err == nil {
t.Errorf("kyte.Errors() should not be nil but got %v", kyte.err)
}
})
t.Run("nil errors", func(t *testing.T) {
kyte := newKyte(nil, true)
if kyte.err != nil {
t.Errorf("kyte.Errors() should be nil but got %v", kyte.err)
}
})
}
func TestKyteHasErrors(t *testing.T) {
t.Parallel()
t.Run("with errors", func(t *testing.T) {
kyte := newKyte("str", true)
if !kyte.hasErrors() {
t.Errorf("kyte.hasErrors() should return true")
}
})
t.Run("without errors", func(t *testing.T) {
kyte := newKyte(nil, true)
if kyte.hasErrors() {
t.Errorf("kyte.hasErrors() should return false")
}
})
}
func TestKyteSetError(t *testing.T) {
t.Run("with error", func(t *testing.T) {
kyte := newKyte(nil, true)
kyte.setError(ErrNotPtrSource)
if kyte.err == nil {
t.Errorf("kyte.Errors() should not be nil but got %v", kyte.err)
}
})
}
func TestKyteValidate(t *testing.T) {
t.Parallel()
t.Run("with errors", func(t *testing.T) {
kyte := newKyte("str", true)
err := kyte.validate(&operation{
field: "field",
value: "value"},
)
if err == nil {
t.Errorf("kyte.validate() should return error")
}
})
t.Run("field is required but nil", func(t *testing.T) {
kyte := newKyte(nil, true)
err := kyte.validate(&operation{
field: nil,
value: "value",
isFieldRequired: true,
})
if err != ErrNilField {
t.Errorf("kyte.validate() should return error %v but got %v", ErrNilField, err)
}
})
t.Run("field is required but not string or pointer", func(t *testing.T) {
kyte := newKyte(nil, true)
err := kyte.validate(&operation{
field: 1,
value: "value",
isFieldRequired: true,
})
if err != ErrFieldMustBePtrOrString {
t.Errorf("kyte.validate() should return error %v but got %v", ErrFieldMustBePtrOrString, err)
}
})
t.Run("field is required but string and empty", func(t *testing.T) {
kyte := newKyte(nil, true)
err := kyte.validate(&operation{
field: "",
value: "value",
isFieldRequired: true,
})
if err != ErrEmptyField {
t.Errorf("kyte.validate() should return error %v but got %v", ErrEmptyField, err)
}
})
t.Run("field is required and check field and field valid", func(t *testing.T) {
kyte := newKyte(&TestTodo{}, true)
err := kyte.validate(&operation{
field: "name",
value: "value",
isFieldRequired: true,
})
if err != nil {
t.Errorf("kyte.validate() should not return error but got %v", err)
}
})
t.Run("field is required and check field and field invalid", func(t *testing.T) {
kyte := newKyte(&TestTodo{}, true)
err := kyte.validate(&operation{
field: "invalid",
value: "value",
isFieldRequired: true,
})
if !errors.Is(err, ErrNotValidFieldForQuery) {
t.Errorf("kyte.validate() should return error %v but got %v", ErrNotValidFieldForQuery, err)
}
})
}
func TestKyteIsFieldValid(t *testing.T) {
t.Parallel()
t.Run("with errors", func(t *testing.T) {
kyte := newKyte("str", true)
err := kyte.isFieldValid("field")
if err == nil {
t.Errorf("kyte.isFieldValid() should return error")
}
})
t.Run("string field and valid", func(t *testing.T) {
kyte := newKyte(&TestTodo{}, true)
err := kyte.isFieldValid("name")
if err != nil {
t.Errorf("kyte.isFieldValid() should not return error but got %v", err)
}
})
t.Run("string field and invalid", func(t *testing.T) {
kyte := newKyte(&TestTodo{}, true)
err := kyte.isFieldValid("invalid")
if !errors.Is(err, ErrNotValidFieldForQuery) {
t.Errorf("kyte.isFieldValid() should return error %v but got %v", ErrNotValidFieldForQuery, err)
}
})
t.Run("pointer field and valid", func(t *testing.T) {
todo := &TestTodo{}
kyte := newKyte(todo, true)
err := kyte.isFieldValid(&todo.Message)
if err != nil {
t.Errorf("kyte.isFieldValid() should not return error but got %v", err)
}
})
}
func TestKyteGetFieldName(t *testing.T) {
t.Parallel()
t.Run("string field", func(t *testing.T) {
kyte := newKyte(&TestTodo{}, true)
field, err := kyte.getFieldName("name")
if err != nil {
t.Errorf("kyte.getFieldName() should not return error but got %v", err)
}
if field != "name" {
t.Errorf("kyte.getFieldName() should return name but got %v", field)
}
})
t.Run("pointer field", func(t *testing.T) {
todo := &TestTodo{}
kyte := newKyte(todo, true)
field, err := kyte.getFieldName(&todo.Message)
if err != nil {
t.Errorf("kyte.getFieldName() should not return error but got %v", err)
}
if field != "message" {
t.Errorf("kyte.getFieldName() should return message but got %v", field)
}
})
t.Run("pointer field and not in fields", func(t *testing.T) {
type temp struct {
Temp string `bson:"temp"`
}
tempS := &temp{}
todo := &TestTodo{}
kyte := newKyte(todo, true)
_, err := kyte.getFieldName(&tempS.Temp)
if !errors.Is(err, ErrNotValidFieldForQuery) {
t.Errorf("kyte.getFieldName() should return error %v but got %v", ErrNotValidFieldForQuery, err)
}
})
t.Run("pointer field and field is nil", func(t *testing.T) {
todo := &TestTodo{}
kyte := newKyte(nil, false)
_, err := kyte.getFieldName(&todo.ID)
if err != ErrFieldMustBeString {
t.Errorf("kyte.getFieldName() should return error %v but got %v", ErrFieldMustBeString, err)
}
})
t.Run("field is not string or pointer", func(t *testing.T) {
kyte := newKyte(nil, false)
_, err := kyte.getFieldName(1)
if err != ErrFieldMustBePtrOrString {
t.Errorf("kyte.getFieldName() should return error %v but got %v", ErrFieldMustBePtrOrString, err)
}
})
}