-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathauto_map_test.go
961 lines (742 loc) · 14.9 KB
/
auto_map_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
package goriak
import (
"reflect"
"testing"
)
type testmapobject struct {
A string
Set []string
RiakContext []byte `goriak:"goriakcontext"`
}
func TestAutoMapSetAndGet(t *testing.T) {
result, errset := bucket().Set(&testmapobject{
A: "Hello",
Set: []string{"One", "Two"},
}).Run(con())
if errset != nil {
t.Error("Set:", errset)
}
var res testmapobject
result2, errget := bucket().Get(result.Key, &res).Run(con())
if errget != nil {
t.Error("Set:", errset)
}
if result2.NotFound {
t.Error("Not found")
}
if res.A != "Hello" {
t.Error("Unexpected A value")
}
if len(res.Set) != 2 {
t.Error("Unexpected set length")
}
foundOne := false
foundTwo := false
for _, v := range res.Set {
if v == "One" {
foundOne = true
}
if v == "Two" {
foundTwo = true
}
}
if !foundOne || !foundTwo {
t.Error("Unexpected set contents")
}
}
func TestIsNotFound(t *testing.T) {
var res testmapobject
result, err := bucket().Get("unknown-key", &res).Run(con())
if !result.NotFound {
t.Error("not marked as not found")
}
if err == nil {
t.Error("did not get error")
}
}
func TestSetNonPointer(t *testing.T) {
input := testmapobject{
A: "I am passed as Value",
}
result, err := bucket().Set(input).Run(con())
if err != nil {
t.Error("Error: ", err.Error())
}
var res testmapobject
result, err = bucket().Get(result.Key, &res).Run(con())
if result.NotFound {
t.Error("Not found")
}
if err != nil {
t.Error(err)
}
if res.A != "I am passed as Value" {
t.Error("Unknown response")
}
}
func TestAbunchOfTypes(t *testing.T) {
type customByteArray [3]byte
type aBunchOfTypes struct {
Int int
String string
Array [3]byte
ByteSlice []byte
StringSlice []string `goriak:"callme_string_slicer"`
IntSlice []int
ByteSliceSlice [][]byte
ByteArraySlice [][4]byte
CustomByteArraySlice []customByteArray
Num int
Num8 int8
Num16 int16
Num32 int32
Num64 int64
Unum uint
Unum8 uint8
Unum16 uint16
Unum32 uint32
Unum64 uint64
}
o := aBunchOfTypes{
Int: 9001,
String: "Hello World",
Array: [3]byte{100, 101, 102},
ByteSlice: []byte{50, 60, 70},
StringSlice: []string{"H", "e", "l", "o"},
IntSlice: []int{4000, 5000, 6000},
ByteSliceSlice: [][]byte{{10, 11, 12}, {100, 110, 120}},
ByteArraySlice: [][4]byte{{1, 2, 3, 4}, {6, 6, 6, 6}},
CustomByteArraySlice: []customByteArray{{1, 2, 3}, {4, 5, 6}},
Num: -1,
Num8: -8,
Num16: -16,
Num32: -32,
Num64: -64,
Unum: 1,
Unum8: 8,
Unum16: 16,
Unum32: 32,
Unum64: 64,
}
result, err := bucket().Set(o).Run(con())
if err != nil {
t.Error("Set", err)
}
var res aBunchOfTypes
result, errGet := bucket().Get(result.Key, &res).Run(con())
if errGet != nil {
t.Error("Get", errGet)
}
if result.NotFound {
t.Error("Not found")
}
if !reflect.DeepEqual(o, res) {
t.Error("Not equal")
t.Errorf("Got: %+v", res)
t.Errorf("Expected: %+v", o)
}
}
func TestFailNonMapType(t *testing.T) {
_, err := bucket().Set(500).Run(con())
if err == nil {
t.Error("Did not receive error")
}
}
func TestFailEmptyArray(t *testing.T) {
type testType struct {
A [0]byte
}
o := testType{}
result, err := bucket().Set(o).Run(con())
if err != nil {
t.Error(err)
}
var res testType
result, getErr := bucket().Get(result.Key, &res).Run(con())
if result.NotFound {
t.Error("not found")
}
if getErr != nil {
t.Error(getErr)
}
}
func TestUnsupportedArrayType(t *testing.T) {
type testType struct {
A [5]string
}
o := testType{}
_, err := bucket().Set(o).Run(con())
if err == nil {
t.Error("Did not get error")
}
if err.Error() != "Unknown Array type: string" {
t.Error("Unknown error")
t.Error(err)
}
}
func TestUnsupportedSliceType(t *testing.T) {
type testType struct {
A []bool
}
o := testType{
A: []bool{false, true, true, true, false, true},
}
_, err := bucket().Set(o).Run(con())
if err == nil {
t.Error("Did not get error")
return
}
if err.Error() != "Unknown slice type: bool" {
t.Error("Unknown error")
t.Error(err)
}
}
func TestUnsupportedType(t *testing.T) {
type testType struct {
A [][]bool
}
o := testType{
A: [][]bool{{true, false, true}},
}
_, err := bucket().Set(o).Run(con())
if err == nil {
t.Error("Did not get error")
return
}
if err.Error() != "Unknown slice slice type: bool" {
t.Error("Unknown error")
t.Error(err)
}
}
func TestMapBool(t *testing.T) {
type testType struct {
A bool
B bool
}
o := testType{
A: true,
B: false,
}
result, err := bucket().Set(o).Run(con())
if err != nil {
t.Error(err)
return
}
var res testType
result, err = bucket().Get(result.Key, &res).Run(con())
if err != nil {
t.Error(err)
}
if result.NotFound {
t.Error("Not Found")
}
if !res.A {
t.Error("A was not true")
}
if res.B {
t.Error("B was not false")
}
}
func TestUnknownTypeFloat(t *testing.T) {
type ourTestType struct {
foo float64
}
item := ourTestType{
foo: 12.34,
}
_, err := bucket().Set(item).Run(con())
if err == nil {
t.Error("Did not get error")
}
if err != nil && err.Error() != "Unexpected type: float64" {
t.Error("Unknown error")
t.Error(err)
}
}
func TestEmptyStruct(t *testing.T) {
type aBunchOfTypes struct {
Int int
String string
Array [3]byte
ByteSlice []byte
StringSlice []string `goriak:"callme_string_slicer"`
IntSlice []int
ByteSliceSlice [][]byte
}
item := aBunchOfTypes{}
_, err := bucket().Set(item).Run(con())
if err != nil {
t.Error(err)
}
}
func TestMapInStruct(t *testing.T) {
type ourTestType struct {
Foo string
Bar map[int]string
Bar8 map[int8]string
Bar16 map[int16]string
Bar32 map[int32]string
Bar64 map[int64]string
BarByte map[int64][]byte
BarString map[string]string
}
item := ourTestType{
Foo: "Foo",
Bar: map[int]string{
10: "Ten",
20: "Twenty",
},
Bar8: map[int8]string{
10: "Ten",
20: "Twenty",
},
Bar16: map[int16]string{
10: "Ten",
20: "Twenty",
},
Bar32: map[int32]string{
10: "Ten",
20: "Twenty",
},
Bar64: map[int64]string{
10: "Ten",
20: "Twenty",
},
BarByte: map[int64][]byte{
4000: {1, 2, 3, 4, 5},
10000: {50, 60, 70, 80},
},
BarString: map[string]string{
"Ten": "TenTen",
"Twenty": "TewntyTwenty",
},
}
result, err := bucket().Set(item).Run(con())
if err != nil {
t.Error("Set", err)
}
var res ourTestType
_, err = bucket().Get(result.Key, &res).Run(con())
if err != nil {
t.Error("Get", err)
}
if !reflect.DeepEqual(item, res) {
t.Error("Not equal")
t.Errorf("Got: %+v", res)
t.Errorf("Expected: %+v", item)
}
}
func TestSubStructs(t *testing.T) {
type ourSubTestType struct {
AA string
BB string
}
type ourOtherSubTestType struct {
DD ourSubTestType
}
type ourTestType struct {
A string
B ourSubTestType
C ourOtherSubTestType
}
item := ourTestType{
A: "Outer A",
B: ourSubTestType{
AA: "Inner A",
BB: "Inner B",
},
C: ourOtherSubTestType{
DD: ourSubTestType{
AA: "Other A",
BB: "Other B",
},
},
}
result, err := bucket().Set(item).Run(con())
if err != nil {
t.Error(err)
}
var res ourTestType
_, err = bucket().Get(result.Key, &res).Run(con())
if err != nil {
t.Error("Get", err)
}
if !reflect.DeepEqual(item, res) {
t.Error("Not equal")
t.Errorf("Got: %+v", res)
t.Errorf("Expected: %+v", item)
}
}
func TestAutoMapSlices(t *testing.T) {
type writeType struct {
A string
}
result, err := bucket().Set(writeType{
A: "aaaa",
}).Run(con())
if err != nil {
t.Error(err)
}
type readType struct {
A string
B float64
}
var res readType
_, err = bucket().Get(result.Key, &res).Run(con())
if err == nil {
t.Error("No error")
return
}
if err.Error() != "Unknown type: float64" {
t.Error("Unexpected error", err)
}
// ---------
type readType2 struct {
A string
B [][]bool
}
var res2 readType2
_, err = bucket().Get(result.Key, &res2).Run(con())
if err == nil {
t.Error("No error")
return
}
if err.Error() != "Unknown slice slice type: bool" {
t.Error("Unexpected error", err)
}
// ---------
type readType3 struct {
A string
B [][5]bool
}
var res3 readType3
_, err = bucket().Get(result.Key, &res3).Run(con())
if err == nil {
t.Error("No error")
return
}
if err.Error() != "Unknown slice array type: bool" {
t.Error("Unexpected error", err)
}
// ---------
type readType4 struct {
A string
B []float64
}
var res4 readType4
_, err = bucket().Get(result.Key, &res4).Run(con())
if err == nil {
t.Error("No error")
return
}
if err.Error() != "Unknown slice type: float64" {
t.Error("Unexpected error", err)
}
// ---------
type writeType5 struct {
A string
B map[string]string
}
result, err = bucket().Set(writeType5{
A: "aaaa",
B: map[string]string{
"AA": "BB",
"CC": "DD",
},
}).Run(con())
if err != nil {
t.Error(err)
}
type readType5 struct {
A string
B map[float64]float64
}
var res5 readType5
_, err = bucket().Get(result.Key, &res5).Run(con())
if err == nil {
t.Error("No error")
return
}
if err.Error() != "Unknown map key type: float64" {
t.Error("Unexpected error", err)
}
// ---------
type writeType6sub struct {
AA string
}
type writeType6 struct {
A string
B writeType6sub
}
result, err = bucket().Set(writeType6{
A: "aaaa",
B: writeType6sub{
AA: "bbbb",
},
}).Run(con())
if err != nil {
t.Error(err)
}
type readType6sub struct {
AA float64
}
type readType6 struct {
A string
B readType6sub
}
var res6 readType6
_, err = bucket().Get(result.Key, &res6).Run(con())
if err == nil {
t.Error("No error")
return
}
if err.Error() != "Unknown type: float64" {
t.Error("Unexpected error", err)
}
// ---------
type writeType7 struct {
A string
B map[string]string
}
result, err = bucket().Set(writeType5{
A: "aaaa",
B: map[string]string{
"AA": "BB",
"CC": "DD",
},
}).Run(con())
if err != nil {
t.Error(err)
}
type readType7 struct {
A string
B map[string]float64
}
var res7 readType7
_, err = bucket().Get(result.Key, &res7).Run(con())
if err == nil {
t.Error("No error")
return
}
if err.Error() != "Unknown map value type" {
t.Error("Unexpected error", err)
}
// ---------
type readType7b struct {
A string
B map[string][]float64
}
var res7b readType7b
_, err = bucket().Get(result.Key, &res7b).Run(con())
if err == nil {
t.Error("No error")
return
}
if err.Error() != "Unknown map value type" {
t.Error("Unexpected error", err)
}
// ---------
type writeType8 struct {
A string
B []string
}
result, err = bucket().Set(writeType8{
A: "aaaa",
B: []string{"a", "b"},
}).Run(con())
if err != nil {
t.Error(err)
}
type readType8 struct {
A string
B []int
}
var res8 readType8
_, err = bucket().Get(result.Key, &res8).Run(con())
if err == nil {
t.Error("No error")
return
}
if err.Error() != `strconv.ParseInt: parsing "a": invalid syntax` {
t.Error("Unexpected error", err)
}
// ---------
type writeType9 struct {
A *string
}
s := "ptr string"
result, err = bucket().Set(writeType9{
A: &s,
}).Run(con())
if err == nil || err.Error() != "Unexpected ptr type: *string" {
t.Error(err)
}
result, err = bucket().Set(writeType{
A: "not a pointer",
}).Run(con())
if err != nil {
t.Error(err)
}
var res9 writeType9
_, err = bucket().Get(result.Key, &res9).Run(con())
if err == nil {
t.Error("No error")
return
}
if err.Error() != `Unexpected ptr type: *string` {
t.Error("Unexpected error", err)
}
}
func TestEncodeErrors(t *testing.T) {
type writeType1 struct {
A map[float64]string
}
_, err := bucket().Set(writeType1{
A: map[float64]string{
2.0: "2",
3.0: "3",
},
}).Run(con())
if err == nil {
t.Error("no error")
}
if err.Error() != "Unknown map key type: float64" {
t.Error("Unexpected error", err)
}
// ----------
type writeType2 struct {
A map[int]float64
}
_, err = bucket().Set(writeType2{
A: map[int]float64{
2: 2.0,
3: 3.0,
},
}).Run(con())
if err == nil {
t.Error("no error")
}
if err.Error() != "Unexpected type: float64" {
t.Error("Unexpected error", err)
}
}
func TestAutoMapIgnore(t *testing.T) {
type ourTestType struct {
SaveA string `goriak:"save_a"`
SaveB string `goriak:"save_b"`
IgnoreC string `goriak:"-"`
}
val := ourTestType{
SaveA: "SaveA",
SaveB: "SaveB",
IgnoreC: "IgnoreC",
}
c := con()
res, err := bucket().Set(val).Run(c)
if err != nil {
t.Error(err)
}
var output ourTestType
_, err = bucket().Get(res.Key, &output).Run(c)
if err != nil {
t.Error(err)
}
if output.IgnoreC != "" {
t.Error("IgnoreC had a value:", output.IgnoreC)
}
if output.SaveA != "SaveA" || output.SaveB != "SaveB" {
t.Error("Unepxected output content")
t.Logf("%+v", output)
}
}
func TestAutoMapMapArray(t *testing.T) {
type ourTestType struct {
Things map[string][4]byte
}
val := ourTestType{
Things: map[string][4]byte{
"a": {1, 1, 1, 1},
"b": {2, 2, 2, 2},
},
}
c := con()
res, err := bucket().Set(val).Run(c)
if err != nil {
t.Error(err)
}
var resVal ourTestType
_, err = bucket().Get(res.Key, &resVal).Run(c)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(val, resVal) {
t.Error("Did not get same value back")
t.Log(val)
t.Log(resVal)
}
}
func TestAutoMapMapArray2(t *testing.T) {
type ourTestType struct {
Things map[int64][32]byte
}
val := ourTestType{
Things: map[int64][32]byte{
500: {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6},
600: {2, 2, 2, 2},
},
}
c := con()
res, err := bucket().Set(val).Run(c)
if err != nil {
t.Error(err)
}
var resVal ourTestType
_, err = bucket().Get(res.Key, &resVal).Run(c)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(val, resVal) {
t.Error("Did not get same value back")
t.Log(val)
t.Log(resVal)
}
}
func TestAutoMapGetNoBucket(t *testing.T) {
var val string
_, err := Bucket("", "").Get("keykeykey", val).Run(con())
if err == nil {
t.Error("no error")
return
}
if err.Error() != "ClientError|Bucket is required" {
t.Error("Unexpected error:", err.Error())
}
}
func TestInsertAndGet(t *testing.T) {
c, _ := Connect(ConnectOpts{
Address: "127.0.0.1",
})
type ourTestType struct {
A string
B []string
}
val := ourTestType{
A: "AAA",
B: []string{"B", "BB", "BBB"},
}
res, err := Bucket("testsuitemap", "maps").Set(val).Run(c)
if err != nil {
t.Error("Insert:", err)
}
if len(res.Key) < 20 {
t.Error("Unexpected key")
}
var out ourTestType
res, err = Bucket("testsuitemap", "maps").Get(res.Key, &out).Run(c)
if err != nil {
t.Error("Get:", err)
}
if !reflect.DeepEqual(val, out) {
t.Error("Not equal")
}
}