forked from guregu/mogi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_test.go
200 lines (167 loc) · 4.63 KB
/
select_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
package mogi_test
import (
"database/sql"
"reflect"
"testing"
"github.com/guregu/mogi"
)
const (
beerCSV = `1,Yona Yona Ale,Yo-Ho Brewing,5.5
2,Punk IPA,BrewDog,5.6`
)
var (
beers = map[int]beer{
1: beer{
id: 1,
name: "Yona Yona Ale",
brewery: "Yo-Ho Brewing",
pct: 5.5,
},
2: beer{
id: 2,
name: "Punk IPA",
brewery: "BrewDog",
pct: 5.6,
},
}
)
type beer struct {
id int64
name string
brewery string
pct float64
}
func TestSelectTable(t *testing.T) {
defer mogi.Reset()
db := openDB()
// filter by table
mogi.Select("id", "name", "brewery", "pct").From("beer").StubCSV(beerCSV)
runBeerSelectQuery(t, db)
// select the wrong table
mogi.Reset()
mogi.Select("id", "name", "brewery", "pct").From("酒").StubCSV(beerCSV)
runUnstubbedSelect(t, db)
}
func TestSelectWhere(t *testing.T) {
defer mogi.Reset()
db := openDB()
// where
mogi.Select().From("beer").Where("pct", 5).StubCSV(beerCSV)
runBeerSelectQuery(t, db)
// where with weird type
type 数字 int
五 := 数字(5)
mogi.Reset()
mogi.Select().From("beer").Where("pct", &五).StubCSV(beerCSV)
runBeerSelectQuery(t, db)
// wrong where
mogi.Reset()
mogi.Select().From("beer").Where("pct", 98).StubCSV(beerCSV)
runUnstubbedSelect(t, db)
}
func TestSelectArgs(t *testing.T) {
defer mogi.Reset()
db := openDB()
// where
mogi.Select().Args(5).StubCSV(beerCSV)
runBeerSelectQuery(t, db)
// wrong args
mogi.Reset()
mogi.Select().Args("サービス残業").StubCSV(beerCSV)
runUnstubbedSelect(t, db)
}
func TestStubError(t *testing.T) {
defer mogi.Reset()
db := openDB()
mogi.Select().StubError(sql.ErrNoRows)
_, err := db.Query("SELECT id, name, brewery, pct FROM beer WHERE pct > ?", 5)
if err != sql.ErrNoRows {
t.Error("after StubError, err should be ErrNoRows but is", err)
}
}
func TestSelectMultipleTables(t *testing.T) {
defer mogi.Reset()
db := openDB()
mogi.Select().From("a", "b").StubCSV(`foo,bar`)
_, err := db.Query("SELECT a.thing, b.thing FROM a, b WHERE a.id = b.id")
checkNil(t, err)
_, err = db.Query("SELECT a.thing, b.thing FROM a JOIN b ON a.id = b.id")
checkNil(t, err)
mogi.Reset()
mogi.Select().From("a", "b", "c").StubCSV(`foo,bar,baz`)
_, err = db.Query("SELECT a.thing, b.thing, c.thing FROM a, b, c WHERE a.id = b.id")
checkNil(t, err)
_, err = db.Query("SELECT a.thing, b.thing, c.thing FROM a JOIN b ON a.id = b.id JOIN c ON a.id = c.id")
checkNil(t, err)
}
func TestSelectColumnNames(t *testing.T) {
defer mogi.Reset()
db := openDB()
// qualified names
mogi.Select("a.thing", "b.thing", "c.thing").From("qqqq", "b", "c").StubCSV(`foo,bar,baz`)
_, err := db.Query("SELECT a.thing, b.thing, c.thing FROM qqqq as a, b, c WHERE a.id = b.id")
checkNil(t, err)
// aliased names
mogi.Reset()
mogi.Select("dog", "cat", "hamster").From("a", "b", "c").StubCSV(`foo,bar,baz`)
_, err = db.Query("SELECT a.thing AS dog, b.thing AS cat, c.thing AS hamster FROM a JOIN b ON a.id = b.id JOIN c ON a.id = c.id")
checkNil(t, err)
}
func TestSelectCount(t *testing.T) {
defer mogi.Reset()
db := openDB()
mogi.Select("COUNT(abc)", "count(*)").StubCSV("1,5")
_, err := db.Query("SELECT COUNT(abc), COUNT(*) FROM beer")
checkNil(t, err)
}
func TestSelectWhereIn(t *testing.T) {
defer mogi.Reset()
db := openDB()
mogi.Select().Where("pct", 5.4, 10.2).StubCSV("2")
_, err := db.Query("SELECT COUNT(*) FROM beer WHERE pct IN (5.4, ?)", 10.2)
checkNil(t, err)
mogi.Reset()
mogi.Select().WhereOp("pct", "IN", 5.4, 10.2).StubCSV("2")
_, err = db.Query("SELECT COUNT(*) FROM beer WHERE pct IN (5.4, ?)", 10.2)
checkNil(t, err)
}
func TestSelectStar(t *testing.T) {
defer mogi.Reset()
db := openDB()
mogi.Select("*").StubCSV("a,b,c")
_, err := db.Query("SELECT * FROM beer")
checkNil(t, err)
}
func runUnstubbedSelect(t *testing.T, db *sql.DB) {
_, err := db.Query("SELECT id, name, brewery, pct FROM beer WHERE pct > ?", 5)
if err != mogi.ErrUnstubbed {
t.Error("with unmatched query, err should be ErrUnstubbed but is", err)
}
}
func runBeerSelectQuery(t *testing.T, db *sql.DB) {
expectCols := []string{"id", "name", "brewery", "pct"}
rows, err := db.Query("SELECT id, name, brewery, pct FROM beer WHERE pct > ?", 5)
checkNil(t, err)
cols, err := rows.Columns()
checkNil(t, err)
if !reflect.DeepEqual(cols, expectCols) {
t.Error("bad columns", cols, "≠", expectCols)
}
i := 0
for rows.Next() {
var b beer
rows.Scan(&b.id, &b.name, &b.brewery, &b.pct)
checkBeer(t, b, i+1)
i++
}
}
func checkBeer(t *testing.T, b beer, id int) {
cmp, ok := beers[id]
if !ok {
t.Error("unknown beer", id)
return
}
if b != cmp {
t.Error("beers don't match", b, "≠", cmp, id)
}
}