-
Notifications
You must be signed in to change notification settings - Fork 25
/
model_test.go
2194 lines (1736 loc) · 55.3 KB
/
model_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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm).
// go-model source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package model
import (
"errors"
"fmt"
"os"
"reflect"
"strconv"
"testing"
"time"
)
//
// Copy test cases
//
func TestConverter(t *testing.T) {
type SampleStructA struct {
Int int
String string
Mixed string
}
type SampleStructB struct {
Int int
String string
Mixed int
}
AddConversion((*int)(nil), (*string)(nil), func(in reflect.Value) (reflect.Value, error) {
return reflect.ValueOf(strconv.FormatInt(in.Int(), 10) + "lala"), nil
})
src := SampleStructB{Mixed: 123, Int: 5, String: "string"}
dst := SampleStructA{}
errs := Copy(&dst, src)
if errs != nil {
t.Error("Error occurred while copying.")
}
assertEqual(t, "123lala", dst.Mixed)
assertEqual(t, 5, dst.Int)
assertEqual(t, "string", dst.String)
}
func TestMissingConverter(t *testing.T) {
type SampleStructA struct {
Int int
String string
Mixed string
}
type SampleStructB struct {
Int int
String string
Mixed int
}
RemoveConversion((*int)(nil), (*string)(nil))
src := SampleStructB{Mixed: 123, Int: 5, String: "string"}
dst := SampleStructA{}
errs := Copy(&dst, src)
if errs == nil {
t.Error("The conversion between int and string should have failed.")
}
assertEqual(t, "", dst.Mixed)
assertEqual(t, 5, dst.Int)
assertEqual(t, "string", dst.String)
}
func TestCopyIntegerAndIntegerPtr(t *testing.T) {
type SampleStruct struct {
Int int
IntPtr *int
Int64 int64
Int64Ptr *int64
}
intPtr := int(1001)
int64Ptr := int64(1002)
src := SampleStruct{
Int: 2001,
IntPtr: &intPtr,
Int64: 2002,
Int64Ptr: &int64Ptr,
}
dst := SampleStruct{}
errs := Copy(&dst, src)
if errs != nil {
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, src.Int, dst.Int)
assertEqual(t, src.Int64, dst.Int64)
assertEqual(t, true, src.IntPtr != dst.IntPtr)
assertEqual(t, *src.IntPtr, *dst.IntPtr)
assertEqual(t, *src.Int64Ptr, *dst.Int64Ptr)
}
func TestCopyStringAndStringPtr(t *testing.T) {
type SampleStruct struct {
String string
StringPtr *string
}
strPtr := "This is string for pointer test"
src := SampleStruct{
String: "This is string for test",
StringPtr: &strPtr,
}
dst := SampleStruct{}
errs := Copy(&dst, &src)
if errs != nil {
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, src.String, dst.String)
assertEqual(t, *src.StringPtr, *dst.StringPtr)
assertEqual(t, true, src.StringPtr != dst.StringPtr)
}
func TestCopyBooleanAndBooleanPtr(t *testing.T) {
type SampleStruct struct {
Boolean bool
BooleanPtr *bool
BooleanOmitEmpty bool `model:",omitempty"`
BooleanOmitEmptyPtr *bool `model:",omitempty"`
}
boolPtr := true
src := SampleStruct{
Boolean: true,
BooleanPtr: &boolPtr,
}
dst := SampleStruct{}
errs := Copy(&dst, &src)
if errs != nil {
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, src.Boolean, dst.Boolean)
assertEqual(t, *src.BooleanPtr, *dst.BooleanPtr)
assertEqual(t, true, src.BooleanPtr != dst.BooleanPtr)
assertEqual(t, false, dst.BooleanOmitEmpty)
assertEqual(t, true, dst.BooleanOmitEmptyPtr == nil)
}
func TestCopyFloatAndFloatPtr(t *testing.T) {
type SampleStruct struct {
Float32 float32
Float64 float64
Float32PtrOmit *float32 `model:"-"`
Float32Ptr *float32
Float64Ptr *float64
}
f32 := float32(0.1)
f64 := float64(0.2)
src := SampleStruct{
Float32: float32(0.11),
Float32Ptr: &f32,
Float64: float64(0.22),
Float64Ptr: &f64,
}
dst := SampleStruct{}
errs := Copy(&dst, &src)
if errs != nil {
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, src.Float32, dst.Float32)
assertEqual(t, *src.Float32Ptr, *dst.Float32Ptr)
assertEqual(t, src.Float64, dst.Float64)
assertEqual(t, *src.Float64Ptr, *dst.Float64Ptr)
assertEqual(t, true, src.Float32Ptr != dst.Float32Ptr)
assertEqual(t, true, src.Float64Ptr != dst.Float64Ptr)
}
func TestCopySliceStringAndSliceStringPtr(t *testing.T) {
type SampleStruct struct {
SliceString []string
SliceStringPtr *[]string
}
sliceStrPtr := []string{"This is slice string test pointer."}
src := SampleStruct{
SliceString: []string{"This is slice string test."},
SliceStringPtr: &sliceStrPtr,
}
dst := SampleStruct{}
errs := Copy(&dst, &src)
if errs != nil {
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, src.SliceString, dst.SliceString)
assertEqual(t, *src.SliceStringPtr, *dst.SliceStringPtr)
assertEqual(t, true, src.SliceStringPtr != dst.SliceStringPtr)
}
func TestCopyByteAndByteSlice(t *testing.T) {
type SampleStruct struct {
Byte byte
SliceBytes []byte
SliceBytesPtr *[]byte
}
bytesPtr := []byte("This is byte pointer value")
src := SampleStruct{
Byte: byte('A'),
SliceBytes: []byte("This is byte value"),
SliceBytesPtr: &bytesPtr,
}
dst := SampleStruct{}
errs := Copy(&dst, &src)
if errs != nil {
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, src.Byte, dst.Byte)
assertEqual(t, true, &src.Byte != &dst.Byte)
assertEqual(t, src.SliceBytes, dst.SliceBytes)
assertEqual(t, true, &src.SliceBytes != &dst.SliceBytes)
assertEqual(t, *src.SliceBytesPtr, *dst.SliceBytesPtr)
assertEqual(t, true, src.SliceBytesPtr != dst.SliceBytesPtr)
}
func TestCopySliceElementsPtr(t *testing.T) {
type SampleSubInfo2 struct {
SliceIntPtr []*int
SliceInt64Ptr []*int64
SliceStringPtr []*string
SliceFloat32 []*float32
SliceFloat64 []*float64
SliceInterface []interface{}
}
type SampleSubInfo1 struct {
SliceIntPtr []*int
SliceInt64Ptr []*int64
SliceStringPtr []*string
SliceFloat32 []*float32
SliceFloat64 []*float64
SliceInterface []interface{}
Level2 SampleSubInfo2
}
type SampleStruct struct {
SliceIntPtr []*int
SliceInt64Ptr []*int64
SliceStringPtr []*string
SliceFloat32 []*float32
SliceFloat64 []*float64
SliceInterface []interface{}
Level1 SampleSubInfo1
}
i1 := int(1)
i2 := int(2)
i3 := int(3)
i11 := int64(11)
i12 := int64(12)
i13 := int64(13)
str1 := "This is string pointer 1"
str2 := "This is string pointer 2"
str3 := "This is string pointer 3"
f1 := float32(0.1)
f2 := float32(0.2)
f3 := float32(0.3)
f11 := float64(0.11)
f12 := float64(0.12)
f13 := float64(0.13)
src := SampleStruct{
SliceIntPtr: []*int{&i1, &i2, &i3},
SliceInt64Ptr: []*int64{&i11, &i12, &i13},
SliceStringPtr: []*string{&str1, &str2, &str3},
SliceFloat32: []*float32{&f1, &f2, &f3},
SliceFloat64: []*float64{&f11, &f12, &f13},
SliceInterface: []interface{}{&i1, i11, &str1, &f1, f11},
Level1: SampleSubInfo1{
SliceIntPtr: []*int{&i1, &i2, &i3},
SliceInt64Ptr: []*int64{&i11, &i12, &i13},
SliceStringPtr: []*string{&str1, &str2, &str3},
SliceFloat32: []*float32{&f1, &f2, &f3},
SliceFloat64: []*float64{&f11, &f12, &f13},
SliceInterface: []interface{}{&i1, i11, &str1, &f1, f11},
Level2: SampleSubInfo2{
SliceIntPtr: []*int{&i1, &i2, &i3},
SliceInt64Ptr: []*int64{&i11, &i12, &i13},
SliceStringPtr: []*string{&str1, &str2, &str3},
SliceFloat32: []*float32{&f1, &f2, &f3},
SliceFloat64: []*float64{&f11, &f12, &f13},
SliceInterface: []interface{}{&i1, i11, &str1, &f1, f11},
},
},
}
dst := SampleStruct{}
errs := Copy(&dst, src)
if errs != nil {
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
// Level 0 assertion
assertEqual(t, true, src.SliceIntPtr[0] != dst.SliceIntPtr[0])
assertEqual(t, src.SliceIntPtr, dst.SliceIntPtr)
assertEqual(t, true, src.SliceInt64Ptr[0] != dst.SliceInt64Ptr[0])
assertEqual(t, src.SliceInt64Ptr, dst.SliceInt64Ptr)
assertEqual(t, true, src.SliceStringPtr[0] != dst.SliceStringPtr[0])
assertEqual(t, src.SliceStringPtr, dst.SliceStringPtr)
assertEqual(t, true, src.SliceFloat32[0] != dst.SliceFloat32[0])
assertEqual(t, src.SliceFloat32, dst.SliceFloat32)
assertEqual(t, true, src.SliceFloat64[0] != dst.SliceFloat64[0])
assertEqual(t, src.SliceFloat64, dst.SliceFloat64)
assertEqual(t, true, src.SliceInterface[0] != dst.SliceInterface[0])
assertEqual(t, src.SliceInterface, dst.SliceInterface)
// Level 1 assertion
assertEqual(t, true, src.Level1.SliceIntPtr[0] != dst.Level1.SliceIntPtr[0])
assertEqual(t, src.Level1.SliceIntPtr, dst.Level1.SliceIntPtr)
assertEqual(t, true, src.Level1.SliceInt64Ptr[0] != dst.Level1.SliceInt64Ptr[0])
assertEqual(t, src.Level1.SliceInt64Ptr, dst.Level1.SliceInt64Ptr)
assertEqual(t, true, src.Level1.SliceStringPtr[0] != dst.Level1.SliceStringPtr[0])
assertEqual(t, src.Level1.SliceStringPtr, dst.Level1.SliceStringPtr)
assertEqual(t, true, src.Level1.SliceFloat32[0] != dst.Level1.SliceFloat32[0])
assertEqual(t, src.Level1.SliceFloat32, dst.Level1.SliceFloat32)
assertEqual(t, true, src.Level1.SliceFloat64[0] != dst.Level1.SliceFloat64[0])
assertEqual(t, src.Level1.SliceFloat64, dst.Level1.SliceFloat64)
assertEqual(t, true, src.Level1.SliceInterface[0] != dst.Level1.SliceInterface[0])
assertEqual(t, src.Level1.SliceInterface, dst.Level1.SliceInterface)
// Level 2 assertion
assertEqual(t, true, src.Level1.Level2.SliceIntPtr[0] != dst.Level1.Level2.SliceIntPtr[0])
assertEqual(t, src.Level1.SliceIntPtr, dst.Level1.SliceIntPtr)
assertEqual(t, true, src.Level1.Level2.SliceInt64Ptr[0] != dst.Level1.Level2.SliceInt64Ptr[0])
assertEqual(t, src.Level1.Level2.SliceInt64Ptr, dst.Level1.Level2.SliceInt64Ptr)
assertEqual(t, true, src.Level1.Level2.SliceStringPtr[0] != dst.Level1.Level2.SliceStringPtr[0])
assertEqual(t, src.Level1.Level2.SliceStringPtr, dst.Level1.Level2.SliceStringPtr)
assertEqual(t, true, src.Level1.Level2.SliceFloat32[0] != dst.Level1.Level2.SliceFloat32[0])
assertEqual(t, src.Level1.Level2.SliceFloat32, dst.Level1.Level2.SliceFloat32)
assertEqual(t, true, src.Level1.Level2.SliceFloat64[0] != dst.Level1.Level2.SliceFloat64[0])
assertEqual(t, src.Level1.Level2.SliceFloat64, dst.Level1.Level2.SliceFloat64)
assertEqual(t, true, src.Level1.Level2.SliceInterface[0] != dst.Level1.Level2.SliceInterface[0])
assertEqual(t, src.Level1.Level2.SliceInterface, dst.Level1.Level2.SliceInterface)
}
func TestCopyMapElements(t *testing.T) {
type SampleSubInfo struct {
Name string
Year int
}
type SampleStruct struct {
MapIntInt map[int]int
MapStringInt map[string]int
MapStringString map[string]string
MapStruct map[string]SampleSubInfo
MapInterfaces map[string]interface{}
}
src := SampleStruct{
MapIntInt: map[int]int{1: 1001, 2: 1002, 3: 1003, 4: 1004},
MapStringInt: map[string]int{"first": 1001, "second": 1002, "third": 1003, "forth": 1004},
MapStringString: map[string]string{"first": "1001", "second": "1002", "third": "1003"},
MapStruct: map[string]SampleSubInfo{
"struct1": {Name: "struct 1 value", Year: 2001},
"struct2": {Name: "struct 2 value", Year: 2002},
"struct3": {Name: "struct 3 value", Year: 2003},
},
MapInterfaces: map[string]interface{}{
"inter1": 100001,
"inter2": "This is my interface string",
"inter3": SampleSubInfo{Name: "inter3: struct 1 value", Year: 2003},
"inter4": float32(1.6546565),
"inter5": float64(1.6546565),
"inter6": &SampleSubInfo{Name: "inter6: struct 2 value", Year: 2006},
},
}
dst := SampleStruct{}
errs := Copy(&dst, &src)
if errs != nil {
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, src.MapIntInt, dst.MapIntInt)
assertEqual(t, src.MapStringInt, dst.MapStringInt)
assertEqual(t, src.MapStringString, dst.MapStringString)
assertEqual(t, src.MapStruct, dst.MapStruct)
assertEqual(t, src.MapInterfaces, dst.MapInterfaces)
}
func TestCopyStructEmbededAndAttribute(t *testing.T) {
type SampleSubInfo struct {
Name string
Year int
}
type SampleStruct struct {
Level1Struct SampleSubInfo `model:",notraverse"`
Level1StructPtr *SampleSubInfo
Level1StructNoTraverse *SampleSubInfo `model:",notraverse"`
CreatedTime time.Time
SampleSubInfo
}
src := SampleStruct{
SampleSubInfo: SampleSubInfo{Name: "This embedded struct", Year: 2016},
Level1Struct: SampleSubInfo{Name: "This level 1 struct", Year: 2015},
Level1StructPtr: &SampleSubInfo{Name: "This level 1 ptr struct", Year: 2014},
Level1StructNoTraverse: &SampleSubInfo{Name: "This nested no traverse struct", Year: 2013},
CreatedTime: time.Now(),
}
dst := SampleStruct{}
errs := Copy(&dst, &src)
if errs != nil {
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, src.Name, dst.Name)
assertEqual(t, src.Year, dst.Year)
assertEqual(t, src.Level1Struct.Name, dst.Level1Struct.Name)
assertEqual(t, src.Level1Struct.Year, dst.Level1Struct.Year)
assertEqual(t, src.Level1StructPtr.Name, dst.Level1StructPtr.Name)
assertEqual(t, src.Level1StructPtr.Year, dst.Level1StructPtr.Year)
assertEqual(t, true, src.CreatedTime == dst.CreatedTime)
assertEqual(t, src.Level1StructNoTraverse.Year, dst.Level1StructNoTraverse.Year)
}
func TestCopyStructEmbededAndAttributeDstPtr(t *testing.T) {
type SampleSubInfo struct {
Name string
Year int
}
type SampleStruct struct {
Level1Struct SampleSubInfo `model:",notraverse"`
Level1StructPtr *SampleSubInfo
Level1StructPtrZero *SampleSubInfo
Level1StructNoTraverse *SampleSubInfo `model:",notraverse"`
CreatedTime time.Time
SampleSubInfo
}
src := SampleStruct{
SampleSubInfo: SampleSubInfo{Name: "This embedded struct", Year: 2016},
Level1Struct: SampleSubInfo{Name: "This level 1 struct", Year: 2015},
Level1StructPtr: &SampleSubInfo{Name: "This level 1 ptr struct", Year: 2014},
Level1StructNoTraverse: &SampleSubInfo{Name: "This nested no traverse struct", Year: 2013},
CreatedTime: time.Now(),
}
dst := SampleStruct{
Level1StructPtrZero: &SampleSubInfo{Name: "This level 1 struct ptr zero", Year: 2015},
}
errs := Copy(&dst, &src)
if errs != nil {
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, src.Name, dst.Name)
assertEqual(t, src.Year, dst.Year)
assertEqual(t, src.Level1Struct.Name, dst.Level1Struct.Name)
assertEqual(t, src.Level1Struct.Year, dst.Level1Struct.Year)
assertEqual(t, src.Level1StructPtr.Name, dst.Level1StructPtr.Name)
assertEqual(t, src.Level1StructPtr.Year, dst.Level1StructPtr.Year)
assertEqual(t, true, src.CreatedTime == dst.CreatedTime)
assertEqual(t, src.Level1StructNoTraverse.Year, dst.Level1StructNoTraverse.Year)
assertEqual(t, true, dst.Level1StructPtrZero == nil)
}
func TestCopyStructEmbededAndAttributeMakeZeroInDst(t *testing.T) {
type SampleSubInfo struct {
Name string
Year int
}
type SampleStruct struct {
Level1Struct SampleSubInfo `model:",notraverse"`
Level1StructPtr *SampleSubInfo
Level1StructNoTraverse *SampleSubInfo `model:",notraverse"`
CreatedTime time.Time
SampleSubInfo
}
src := SampleStruct{CreatedTime: time.Now()}
dst := SampleStruct{
SampleSubInfo: SampleSubInfo{Name: "This embedded struct", Year: 2016},
Level1Struct: SampleSubInfo{Name: "This level 1 struct", Year: 2015},
Level1StructPtr: &SampleSubInfo{Name: "This level 1 ptr struct", Year: 2014},
Level1StructNoTraverse: &SampleSubInfo{Name: "This nested no traverse struct", Year: 2013},
}
errs := Copy(&dst, &src)
if errs != nil {
fmt.Println(errs)
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, true, src.CreatedTime == dst.CreatedTime)
assertEqual(t, true, IsZero(dst.Level1Struct))
assertEqual(t, true, IsZero(dst.SampleSubInfo))
assertEqual(t, true, dst.Level1StructPtr == nil)
assertEqual(t, true, dst.Level1StructNoTraverse == nil)
}
func TestCopyStructEmbededAndAttributeOmitEmpty(t *testing.T) {
type SampleSubInfo struct {
Name string
Year int
}
type SampleStruct struct {
Level1Struct SampleSubInfo `model:",omitempty,notraverse"`
Level1StructPtr *SampleSubInfo `model:",omitempty"`
Level1StructNoTraverse *SampleSubInfo `model:",omitempty,notraverse"`
CreatedTime time.Time
SampleSubInfo `model:",omitempty"`
}
src := SampleStruct{CreatedTime: time.Now()}
dst := SampleStruct{
SampleSubInfo: SampleSubInfo{Name: "This embedded struct", Year: 2016},
Level1Struct: SampleSubInfo{Name: "This level 1 struct", Year: 2015},
Level1StructPtr: &SampleSubInfo{Name: "This level 1 ptr struct", Year: 2014},
Level1StructNoTraverse: &SampleSubInfo{Name: "This nested no traverse struct", Year: 2013},
}
errs := Copy(&dst, src)
if errs != nil {
fmt.Println(errs)
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, true, src.CreatedTime == dst.CreatedTime)
assertEqual(t, 2016, dst.Year)
assertEqual(t, "This embedded struct", dst.Name)
assertEqual(t, 2013, dst.Level1StructNoTraverse.Year)
assertEqual(t, "This nested no traverse struct", dst.Level1StructNoTraverse.Name)
assertEqual(t, 2015, dst.Level1Struct.Year)
assertEqual(t, "This level 1 struct", dst.Level1Struct.Name)
assertEqual(t, 2014, dst.Level1StructPtr.Year)
assertEqual(t, "This level 1 ptr struct", dst.Level1StructPtr.Name)
}
func TestCopyDestinationIsNotPointer(t *testing.T) {
type SampleStruct struct {
Name string
}
errs := Copy(SampleStruct{}, SampleStruct{Name: "Not a pointer"})
assertEqual(t, "Destination struct is not a pointer", errs[0].Error())
}
func TestCopyInputIsNotStruct(t *testing.T) {
type SampleStruct struct {
Name string
}
errs := Copy(&SampleStruct{}, map[string]string{"1": "2001"})
assertEqual(t, "Source or Destination is not a struct", errs[0].Error())
}
func TestCopyStructElementKindDiff(t *testing.T) {
type Source struct {
Name string
}
type Destination struct {
Name int
}
errs := Copy(&Destination{}, Source{Name: "This struct element kind is different"})
assertEqual(t, "Field: 'Name', src [string] & dst [int] kind didn't match", errs[0].Error())
}
func TestCopyStructElementTypeDiffOnLevel1(t *testing.T) {
type SampleLevelSrc struct {
Name string
}
type SampleLevelDst struct {
Name int
}
type Source struct {
Name string
Level1 SampleLevelSrc
}
type Destination struct {
Name int
Level1 SampleLevelDst
}
src := Source{
Name: "This struct element kind is different",
Level1: SampleLevelSrc{
Name: "Level1: This struct element kind is different",
},
}
dst := Destination{}
errs := Copy(&dst, src)
logSrcDst(t, src, dst)
assertEqual(t, "Field: 'Name', src [string] & dst [int] kind didn't match", errs[0].Error())
assertEqual(t,
"Field: 'Level1', src [model.SampleLevelSrc] & dst [model.SampleLevelDst] type didn't match",
errs[1].Error(),
)
}
func TestCopyStructTypeDiffOnLevel1Interface(t *testing.T) {
type SampleLevelSrc struct {
Name string
}
type Source struct {
Name string
Level1 SampleLevelSrc
}
type Destination struct {
Name int
Level1 interface{}
}
src := Source{
Name: "This struct element kind is different",
Level1: SampleLevelSrc{
Name: "Level1: This struct element kind is different",
},
}
dst := Destination{}
errs := Copy(&dst, src)
logSrcDst(t, src, dst)
assertEqual(t, "Field: 'Name', src [string] & dst [int] kind didn't match", errs[0].Error())
assertEqual(t, 0, dst.Name)
assertEqual(t, src.Level1.Name, dst.Level1.(SampleLevelSrc).Name)
}
func TestCopyStructZeroValToDst(t *testing.T) {
type Source struct {
Name string
Year int
}
type Destination struct {
Name string
Year int
}
src := Source{Year: 2016}
dst := Destination{Name: "Value is gonna disappear"}
errs := Copy(&dst, src)
if errs != nil {
t.Error("Error occurred while copying.")
}
logSrcDst(t, src, dst)
assertEqual(t, "", dst.Name)
assertEqual(t, 2016, dst.Year)
}
//
// NoTraverseTypeLis test cases
//
func TestAddNoTraverseType(t *testing.T) {
if !isNoTraverseType(valueOf(os.File{})) {
t.Errorf("Given type not found in omit list")
}
// Already registered
AddNoTraverseType(os.File{})
}
func TestRemoveNoTraverseType(t *testing.T) {
RemoveNoTraverseType(os.File{})
if isNoTraverseType(valueOf(os.File{})) {
t.Errorf("Type should not exists in the NoTraverseTypeList")
}
AddNoTraverseType(os.File{})
// test again
if !isNoTraverseType(valueOf(os.File{})) {
t.Errorf("Type should exists in the NoTraverseTypeList")
}
}
//
// Zero test cases
//
type SampleStruct struct {
Integer int
IntegerPtr *int
String string
StringPtr *string
Boolean bool
BooleanPtr *bool
BooleanOmit bool `model:"-"`
SliceString []string
SliceStringOmit []string `model:"-"`
SliceStringPtr *[]string
SliceStringPtrOmit *[]string `model:"-"`
SliceStringPtrStr []*string
Float32 float32
Float32Ptr *float32
Float32Omit float32 `model:"-"`
Float32PtrOmit *float32 `model:"-"`
Float64 float64
Float64Ptr *float64
Float64Omit float64 `model:"-"`
Float64PtrOmit *float64 `model:"-"`
SliceStruct []SampleSubInfo
SliceStructPtr []*SampleSubInfo
SliceInt []int
SliceIntPtr []*int
Time time.Time
TimePtr *time.Time
Struct SampleSubInfo
StructPtr *SampleSubInfo
StructNoTraverse SampleSubInfo `model:",notraverse"`
StructPtrNoTraverse *SampleSubInfo `model:",notraverse"`
StructDeep SampleSubInfoDeep
StructDeepPtr *SampleSubInfoDeep
SampleSubInfo
}
type SampleSubInfo struct {
Name string
Year int
}
type SampleSubInfoDeep struct {
Name string
NamePtr *string `model:"-"`
Year int `model:"-"`
YearPtr *int
Struct SampleSubInfo
StructPtr *SampleSubInfo
StructNoTraverse SampleSubInfo `model:",notraverse"`
StructPtrNoTraverse *SampleSubInfo `model:",notraverse"`
SampleSubInfo
}
func TestCopyZeroInput(t *testing.T) {
errs := Copy(&SampleStruct{}, SampleStruct{})
assertEqual(t, "Source struct is empty", errs[0].Error())
errs = Copy(nil, nil)
assertEqual(t, "Source or Destination is nil", errs[0].Error())
}
func TestIsZero(t *testing.T) {
IsZero(nil) // nil check
if !IsZero(SampleStruct{}) {
t.Error("SampleStruct - supposed to be zero")
}
if !IsZero(&SampleStruct{}) {
t.Error("SampleStruct Ptr - supposed to be zero")
}
if !IsZero(&SampleStruct{Struct: SampleSubInfo{}, StructPtr: &SampleSubInfo{}}) {
t.Error("SampleStruct with sub struct 1 - supposed to be zero")
}
if !IsZero(&SampleStruct{Struct: SampleSubInfo{Name: "go-model"}, StructPtr: &SampleSubInfo{}}) {
t.Log("SampleStruct with sub struct 2 - supposed to be zero")
} else {
t.Error("SampleStruct with sub struct 2 - supposed to be zero")
}
deepStruct := SampleStruct{
StructDeepPtr: &SampleSubInfoDeep{
StructPtr: &SampleSubInfo{
Name: "I'm here",
},
StructNoTraverse: SampleSubInfo{
Year: 2005,
},
},
}
if IsZero(deepStruct) {
t.Error("SampleStruct deep level - supposed to be non-zero")
}
}
func TestNonZeroCheck(t *testing.T) {
if IsZero(&SampleStruct{Time: time.Now()}) {
t.Error("SampleStruct notraverse - supposed to be zero")
}
if IsZero(SampleStruct{SampleSubInfo: SampleSubInfo{Year: 2010}}) {
t.Error("SampleStruct embedded struct - supposed to be non-zero")
}
}
func TestNonHasZeroCheck(t *testing.T) {
type SampleSubInfo struct {
Name string
Year int
}
type SampleStruct struct {
Level1Struct SampleSubInfo
Level1StructPtr *SampleSubInfo
Level1StructNoTraverse *SampleSubInfo `model:",notraverse"`
CreatedTime time.Time
SampleSubInfo
}
src1 := SampleStruct{
SampleSubInfo: SampleSubInfo{Name: "This embedded struct", Year: 2016},
Level1Struct: SampleSubInfo{Name: "This level 1 struct", Year: 2015},
Level1StructPtr: &SampleSubInfo{Name: "This level 1 ptr struct", Year: 2014},
Level1StructNoTraverse: &SampleSubInfo{Name: "This nested no traverse struct", Year: 2013},
CreatedTime: time.Now(),
}
if HasZero(src1) {
t.Error("SampleStruct supposed to be non-zero")
}
src2 := SampleStruct{
SampleSubInfo: SampleSubInfo{Name: "This embedded struct", Year: 2016},
Level1Struct: SampleSubInfo{Name: "This level 1 struct", Year: 2015},
Level1StructPtr: &SampleSubInfo{Name: "This level 1 ptr struct", Year: 2014},
Level1StructNoTraverse: &SampleSubInfo{Name: "This nested no traverse struct", Year: 2013},
}
if !HasZero(src2) {
t.Error("SampleStruct supposed to have one-zero i.e. CreatedTime field")
}
src3 := SampleStruct{
Level1Struct: SampleSubInfo{Name: "This level 1 struct", Year: 2015},
Level1StructPtr: &SampleSubInfo{Name: "This level 1 ptr struct", Year: 2014},
Level1StructNoTraverse: &SampleSubInfo{Name: "This nested no traverse struct", Year: 2013},
SampleSubInfo: SampleSubInfo{Name: "This embedded struct"},
}
if !HasZero(src3) {
t.Error("SampleStruct supposed to have one-zero i.e. SampleSubInfo -> Year field")
}
}
func TestHasZeroForField(t *testing.T) {
type SampleSubInfo struct {
OmitThisField string `model:"-"`
Name string
Year int
}
if !HasZero(&SampleSubInfo{Name: "only I have populated"}) {
t.Error("Supposed to have one-zero filed i.e. Year")
}
type SampleSrcStruct1 struct {
Level1StructOmit SampleSubInfo `model:"-"`
Level1StructPtr *SampleSubInfo
Level1Struct SampleSubInfo
Level1StructNoTraverse *SampleSubInfo `model:",notraverse"`
CreatedTime time.Time
SampleSubInfo
}
src1 := SampleSrcStruct1{}
if !HasZero(src1) {
t.Error("Suppose to be empty")
}
type SampleSrcStruct2 struct {
Level1StructOmit SampleSubInfo
Level1StructPtr *SampleSubInfo
Level1Struct SampleSubInfo
Level1StructNoTraverse *SampleSubInfo `model:",notraverse"`
CreatedTime time.Time
SampleSubInfo
}
src2 := SampleSrcStruct2{}
if !HasZero(src2) {
t.Error("Suppose to be empty")
}
}
func TestIsStructMethod(t *testing.T) {
src := map[string]interface{}{
"struct": &SampleStruct{Time: time.Now()},
}
mv := valueOf(src)
keys := mv.MapKeys()