forked from maddyblue/goon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoon_test.go
2505 lines (2228 loc) · 84.9 KB
/
goon_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) 2013 Matt Jibson <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package goon
import (
"errors"
"fmt"
"reflect"
"sync"
"testing"
"time"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/aetest"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/memcache"
)
// *[]S, *[]*S, *[]I, []S, []*S, []I
const (
ivTypePtrToSliceOfStructs = iota
ivTypePtrToSliceOfPtrsToStruct
ivTypePtrToSliceOfInterfaces
ivTypeSliceOfStructs
ivTypeSliceOfPtrsToStruct
ivTypeSliceOfInterfaces
ivTypeTotal
)
const (
ivModeDatastore = iota
ivModeMemcache
ivModeMemcacheAndDatastore
ivModeLocalcache
ivModeLocalcacheAndMemcache
ivModeLocalcacheAndDatastore
ivModeLocalcacheAndMemcacheAndDatastore
ivModeTotal
)
// Have a bunch of different supported types to detect any wild errors
// https://developers.google.com/appengine/docs/go/datastore/reference
type ivItem struct {
Id int64 `datastore:"-" goon:"id"`
Int int `datastore:"int,noindex"`
Int8 int8 `datastore:"int8,noindex"`
Int16 int16 `datastore:"int16,noindex"`
Int32 int32 `datastore:"int32,noindex"`
Int64 int64 `datastore:"int64,noindex"`
Float32 float32 `datastore:"float32,noindex"`
Float64 float64 `datastore:"float64,noindex"`
Bool bool `datastore:"bool,noindex"`
String string `datastore:"string,noindex"`
CustomTypes ivItemCustom `datastore:"custom,noindex"`
SliceTypes ivItemSlice `datastore:"slice,noindex"`
ByteSlice []byte `datastore:"byte_slice,noindex"`
BSSlice [][]byte `datastore:"bs_slice,noindex"`
Time time.Time `datastore:"time,noindex"`
TimeSlice []time.Time `datastore:"time_slice,noindex"`
NoIndex int `datastore:",noindex"`
Casual string
Ζεύς string
Key *datastore.Key
ChildKey *datastore.Key
ZeroKey *datastore.Key
KeySlice []*datastore.Key
KeySliceNil []*datastore.Key
BlobKey appengine.BlobKey
BKSlice []appengine.BlobKey
Sub ivItemSub
Subs []ivItemSubs
ZZZV []ivZZZV
}
type ivItemInt int
type ivItemInt8 int8
type ivItemInt16 int16
type ivItemInt32 int32
type ivItemInt64 int64
type ivItemFloat32 float32
type ivItemFloat64 float64
type ivItemBool bool
type ivItemString string
type ivItemDeepInt ivItemInt
type ivItemCustom struct {
Int ivItemInt
Int8 ivItemInt8
Int16 ivItemInt16
Int32 ivItemInt32
Int64 ivItemInt64
Float32 ivItemFloat32
Float64 ivItemFloat64
Bool ivItemBool
String ivItemString
DeepInt ivItemDeepInt
}
type ivItemSlice struct {
Int []int
Int8 []int8
Int16 []int16
Int32 []int32
Int64 []int64
Float32 []float32
Float64 []float64
Bool []bool
String []string
IntC []ivItemInt
Int8C []ivItemInt8
Int16C []ivItemInt16
Int32C []ivItemInt32
Int64C []ivItemInt64
Float32C []ivItemFloat32
Float64C []ivItemFloat64
BoolC []ivItemBool
StringC []ivItemString
DeepInt []ivItemDeepInt
}
type ivItemSub struct {
Data string `datastore:"data,noindex"`
Ints []int `datastore:"ints,noindex"`
}
type ivItemSubs struct {
Data string `datastore:"data,noindex"`
Extra string `datastore:",noindex"`
}
type ivZZZV struct {
Key *datastore.Key `datastore:"key,noindex"`
Data string `datastore:"data,noindex"`
}
func (ivi *ivItem) ForInterface() {}
type ivItemI interface {
ForInterface()
}
var ivItems []ivItem
func initializeIvItems(c context.Context) {
// We force UTC, because the datastore API will always return UTC
t1 := time.Now().UTC().Truncate(time.Microsecond)
t2 := t1.Add(time.Second * 1)
t3 := t1.Add(time.Second * 2)
ivItems = []ivItem{
{Id: 1, Int: 123, Int8: 77, Int16: 13001, Int32: 1234567890, Int64: 123456789012345,
Float32: (float32(10) / float32(3)), Float64: (float64(10000000) / float64(9998)),
Bool: true, String: "one",
CustomTypes: ivItemCustom{Int: 123, Int8: 77, Int16: 13001, Int32: 1234567890, Int64: 123456789012345,
Float32: ivItemFloat32(float32(10) / float32(3)), Float64: ivItemFloat64(float64(10000000) / float64(9998)),
Bool: true, String: "one", DeepInt: 1},
SliceTypes: ivItemSlice{Int: []int{1, 2}, Int8: []int8{1, 2}, Int16: []int16{1, 2}, Int32: []int32{1, 2}, Int64: []int64{1, 2},
Float32: []float32{1.0, 2.0}, Float64: []float64{1.0, 2.0}, Bool: []bool{true, false}, String: []string{"one", "two"},
IntC: []ivItemInt{1, 2}, Int8C: []ivItemInt8{1, 2}, Int16C: []ivItemInt16{1, 2}, Int32C: []ivItemInt32{1, 2}, Int64C: []ivItemInt64{1, 2},
Float32C: []ivItemFloat32{1.0, 2.0}, Float64C: []ivItemFloat64{1.0, 2.0},
BoolC: []ivItemBool{true, false}, StringC: []ivItemString{"one", "two"}, DeepInt: []ivItemDeepInt{1, 2}},
ByteSlice: []byte{0xDE, 0xAD}, BSSlice: [][]byte{{0x01, 0x02}, {0x03, 0x04}},
Time: t1, TimeSlice: []time.Time{t1, t2, t3}, NoIndex: 1,
Casual: "clothes", Ζεύς: "Zeus",
Key: datastore.NewKey(c, "Fruit", "Apple", 0, nil),
ChildKey: datastore.NewKey(c, "Person", "Jane", 0, datastore.NewKey(c, "Person", "John", 0, datastore.NewKey(c, "Person", "Jack", 0, nil))),
KeySlice: []*datastore.Key{datastore.NewKey(c, "Key", "", 1, nil), datastore.NewKey(c, "Key", "", 2, nil), datastore.NewKey(c, "Key", "", 3, nil)},
KeySliceNil: []*datastore.Key{datastore.NewKey(c, "Number", "", 1, nil), nil, datastore.NewKey(c, "Number", "", 2, nil)},
BlobKey: "fake #1", BKSlice: []appengine.BlobKey{"fake #1.1", "fake #1.2"},
Sub: ivItemSub{Data: "yay #1", Ints: []int{1, 2, 3}},
Subs: []ivItemSubs{
{Data: "sub #1.1", Extra: "xtra #1.1"},
{Data: "sub #1.2", Extra: "xtra #1.2"},
{Data: "sub #1.3", Extra: "xtra #1.3"}},
ZZZV: []ivZZZV{{Data: "None"}, {Key: datastore.NewKey(c, "Fruit", "Banana", 0, nil)}}},
{Id: 2, Int: 124, Int8: 78, Int16: 13002, Int32: 1234567891, Int64: 123456789012346,
Float32: (float32(10) / float32(3)), Float64: (float64(10000000) / float64(9998)),
Bool: true, String: "two",
CustomTypes: ivItemCustom{Int: 124, Int8: 78, Int16: 13002, Int32: 1234567891, Int64: 123456789012346,
Float32: ivItemFloat32(float32(10) / float32(3)), Float64: ivItemFloat64(float64(10000000) / float64(9998)),
Bool: true, String: "two", DeepInt: 2},
SliceTypes: ivItemSlice{Int: []int{1, 2}, Int8: []int8{1, 2}, Int16: []int16{1, 2}, Int32: []int32{1, 2}, Int64: []int64{1, 2},
Float32: []float32{1.0, 2.0}, Float64: []float64{1.0, 2.0}, Bool: []bool{true, false}, String: []string{"one", "two"},
IntC: []ivItemInt{1, 2}, Int8C: []ivItemInt8{1, 2}, Int16C: []ivItemInt16{1, 2}, Int32C: []ivItemInt32{1, 2}, Int64C: []ivItemInt64{1, 2},
Float32C: []ivItemFloat32{1.0, 2.0}, Float64C: []ivItemFloat64{1.0, 2.0},
BoolC: []ivItemBool{true, false}, StringC: []ivItemString{"one", "two"}, DeepInt: []ivItemDeepInt{1, 2}},
ByteSlice: []byte{0xBE, 0xEF}, BSSlice: [][]byte{{0x05, 0x06}, {0x07, 0x08}},
Time: t2, TimeSlice: []time.Time{t2, t3, t1}, NoIndex: 2,
Casual: "manners", Ζεύς: "Alcmene",
Key: datastore.NewKey(c, "Fruit", "Banana", 0, nil),
ChildKey: datastore.NewKey(c, "Person", "Jane", 0, datastore.NewKey(c, "Person", "John", 0, datastore.NewKey(c, "Person", "Jack", 0, nil))),
KeySlice: []*datastore.Key{datastore.NewKey(c, "Key", "", 4, nil), datastore.NewKey(c, "Key", "", 5, nil), datastore.NewKey(c, "Key", "", 6, nil)},
KeySliceNil: []*datastore.Key{datastore.NewKey(c, "Number", "", 3, nil), nil, datastore.NewKey(c, "Number", "", 4, nil)},
BlobKey: "fake #2", BKSlice: []appengine.BlobKey{"fake #2.1", "fake #2.2"},
Sub: ivItemSub{Data: "yay #2", Ints: []int{4, 5, 6}},
Subs: []ivItemSubs{
{Data: "sub #2.1", Extra: "xtra #2.1"},
{Data: "sub #2.2", Extra: "xtra #2.2"},
{Data: "sub #2.3", Extra: "xtra #2.3"}},
ZZZV: []ivZZZV{{Data: "None"}, {Key: datastore.NewKey(c, "Fruit", "Banana", 0, nil)}}},
{Id: 3, Int: 125, Int8: 79, Int16: 13003, Int32: 1234567892, Int64: 123456789012347,
Float32: (float32(10) / float32(3)), Float64: (float64(10000000) / float64(9998)),
Bool: true, String: "tri",
CustomTypes: ivItemCustom{Int: 125, Int8: 79, Int16: 13003, Int32: 1234567892, Int64: 123456789012347,
Float32: ivItemFloat32(float32(10) / float32(3)), Float64: ivItemFloat64(float64(10000000) / float64(9998)),
Bool: true, String: "tri", DeepInt: 3},
SliceTypes: ivItemSlice{Int: []int{1, 2}, Int8: []int8{1, 2}, Int16: []int16{1, 2}, Int32: []int32{1, 2}, Int64: []int64{1, 2},
Float32: []float32{1.0, 2.0}, Float64: []float64{1.0, 2.0}, Bool: []bool{true, false}, String: []string{"one", "two"},
IntC: []ivItemInt{1, 2}, Int8C: []ivItemInt8{1, 2}, Int16C: []ivItemInt16{1, 2}, Int32C: []ivItemInt32{1, 2}, Int64C: []ivItemInt64{1, 2},
Float32C: []ivItemFloat32{1.0, 2.0}, Float64C: []ivItemFloat64{1.0, 2.0},
BoolC: []ivItemBool{true, false}, StringC: []ivItemString{"one", "two"}, DeepInt: []ivItemDeepInt{1, 2}},
ByteSlice: []byte{0xF0, 0x0D}, BSSlice: [][]byte{{0x09, 0x0A}, {0x0B, 0x0C}},
Time: t3, TimeSlice: []time.Time{t3, t1, t2}, NoIndex: 3,
Casual: "weather", Ζεύς: "Hercules",
Key: datastore.NewKey(c, "Fruit", "Cherry", 0, nil),
ChildKey: datastore.NewKey(c, "Person", "Jane", 0, datastore.NewKey(c, "Person", "John", 0, datastore.NewKey(c, "Person", "Jack", 0, nil))),
KeySlice: []*datastore.Key{datastore.NewKey(c, "Key", "", 7, nil), datastore.NewKey(c, "Key", "", 8, nil), datastore.NewKey(c, "Key", "", 9, nil)},
KeySliceNil: []*datastore.Key{datastore.NewKey(c, "Number", "", 5, nil), nil, datastore.NewKey(c, "Number", "", 6, nil)},
BlobKey: "fake #3", BKSlice: []appengine.BlobKey{"fake #3.1", "fake #3.2"},
Sub: ivItemSub{Data: "yay #3", Ints: []int{7, 8, 9}},
Subs: []ivItemSubs{
{Data: "sub #3.1", Extra: "xtra #3.1"},
{Data: "sub #3.2", Extra: "xtra #3.2"},
{Data: "sub #3.3", Extra: "xtra #3.3"}},
ZZZV: []ivZZZV{{Data: "None"}, {Key: datastore.NewKey(c, "Fruit", "Banana", 0, nil)}}}}
}
func getIVItemCopy(g *Goon, index int) *ivItem {
// All basic value types are copied easily
ivi := ivItems[index]
// .. but pointer based types require extra work
ivi.SliceTypes.Int = []int{}
for _, v := range ivItems[index].SliceTypes.Int {
ivi.SliceTypes.Int = append(ivi.SliceTypes.Int, v)
}
ivi.SliceTypes.Int8 = []int8{}
for _, v := range ivItems[index].SliceTypes.Int8 {
ivi.SliceTypes.Int8 = append(ivi.SliceTypes.Int8, v)
}
ivi.SliceTypes.Int16 = []int16{}
for _, v := range ivItems[index].SliceTypes.Int16 {
ivi.SliceTypes.Int16 = append(ivi.SliceTypes.Int16, v)
}
ivi.SliceTypes.Int32 = []int32{}
for _, v := range ivItems[index].SliceTypes.Int32 {
ivi.SliceTypes.Int32 = append(ivi.SliceTypes.Int32, v)
}
ivi.SliceTypes.Int64 = []int64{}
for _, v := range ivItems[index].SliceTypes.Int64 {
ivi.SliceTypes.Int64 = append(ivi.SliceTypes.Int64, v)
}
ivi.SliceTypes.Float32 = []float32{}
for _, v := range ivItems[index].SliceTypes.Float32 {
ivi.SliceTypes.Float32 = append(ivi.SliceTypes.Float32, v)
}
ivi.SliceTypes.Float64 = []float64{}
for _, v := range ivItems[index].SliceTypes.Float64 {
ivi.SliceTypes.Float64 = append(ivi.SliceTypes.Float64, v)
}
ivi.SliceTypes.Bool = []bool{}
for _, v := range ivItems[index].SliceTypes.Bool {
ivi.SliceTypes.Bool = append(ivi.SliceTypes.Bool, v)
}
ivi.SliceTypes.String = []string{}
for _, v := range ivItems[index].SliceTypes.String {
ivi.SliceTypes.String = append(ivi.SliceTypes.String, v)
}
ivi.SliceTypes.IntC = []ivItemInt{}
for _, v := range ivItems[index].SliceTypes.IntC {
ivi.SliceTypes.IntC = append(ivi.SliceTypes.IntC, v)
}
ivi.SliceTypes.Int8C = []ivItemInt8{}
for _, v := range ivItems[index].SliceTypes.Int8C {
ivi.SliceTypes.Int8C = append(ivi.SliceTypes.Int8C, v)
}
ivi.SliceTypes.Int16C = []ivItemInt16{}
for _, v := range ivItems[index].SliceTypes.Int16C {
ivi.SliceTypes.Int16C = append(ivi.SliceTypes.Int16C, v)
}
ivi.SliceTypes.Int32C = []ivItemInt32{}
for _, v := range ivItems[index].SliceTypes.Int32C {
ivi.SliceTypes.Int32C = append(ivi.SliceTypes.Int32C, v)
}
ivi.SliceTypes.Int64C = []ivItemInt64{}
for _, v := range ivItems[index].SliceTypes.Int64C {
ivi.SliceTypes.Int64C = append(ivi.SliceTypes.Int64C, v)
}
ivi.SliceTypes.Float32C = []ivItemFloat32{}
for _, v := range ivItems[index].SliceTypes.Float32C {
ivi.SliceTypes.Float32C = append(ivi.SliceTypes.Float32C, v)
}
ivi.SliceTypes.Float64C = []ivItemFloat64{}
for _, v := range ivItems[index].SliceTypes.Float64C {
ivi.SliceTypes.Float64C = append(ivi.SliceTypes.Float64C, v)
}
ivi.SliceTypes.BoolC = []ivItemBool{}
for _, v := range ivItems[index].SliceTypes.BoolC {
ivi.SliceTypes.BoolC = append(ivi.SliceTypes.BoolC, v)
}
ivi.SliceTypes.StringC = []ivItemString{}
for _, v := range ivItems[index].SliceTypes.StringC {
ivi.SliceTypes.StringC = append(ivi.SliceTypes.StringC, v)
}
ivi.SliceTypes.DeepInt = []ivItemDeepInt{}
for _, v := range ivItems[index].SliceTypes.DeepInt {
ivi.SliceTypes.DeepInt = append(ivi.SliceTypes.DeepInt, v)
}
ivi.ByteSlice = []byte{}
for _, v := range ivItems[index].ByteSlice {
ivi.ByteSlice = append(ivi.ByteSlice, v)
}
ivi.BSSlice = [][]byte{}
for _, v := range ivItems[index].BSSlice {
vCopy := []byte{}
for _, v := range v {
vCopy = append(vCopy, v)
}
ivi.BSSlice = append(ivi.BSSlice, vCopy)
}
ivi.TimeSlice = []time.Time{}
for _, v := range ivItems[index].TimeSlice {
ivi.TimeSlice = append(ivi.TimeSlice, v)
}
ivi.Key = datastore.NewKey(g.Context, ivItems[index].Key.Kind(), ivItems[index].Key.StringID(), ivItems[index].Key.IntID(), nil)
ivi.ChildKey = datastore.NewKey(g.Context, ivItems[index].ChildKey.Kind(), ivItems[index].ChildKey.StringID(), ivItems[index].ChildKey.IntID(),
datastore.NewKey(g.Context, ivItems[index].ChildKey.Parent().Kind(), ivItems[index].ChildKey.Parent().StringID(), ivItems[index].ChildKey.Parent().IntID(),
datastore.NewKey(g.Context, ivItems[index].ChildKey.Parent().Parent().Kind(), ivItems[index].ChildKey.Parent().Parent().StringID(), ivItems[index].ChildKey.Parent().Parent().IntID(), nil)))
ivi.KeySlice = []*datastore.Key{}
for _, key := range ivItems[index].KeySlice {
ivi.KeySlice = append(ivi.KeySlice, datastore.NewKey(g.Context, key.Kind(), key.StringID(), key.IntID(), nil))
}
ivi.KeySliceNil = []*datastore.Key{}
for _, key := range ivItems[index].KeySliceNil {
if key == nil {
ivi.KeySliceNil = append(ivi.KeySliceNil, nil)
} else {
ivi.KeySliceNil = append(ivi.KeySliceNil, datastore.NewKey(g.Context, key.Kind(), key.StringID(), key.IntID(), nil))
}
}
ivi.BKSlice = []appengine.BlobKey{}
for _, v := range ivItems[index].BKSlice {
ivi.BKSlice = append(ivi.BKSlice, v)
}
ivi.Sub = ivItemSub{}
ivi.Sub.Data = ivItems[index].Sub.Data
for _, v := range ivItems[index].Sub.Ints {
ivi.Sub.Ints = append(ivi.Sub.Ints, v)
}
ivi.Subs = []ivItemSubs{}
for _, sub := range ivItems[index].Subs {
ivi.Subs = append(ivi.Subs, ivItemSubs{Data: sub.Data, Extra: sub.Extra})
}
ivi.ZZZV = []ivZZZV{}
for _, zzzv := range ivItems[index].ZZZV {
ivi.ZZZV = append(ivi.ZZZV, ivZZZV{Key: zzzv.Key, Data: zzzv.Data})
}
return &ivi
}
func getInputVarietySrc(t *testing.T, g *Goon, ivType int, indices ...int) interface{} {
if ivType >= ivTypeTotal {
t.Fatalf("Invalid input variety type! %v >= %v", ivType, ivTypeTotal)
return nil
}
var result interface{}
switch ivType {
case ivTypePtrToSliceOfStructs:
s := []ivItem{}
for _, index := range indices {
s = append(s, *getIVItemCopy(g, index))
}
result = &s
case ivTypePtrToSliceOfPtrsToStruct:
s := []*ivItem{}
for _, index := range indices {
s = append(s, getIVItemCopy(g, index))
}
result = &s
case ivTypePtrToSliceOfInterfaces:
s := []ivItemI{}
for _, index := range indices {
s = append(s, getIVItemCopy(g, index))
}
result = &s
case ivTypeSliceOfStructs:
s := []ivItem{}
for _, index := range indices {
s = append(s, *getIVItemCopy(g, index))
}
result = s
case ivTypeSliceOfPtrsToStruct:
s := []*ivItem{}
for _, index := range indices {
s = append(s, getIVItemCopy(g, index))
}
result = s
case ivTypeSliceOfInterfaces:
s := []ivItemI{}
for _, index := range indices {
s = append(s, getIVItemCopy(g, index))
}
result = s
}
return result
}
func getInputVarietyDst(t *testing.T, ivType int) interface{} {
if ivType >= ivTypeTotal {
t.Fatalf("Invalid input variety type! %v >= %v", ivType, ivTypeTotal)
return nil
}
var result interface{}
switch ivType {
case ivTypePtrToSliceOfStructs:
result = &[]ivItem{{Id: ivItems[0].Id}, {Id: ivItems[1].Id}, {Id: ivItems[2].Id}}
case ivTypePtrToSliceOfPtrsToStruct:
result = &[]*ivItem{{Id: ivItems[0].Id}, {Id: ivItems[1].Id}, {Id: ivItems[2].Id}}
case ivTypePtrToSliceOfInterfaces:
result = &[]ivItemI{&ivItem{Id: ivItems[0].Id}, &ivItem{Id: ivItems[1].Id}, &ivItem{Id: ivItems[2].Id}}
case ivTypeSliceOfStructs:
result = []ivItem{{Id: ivItems[0].Id}, {Id: ivItems[1].Id}, {Id: ivItems[2].Id}}
case ivTypeSliceOfPtrsToStruct:
result = []*ivItem{{Id: ivItems[0].Id}, {Id: ivItems[1].Id}, {Id: ivItems[2].Id}}
case ivTypeSliceOfInterfaces:
result = []ivItemI{&ivItem{Id: ivItems[0].Id}, &ivItem{Id: ivItems[1].Id}, &ivItem{Id: ivItems[2].Id}}
}
return result
}
func getPrettyIVMode(ivMode int) string {
result := "N/A"
switch ivMode {
case ivModeDatastore:
result = "DS"
case ivModeMemcache:
result = "MC"
case ivModeMemcacheAndDatastore:
result = "DS+MC"
case ivModeLocalcache:
result = "LC"
case ivModeLocalcacheAndMemcache:
result = "MC+LC"
case ivModeLocalcacheAndDatastore:
result = "DS+LC"
case ivModeLocalcacheAndMemcacheAndDatastore:
result = "DS+MC+LC"
}
return result
}
func getPrettyIVType(ivType int) string {
result := "N/A"
switch ivType {
case ivTypePtrToSliceOfStructs:
result = "*[]S"
case ivTypePtrToSliceOfPtrsToStruct:
result = "*[]*S"
case ivTypePtrToSliceOfInterfaces:
result = "*[]I"
case ivTypeSliceOfStructs:
result = "[]S"
case ivTypeSliceOfPtrsToStruct:
result = "[]*S"
case ivTypeSliceOfInterfaces:
result = "[]I"
}
return result
}
func ivWipe(t *testing.T, g *Goon, prettyInfo string) {
// Make sure the datastore is clear of any previous tests
// TODO: Batch this once goon gets more convenient batch delete support
for _, ivi := range ivItems {
if err := g.Delete(g.Key(ivi)); err != nil {
t.Errorf("%s > Unexpected error on delete - %v", prettyInfo, err)
}
}
// Make sure the caches are clear, so any caching is done by our specific test
g.FlushLocalCache()
memcache.Flush(g.Context)
}
func ivGetMulti(t *testing.T, g *Goon, ref, dst interface{}, prettyInfo string) error {
// Get our data back and make sure it's correct
if err := g.GetMulti(dst); err != nil {
t.Errorf("%s > Unexpected error on GetMulti - %v", prettyInfo, err)
return err
} else {
dstLen := reflect.Indirect(reflect.ValueOf(dst)).Len()
refLen := reflect.Indirect(reflect.ValueOf(ref)).Len()
if dstLen != refLen {
t.Errorf("%s > Unexpected dst len (%v) doesn't match ref len (%v)", prettyInfo, dstLen, refLen)
} else if !reflect.DeepEqual(ref, dst) {
t.Errorf("%s > Expected - %v, %v, %v - got %v, %v, %v", prettyInfo,
reflect.Indirect(reflect.ValueOf(ref)).Index(0).Interface(),
reflect.Indirect(reflect.ValueOf(ref)).Index(1).Interface(),
reflect.Indirect(reflect.ValueOf(ref)).Index(2).Interface(),
reflect.Indirect(reflect.ValueOf(dst)).Index(0).Interface(),
reflect.Indirect(reflect.ValueOf(dst)).Index(1).Interface(),
reflect.Indirect(reflect.ValueOf(dst)).Index(2).Interface())
}
}
return nil
}
func validateInputVariety(t *testing.T, g *Goon, srcType, dstType, mode int) {
if mode >= ivModeTotal {
t.Fatalf("Invalid input variety mode! %v >= %v", mode, ivModeTotal)
return
}
// Generate a nice debug info string for clear logging
prettyInfo := getPrettyIVType(srcType) + " " + getPrettyIVType(dstType) + " " + getPrettyIVMode(mode)
// This function just gets the entities based on a predefined list, helper for cache population
loadIVItem := func(indices ...int) {
for _, index := range indices {
ivi := &ivItem{Id: ivItems[index].Id}
if err := g.Get(ivi); err != nil {
t.Errorf("%s > Unexpected error on get - %v", prettyInfo, err)
} else if !reflect.DeepEqual(ivItems[index], *ivi) {
t.Errorf("%s > Expected - %v, got %v", prettyInfo, ivItems[index], *ivi)
}
}
}
// Start with a clean slate
ivWipe(t, g, prettyInfo)
// Generate test data with the specified types
src := getInputVarietySrc(t, g, srcType, 0, 1, 2)
ref := getInputVarietySrc(t, g, dstType, 0, 1, 2)
dst := getInputVarietyDst(t, dstType)
// Save our test data
if _, err := g.PutMulti(src); err != nil {
t.Errorf("%s > Unexpected error on PutMulti - %v", prettyInfo, err)
}
// Clear the caches, as we're going to precisely set the caches via Get
g.FlushLocalCache()
memcache.Flush(g.Context)
// Set the caches into proper state based on given mode
switch mode {
case ivModeDatastore:
// Caches already clear
case ivModeMemcache:
loadIVItem(0, 1, 2) // Left in memcache
g.FlushLocalCache()
case ivModeMemcacheAndDatastore:
loadIVItem(0, 1) // Left in memcache
g.FlushLocalCache()
case ivModeLocalcache:
loadIVItem(0, 1, 2) // Left in local cache
case ivModeLocalcacheAndMemcache:
loadIVItem(0) // Left in memcache
g.FlushLocalCache()
loadIVItem(1, 2) // Left in local cache
case ivModeLocalcacheAndDatastore:
loadIVItem(0, 1) // Left in local cache
case ivModeLocalcacheAndMemcacheAndDatastore:
loadIVItem(0) // Left in memcache
g.FlushLocalCache()
loadIVItem(1) // Left in local cache
}
// Get our data back and make sure it's correct
ivGetMulti(t, g, ref, dst, prettyInfo)
}
func validateInputVarietyTXNPut(t *testing.T, g *Goon, srcType, dstType, mode int) {
if mode >= ivModeTotal {
t.Fatalf("Invalid input variety mode! %v >= %v", mode, ivModeTotal)
return
}
// The following modes are redundant with the current goon transaction implementation
switch mode {
case ivModeMemcache:
return
case ivModeMemcacheAndDatastore:
return
case ivModeLocalcacheAndMemcache:
return
case ivModeLocalcacheAndMemcacheAndDatastore:
return
}
// Generate a nice debug info string for clear logging
prettyInfo := getPrettyIVType(srcType) + " " + getPrettyIVType(dstType) + " " + getPrettyIVMode(mode) + " TXNPut"
// Start with a clean slate
ivWipe(t, g, prettyInfo)
// Generate test data with the specified types
src := getInputVarietySrc(t, g, srcType, 0, 1, 2)
ref := getInputVarietySrc(t, g, dstType, 0, 1, 2)
dst := getInputVarietyDst(t, dstType)
// Save our test data
if err := g.RunInTransaction(func(tg *Goon) error {
_, err := tg.PutMulti(src)
return err
}, &datastore.TransactionOptions{XG: true}); err != nil {
t.Errorf("%s > Unexpected error on PutMulti - %v", prettyInfo, err)
}
// Set the caches into proper state based on given mode
switch mode {
case ivModeDatastore:
g.FlushLocalCache()
memcache.Flush(g.Context)
case ivModeLocalcache:
// Entities already in local cache
case ivModeLocalcacheAndDatastore:
g.FlushLocalCache()
memcache.Flush(g.Context)
subSrc := getInputVarietySrc(t, g, srcType, 0)
if err := g.RunInTransaction(func(tg *Goon) error {
_, err := tg.PutMulti(subSrc)
return err
}, &datastore.TransactionOptions{XG: true}); err != nil {
t.Errorf("%s > Unexpected error on PutMulti - %v", prettyInfo, err)
}
}
// Get our data back and make sure it's correct
ivGetMulti(t, g, ref, dst, prettyInfo)
}
func validateInputVarietyTXNGet(t *testing.T, g *Goon, srcType, dstType, mode int) {
if mode >= ivModeTotal {
t.Fatalf("Invalid input variety mode! %v >= %v", mode, ivModeTotal)
return
}
// The following modes are redundant with the current goon transaction implementation
switch mode {
case ivModeMemcache:
return
case ivModeMemcacheAndDatastore:
return
case ivModeLocalcache:
return
case ivModeLocalcacheAndMemcache:
return
case ivModeLocalcacheAndDatastore:
return
case ivModeLocalcacheAndMemcacheAndDatastore:
return
}
// Generate a nice debug info string for clear logging
prettyInfo := getPrettyIVType(srcType) + " " + getPrettyIVType(dstType) + " " + getPrettyIVMode(mode) + " TXNGet"
// Start with a clean slate
ivWipe(t, g, prettyInfo)
// Generate test data with the specified types
src := getInputVarietySrc(t, g, srcType, 0, 1, 2)
ref := getInputVarietySrc(t, g, dstType, 0, 1, 2)
dst := getInputVarietyDst(t, dstType)
// Save our test data
if _, err := g.PutMulti(src); err != nil {
t.Errorf("%s > Unexpected error on PutMulti - %v", prettyInfo, err)
}
// Set the caches into proper state based on given mode
// TODO: Instead of clear, fill the caches with invalid data, because we're supposed to always fetch from the datastore
switch mode {
case ivModeDatastore:
g.FlushLocalCache()
memcache.Flush(g.Context)
}
// Get our data back and make sure it's correct
if err := g.RunInTransaction(func(tg *Goon) error {
return ivGetMulti(t, tg, ref, dst, prettyInfo)
}, &datastore.TransactionOptions{XG: true}); err != nil {
t.Errorf("%s > Unexpected error on transaction - %v", prettyInfo, err)
}
}
func TestInputVariety(t *testing.T) {
c, done, err := aetest.NewContext()
if err != nil {
t.Fatalf("Could not start aetest - %v", err)
}
defer done()
g := FromContext(c)
initializeIvItems(c)
for srcType := 0; srcType < ivTypeTotal; srcType++ {
for dstType := 0; dstType < ivTypeTotal; dstType++ {
for mode := 0; mode < ivModeTotal; mode++ {
validateInputVariety(t, g, srcType, dstType, mode)
validateInputVarietyTXNPut(t, g, srcType, dstType, mode)
validateInputVarietyTXNGet(t, g, srcType, dstType, mode)
}
}
}
}
type MigrationEntity interface {
number() int32
parent() *datastore.Key
}
type MigrationA struct {
_kind string `goon:"kind,Migration"`
Parent *datastore.Key `datastore:"-" goon:"parent"`
Id int64 `datastore:"-" goon:"id"`
Number int32 `datastore:"number"`
Word string `datastore:"word,noindex"`
Car string `datastore:"car,noindex"`
Holiday time.Time `datastore:"holiday,noindex"`
α int `datastore:",noindex"`
Level MigrationIntA `datastore:"level,noindex"`
Floor MigrationIntA `datastore:"floor,noindex"`
Sub MigrationSub `datastore:"sub,noindex"`
Son MigrationPerson `datastore:"son,noindex"`
Daughter MigrationPerson `datastore:"daughter,noindex"`
Parents []MigrationPerson `datastore:"parents,noindex"`
DeepSlice MigrationDeepA `datastore:"deep,noindex"`
ZZs []ZigZag `datastore:"zigzag,noindex"`
ZeroKey *datastore.Key `datastore:",noindex"`
File []byte
DeprecatedField string `datastore:"depf,noindex"`
DeprecatedStruct MigrationSub `datastore:"deps,noindex"`
FinalField string `datastore:"final,noindex"` // This should always be last, to test deprecating middle properties
}
func (m MigrationA) parent() *datastore.Key {
return m.Parent
}
func (m MigrationA) number() int32 {
return m.Number
}
type MigrationSub struct {
Data string `datastore:"data,noindex"`
Noise []int `datastore:"noise,noindex"`
Sub MigrationSubSub `datastore:"sub,noindex"`
}
type MigrationSubSub struct {
Data string `datastore:"data,noindex"`
}
type MigrationPerson struct {
Name string `datastore:"name,noindex"`
Age int `datastore:"age,noindex"`
}
type MigrationDeepA struct {
Deep MigrationDeepB `datastore:"deep,noindex"`
}
type MigrationDeepB struct {
Deep MigrationDeepC `datastore:"deep,noindex"`
}
type MigrationDeepC struct {
Slice []int `datastore:"slice,noindex"`
}
type ZigZag struct {
Zig int `datastore:"zig,noindex"`
Zag int `datastore:"zag,noindex"`
}
type ZigZags struct {
Zig []int `datastore:"zig,noindex"`
Zag []int `datastore:"zag,noindex"`
}
type MigrationIntA int
type MigrationIntB int
type MigrationB struct {
_kind string `goon:"kind,Migration"`
Parent *datastore.Key `datastore:"-" goon:"parent"`
Identification int64 `datastore:"-" goon:"id"`
FancyNumber int32 `datastore:"number"`
Slang string `datastore:"word,noindex"`
Cars []string `datastore:"car,noindex"`
Holidays []time.Time `datastore:"holiday,noindex"`
β int `datastore:"α,noindex"`
Level MigrationIntB `datastore:"level,noindex"`
Floors []MigrationIntB `datastore:"floor,noindex"`
Animal string `datastore:"sub.data,noindex"`
Music []int `datastore:"sub.noise,noindex"`
Flower string `datastore:"sub.sub.data,noindex"`
Sons []MigrationPerson `datastore:"son,noindex"`
DaughterName string `datastore:"daughter.name,noindex"`
DaughterAge int `datastore:"daughter.age,noindex"`
OldFolks []MigrationPerson `datastore:"parents,noindex"`
FarSlice MigrationDeepA `datastore:"deep,noindex"`
ZZs ZigZags `datastore:"zigzag,noindex"`
Keys []*datastore.Key `datastore:"ZeroKey,noindex"`
Files [][]byte `datastore:"File,noindex"`
FinalField string `datastore:"final,noindex"`
}
func (m MigrationB) parent() *datastore.Key {
return m.Parent
}
func (m MigrationB) number() int32 {
return m.FancyNumber
}
// MigrationA with PropertyLoadSaver interface
type MigrationPlsA MigrationA
// MigrationB with PropertyLoadSaver interface
type MigrationPlsB MigrationB
func (m MigrationPlsA) parent() *datastore.Key {
return m.Parent
}
func (m MigrationPlsA) number() int32 {
return m.Number
}
func (m *MigrationPlsA) Save() ([]datastore.Property, error) {
return datastore.SaveStruct(m)
}
func (m *MigrationPlsA) Load(props []datastore.Property) error {
return datastore.LoadStruct(m, props)
}
func (m MigrationPlsB) parent() *datastore.Key {
return m.Parent
}
func (m MigrationPlsB) number() int32 {
return m.FancyNumber
}
func (m *MigrationPlsB) Save() ([]datastore.Property, error) {
return datastore.SaveStruct(m)
}
func (m *MigrationPlsB) Load(props []datastore.Property) error {
return datastore.LoadStruct(m, props)
}
// Make sure these implement datastore.PropertyLoadSaver
var _, _ datastore.PropertyLoadSaver = &MigrationPlsA{}, &MigrationPlsB{}
const (
migrationMethodGet = iota
migrationMethodGetAll
migrationMethodGetAllMulti
migrationMethodNext
migrationMethodCount
)
func TestMigration(t *testing.T) {
c, done, err := aetest.NewContext()
if err != nil {
t.Fatalf("Could not start aetest - %v", err)
}
defer done()
g := FromContext(c)
// Create & save an entity with the original structure
parentKey := g.Key(&HasId{Id: 9999})
migA := &MigrationA{Parent: parentKey, Id: 1, Number: 123, Word: "rabbit", Car: "BMW",
Holiday: time.Now().UTC().Truncate(time.Microsecond), α: 1, Level: 9001, Floor: 5,
Sub: MigrationSub{Data: "fox", Noise: []int{1, 2, 3}, Sub: MigrationSubSub{Data: "rose"}},
Son: MigrationPerson{Name: "John", Age: 5}, Daughter: MigrationPerson{Name: "Nancy", Age: 6},
Parents: []MigrationPerson{{Name: "Sven", Age: 56}, {Name: "Sonya", Age: 49}},
DeepSlice: MigrationDeepA{Deep: MigrationDeepB{Deep: MigrationDeepC{Slice: []int{1, 2, 3}}}},
ZZs: []ZigZag{{Zig: 1}, {Zag: 1}}, File: []byte{0xF0, 0x0D},
DeprecatedField: "dep", DeprecatedStruct: MigrationSub{Data: "dep", Noise: []int{1, 2, 3}}, FinalField: "fin"}
if _, err := g.Put(migA); err != nil {
t.Errorf("Unexpected error on Put: %v", err)
}
// Also save an already migrated structure
migB := &MigrationB{Parent: migA.Parent, Identification: migA.Id + 1, FancyNumber: migA.Number + 1}
if _, err := g.Put(migB); err != nil {
t.Errorf("Unexpected error on Put: %v", err)
}
// Run migration tests with both IgnoreFieldMismatch on & off
for _, IgnoreFieldMismatch = range []bool{true, false} {
testcase := []struct {
name string
src MigrationEntity
dst MigrationEntity
}{
{
name: "NormalCache -> NormalCache",
src: &MigrationA{Parent: parentKey, Id: 1},
dst: &MigrationB{Parent: parentKey, Identification: 1},
},
{
// struct can fetch PropertyListCache
name: "PropertyListCache -> NormalCache",
src: &MigrationPlsA{Parent: parentKey, Id: 1},
dst: &MigrationB{Parent: parentKey, Identification: 1},
},
{
name: "PropertyListCache -> PropertyListCache",
src: &MigrationPlsA{Parent: parentKey, Id: 1},
dst: &MigrationPlsB{Parent: parentKey, Identification: 1},
},
{
// PropertyLoadSaver should not fetch NormalCache but simply falls back to datastore
name: "NormalCache -> PropertyListCache",
src: &MigrationA{Parent: parentKey, Id: 1},
dst: &MigrationPlsB{Parent: parentKey, Identification: 1},
},
}
for _, tt := range testcase {
// Clear all the caches
g.FlushLocalCache()
memcache.Flush(c)
// Get it back, so it's in the cache
if err := g.Get(tt.src); err != nil {
t.Errorf("Unexpected error on Get: %v", err)
}
// Clear the local cache, because it doesn't need to support migration
g.FlushLocalCache()
// Test whether memcache supports migration
var fetched MigrationEntity
debugInfo := fmt.Sprintf("%s - field mismatch: %v - MC", tt.name, IgnoreFieldMismatch)
fetched = verifyMigration(t, g, tt.src, tt.dst, migrationMethodGet, debugInfo)
checkMigrationResult(t, g, tt.src, fetched, debugInfo)
// Test whether datastore supports migration
for method := 0; method < migrationMethodCount; method++ {