forked from Masterminds/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_test.go
100 lines (80 loc) · 2.5 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
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").
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 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, 14}
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)
}
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)
}