-
Notifications
You must be signed in to change notification settings - Fork 31
/
decoder_test.go
443 lines (423 loc) · 9.23 KB
/
decoder_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
//
// Copyright 2020 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 main
import (
"math/big"
"testing"
"time"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/typepb"
"cloud.google.com/go/civil"
"cloud.google.com/go/spanner"
)
func createRow(t *testing.T, values []interface{}) *spanner.Row {
t.Helper()
// column names are not important in this test, so use dummy name
names := make([]string, len(values))
for i := 0; i < len(names); i++ {
names[i] = "dummy"
}
row, err := spanner.NewRow(names, values)
if err != nil {
t.Fatalf("Creating spanner row failed unexpectedly: %v", err)
}
return row
}
func createColumnValue(t *testing.T, value interface{}) spanner.GenericColumnValue {
t.Helper()
row := createRow(t, []interface{}{value})
var cv spanner.GenericColumnValue
if err := row.Column(0, &cv); err != nil {
t.Fatalf("Creating spanner column value failed unexpectedly: %v", err)
}
return cv
}
func equalStringSlice(a []string, b []string) bool {
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
type jsonMessage struct {
Msg string `json:"msg"`
}
func TestDecodeColumn(t *testing.T) {
tests := []struct {
desc string
value interface{}
want string
}{
// non-nullable
{
desc: "bool",
value: true,
want: "true",
},
{
desc: "bytes",
value: []byte{'a', 'b', 'c', 'd'},
want: "YWJjZA==", // base64 encoded 'abc'
},
{
desc: "float32",
value: float32(1.23),
want: "1.230000",
},
{
desc: "float64",
value: 1.23,
want: "1.230000",
},
{
desc: "int64",
value: 123,
want: "123",
},
{
desc: "numeric",
value: big.NewRat(123, 100),
want: "1.23",
},
{
desc: "string",
value: "foo",
want: "foo",
},
{
desc: "timestamp",
value: time.Unix(1516676400, 0),
want: "2018-01-23T03:00:00Z",
},
{
desc: "date",
value: civil.DateOf(time.Unix(1516676400, 0)),
want: "2018-01-23",
},
{
desc: "json",
value: spanner.NullJSON{Value: jsonMessage{Msg: "foo"}, Valid: true},
want: `{"msg":"foo"}`,
},
{
desc: "json null is not NULL",
value: spanner.NullJSON{Value: nil, Valid: true},
want: `null`,
},
// nullable
{
desc: "null bool",
value: spanner.NullBool{Bool: false, Valid: false},
want: "NULL",
},
{
desc: "null bytes",
value: []byte(nil),
want: "NULL",
},
{
desc: "null float32",
value: spanner.NullFloat32{Float32: 0, Valid: false},
want: "NULL",
},
{
desc: "null float64",
value: spanner.NullFloat64{Float64: 0, Valid: false},
want: "NULL",
},
{
desc: "null int64",
value: spanner.NullInt64{Int64: 0, Valid: false},
want: "NULL",
},
{
desc: "null numeric",
value: spanner.NullNumeric{Numeric: big.Rat{}, Valid: false},
want: "NULL",
},
{
desc: "null string",
value: spanner.NullString{StringVal: "", Valid: false},
want: "NULL",
},
{
desc: "null time",
value: spanner.NullTime{Time: time.Unix(0, 0), Valid: false},
want: "NULL",
},
{
desc: "null date",
value: spanner.NullDate{Date: civil.DateOf(time.Unix(0, 0)), Valid: false},
want: "NULL",
},
{
desc: "null json",
value: spanner.NullJSON{Value: nil, Valid: false},
want: "NULL",
},
// array non-nullable
{
desc: "empty array",
value: []bool{},
want: "[]",
},
{
desc: "array bool",
value: []bool{true, false},
want: "[true, false]",
},
{
desc: "array bytes",
value: [][]byte{{'a', 'b', 'c', 'd'}, {'e', 'f', 'g', 'h'}},
want: "[YWJjZA==, ZWZnaA==]",
},
{
desc: "array float32",
value: []float32{1.23, 2.45},
want: "[1.230000, 2.450000]",
},
{
desc: "array float64",
value: []float64{1.23, 2.45},
want: "[1.230000, 2.450000]",
},
{
desc: "array int64",
value: []int64{123, 456},
want: "[123, 456]",
},
{
desc: "array numeric",
value: []*big.Rat{big.NewRat(123, 100), big.NewRat(456, 1)},
want: "[1.23, 456]",
},
{
desc: "array string",
value: []string{"foo", "bar"},
want: "[foo, bar]",
},
{
desc: "array timestamp",
value: []time.Time{time.Unix(1516676400, 0), time.Unix(1516680000, 0)},
want: "[2018-01-23T03:00:00Z, 2018-01-23T04:00:00Z]",
},
{
desc: "array date",
value: []civil.Date{civil.DateOf(time.Unix(1516676400, 0)), civil.DateOf(time.Unix(1516762800, 0))},
want: "[2018-01-23, 2018-01-24]",
},
{
desc: "array struct",
value: []struct {
X int64
Y spanner.NullString
}{
{
X: 10,
Y: spanner.NullString{StringVal: "Hello", Valid: true},
},
{
X: 20,
Y: spanner.NullString{StringVal: "", Valid: false},
},
},
want: "[[10, Hello], [20, NULL]]",
},
{
desc: "array json",
value: []spanner.NullJSON{
{Value: jsonMessage{Msg: "foo"}, Valid: true},
{Value: jsonMessage{Msg: "bar"}, Valid: true},
},
want: `[{"msg":"foo"}, {"msg":"bar"}]`,
},
// array nullable
{
desc: "null array bool",
value: []bool(nil),
want: "NULL",
},
{
desc: "null array bytes",
value: [][]byte(nil),
want: "NULL",
},
{
desc: "null array float32",
value: []float32(nil),
want: "NULL",
},
{
desc: "null array float64",
value: []float64(nil),
want: "NULL",
},
{
desc: "null array int64",
value: []int64(nil),
want: "NULL",
},
{
desc: "null array numeric",
value: []*big.Rat(nil),
want: "NULL",
},
{
desc: "null array string",
value: []string(nil),
want: "NULL",
},
{
desc: "null array timestamp",
value: []time.Time(nil),
want: "NULL",
},
{
desc: "null array date",
value: []civil.Date(nil),
want: "NULL",
},
{
desc: "null array json",
value: []spanner.NullJSON(nil),
want: "NULL",
},
// PROTO
// This table tests uses spanner.GenericColumnValue because of non-stability
{
desc: "proto",
value: spanner.GenericColumnValue{
Type: &sppb.Type{
Code: sppb.TypeCode_PROTO,
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
},
Value: structpb.NewStringValue("YWJjZA=="),
},
want: "YWJjZA==",
},
{
desc: "null proto",
value: spanner.GenericColumnValue{
Type: &sppb.Type{
Code: sppb.TypeCode_PROTO,
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
},
Value: structpb.NewNullValue(),
},
want: "NULL",
},
{
desc: "array proto",
value: spanner.GenericColumnValue{
Type: &sppb.Type{
Code: sppb.TypeCode_ARRAY,
ArrayElementType: &sppb.Type{
Code: sppb.TypeCode_PROTO,
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
},
},
Value: structpb.NewListValue(&structpb.ListValue{Values: []*structpb.Value{
structpb.NewStringValue("YWJjZA=="),
structpb.NewStringValue("ZWZnaA=="),
}}),
},
want: "[YWJjZA==, ZWZnaA==]",
},
{
desc: "null array proto",
value: spanner.GenericColumnValue{
Type: &sppb.Type{
Code: sppb.TypeCode_ARRAY,
ArrayElementType: &sppb.Type{
Code: sppb.TypeCode_PROTO,
ProtoTypeFqn: "examples.spanner.music.SingerInfo",
},
},
Value: structpb.NewNullValue(),
},
want: "NULL",
},
// ENUM
{
desc: "enum",
value: typepb.Syntax_SYNTAX_PROTO3,
want: "1",
},
{
desc: "null enum",
value: (*typepb.Syntax)(nil),
want: "NULL",
},
{
desc: "array enum",
value: []*typepb.Syntax{typepb.Syntax_SYNTAX_PROTO2.Enum(), typepb.Syntax_SYNTAX_PROTO3.Enum()},
want: "[0, 1]",
},
{
desc: "null array enum",
value: []*typepb.Syntax(nil),
want: "NULL",
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
got, err := DecodeColumn(createColumnValue(t, test.value))
if err != nil {
t.Error(err)
}
if got != test.want {
t.Errorf("DecodeColumn(%v) = %v, want = %v", test.value, got, test.want)
}
})
}
}
func TestDecodeRow(t *testing.T) {
tests := []struct {
desc string
values []interface{}
want []string
}{
{
desc: "non-null columns",
values: []interface{}{"foo", 123},
want: []string{"foo", "123"},
},
{
desc: "non-null column and null column",
values: []interface{}{"foo", spanner.NullString{StringVal: "", Valid: false}},
want: []string{"foo", "NULL"},
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
got, err := DecodeRow(createRow(t, test.values))
if err != nil {
t.Error(err)
}
if !equalStringSlice(got, test.want) {
t.Errorf("DecodeRow(%v) = %v, want = %v", test.values, got, test.want)
}
})
}
}