forked from Masterminds/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_test.go
241 lines (186 loc) · 6.51 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
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
package squirrel
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSelectBuilderToSql(t *testing.T) {
subQ := Select("aa", "bb").From("dd")
b := Select("a", "b").
Prefix("WITH prefix AS ?", 0).
Distinct().
Columns("c").
Column("IF(d IN ("+Placeholders(3)+"), 1, 0) as stat_column", 1, 2, 3).
Column(Expr("a > ?", 100)).
Column(Alias(Eq{"b": []int{101, 102, 103}}, "b_alias")).
Column(Alias(subQ, "subq")).
From("e").
JoinClause("CROSS JOIN j1").
Join("j2").
LeftJoin("j3").
RightJoin("j4").
Where("f = ?", 4).
Where(Eq{"g": 5}).
Where(map[string]interface{}{"h": 6}).
Where(Eq{"i": []int{7, 8, 9}}).
Where(Or{Expr("j = ?", 10), And{Eq{"k": 11}, Expr("true")}}).
GroupBy("l").
Having("m = n").
OrderByClause("? DESC", 1).
OrderBy("o ASC", "p DESC").
Limit(12).
Offset(13).
Suffix("FETCH FIRST ? ROWS ONLY", 14)
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql :=
"WITH prefix AS ? " +
"SELECT DISTINCT a, b, c, IF(d IN (?,?,?), 1, 0) as stat_column, a > ?, " +
"(b IN (?,?,?)) AS b_alias, " +
"(SELECT aa, bb FROM dd) AS subq " +
"FROM e " +
"CROSS JOIN j1 JOIN j2 LEFT JOIN j3 RIGHT JOIN j4 " +
"WHERE f = ? AND g = ? AND h = ? AND i IN (?,?,?) AND (j = ? OR (k = ? AND true)) " +
"GROUP BY l HAVING m = n ORDER BY ? DESC, o ASC, p DESC LIMIT 12 OFFSET 13 " +
"FETCH FIRST ? ROWS ONLY"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{0, 1, 2, 3, 100, 101, 102, 103, 4, 5, 6, 7, 8, 9, 10, 11, 1, 14}
assert.Equal(t, expectedArgs, args)
}
func TestSelectBuilderFromSelect(t *testing.T) {
subQ := Select("c").From("d").Where(Eq{"i": 0})
b := Select("a", "b").FromSelect(subQ, "subq")
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql := "SELECT a, b FROM (SELECT c FROM d WHERE i = ?) AS subq"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{0}
assert.Equal(t, expectedArgs, args)
}
func TestSelectBuilderFromSelectNestedDollarPlaceholders(t *testing.T) {
subQ := Select("c").
From("t").
Where(Gt{"c": 1}).
PlaceholderFormat(Dollar)
b := Select("c").
FromSelect(subQ, "subq").
Where(Lt{"c": 2}).
PlaceholderFormat(Dollar)
sql, args, err := b.ToSql()
assert.NoError(t, err)
expectedSql := "SELECT c FROM (SELECT c FROM t WHERE c > $1) AS subq WHERE c < $2"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{1, 2}
assert.Equal(t, expectedArgs, args)
}
func TestSelectBuilderToSqlErr(t *testing.T) {
_, _, err := Select().From("x").ToSql()
assert.Error(t, err)
}
func TestSelectBuilderPlaceholders(t *testing.T) {
b := Select("test").Where("x = ? AND y = ?")
sql, _, _ := b.PlaceholderFormat(Question).ToSql()
assert.Equal(t, "SELECT test WHERE x = ? AND y = ?", sql)
sql, _, _ = b.PlaceholderFormat(Dollar).ToSql()
assert.Equal(t, "SELECT test WHERE x = $1 AND y = $2", sql)
sql, _, _ = b.PlaceholderFormat(Colon).ToSql()
assert.Equal(t, "SELECT test WHERE x = :1 AND y = :2", sql)
}
func TestSelectBuilderRunners(t *testing.T) {
db := &DBStub{}
b := Select("test").RunWith(db)
expectedSql := "SELECT test"
b.Exec()
assert.Equal(t, expectedSql, db.LastExecSql)
b.Query()
assert.Equal(t, expectedSql, db.LastQuerySql)
b.QueryRow()
assert.Equal(t, expectedSql, db.LastQueryRowSql)
err := b.Scan()
assert.NoError(t, err)
}
func TestSelectBuilderNoRunner(t *testing.T) {
b := Select("test")
_, err := b.Exec()
assert.Equal(t, RunnerNotSet, err)
_, err = b.Query()
assert.Equal(t, RunnerNotSet, err)
err = b.Scan()
assert.Equal(t, RunnerNotSet, err)
}
func TestSelectBuilderSimpleJoin(t *testing.T) {
expectedSql := "SELECT * FROM bar JOIN baz ON bar.foo = baz.foo"
expectedArgs := []interface{}(nil)
b := Select("*").From("bar").Join("baz ON bar.foo = baz.foo")
sql, args, err := b.ToSql()
assert.NoError(t, err)
assert.Equal(t, expectedSql, sql)
assert.Equal(t, args, expectedArgs)
}
func TestSelectBuilderParamJoin(t *testing.T) {
expectedSql := "SELECT * FROM bar JOIN baz ON bar.foo = baz.foo AND baz.foo = ?"
expectedArgs := []interface{}{42}
b := Select("*").From("bar").Join("baz ON bar.foo = baz.foo AND baz.foo = ?", 42)
sql, args, err := b.ToSql()
assert.NoError(t, err)
assert.Equal(t, expectedSql, sql)
assert.Equal(t, args, expectedArgs)
}
func TestSelectBuilderNestedSelectJoin(t *testing.T) {
expectedSql := "SELECT * FROM bar JOIN ( SELECT * FROM baz WHERE foo = ? ) r ON bar.foo = r.foo"
expectedArgs := []interface{}{42}
nestedSelect := Select("*").From("baz").Where("foo = ?", 42)
b := Select("*").From("bar").JoinClause(nestedSelect.Prefix("JOIN (").Suffix(") r ON bar.foo = r.foo"))
sql, args, err := b.ToSql()
assert.NoError(t, err)
assert.Equal(t, expectedSql, sql)
assert.Equal(t, args, expectedArgs)
}
func TestSelectWithOptions(t *testing.T) {
sql, _, err := Select("*").From("foo").Distinct().Options("SQL_NO_CACHE").ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT DISTINCT SQL_NO_CACHE * FROM foo", sql)
}
func TestSelectWithRemoveLimit(t *testing.T) {
sql, _, err := Select("*").From("foo").Limit(10).RemoveLimit().ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT * FROM foo", sql)
}
func TestSelectWithRemoveOffset(t *testing.T) {
sql, _, err := Select("*").From("foo").Offset(10).RemoveOffset().ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT * FROM foo", sql)
}
func TestSelectBuilderNestedSelectDollar(t *testing.T) {
nestedBuilder := StatementBuilder.PlaceholderFormat(Dollar).Select("*").Prefix("NOT EXISTS (").
From("bar").Where("y = ?", 42).Suffix(")")
outerSql, _, err := StatementBuilder.PlaceholderFormat(Dollar).Select("*").
From("foo").Where("x = ?").Where(nestedBuilder).ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT * FROM foo WHERE x = $1 AND NOT EXISTS ( SELECT * FROM bar WHERE y = $2 )", outerSql)
}
func TestMustSql(t *testing.T) {
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("TestUserFail should have panicked!")
}
}()
// This function should cause a panic
Select().From("foo").MustSql()
}()
}
func TestSelectWithoutWhereClause(t *testing.T) {
sql, _, err := Select("*").From("users").ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT * FROM users", sql)
}
func TestSelectWithNilWhereClause(t *testing.T) {
sql, _, err := Select("*").From("users").Where(nil).ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT * FROM users", sql)
}
func TestSelectWithEmptyStringWhereClause(t *testing.T) {
sql, _, err := Select("*").From("users").Where("").ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT * FROM users", sql)
}