-
Notifications
You must be signed in to change notification settings - Fork 0
/
row_test.go
2435 lines (2399 loc) · 68.6 KB
/
row_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 2017 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package spanner
import (
"encoding/base64"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"testing"
"time"
"cloud.google.com/go/civil"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/google/go-cmp/cmp"
"github.com/storj/exp-spanner/internal/testutil"
"google.golang.org/api/iterator"
proto "google.golang.org/protobuf/proto"
proto3 "google.golang.org/protobuf/types/known/structpb"
)
var (
tm = time.Date(2016, 11, 15, 0, 0, 0, 0, time.UTC)
dt, _ = civil.ParseDate("2016-11-15")
// row contains a column for each unique Cloud Spanner type.
row = Row{
[]*sppb.StructType_Field{
// STRING / STRING ARRAY
{Name: "STRING", Type: stringType()},
{Name: "NULL_STRING", Type: stringType()},
{Name: "STRING_ARRAY", Type: listType(stringType())},
{Name: "NULL_STRING_ARRAY", Type: listType(stringType())},
// BYTES / BYTES ARRAY
{Name: "BYTES", Type: bytesType()},
{Name: "NULL_BYTES", Type: bytesType()},
{Name: "BYTES_ARRAY", Type: listType(bytesType())},
{Name: "NULL_BYTES_ARRAY", Type: listType(bytesType())},
// INT64 / INT64 ARRAY
{Name: "INT64", Type: intType()},
{Name: "NULL_INT64", Type: intType()},
{Name: "INT64_ARRAY", Type: listType(intType())},
{Name: "NULL_INT64_ARRAY", Type: listType(intType())},
// BOOL / BOOL ARRAY
{Name: "BOOL", Type: boolType()},
{Name: "NULL_BOOL", Type: boolType()},
{Name: "BOOL_ARRAY", Type: listType(boolType())},
{Name: "NULL_BOOL_ARRAY", Type: listType(boolType())},
// FLOAT64 / FLOAT64 ARRAY
{Name: "FLOAT64", Type: floatType()},
{Name: "NULL_FLOAT64", Type: floatType()},
{Name: "FLOAT64_ARRAY", Type: listType(floatType())},
{Name: "NULL_FLOAT64_ARRAY", Type: listType(floatType())},
// FLOAT32 / FLOAT32 ARRAY
{Name: "FLOAT32", Type: float32Type()},
{Name: "NULL_FLOAT32", Type: float32Type()},
{Name: "FLOAT32_ARRAY", Type: listType(float32Type())},
{Name: "NULL_FLOAT32_ARRAY", Type: listType(float32Type())},
// TIMESTAMP / TIMESTAMP ARRAY
{Name: "TIMESTAMP", Type: timeType()},
{Name: "NULL_TIMESTAMP", Type: timeType()},
{Name: "TIMESTAMP_ARRAY", Type: listType(timeType())},
{Name: "NULL_TIMESTAMP_ARRAY", Type: listType(timeType())},
// DATE / DATE ARRAY
{Name: "DATE", Type: dateType()},
{Name: "NULL_DATE", Type: dateType()},
{Name: "DATE_ARRAY", Type: listType(dateType())},
{Name: "NULL_DATE_ARRAY", Type: listType(dateType())},
// STRUCT ARRAY
{
Name: "STRUCT_ARRAY",
Type: listType(
structType(
mkField("Col1", intType()),
mkField("Col2", floatType()),
mkField("Col3", float32Type()),
mkField("Col4", stringType()),
),
),
},
{
Name: "NULL_STRUCT_ARRAY",
Type: listType(
structType(
mkField("Col1", intType()),
mkField("Col2", floatType()),
mkField("Col3", float32Type()),
mkField("Col4", stringType()),
),
),
},
},
[]*proto3.Value{
// STRING / STRING ARRAY
stringProto("value"),
nullProto(),
listProto(stringProto("value1"), nullProto(), stringProto("value3")),
nullProto(),
// BYTES / BYTES ARRAY
bytesProto([]byte("value")),
nullProto(),
listProto(bytesProto([]byte("value1")), nullProto(), bytesProto([]byte("value3"))),
nullProto(),
// INT64 / INT64 ARRAY
intProto(17),
nullProto(),
listProto(intProto(1), intProto(2), nullProto()),
nullProto(),
// BOOL / BOOL ARRAY
boolProto(true),
nullProto(),
listProto(nullProto(), boolProto(true), boolProto(false)),
nullProto(),
// FLOAT64 / FLOAT64 ARRAY
floatProto(1.7),
nullProto(),
listProto(nullProto(), nullProto(), floatProto(1.7)),
nullProto(),
// FLOAT32 / FLOAT32 ARRAY
float32Proto(0.3),
nullProto(),
listProto(nullProto(), nullProto(), float32Proto(0.3)),
nullProto(),
// TIMESTAMP / TIMESTAMP ARRAY
timeProto(tm),
nullProto(),
listProto(nullProto(), timeProto(tm)),
nullProto(),
// DATE / DATE ARRAY
dateProto(dt),
nullProto(),
listProto(nullProto(), dateProto(dt)),
nullProto(),
// STRUCT ARRAY
listProto(
nullProto(),
listProto(intProto(3), floatProto(33.3), float32Proto(0.3), stringProto("three")),
nullProto(),
),
nullProto(),
},
}
)
// Test helpers for getting column values.
func TestColumnValues(t *testing.T) {
vals := []interface{}{}
wantVals := []interface{}{}
// Test getting column values.
for i, wants := range [][]interface{}{
// STRING / STRING ARRAY
{"value", NullString{"value", true}},
{NullString{}},
{[]NullString{{"value1", true}, {}, {"value3", true}}},
{[]NullString(nil)},
// BYTES / BYTES ARRAY
{[]byte("value")},
{[]byte(nil)},
{[][]byte{[]byte("value1"), nil, []byte("value3")}},
{[][]byte(nil)},
// INT64 / INT64 ARRAY
{int64(17), NullInt64{17, true}},
{NullInt64{}},
{[]NullInt64{{1, true}, {2, true}, {}}},
{[]NullInt64(nil)},
// BOOL / BOOL ARRAY
{true, NullBool{true, true}},
{NullBool{}},
{[]NullBool{{}, {true, true}, {false, true}}},
{[]NullBool(nil)},
// FLOAT64 / FLOAT64 ARRAY
{1.7, NullFloat64{1.7, true}},
{NullFloat64{}},
{[]NullFloat64{{}, {}, {1.7, true}}},
{[]NullFloat64(nil)},
// FLOAT32 / FLOAT64 ARRAY
{float32(0.3), NullFloat32{0.3, true}},
{NullFloat32{}},
{[]NullFloat32{{}, {}, {float32(0.3), true}}},
{[]NullFloat32(nil)},
// TIMESTAMP / TIMESTAMP ARRAY
{tm, NullTime{tm, true}},
{NullTime{}},
{[]NullTime{{}, {tm, true}}},
{[]NullTime(nil)},
// DATE / DATE ARRAY
{dt, NullDate{dt, true}},
{NullDate{}},
{[]NullDate{{}, {dt, true}}},
{[]NullDate(nil)},
// STRUCT ARRAY
{
[]*struct {
Col1 NullInt64
Col2 NullFloat64
Col3 NullFloat32
Col4 string
}{
nil,
{
NullInt64{3, true},
NullFloat64{33.3, true},
NullFloat32{0.3, true},
"three",
},
nil,
},
[]NullRow{
{},
{
Row: Row{
fields: []*sppb.StructType_Field{
mkField("Col1", intType()),
mkField("Col2", floatType()),
mkField("Col3", float32Type()),
mkField("Col4", stringType()),
},
vals: []*proto3.Value{
intProto(3),
floatProto(33.3),
float32Proto(0.3),
stringProto("three"),
},
},
Valid: true,
},
{},
},
},
{
[]*struct {
Col1 NullInt64
Col2 NullFloat64
Col3 NullFloat32
Col4 string
}(nil),
[]NullRow(nil),
},
} {
for j, want := range wants {
// Prepare Value vector to test Row.Columns.
if j == 0 {
vals = append(vals, reflect.New(reflect.TypeOf(want)).Interface())
wantVals = append(wantVals, want)
}
// Column
gotp := reflect.New(reflect.TypeOf(want))
err := row.Column(i, gotp.Interface())
if err != nil {
t.Errorf("\t row.Column(%v, %T) returns error: %v, want nil", i, gotp.Interface(), err)
}
if got := reflect.Indirect(gotp).Interface(); !testEqual(got, want) {
t.Errorf("\t row.Column(%v, %T) retrives %v, want %v", i, gotp.Interface(), got, want)
}
// ColumnByName
gotp = reflect.New(reflect.TypeOf(want))
err = row.ColumnByName(row.fields[i].Name, gotp.Interface())
if err != nil {
t.Errorf("\t row.ColumnByName(%v, %T) returns error: %v, want nil", row.fields[i].Name, gotp.Interface(), err)
}
if got := reflect.Indirect(gotp).Interface(); !testEqual(got, want) {
t.Errorf("\t row.ColumnByName(%v, %T) retrives %v, want %v", row.fields[i].Name, gotp.Interface(), got, want)
}
}
}
// Test Row.Columns.
if err := row.Columns(vals...); err != nil {
t.Errorf("row.Columns() returns error: %v, want nil", err)
}
for i, want := range wantVals {
if got := reflect.Indirect(reflect.ValueOf(vals[i])).Interface(); !testEqual(got, want) {
t.Errorf("\t got %v(%T) for column[%v], want %v(%T)", got, got, row.fields[i].Name, want, want)
}
}
}
// Test decoding into nil destination.
func TestNilDst(t *testing.T) {
for i, test := range []struct {
r *Row
dst interface{}
wantErr error
structDst interface{}
wantToStructErr error
}{
{
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: stringType()},
},
[]*proto3.Value{stringProto("value")},
},
nil,
errDecodeColumn(0, errNilDst(nil)),
nil,
errToStructArgType(nil),
},
{
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: stringType()},
},
[]*proto3.Value{stringProto("value")},
},
(*string)(nil),
errDecodeColumn(0, errNilDst((*string)(nil))),
(*struct{ STRING string })(nil),
errNilDst((*struct{ STRING string })(nil)),
},
{
&Row{
[]*sppb.StructType_Field{
{
Name: "Col0",
Type: listType(
structType(
mkField("Col1", intType()),
mkField("Col2", floatType()),
mkField("Col3", float32Type()),
),
),
},
},
[]*proto3.Value{listProto(
listProto(intProto(3), floatProto(33.3), float32Proto(0.3)),
)},
},
(*[]*struct {
Col1 int
Col2 float64
Col3 float32
})(nil),
errDecodeColumn(0, errNilDst((*[]*struct {
Col1 int
Col2 float64
Col3 float32
})(nil))),
(*struct {
StructArray []*struct {
Col1 int
Col2 float64
Col3 float32
} `spanner:"STRUCT_ARRAY"`
})(nil),
errNilDst((*struct {
StructArray []*struct {
Col1 int
Col2 float64
Col3 float32
} `spanner:"STRUCT_ARRAY"`
})(nil)),
},
} {
for j, toStuct := range []func(ptr interface{}) error{test.r.ToStruct, test.r.ToStructLenient} {
if gotErr := test.r.Column(0, test.dst); !testEqual(gotErr, test.wantErr) {
t.Errorf("%v: test.r.Column() returns error %v, want %v", i, gotErr, test.wantErr)
}
if gotErr := test.r.ColumnByName("Col0", test.dst); !testEqual(gotErr, test.wantErr) {
t.Errorf("%v: test.r.ColumnByName() returns error %v, want %v", i, gotErr, test.wantErr)
}
// Row.Columns(T) should return nil on T == nil, otherwise, it should return test.wantErr.
wantColumnsErr := test.wantErr
if test.dst == nil {
wantColumnsErr = nil
}
if gotErr := test.r.Columns(test.dst); !testEqual(gotErr, wantColumnsErr) {
t.Errorf("%v: test.r.Columns() returns error %v, want %v", i, gotErr, wantColumnsErr)
}
if gotErr := toStuct(test.structDst); !testEqual(gotErr, test.wantToStructErr) {
if j == 0 {
t.Errorf("%v: test.r.ToStruct() returns error %v, want %v", i, gotErr, test.wantToStructErr)
} else {
t.Errorf("%v: test.r.ToStructLenient() returns error %v, want %v", i, gotErr, test.wantToStructErr)
}
}
}
}
}
// Test decoding NULL columns using Go types that don't support NULL.
func TestNullTypeErr(t *testing.T) {
var tm time.Time
ntoi := func(n string) int {
for i, f := range row.fields {
if f.Name == n {
return i
}
}
t.Errorf("cannot find column name %q in row", n)
return 0
}
for _, test := range []struct {
colName string
dst interface{}
}{
{
"NULL_STRING",
proto.String(""),
},
{
"NULL_INT64",
proto.Int64(0),
},
{
"NULL_BOOL",
proto.Bool(false),
},
{
"NULL_FLOAT64",
proto.Float64(0.0),
},
{
"NULL_FLOAT32",
proto.Float32(0.0),
},
{
"NULL_TIMESTAMP",
&tm,
},
{
"NULL_DATE",
&dt,
},
} {
wantErr := errDecodeColumn(ntoi(test.colName), errDstNotForNull(test.dst))
if gotErr := row.ColumnByName(test.colName, test.dst); !testEqual(gotErr, wantErr) {
t.Errorf("row.ColumnByName(%v) returns error %v, want %v", test.colName, gotErr, wantErr)
}
}
}
// Test using wrong destination type in column decoders.
func TestColumnTypeErr(t *testing.T) {
// badDst cannot hold any of the column values.
badDst := &struct{}{}
for i, f := range row.fields { // For each of the columns, try to decode it into badDst.
tc := f.Type.Code
var etc sppb.TypeCode
if strings.Contains(f.Name, "ARRAY") {
etc = f.Type.ArrayElementType.Code
}
wantErr := errDecodeColumn(i, errTypeMismatch(tc, etc, badDst))
if strings.Contains(f.Name, "STRUCT_ARRAY") {
wantErr = errDecodeColumn(i, fmt.Errorf("the container is not a slice of struct pointers: %v", errTypeMismatch(tc, etc, badDst)))
}
if gotErr := row.Column(i, badDst); !testEqual(gotErr, wantErr) {
t.Errorf("Column(%v): decoding into destination with wrong type %T returns error %v, want %v",
i, badDst, gotErr, wantErr)
}
if gotErr := row.ColumnByName(f.Name, badDst); !testEqual(gotErr, wantErr) {
t.Errorf("ColumnByName(%v): decoding into destination with wrong type %T returns error %v, want %v",
f.Name, badDst, gotErr, wantErr)
}
}
wantErr := errDecodeColumn(1, errTypeMismatch(sppb.TypeCode_STRING, sppb.TypeCode_TYPE_CODE_UNSPECIFIED, badDst))
// badDst is used to receive column 1.
vals := []interface{}{nil, badDst} // Row.Column() is expected to fail at column 1.
// Skip decoding the rest columns by providing nils as the destinations.
for i := 2; i < len(row.fields); i++ {
vals = append(vals, nil)
}
if gotErr := row.Columns(vals...); !testEqual(gotErr, wantErr) {
t.Errorf("Columns(): decoding column 1 with wrong type %T returns error %v, want %v",
badDst, gotErr, wantErr)
}
}
// Test the handling of invalid column decoding requests which cannot be mapped to correct column(s).
func TestInvalidColumnRequest(t *testing.T) {
for _, test := range []struct {
desc string
f func() error
wantErr error
}{
{
"Request column index is out of range",
func() error {
return row.Column(10000, &struct{}{})
},
errColIdxOutOfRange(10000, &row),
},
{
"Cannot find the named column",
func() error {
return row.ColumnByName("string", &struct{}{})
},
errColNotFound("string"),
},
{
"Not enough arguments to call row.Columns()",
func() error {
return row.Columns(nil, nil)
},
errNumOfColValue(2, &row),
},
{
"Call ColumnByName on row with duplicated column names",
func() error {
var s string
r := &Row{
[]*sppb.StructType_Field{
{Name: "Val", Type: stringType()},
{Name: "Val", Type: stringType()},
},
[]*proto3.Value{stringProto("value1"), stringProto("value2")},
}
return r.ColumnByName("Val", &s)
},
errDupColName("Val"),
},
{
"Call ToStruct on row with duplicated column names",
func() error {
s := &struct {
Val string
}{}
r := &Row{
[]*sppb.StructType_Field{
{Name: "Val", Type: stringType()},
{Name: "Val", Type: stringType()},
},
[]*proto3.Value{stringProto("value1"), stringProto("value2")},
}
return r.ToStruct(s)
},
errDupSpannerField("Val", &sppb.StructType{
Fields: []*sppb.StructType_Field{
{Name: "Val", Type: stringType()},
{Name: "Val", Type: stringType()},
},
}),
},
{
"Call ToStruct on a row with unnamed field",
func() error {
s := &struct {
Val string
}{}
r := &Row{
[]*sppb.StructType_Field{
{Name: "", Type: stringType()},
},
[]*proto3.Value{stringProto("value1")},
}
return r.ToStruct(s)
},
errUnnamedField(&sppb.StructType{Fields: []*sppb.StructType_Field{
{Name: "", Type: stringType()},
}}, 0),
},
{
"Call ToStructLenient on row with duplicated column names",
func() error {
s := &struct {
Val string
}{}
r := &Row{
[]*sppb.StructType_Field{
{Name: "Val", Type: stringType()},
{Name: "Val", Type: stringType()},
},
[]*proto3.Value{stringProto("value1"), stringProto("value2")},
}
return r.ToStructLenient(s)
},
errDupSpannerField("Val", &sppb.StructType{
Fields: []*sppb.StructType_Field{
{Name: "Val", Type: stringType()},
{Name: "Val", Type: stringType()},
},
}),
},
{
"Call ToStructLenient on a row with unnamed field",
func() error {
s := &struct {
Val string
}{}
r := &Row{
[]*sppb.StructType_Field{
{Name: "", Type: stringType()},
},
[]*proto3.Value{stringProto("value1")},
}
return r.ToStructLenient(s)
},
errUnnamedField(&sppb.StructType{Fields: []*sppb.StructType_Field{
{Name: "", Type: stringType()},
}}, 0),
},
} {
if gotErr := test.f(); !testEqual(gotErr, test.wantErr) {
t.Errorf("%v: test.f() returns error %v, want %v", test.desc, gotErr, test.wantErr)
}
}
}
// Test decoding the row with row.ToStruct into an invalid destination.
func TestToStructInvalidDst(t *testing.T) {
for _, test := range []struct {
desc string
dst interface{}
wantErr error
toStruct func(ptr interface{}) error
}{
{
"row.ToStruct(): Decode row as STRUCT into int32",
proto.Int32(1),
errToStructArgType(proto.Int32(1)),
row.ToStruct,
},
{
"ToStructLenient(): Decode row as STRUCT into int32",
proto.Int32(1),
errToStructArgType(proto.Int32(1)),
row.ToStructLenient,
},
{
"row.ToStruct(): Decode row as STRUCT to nil Go struct",
(*struct{})(nil),
errNilDst((*struct{})(nil)),
row.ToStruct,
},
{
"row.ToStructLenient(): Decode row as STRUCT to nil Go struct",
(*struct{})(nil),
errNilDst((*struct{})(nil)),
row.ToStructLenient,
},
{
"row.ToStruct(): Decode row as STRUCT to Go struct with duplicated fields for the PK column",
&struct {
PK1 string `spanner:"STRING"`
PK2 string `spanner:"STRING"`
}{},
errNoOrDupGoField(&struct {
PK1 string `spanner:"STRING"`
PK2 string `spanner:"STRING"`
}{}, "STRING"),
row.ToStruct,
},
{
"row.ToStructLenient(): Decode row as STRUCT to Go struct with duplicated fields for the PK column",
&struct {
PK1 string `spanner:"STRING"`
PK2 string `spanner:"STRING"`
}{},
errDupGoField(&struct {
PK1 string `spanner:"STRING"`
PK2 string `spanner:"STRING"`
}{}, "STRING"),
row.ToStructLenient,
},
{
"row.ToStruct(): Decode row as STRUCT to Go struct with no field for the PK column",
&struct {
PK1 string `spanner:"_STRING"`
}{},
errNoOrDupGoField(&struct {
PK1 string `spanner:"_STRING"`
}{}, "STRING"),
row.ToStruct,
},
{
"row.ToStructLenient(): Decode row as STRUCT to Go struct with no field for the PK column",
&struct {
PK1 string `spanner:"_STRING"`
}{},
nil,
row.ToStructLenient,
},
{
"row.ToStruct(): Decode row as STRUCT to Go struct with wrong type for the PK column",
&struct {
PK1 int64 `spanner:"STRING"`
}{},
errDecodeStructField(&sppb.StructType{Fields: row.fields}, "STRING",
errTypeMismatch(sppb.TypeCode_STRING, sppb.TypeCode_TYPE_CODE_UNSPECIFIED, proto.Int64(0))),
row.ToStruct,
},
{
"row.ToStructLenient(): Decode row as STRUCT to Go struct with wrong type for the PK column",
&struct {
PK1 int64 `spanner:"STRING"`
}{},
errDecodeStructField(&sppb.StructType{Fields: row.fields}, "STRING",
errTypeMismatch(sppb.TypeCode_STRING, sppb.TypeCode_TYPE_CODE_UNSPECIFIED, proto.Int64(0))),
row.ToStructLenient,
},
} {
if gotErr := test.toStruct(test.dst); !testEqual(gotErr, test.wantErr) {
t.Errorf("%v: decoding:\ngot %v\nwant %v", test.desc, gotErr, test.wantErr)
}
}
}
// Test decoding a broken row.
func TestBrokenRow(t *testing.T) {
for i, test := range []struct {
row *Row
dst interface{}
wantErr error
}{
{
// A row with no field.
&Row{
[]*sppb.StructType_Field{},
[]*proto3.Value{stringProto("value")},
},
&NullString{"value", true},
errFieldsMismatchVals(&Row{
[]*sppb.StructType_Field{},
[]*proto3.Value{stringProto("value")},
}),
},
{
// A row with nil field.
&Row{
[]*sppb.StructType_Field{nil},
[]*proto3.Value{stringProto("value")},
},
&NullString{"value", true},
errNilColType(0),
},
{
// Field is not nil, but its type is nil.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: nil},
},
[]*proto3.Value{listProto(stringProto("value1"), stringProto("value2"))},
},
&[]NullString{},
errDecodeColumn(0, errNilSpannerType()),
},
{
// Field is not nil, field type is not nil, but it is an array and its array element type is nil.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: &sppb.Type{Code: sppb.TypeCode_ARRAY}},
},
[]*proto3.Value{listProto(stringProto("value1"), stringProto("value2"))},
},
&[]NullString{},
errDecodeColumn(0, errNilArrElemType(&sppb.Type{Code: sppb.TypeCode_ARRAY})),
},
{
// Field specifies valid type, value is nil.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: intType()},
},
[]*proto3.Value{nil},
},
&NullInt64{1, true},
errDecodeColumn(0, errNilSrc()),
},
{
// Field specifies INT64 type, value is having a nil Kind.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: intType()},
},
[]*proto3.Value{{Kind: (*proto3.Value_StringValue)(nil)}},
},
&NullInt64{1, true},
errDecodeColumn(0, errSrcVal(&proto3.Value{Kind: (*proto3.Value_StringValue)(nil)}, "String")),
},
{
// Field specifies INT64 type, but value is for Number type.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: intType()},
},
[]*proto3.Value{floatProto(1.0)},
},
&NullInt64{1, true},
errDecodeColumn(0, errSrcVal(floatProto(1.0), "String")),
},
{
// Field specifies INT64 type, but value is wrongly encoded.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: intType()},
},
[]*proto3.Value{stringProto("&1")},
},
proto.Int64(0),
errDecodeColumn(0, errBadEncoding(stringProto("&1"), func() error {
_, err := strconv.ParseInt("&1", 10, 64)
return err
}())),
},
{
// Field specifies INT64 type, but value is wrongly encoded.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: intType()},
},
[]*proto3.Value{stringProto("&1")},
},
&NullInt64{},
errDecodeColumn(0, errBadEncoding(stringProto("&1"), func() error {
_, err := strconv.ParseInt("&1", 10, 64)
return err
}())),
},
{
// Field specifies STRING type, but value is having a nil Kind.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: stringType()},
},
[]*proto3.Value{{Kind: (*proto3.Value_StringValue)(nil)}},
},
&NullString{"value", true},
errDecodeColumn(0, errSrcVal(&proto3.Value{Kind: (*proto3.Value_StringValue)(nil)}, "String")),
},
{
// Field specifies STRING type, but value is for ARRAY type.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: stringType()},
},
[]*proto3.Value{listProto(stringProto("value"))},
},
&NullString{"value", true},
errDecodeColumn(0, errSrcVal(listProto(stringProto("value")), "String")),
},
{
// Field specifies FLOAT64 type, value is having a nil Kind.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: floatType()},
},
[]*proto3.Value{{Kind: (*proto3.Value_NumberValue)(nil)}},
},
&NullFloat64{1.0, true},
errDecodeColumn(0, errSrcVal(&proto3.Value{Kind: (*proto3.Value_NumberValue)(nil)}, "Number")),
},
{
// Field specifies FLOAT64 type, but value is for BOOL type.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: floatType()},
},
[]*proto3.Value{boolProto(true)},
},
&NullFloat64{1.0, true},
errDecodeColumn(0, errSrcVal(boolProto(true), "Number")),
},
{
// Field specifies FLOAT64 type, but value is wrongly encoded.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: floatType()},
},
[]*proto3.Value{stringProto("nan")},
},
&NullFloat64{},
errDecodeColumn(0, errUnexpectedFloat64Str("nan")),
},
{
// Field specifies FLOAT64 type, but value is wrongly encoded.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: floatType()},
},
[]*proto3.Value{stringProto("nan")},
},
proto.Float64(0),
errDecodeColumn(0, errUnexpectedFloat64Str("nan")),
},
{
// Field specifies FLOAT32 type, value is having a nil Kind.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: float32Type()},
},
[]*proto3.Value{{Kind: (*proto3.Value_NumberValue)(nil)}},
},
&NullFloat32{1.0, true},
errDecodeColumn(0, errSrcVal(&proto3.Value{Kind: (*proto3.Value_NumberValue)(nil)}, "Number")),
},
{
// Field specifies FLOAT32 type, but value is for BOOL type.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: float32Type()},
},
[]*proto3.Value{boolProto(true)},
},
&NullFloat32{1.0, true},
errDecodeColumn(0, errSrcVal(boolProto(true), "Number")),
},
{
// Field specifies FLOAT32 type, but value is wrongly encoded.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: float32Type()},
},
[]*proto3.Value{stringProto("nan")},
},
&NullFloat32{},
errDecodeColumn(0, errUnexpectedFloat32Str("nan")),
},
{
// Field specifies FLOAT32 type, but value is wrongly encoded.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: float32Type()},
},
[]*proto3.Value{stringProto("nan")},
},
proto.Float32(0),
errDecodeColumn(0, errUnexpectedFloat32Str("nan")),
},
{
// Field specifies BYTES type, value is having a nil Kind.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: bytesType()},
},
[]*proto3.Value{{Kind: (*proto3.Value_StringValue)(nil)}},
},
&[]byte{},
errDecodeColumn(0, errSrcVal(&proto3.Value{Kind: (*proto3.Value_StringValue)(nil)}, "String")),
},
{
// Field specifies BYTES type, but value is for BOOL type.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: bytesType()},
},
[]*proto3.Value{boolProto(false)},
},
&[]byte{},
errDecodeColumn(0, errSrcVal(boolProto(false), "String")),
},
{
// Field specifies BYTES type, but value is wrongly encoded.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: bytesType()},
},
[]*proto3.Value{stringProto("&&")},
},
&[]byte{},
errDecodeColumn(0, errBadEncoding(stringProto("&&"), func() error {
_, err := base64.StdEncoding.DecodeString("&&")
return err
}())),
},
{
// Field specifies BOOL type, value is having a nil Kind.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: boolType()},
},
[]*proto3.Value{{Kind: (*proto3.Value_BoolValue)(nil)}},
},
&NullBool{false, true},
errDecodeColumn(0, errSrcVal(&proto3.Value{Kind: (*proto3.Value_BoolValue)(nil)}, "Bool")),
},
{
// Field specifies BOOL type, but value is for STRING type.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: boolType()},
},
[]*proto3.Value{stringProto("false")},
},
&NullBool{false, true},
errDecodeColumn(0, errSrcVal(stringProto("false"), "Bool")),
},
{
// Field specifies TIMESTAMP type, value is having a nil Kind.
&Row{
[]*sppb.StructType_Field{
{Name: "Col0", Type: timeType()},
},
[]*proto3.Value{{Kind: (*proto3.Value_StringValue)(nil)}},
},