forked from blockloop/scan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
333 lines (266 loc) · 7.15 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
package scan_test
import (
"database/sql"
"errors"
"reflect"
"sync"
"testing"
"github.com/blockloop/scan"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRowsConvertsColumnNamesToTitleText(t *testing.T) {
var item struct {
First string
}
expected := "Brett Jones"
rows := fakeRowsWithRecords(t, []string{"First"},
[]interface{}{expected},
)
require.NoError(t, scan.Row(&item, rows))
assert.Equal(t, 1, rows.ScanCallCount())
assert.Equal(t, expected, item.First)
}
func TestRowsUsesTagName(t *testing.T) {
expected := "Brett Jones"
rows := fakeRowsWithRecords(t, []string{"first_and_last_name"},
[]interface{}{expected},
)
var item struct {
FirstAndLastName string `db:"first_and_last_name"`
}
require.NoError(t, scan.Row(&item, rows))
assert.Equal(t, 1, rows.ScanCallCount())
assert.Equal(t, expected, item.FirstAndLastName)
}
func TestRowsIgnoresUnsetableColumns(t *testing.T) {
expected := "Brett Jones"
rows := fakeRowsWithRecords(t, []string{"first_and_last_name"},
[]interface{}{expected},
)
var item struct {
// private, unsetable
firstAndLastName string `db:"first_and_last_name"`
}
require.NoError(t, scan.Row(&item, rows))
assert.NotEqual(t, expected, item.firstAndLastName)
}
func TestErrorsWhenScanErrors(t *testing.T) {
expected := errors.New("asdf")
rows := fakeRowsWithColumns(t, 1, "first_and_last_name")
rows.ScanStub = func(...interface{}) error {
return expected
}
var item struct {
FirstAndLastName string `db:"first_and_last_name"`
}
err := scan.Row(&item, rows)
assert.Equal(t, expected, err)
}
func TestRowsErrorsWhenNotGivenAPointer(t *testing.T) {
rows := fakeRowsWithColumns(t, 1, "name")
err := scan.Rows("hello", rows)
require.Error(t, err)
assert.Contains(t, err.Error(), "pointer")
}
func TestRowsErrorsWhenNotGivenAPointerToSlice(t *testing.T) {
rows := fakeRowsWithColumns(t, 1, "name")
var item struct{}
err := scan.Rows(&item, rows)
require.Error(t, err)
assert.Contains(t, err.Error(), "slice")
}
func TestErrorsWhenColumnsReturnsError(t *testing.T) {
expected := errors.New("asdf")
rows := &FakeRowsScanner{
ColumnsStub: func() ([]string, error) {
return nil, expected
},
}
var items []struct {
Name string
Age int
}
err := scan.Rows(&items, rows)
assert.Equal(t, expected, err)
}
func TestDoesNothingWhenNoColumns(t *testing.T) {
rows := fakeRowsWithColumns(t, 1)
var items []struct {
Name string
Age int
}
err := scan.Rows(&items, rows)
assert.NoError(t, err)
assert.Nil(t, items)
}
func TestDoesNothingWhenNextIsFalse(t *testing.T) {
rows := fakeRowsWithColumns(t, 0, "Name")
var items []struct {
Name string
Age int
}
err := scan.Rows(&items, rows)
assert.NoError(t, err)
assert.Nil(t, items)
}
func TestIgnoresColumnsThatDoNotHaveFields(t *testing.T) {
rows := fakeRowsWithRecords(t, []string{"First", "Last", "Age"},
[]interface{}{"Brett", "Jones"},
[]interface{}{"Fred", "Jones"},
)
var items []struct {
First string
Last string
}
require.NoError(t, scan.Rows(&items, rows))
require.Len(t, items, 2)
assert.Equal(t, "Brett", items[0].First)
assert.Equal(t, "Jones", items[0].Last)
assert.Equal(t, "Fred", items[1].First)
assert.Equal(t, "Jones", items[1].Last)
}
func TestIgnoresFieldsThatDoNotHaveColumns(t *testing.T) {
rows := fakeRowsWithRecords(t, []string{"first", "age"},
[]interface{}{"Brett", int8(40)},
[]interface{}{"Fred", int8(50)},
)
var items []struct {
First string
Last string
Age int8
}
require.NoError(t, scan.Rows(&items, rows))
require.Len(t, items, 2)
assert.EqualValues(t, "Brett", items[0].First)
assert.EqualValues(t, "", items[0].Last)
assert.EqualValues(t, 40, items[0].Age)
assert.EqualValues(t, "Fred", items[1].First)
assert.EqualValues(t, "", items[1].Last)
assert.EqualValues(t, 50, items[1].Age)
}
func TestRowScansToPrimitiveType(t *testing.T) {
expected := "Bob"
rows := fakeRowsWithRecords(t, []string{"name"},
[]interface{}{expected},
)
var name string
assert.NoError(t, scan.Row(&name, rows))
assert.Equal(t, expected, name)
}
func TestReturnsScannerError(t *testing.T) {
scanErr := errors.New("broken")
rows := fakeRowsWithColumns(t, 1, "Name")
rows.ErrReturns(scanErr)
var persons []struct {
Name string
}
err := scan.Rows(&persons, rows)
assert.EqualValues(t, scanErr, err)
}
func TestScansPrimitiveSlices(t *testing.T) {
table := [][]interface{}{
{1, 2, 3},
{"brett", "fred", "geoff"},
{true, false},
{1.0, 1.1, 1.2},
}
for _, items := range table {
// each item in items is a single value which needs to be converted
// to a single row with a scalar value
dbrows := make([][]interface{}, len(items))
for i, item := range items {
dbrows[i] = []interface{}{item}
}
rows := fakeRowsWithRecords(t, []string{"a"}, dbrows...)
var scanned []interface{}
require.NoError(t, scan.Rows(&scanned, rows))
assert.EqualValues(t, items, scanned)
}
}
func TestErrorsWhenMoreThanOneColumnForPrimitiveSlice(t *testing.T) {
rows := fakeRowsWithColumns(t, 1, "fname", "lname")
var fnames []string
err := scan.Rows(&fnames, rows)
assert.EqualValues(t, scan.ErrTooManyColumns, err)
}
func TestErrorsWhenScanRowToSlice(t *testing.T) {
rows := &FakeRowsScanner{}
var persons []struct {
ID int
}
err := scan.Row(&persons, rows)
assert.EqualValues(t, scan.ErrSliceForRow, err)
}
func TestRowReturnsErrNoRowsWhenQueryHasNoRows(t *testing.T) {
rows := fakeRowsWithColumns(t, 0, "First")
var item struct {
First string
}
assert.EqualValues(t, sql.ErrNoRows, scan.Row(&item, rows))
}
func TestRowErrorsWhenItemIsNotAPointer(t *testing.T) {
rows := &FakeRowsScanner{}
var item struct {
First string
}
err := scan.Row(item, rows)
require.Error(t, err)
assert.Contains(t, err.Error(), "pointer")
}
func TestRowStrictIgnoresFieldsWithoutDBTag(t *testing.T) {
rows := fakeRowsWithRecords(t, []string{"First", "Last"},
[]interface{}{"Brett", "Jones"},
)
var item struct {
First string `db:"First"`
Last string
}
require.NoError(t, scan.RowStrict(&item, rows))
assert.Equal(t, "Brett", item.First)
assert.Equal(t, "", item.Last)
}
func TestRowsStrictIgnoresFieldsWithoutDBTag(t *testing.T) {
rows := fakeRowsWithRecords(t, []string{"First", "Last"},
[]interface{}{"Brett", "Jones"},
[]interface{}{"Fred", "Jones"},
)
var items []struct {
First string `db:"First"`
Last string
}
require.NoError(t, scan.RowsStrict(&items, rows))
require.Len(t, items, 2)
assert.Equal(t, "Brett", items[0].First)
assert.Equal(t, "", items[0].Last)
assert.Equal(t, "Fred", items[1].First)
assert.Equal(t, "", items[1].Last)
}
func setValue(ptr, val interface{}) {
reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(val))
}
type simpleQueue struct {
items []interface{}
m *sync.Mutex
}
func newSimpleQueue(items []interface{}) *simpleQueue {
return &simpleQueue{
items: items,
m: &sync.Mutex{},
}
}
func (q *simpleQueue) Push(v interface{}) {
q.m.Lock()
defer q.m.Unlock()
q.items = append([]interface{}{v}, q.items...)
}
func (q *simpleQueue) Pop() (v interface{}, ok bool) {
q.m.Lock()
defer q.m.Unlock()
if len(q.items) == 0 {
return nil, false
}
v = q.items[0]
q.items = q.items[1:]
return v, true
}