forked from VividCortex/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlx_test.go
196 lines (167 loc) · 4.66 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
package sqlx
import (
"database/sql"
"fmt"
_ "github.com/bmizerany/pq"
_ "github.com/mattn/go-sqlite3"
"os/user"
"strings"
"testing"
)
var TestPostgres = true
var TestSqlite = true
var sldb *DB
var pgdb *DB
var active = []*DB{}
func init() {
PostgresConnect()
SqliteConnect()
}
func PostgresConnect() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Could not connect to postgres db, try `createdb sqlxtest`, disabling PG tests:\n %v", r)
TestPostgres = false
}
}()
var username string
u, err := user.Current()
if err != nil {
fmt.Printf("Could not find current user username, trying 'test' instead.")
username = "test"
} else {
username = u.Username
}
pgdb = MustConnect("postgres", "user="+username+" dbname=sqlxtest sslmode=disable")
}
func SqliteConnect() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Could not create sqlite3 db in /tmp/sqlxtest.db:\n %v", r)
TestSqlite = false
}
}()
sldb = MustConnect("sqlite3", "/tmp/sqlxtest.db")
}
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, ";")
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) {
defer func(dbtype string) {
if dbtype == "sqlite" {
MultiExec(db, drop)
} else {
db.Execf(drop)
}
}(dbtype)
// pq will execute multi-query statements, but sqlite3 won't!
if dbtype == "sqlite" {
MultiExec(db, schema)
} else {
db.Execf(schema)
}
tx := db.MustBegin()
tx.Execl("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Jason", "Moiron", "[email protected]")
tx.Execl("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "John", "Doe", "[email protected]")
tx.Execl("INSERT INTO place (country, city, telcode) VALUES ($1, $2, $3)", "United States", "New York", "1")
tx.Execl("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Hong Kong", "852")
tx.Execl("INSERT INTO place (country, telcode) VALUES ($1, $2)", "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")
}
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)
}
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
places = []*Place{}
err = db.Select(&places, "SELECT * 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)
}
stmt, err := db.Preparex("SELECT country, telcode FROM place WHERE telcode > $1 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)
}
}
}
if TestPostgres {
RunTest(pgdb, t, "postgres")
}
if TestSqlite {
RunTest(sldb, t, "sqlite")
}
}