-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrow_test.go
447 lines (367 loc) · 10.3 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
package sqle
import (
"database/sql"
"database/sql/driver"
"errors"
"reflect"
"testing"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/require"
)
type NullStr struct {
String string
Valid bool // Valid is true if String is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullStr) Scan(value any) error {
if value == nil {
ns.String, ns.Valid = "", false
return nil
}
sv, err := driver.String.ConvertValue(value)
if err != nil {
return err
}
if s, ok := sv.(string); ok {
ns.Valid = true
ns.String = s
return nil
}
// otherwise, return an error
return errors.New("failed to scan string")
}
type customBinder struct {
UserID int
Status int
Email string
}
func (cb *customBinder) Bind(_ reflect.Value, columns []string) []any {
var missed any
values := make([]any, 0, len(columns))
for _, col := range columns {
switch col {
case "id":
values = append(values, &cb.UserID)
case "status":
values = append(values, &cb.Status)
case "email":
values = append(values, &cb.Email)
default:
values = append(values, &missed)
}
}
return values
}
func TestRow(t *testing.T) {
d, err := sql.Open("sqlite3", "file::memory:?cache=shared")
require.NoError(t, err)
_, err = d.Exec("CREATE TABLE `users` (`id` int , `status` tinyint,`email` varchar(50),`passwd` varchar(120), `salt` varchar(45), `created` DATETIME, PRIMARY KEY (`id`))")
require.NoError(t, err)
now := time.Now()
_, err = d.Exec("INSERT INTO `users`(`id`,`status`,`email`,`passwd`,`salt`,`created`) VALUES(1, 1,'[email protected]','1xxxx','1zzzz', ?)", now)
require.NoError(t, err)
_, err = d.Exec("INSERT INTO `users`(`id`,`status`,`email`,`passwd`,`salt`) VALUES(2, 2,'[email protected]','2xxxx','2zzzz')")
require.NoError(t, err)
_, err = d.Exec("INSERT INTO `users`(`id`,`status`,`email`,`passwd`,`salt`) VALUES(3, 3,'[email protected]','3xxxx','3zzzz')")
require.NoError(t, err)
_, err = d.Exec("INSERT INTO `users`(`id`, `email`) VALUES(4, '[email protected]')")
require.NoError(t, err)
db := Open(d)
tests := []struct {
name string
run func(t *testing.T)
}{
{
name: "close_should_always_work",
run: func(*testing.T) {
var row *Row
row.Close()
row = &Row{}
row.Close()
},
},
{
name: "bind_only_work_with_non_nil_pointer",
run: func(t *testing.T) {
row := &Row{}
var dest int
err := row.Bind(dest)
require.ErrorIs(t, err, ErrMustPointer)
var dest2 *int
err = row.Bind(dest2)
require.ErrorIs(t, err, ErrMustNotNilPointer)
},
},
{
name: "full_columns_should_work",
run: func(t *testing.T) {
type user struct {
ID uint
Status int
Email string
Passwd string
Salt string
Created *time.Time
}
row := db.QueryRow("SELECT * FROM users WHERE id=?", 1)
var u user
err := row.Bind(&u)
require.NoError(t, err)
require.Equal(t, uint(1), u.ID)
require.Equal(t, 1, u.Status)
require.Equal(t, "[email protected]", u.Email)
require.Equal(t, "1xxxx", u.Passwd)
require.Equal(t, "1zzzz", u.Salt)
var u2 user
row2 := db.QueryRow("SELECT * FROM users WHERE id=?", 2)
err = row2.Bind(&u2)
require.NoError(t, err)
require.Equal(t, uint(2), u2.ID)
require.Equal(t, 2, u2.Status)
require.Equal(t, "[email protected]", u2.Email)
require.Equal(t, "2xxxx", u2.Passwd)
require.Equal(t, "2zzzz", u2.Salt)
},
},
{
name: "missed_column_should_work",
run: func(t *testing.T) {
type user struct {
ID uint
Status int
Email string
Passwd string
Salt string
}
row := db.QueryRow("SELECT id,status,email,salt FROM users WHERE id=?", 1)
var u user
err := row.Bind(&u)
require.NoError(t, err)
require.Equal(t, uint(1), u.ID)
require.Equal(t, 1, u.Status)
require.Equal(t, "[email protected]", u.Email)
require.Equal(t, "", u.Passwd)
require.Equal(t, "1zzzz", u.Salt)
},
},
{
name: "missed_field_should_work",
run: func(t *testing.T) {
type user struct {
ID uint
Status int
Salt string
}
row := db.QueryRow("SELECT * FROM users WHERE id=?", 1)
var u user
err := row.Bind(&u)
require.NoError(t, err)
require.Equal(t, uint(1), u.ID)
require.Equal(t, 1, u.Status)
require.Equal(t, "1zzzz", u.Salt)
},
},
{
name: "alias_should_work",
run: func(t *testing.T) {
type user struct {
ID uint
Status int
Password string `db:"passwd"`
}
row := db.QueryRow("SELECT * FROM users WHERE id=?", 1)
var u user
err := row.Bind(&u)
require.NoError(t, err)
require.Equal(t, uint(1), u.ID)
require.Equal(t, 1, u.Status)
require.Equal(t, "1xxxx", u.Password)
},
},
{
name: "bind_is_case_insensitive",
run: func(t *testing.T) {
type user struct {
ID uint
Status int
Password string `db:"passwd"`
}
row := db.QueryRow("SELECT id,StaTus,PassWD FROM users WHERE id=?", 1)
var u user
err := row.Bind(&u)
require.NoError(t, err)
require.Equal(t, uint(1), u.ID)
require.Equal(t, 1, u.Status)
require.Equal(t, "1xxxx", u.Password)
},
},
{
name: "hyphen_in_column_should_work",
run: func(t *testing.T) {
type user struct {
UserID uint
Status int
Passwd string
}
row := db.QueryRow("SELECT id as user_id,StaTus,PassWD FROM users WHERE id=?", 1)
var u user
err := row.Bind(&u)
require.NoError(t, err)
require.Equal(t, uint(1), u.UserID)
require.Equal(t, 1, u.Status)
require.Equal(t, "1xxxx", u.Passwd)
},
},
{
name: "bind_map_should_work",
run: func(t *testing.T) {
row := db.QueryRow("SELECT id,status,passwd FROM users WHERE id=?", 1)
u := make(map[string]interface{})
err := row.Bind(&u)
require.NoError(t, err)
require.EqualValues(t, uint(1), u["id"])
require.EqualValues(t, 1, u["status"])
require.Equal(t, "1xxxx", u["passwd"])
row = db.QueryRow("SELECT id,status FROM users WHERE id=?", 2)
u2 := make(map[string]int)
err = row.Bind(&u2)
require.NoError(t, err)
require.Equal(t, 2, u2["id"])
require.Equal(t, 2, u2["status"])
row = db.QueryRow("SELECT id,status FROM users WHERE id=?", 3)
u3 := make(map[string]string)
err = row.Bind(&u3)
require.NoError(t, err)
require.Equal(t, "3", u3["id"])
require.Equal(t, "3", u3["status"])
},
},
{
name: "bind_scanner_should_work",
run: func(t *testing.T) {
row := db.QueryRow("SELECT status FROM users WHERE id=?", 4)
var b sql.NullBool
err := row.Bind(&b)
require.NoError(t, err)
require.False(t, b.Bool)
require.False(t, b.Valid)
row = db.QueryRow("SELECT status FROM users WHERE id=?", 4)
var i16 sql.NullInt16
err = row.Bind(&i16)
require.NoError(t, err)
require.EqualValues(t, 0, i16.Int16)
require.False(t, i16.Valid)
row = db.QueryRow("SELECT status FROM users WHERE id=?", 4)
var i32 sql.NullInt32
err = row.Bind(&i32)
require.NoError(t, err)
require.EqualValues(t, 0, i32.Int32)
require.False(t, i32.Valid)
row = db.QueryRow("SELECT status FROM users WHERE id=?", 4)
var i64 sql.NullInt64
err = row.Bind(&i64)
require.NoError(t, err)
require.EqualValues(t, 0, i64.Int64)
require.False(t, i64.Valid)
row = db.QueryRow("SELECT status FROM users WHERE id=?", 4)
var f64 sql.NullFloat64
err = row.Bind(&f64)
require.NoError(t, err)
require.EqualValues(t, 0, f64.Float64)
require.False(t, f64.Valid)
row = db.QueryRow("SELECT status FROM users WHERE id=?", 4)
var nt sql.NullTime
err = row.Bind(&nt)
require.NoError(t, err)
require.Equal(t, time.Time{}, nt.Time)
require.False(t, nt.Valid)
row = db.QueryRow("SELECT status FROM users WHERE id=?", 4)
var str sql.NullString
err = row.Bind(&str)
require.NoError(t, err)
require.Equal(t, "", str.String)
require.False(t, str.Valid)
row = db.QueryRow("SELECT status FROM users WHERE id=?", 4)
var ns NullStr
err = row.Bind(&ns)
require.NoError(t, err)
require.Equal(t, "", ns.String)
require.False(t, ns.Valid)
},
},
{
name: "bind_primitive_types_should_work",
run: func(t *testing.T) {
row := db.QueryRow("SELECT id FROM users WHERE id=?", 2)
var i int
err := row.Bind(&i)
require.NoError(t, err)
require.Equal(t, 2, i)
row = db.QueryRow("SELECT id FROM users WHERE id=?", 2)
var str string
err = row.Bind(&str)
require.NoError(t, err)
require.Equal(t, "2", str)
row = db.QueryRow("SELECT created FROM users WHERE id=?", 1)
var tm time.Time
err = row.Bind(&tm)
require.NoError(t, err)
require.Equal(t, now.UnixNano(), tm.UnixNano())
},
},
{
name: "bind_null_column_should_work",
run: func(t *testing.T) {
type user struct {
ID int
Status *int
Email *string
Passwd *string
Salt *string
Created *time.Time
NullStatus sql.NullInt32
NullEmail sql.NullString
NullPasswd sql.NullString
NullSalt sql.NullString
NullCreated sql.NullTime
}
row := db.QueryRow(`SELECT id, status, email, passwd, salt, created,
status as null_status, email as null_email, passwd as null_passwd, salt as null_salt, created as null_created
FROM users WHERE id=?`, 4)
var u user
err := row.Bind(&u)
require.NoError(t, err)
require.Equal(t, 4, u.ID)
require.Nil(t, u.Status)
require.NotNil(t, u.Email)
require.Equal(t, "[email protected]", *u.Email)
require.Nil(t, u.Passwd)
require.Nil(t, u.Salt)
require.Nil(t, u.Created)
require.False(t, u.NullStatus.Valid)
require.True(t, u.NullEmail.Valid)
require.Equal(t, "[email protected]", u.NullEmail.String)
require.False(t, u.NullPasswd.Valid)
require.False(t, u.NullSalt.Valid)
require.False(t, u.NullCreated.Valid)
},
},
{
name: "bind_custom_binder_should_work",
run: func(t *testing.T) {
row := db.QueryRow("SELECT id,status,email,salt FROM users WHERE id=?", 1)
var u customBinder
err := row.Bind(&u)
require.NoError(t, err)
require.Equal(t, 1, u.UserID)
require.Equal(t, 1, u.Status)
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.run(t)
})
}
}