forked from monzo/gocassa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
428 lines (367 loc) · 12.2 KB
/
scanner_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
package gocassa
import (
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Account struct {
ID string
Name string
}
func TestScanIterSlice(t *testing.T) {
results := []map[string]interface{}{
{"id": "acc_abcd1", "name": "John", "created": "2018-05-01 19:00:00+0000"},
{"id": "acc_abcd2", "name": "Jane", "created": "2018-05-02 20:00:00+0000"},
}
fieldNames := []string{"id", "name", "created"}
stmt := SelectStatement{keyspace: "test", table: "bench", fields: fieldNames}
iter := newMockIterator(results, stmt.fields)
expected := []Account{
{ID: "acc_abcd1", Name: "John"},
{ID: "acc_abcd2", Name: "Jane"},
}
// Test with decoding into a slice of structs
a1 := []Account{}
rowsRead, err := NewScanner(stmt, &a1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 2, rowsRead)
assert.Equal(t, expected, a1)
iter.Reset()
// Test with decoding into a pointer of slice of structs
b1 := &[]Account{}
rowsRead, err = NewScanner(stmt, &b1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 2, rowsRead)
assert.Equal(t, expected, *b1)
iter.Reset()
// Test with decoding into a pre-populated struct. It should
// remove existing elements
c1 := &[]Account{{ID: "acc_abcd3", Name: "Joe"}}
rowsRead, err = NewScanner(stmt, &c1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 2, rowsRead)
assert.Equal(t, expected, *c1)
iter.Reset()
// Test decoding into a nil slice
var d1 []Account
assert.Nil(t, d1)
rowsRead, err = NewScanner(stmt, &d1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 2, rowsRead)
assert.Equal(t, expected, d1)
iter.Reset()
// Test decoding into a pointer of pointer of nil-ness
var e1 **[]Account
assert.Nil(t, e1)
rowsRead, err = NewScanner(stmt, &e1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 2, rowsRead)
assert.Equal(t, expected, **e1)
iter.Reset()
// Test decoding into a slice of pointers
var f1 []*Account
assert.Nil(t, f1)
rowsRead, err = NewScanner(stmt, &f1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 2, rowsRead)
assert.Equal(t, expected[0], *f1[0])
assert.Equal(t, expected[1], *f1[1])
iter.Reset()
// Test decoding into a completely tangent struct
type fakeStruct struct {
Foo string
Bar string
}
var g1 []fakeStruct
assert.Nil(t, g1)
rowsRead, err = NewScanner(stmt, &g1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 2, rowsRead)
assert.Equal(t, fakeStruct{}, g1[0])
assert.Equal(t, fakeStruct{}, g1[1])
iter.Reset()
// Test decoding into a struct with no fields
type emptyStruct struct{}
var h1 []emptyStruct
assert.Nil(t, h1)
rowsRead, err = NewScanner(stmt, &h1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 2, rowsRead)
assert.Equal(t, emptyStruct{}, h1[0])
assert.Equal(t, emptyStruct{}, h1[1])
iter.Reset()
// Test decoding into a struct with invalid types panics
type badStruct struct {
ID int64
Name int32
}
var i1 []badStruct
assert.Nil(t, i1)
_, err = NewScanner(stmt, &i1).ScanIter(iter)
assert.Error(t, err)
iter.Reset()
// Test decoding with an error
var j1 []fakeStruct
errorerIter := newMockIterator([]map[string]interface{}{}, stmt.fields)
errorScanner := NewScanner(stmt, &j1)
expectedErr := fmt.Errorf("Something went baaaad")
errorerIter.err = expectedErr
rowsRead, err = errorScanner.ScanIter(errorerIter)
assert.Equal(t, 0, rowsRead)
assert.Equal(t, err, expectedErr)
}
func TestScanIterStruct(t *testing.T) {
results := []map[string]interface{}{
{"id": "acc_abcd1", "name": "John", "created": "2018-05-01 19:00:00+0000"},
{"id": "acc_abcd2", "name": "Jane", "created": "2018-05-02 20:00:00+0000"},
}
fieldNames := []string{"id", "name", "created"}
stmt := SelectStatement{keyspace: "test", table: "bench", fields: fieldNames}
iter := newMockIterator(results, stmt.fields)
expected := []Account{
{ID: "acc_abcd1", Name: "John"},
{ID: "acc_abcd2", Name: "Jane"},
}
// Test with decoding into a struct
a1 := Account{}
rowsRead, err := NewScanner(stmt, &a1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 1, rowsRead)
assert.Equal(t, expected[0], a1)
iter.Reset()
// Test decoding into a pointer of pointer to struct
b1 := &Account{}
rowsRead, err = NewScanner(stmt, &b1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 1, rowsRead)
assert.Equal(t, expected[0], *b1)
iter.Reset()
// Test decoding into a nil struct
var c1 *Account
assert.Nil(t, c1)
rowsRead, err = NewScanner(stmt, &c1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 1, rowsRead)
assert.Equal(t, expected[0], *c1)
iter.Reset()
// Test decoding into a pointer of pointer of pointer to struct
var d1 **Account
assert.Nil(t, d1)
rowsRead, err = NewScanner(stmt, &d1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 1, rowsRead)
assert.Equal(t, expected[0], **d1)
iter.Reset()
// Test with multiple scans into different structs
var e1 *Account
var e2 ****Account
rowsRead, err = NewScanner(stmt, &e1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 1, rowsRead)
rowsRead, err = NewScanner(stmt, &e2).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 1, rowsRead)
assert.Equal(t, expected[0], *e1)
assert.Equal(t, expected[1], ****e2)
iter.Reset()
// Test for row not found
var f1 *Account
noResultsIter := newMockIterator([]map[string]interface{}{}, stmt.fields)
rowsRead, err = NewScanner(stmt, &f1).ScanIter(noResultsIter)
assert.EqualError(t, err, ":0: No rows returned")
// Test for a non-rows-not-found error
var g1 *Account
errorerIter := newMockIterator([]map[string]interface{}{}, stmt.fields)
errorScanner := NewScanner(stmt, &g1)
expectedErr := fmt.Errorf("Something went baaaad")
errorerIter.err = expectedErr
rowsRead, err = errorScanner.ScanIter(errorerIter)
assert.Equal(t, 0, rowsRead)
assert.Equal(t, err, expectedErr)
}
func TestScanIterComposite(t *testing.T) {
results := []map[string]interface{}{
{"id": "acc_abcd1", "name": "John", "created": "2018-05-01 19:00:00+0000"},
{"id": "acc_abcd2", "name": "Jane", "created": "2018-05-02 20:00:00+0000"},
}
fieldNames := []string{"id", "name", "metadata", "tags"}
stmt := SelectStatement{keyspace: "test", table: "bench", fields: fieldNames}
iter := newMockIterator(results, stmt.fields)
// Test decoding into a sturct with maps and slices
type metadataType map[string]string
type compositeAccountStruct struct {
ID string
Name string
Metadata metadataType
Tags []string
}
var j1 []compositeAccountStruct
assert.Nil(t, j1)
rowsRead, err := NewScanner(stmt, &j1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 2, rowsRead)
assert.Equal(t, "acc_abcd1", j1[0].ID)
assert.Equal(t, metadataType(map[string]string{}), j1[0].Metadata)
assert.Equal(t, []string{}, j1[0].Tags)
assert.Equal(t, "acc_abcd2", j1[1].ID)
assert.Equal(t, metadataType(map[string]string{}), j1[1].Metadata)
assert.Equal(t, []string{}, j1[1].Tags)
iter.Reset()
}
func TestScanIterEmbedded(t *testing.T) {
results := []map[string]interface{}{
{"id": "acc_abcd1", "name": "John", "created": "2018-05-01 19:00:00+0000"},
{"id": "acc_abcd2", "name": "Jane", "created": "2018-05-02 20:00:00+0000"},
}
fieldNames := []string{"id", "name", "created"}
stmt := SelectStatement{keyspace: "test", table: "bench", fields: fieldNames}
iter := newMockIterator(results, stmt.fields)
type embeddedStruct struct {
*Account
Created string
}
account := Account{}
a1 := embeddedStruct{Account: &account}
assert.NotPanics(t, func() {
rowsRead, err := NewScanner(stmt, &a1).ScanIter(iter)
assert.NoError(t, err)
assert.Equal(t, 1, rowsRead)
})
iter.Reset()
}
func TestScanWithSentinelValues(t *testing.T) {
type accountStruct struct {
ID string
Name string
Metadata []byte
}
t.Run("SliceValues", func(t *testing.T) {
results := []map[string]interface{}{
{"id": "acc_abcd1", "name": ClusteringSentinel, "metadata": []byte{}},
{"id": "acc_abcd2", "name": "Jane", "metadata": []byte(ClusteringSentinel)},
}
fieldNames := []string{"id", "name", "metadata"}
stmt := SelectStatement{keyspace: "test", table: "bench", fields: fieldNames}
iter := newMockIterator(results, stmt.fields)
rows := []*accountStruct{}
rowsRead, err := NewScanner(stmt, &rows).ScanIter(iter)
require.NoError(t, err)
require.Equal(t, 2, rowsRead)
assert.Equal(t, "acc_abcd1", rows[0].ID)
assert.Equal(t, "", rows[0].Name)
assert.Equal(t, []byte{}, rows[0].Metadata)
assert.Equal(t, "acc_abcd2", rows[1].ID)
assert.Equal(t, "Jane", rows[1].Name)
assert.Equal(t, []byte{}, rows[1].Metadata)
})
t.Run("StructValues", func(t *testing.T) {
results := []map[string]interface{}{
{"id": "acc_abcd1", "name": ClusteringSentinel, "metadata": []byte{}},
}
fieldNames := []string{"id", "name", "metadata"}
stmt := SelectStatement{keyspace: "test", table: "bench", fields: fieldNames}
iter := newMockIterator(results, stmt.fields)
row := &accountStruct{}
rowsRead, err := NewScanner(stmt, row).ScanIter(iter)
require.NoError(t, err)
require.Equal(t, 1, rowsRead)
assert.Equal(t, "acc_abcd1", row.ID)
assert.Equal(t, "", row.Name)
assert.Equal(t, []byte{}, row.Metadata)
})
}
func TestFillInZeroedPtrs(t *testing.T) {
str := ""
strSlice := []string{}
strMap := map[string]string{}
strSliceNil := []string(nil)
strMapNil := map[string]string(nil)
// Test with already allocated
fillInZeroedPtrs([]interface{}{&str, &strSlice, &strMap})
assert.Equal(t, "", str)
assert.Equal(t, []string{}, strSlice)
assert.Equal(t, map[string]string{}, strMap)
// Test with nil allocated
assert.NotEqual(t, []string{}, strSliceNil)
assert.NotEqual(t, map[string]string{}, strMapNil)
fillInZeroedPtrs([]interface{}{&strSliceNil, &strMapNil})
assert.Equal(t, []string{}, strSliceNil)
assert.Equal(t, map[string]string{}, strMapNil)
}
func TestRemoveSentinelValues(t *testing.T) {
str := ""
byteSlice := []byte{}
intVal := 0
removeSentinelValues([]interface{}{&str, &byteSlice, &intVal})
assert.Equal(t, "", str)
assert.Equal(t, []byte{}, byteSlice)
assert.Equal(t, 0, intVal)
str = ClusteringSentinel
byteSlice = []byte(ClusteringSentinel)
removeSentinelValues([]interface{}{&str, &byteSlice, &intVal})
assert.Equal(t, "", str)
assert.Equal(t, []byte{}, byteSlice)
assert.Equal(t, 0, intVal)
}
func TestAllocateNilReference(t *testing.T) {
// Test non pointer, should do nothing
var a string
assert.Equal(t, "", a)
assert.NoError(t, allocateNilReference(a))
assert.Equal(t, "", a)
// Test pointer which hasn't been passed in by reference, should err
var b *string
assert.Nil(t, b)
assert.Error(t, allocateNilReference(b))
// Test pointer which is passed in by ref
assert.Nil(t, b)
assert.NoError(t, allocateNilReference(&b))
assert.Equal(t, "", *b)
// Test with a struct
type test struct{}
var c *test
assert.Nil(t, c)
assert.NoError(t, allocateNilReference(&c))
assert.Equal(t, test{}, *c)
// Test with a slice
var d *[]test
assert.Nil(t, d)
assert.NoError(t, allocateNilReference(&d))
assert.Equal(t, []test{}, *d)
// Test with a slice of pointers
var e *[]*test
assert.Nil(t, e)
assert.NoError(t, allocateNilReference(&e))
assert.Equal(t, []*test{}, *e)
// Test with a map
var f map[string]test
assert.Nil(t, f)
assert.NoError(t, allocateNilReference(&f))
assert.Equal(t, map[string]test{}, f)
// Test with an allocated struct, it should just return
g := []*test{}
ref := &g
assert.NoError(t, allocateNilReference(&g))
assert.True(t, &g == ref) // These should be the same pointer
}
func TestGetNonPtrType(t *testing.T) {
var a int
assert.Equal(t, reflect.TypeOf(int(0)), getNonPtrType(reflect.TypeOf(a)))
assert.Equal(t, reflect.TypeOf(int(0)), getNonPtrType(reflect.TypeOf(&a)))
var b *int
assert.Equal(t, reflect.TypeOf(int(0)), getNonPtrType(reflect.TypeOf(&b)))
var c []*int
assert.Equal(t, reflect.TypeOf([]*int{}), getNonPtrType(reflect.TypeOf(c)))
assert.Equal(t, reflect.TypeOf([]*int{}), getNonPtrType(reflect.TypeOf(&c)))
}
func TestWrapPtrValue(t *testing.T) {
// Test with no pointers, should do nothing
a := reflect.ValueOf("")
assert.Equal(t, string(""), wrapPtrValue(a, reflect.TypeOf("")).String())
// Go ham with a double pointer
var s **string
targetType := reflect.TypeOf(s)
assert.Equal(t, string(""), wrapPtrValue(a, targetType).Elem().Elem().String())
}