This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
forked from jmoiron/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlx_test.go
522 lines (441 loc) · 12.8 KB
/
sqlx_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
// The following environment variables, if set, will be used:
//
// Sqlite:
// * SQLX_SQLITEPATH
//
// Postgres:
// * SQLX_PGUSER
// * SQLX_PGPASS
//
// MySQL:
// * SQLX_MYSQLUSER
// * SQLX_MYSQLPASS
//
// To disable testing against any of these databases, set one of:
// * SQLX_NOPG, SQLX_NOMYSQL, SQLX_NOSQLITE
package sqlx
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"os"
"os/user"
"strings"
"testing"
)
var TestPostgres = true
var TestSqlite = true
var TestMysql = true
var sldb *DB
var pgdb *DB
var mysqldb *DB
var active = []*DB{}
func init() {
PostgresConnect()
SqliteConnect()
MysqlConnect()
}
func PostgresConnect() {
if len(os.Getenv("SQLX_NOPG")) > 0 {
TestPostgres = false
fmt.Printf("Skipping Postgres tests, SQLX_NOPG set.\n")
return
}
var username, password string
var err error
username = os.Getenv("SQLX_PGUSER")
password = os.Getenv("SQLX_PGPASS")
if len(username) == 0 {
u, err := user.Current()
if err != nil {
fmt.Printf("Could not find current user username, trying 'test' instead.")
username = "test"
} else {
username = u.Username
}
}
dsn := fmt.Sprintf("user=%s dbname=sqlxtest sslmode=disable", username)
if len(password) > 0 {
dsn = fmt.Sprintf("user=%s password=%s dbname=sqlxtest sslmode=disable", username, password)
}
pgdb, err = Connect("postgres", dsn)
if err != nil {
fmt.Printf("Could not connect to postgres, try `createdb sqlxtext`, disabling PG tests:\n %v\n", err)
TestPostgres = false
}
}
func SqliteConnect() {
if len(os.Getenv("SQLX_NOSQLITE")) > 0 {
TestSqlite = false
fmt.Printf("Skipping sqlite tests, SQLX_NOSQLITE set.\n")
return
}
var path string
var err error
path = os.Getenv("SQLX_SQLITE_PATH")
if len(path) == 0 {
path = "/tmp/sqlxtest.db"
}
sldb, err = Connect("sqlite3", path)
if err != nil {
fmt.Printf("Could not create sqlite3 db in %s:\n %v", path, err)
TestSqlite = false
}
}
func MysqlConnect() {
if len(os.Getenv("SQLX_NOMYSQL")) > 0 {
TestSqlite = false
fmt.Printf("Skipping mysql tests, SQLX_NOMYSQL set.\n")
return
}
var username, dbname, password string
var err error
username = os.Getenv("SQLX_MYSQLUSER")
password = os.Getenv("SQLX_MYSQLPASS")
dbname = "sqlxtest"
if len(username) == 0 {
u, err := user.Current()
if err != nil {
fmt.Printf("Could not find current user username, trying 'test' instead.")
username = "test"
} else {
username = u.Username
}
}
mysqldb, err = Connect("mysql", fmt.Sprintf("%s:%s@/%s", username, password, dbname))
if err != nil {
fmt.Printf("Could not connect to mysql db, try `mysql -e 'create database sqlxtest'`, disabling MySQL tests:\n %v", err)
TestMysql = false
}
}
var schema = `
CREATE TABLE person (
first_name text,
last_name text,
email text
);
CREATE TABLE place (
country text,
city text NULL,
telcode integer
)`
var drop = `
drop table person;
drop table place;
`
type Person struct {
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string
}
type Place struct {
Country string
City sql.NullString
TelCode int
}
func MultiExec(e Execer, query string) {
stmts := strings.Split(query, ";\n")
if len(strings.Trim(stmts[len(stmts)-1], " \n\t\r")) == 0 {
stmts = stmts[:len(stmts)-1]
}
for _, s := range stmts {
e.Exec(s)
}
}
func TestUsage(t *testing.T) {
RunTest := func(db *DB, t *testing.T, dbtype string) {
var err error
defer func(dbtype string) {
if dbtype != "postgres" {
MultiExec(db, drop)
} else {
db.Execf(drop)
}
}(dbtype)
// pq will execute multi-query statements, but sqlite3 won't!
if dbtype != "postgres" {
MultiExec(db, schema)
} else {
db.Execf(schema)
}
tx := db.MustBegin()
tx.Execl(tx.Rebind("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)"), "Jason", "Moiron", "[email protected]")
tx.Execl(tx.Rebind("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)"), "John", "Doe", "[email protected]")
tx.Execl(tx.Rebind("INSERT INTO place (country, city, telcode) VALUES (?, ?, ?)"), "United States", "New York", "1")
tx.Execl(tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Hong Kong", "852")
tx.Execl(tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Singapore", "65")
tx.Commit()
people := []Person{}
err = db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
if err != nil {
t.Fatalf("Could not select from people: %s", err)
}
jason, john := people[0], people[1]
if jason.FirstName != "Jason" {
t.Errorf("Expecting FirstName of Jason, got %s", jason.FirstName)
}
if jason.LastName != "Moiron" {
t.Errorf("Expecting LastName of Moiron, got %s", jason.LastName)
}
if jason.Email != "[email protected]" {
t.Errorf("Expecting Email of [email protected], got %s", jason.Email)
}
if john.FirstName != "John" || john.LastName != "Doe" || john.Email != "[email protected]" {
t.Errorf("John Doe's person record not what expected: Got %v\n", john)
}
jason = Person{}
err = db.Get(&jason, db.Rebind("SELECT * FROM person WHERE first_name=?"), "Jason")
if err != nil {
t.Errorf("Expecting no error, got %v\n", err)
}
if jason.FirstName != "Jason" {
t.Errorf("Expecting to get back Jason, but got %v\n", jason.FirstName)
}
err = db.Get(&jason, db.Rebind("SELECT * FROM person WHERE first_name=?"), "Foobar")
if err == nil {
t.Errorf("Expecting an error, got nil\n")
}
if err != sql.ErrNoRows {
t.Errorf("Expected sql.ErrNoRows, got %v\n", err)
}
places := []*Place{}
err = db.Select(&places, "SELECT telcode FROM place ORDER BY telcode ASC")
usa, singsing, honkers := places[0], places[1], places[2]
if usa.TelCode != 1 || honkers.TelCode != 852 || singsing.TelCode != 65 {
t.Errorf("Expected integer telcodes to work, got %#v", places)
}
// if you have null fields and use SELECT *, you must use sql.Null* in your struct
// this test also verifies that you can use either a []Struct{} or a []*Struct{}
places2 := []Place{}
err = db.Select(&places2, "SELECT * FROM place ORDER BY telcode ASC")
usa, singsing, honkers = &places2[0], &places2[1], &places2[2]
// this should return a type error that &p is not a pointer to a struct slice
p := Place{}
err = db.Select(&p, "SELECT * FROM place ORDER BY telcode ASC")
if err == nil {
t.Errorf("Expected an error, argument to select should be a pointer to a struct slice")
}
// this should be an error because
pl := []Place{}
err = db.Select(pl, "SELECT * FROM place ORDER BY telcode ASC")
if err == nil {
t.Errorf("Expected an error, argument to select should be a pointer to a struct slice, not a slice.")
}
if usa.TelCode != 1 || honkers.TelCode != 852 || singsing.TelCode != 65 {
t.Errorf("Expected integer telcodes to work, got %#v", places)
}
stmt, err := db.Preparex(db.Rebind("SELECT country, telcode FROM place WHERE telcode > ? ORDER BY telcode ASC"))
if err != nil {
t.Error(err)
}
places = []*Place{}
err = stmt.Select(&places, 10)
if len(places) != 2 {
t.Error("Expected 2 places, got 0.")
}
if err != nil {
t.Fatal(err)
}
singsing, honkers = places[0], places[1]
if singsing.TelCode != 65 || honkers.TelCode != 852 {
t.Errorf("Expected the right telcodes, got %#v", places)
}
rows, err := db.Queryx("SELECT * FROM place")
if err != nil {
t.Fatal(err)
}
place := Place{}
for rows.Next() {
err = rows.StructScan(&place)
if err != nil {
t.Fatal(err)
}
}
// test advanced querying
_, err = db.NamedExecMap("INSERT INTO person (first_name, last_name, email) VALUES (:first, :last, :email)", map[string]interface{}{
"first": "Bin",
"last": "Smuth",
"email": "[email protected]",
})
if err != nil {
t.Fatal(err)
}
// ensure that if the named param happens right at the end it still works
rows, err = db.NamedQueryMap("SELECT * FROM person WHERE first_name=:first", map[string]interface{}{"first": "Bin"})
if err != nil {
t.Fatal(err)
}
ben := &Person{}
for rows.Next() {
err = rows.StructScan(ben)
if err != nil {
t.Fatal(err)
}
if ben.FirstName != "Bin" {
t.Fatal("Expected first name of `Bin`, got " + ben.FirstName)
}
if ben.LastName != "Smuth" {
t.Fatal("Expected first name of `Smuth`, got " + ben.LastName)
}
}
ben.FirstName = "Ben"
ben.LastName = "Smith"
// Insert via a named query using the struct
_, err = db.NamedExec("INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", ben)
if err != nil {
t.Fatal(err)
}
rows, err = db.NamedQuery("SELECT * FROM person WHERE first_name=:first_name", ben)
if err != nil {
t.Fatal(err)
}
for rows.Next() {
err = rows.StructScan(ben)
if err != nil {
t.Fatal(err)
}
if ben.FirstName != "Ben" {
t.Fatal("Expected first name of `Ben`, got " + ben.FirstName)
}
if ben.LastName != "Smith" {
t.Fatal("Expected first name of `Smith`, got " + ben.LastName)
}
}
}
if TestPostgres {
RunTest(pgdb, t, "postgres")
}
if TestSqlite {
RunTest(sldb, t, "sqlite")
}
if TestMysql {
RunTest(mysqldb, t, "mysql")
}
}
// tests that sqlx will not panic when the wrong driver is passed because
// of an automatic nil dereference in sqlx.Open(), which was fixed.
func TestDoNotPanicOnConnect(t *testing.T) {
_, err := Connect("bogus", "hehe")
if err == nil {
t.Errorf("Should return error when using bogus driverName")
}
}
func TestRebind(t *testing.T) {
q1 := `INSERT INTO foo (a, b, c, d, e, f, g, h, i) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
q2 := `INSERT INTO foo (a, b, c) VALUES (?, ?, "foo"), ("Hi", ?, ?)`
s1 := Rebind(DOLLAR, q1)
s2 := Rebind(DOLLAR, q2)
if s1 != `INSERT INTO foo (a, b, c, d, e, f, g, h, i) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)` {
t.Errorf("q1 failed")
}
if s2 != `INSERT INTO foo (a, b, c) VALUES ($1, $2, "foo"), ("Hi", $3, $4)` {
t.Errorf("q2 failed")
}
}
func TestBindMap(t *testing.T) {
// Test that it works..
q1 := `INSERT INTO foo (a, b, c, d) VALUES (:name, :age, :first, :last)`
am := map[string]interface{}{
"name": "Jason Moiron",
"age": 30,
"first": "Jason",
"last": "Moiron",
}
bq, args, _ := BindMap(QUESTION, q1, am)
expect := `INSERT INTO foo (a, b, c, d) VALUES (?, ?, ?, ?)`
if bq != expect {
t.Errorf("Interpolation of query failed: got `%v`, expected `%v`\n", bq, expect)
}
if args[0].(string) != "Jason Moiron" {
t.Errorf("Expected `Jason Moiron`, got %v\n", args[0])
}
if args[1].(int) != 30 {
t.Errorf("Expected 30, got %v\n", args[1])
}
if args[2].(string) != "Jason" {
t.Errorf("Expected Jason, got %v\n", args[2])
}
if args[3].(string) != "Moiron" {
t.Errorf("Expected Moiron, got %v\n", args[3])
}
}
func TestBindStruct(t *testing.T) {
q1 := `INSERT INTO foo (a, b, c, d) VALUES (:name, :age, :first, :last)`
type tt struct {
Name string
Age int
First string
Last string
}
am := tt{"Jason Moiron", 30, "Jason", "Moiron"}
bq, args, _ := BindStruct(QUESTION, q1, am)
expect := `INSERT INTO foo (a, b, c, d) VALUES (?, ?, ?, ?)`
if bq != expect {
t.Errorf("Interpolation of query failed: got `%v`, expected `%v`\n", bq, expect)
}
if args[0].(string) != "Jason Moiron" {
t.Errorf("Expected `Jason Moiron`, got %v\n", args[0])
}
if args[1].(int) != 30 {
t.Errorf("Expected 30, got %v\n", args[1])
}
if args[2].(string) != "Jason" {
t.Errorf("Expected Jason, got %v\n", args[2])
}
if args[3].(string) != "Moiron" {
t.Errorf("Expected Moiron, got %v\n", args[3])
}
}
func BenchmarkBindStruct(b *testing.B) {
b.StopTimer()
q1 := `INSERT INTO foo (a, b, c, d) VALUES (:name, :age, :first, :last)`
type t struct {
Name string
Age int
First string
Last string
}
am := t{"Jason Moiron", 30, "Jason", "Moiron"}
b.StartTimer()
for i := 0; i < b.N; i++ {
BindStruct(DOLLAR, q1, am)
//bindMap(QUESTION, q1, am)
}
}
func BenchmarkBindMap(b *testing.B) {
b.StopTimer()
q1 := `INSERT INTO foo (a, b, c, d) VALUES (:name, :age, :first, :last)`
am := map[string]interface{}{
"name": "Jason Moiron",
"age": 30,
"first": "Jason",
"last": "Moiron",
}
b.StartTimer()
for i := 0; i < b.N; i++ {
BindMap(DOLLAR, q1, am)
//bindMap(QUESTION, q1, am)
}
}
func BenchmarkRebind(b *testing.B) {
b.StopTimer()
q1 := `INSERT INTO foo (a, b, c, d, e, f, g, h, i) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
q2 := `INSERT INTO foo (a, b, c) VALUES (?, ?, "foo"), ("Hi", ?, ?)`
b.StartTimer()
for i := 0; i < b.N; i++ {
Rebind(DOLLAR, q1)
Rebind(DOLLAR, q2)
}
}
func BenchmarkRebindBuffer(b *testing.B) {
b.StopTimer()
q1 := `INSERT INTO foo (a, b, c, d, e, f, g, h, i) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
q2 := `INSERT INTO foo (a, b, c) VALUES (?, ?, "foo"), ("Hi", ?, ?)`
b.StartTimer()
for i := 0; i < b.N; i++ {
rebindBuff(DOLLAR, q1)
rebindBuff(DOLLAR, q2)
}
}