-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathselect_test.go
62 lines (54 loc) · 1.69 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
package sqlbuilder
import "testing"
//
// Author: 陈永佳 [email protected], [email protected]
//
func TestSelectAll(t *testing.T) {
sb := NewContext()
sql := sb.Select("*").From("t_users").
ToSQL()
checkSQLMatches(sql, "SELECT * FROM `t_users`;", t)
}
func TestSelect(t *testing.T) {
sb := NewContext()
sql := sb.Select("id", "username").
From("t_users").
ToSQL()
checkSQLMatches(sql, "SELECT `id`, `username` FROM `t_users`;", t)
}
func TestSelectWhere(t *testing.T) {
sb := NewContext()
sql := sb.Select("id", "username").
From("t_users").
Where(sb.Eq("password").
Or().EqTo("password", "*")).
ToSQL()
checkSQLMatches(sql, "SELECT `id`, `username` FROM `t_users` WHERE `password` = ? OR `password` = '*';", t)
}
func TestSelectWhereOrder(t *testing.T) {
sb := NewContext()
sql := sb.Select("id", "username").
From("t_users").
Where(sb.Eq("password")).
OrderBy("id").ASC().
ToSQL()
checkSQLMatches(sql, "SELECT `id`, `username` FROM `t_users` WHERE `password` = ? ORDER BY `id` ASC;", t)
}
func TestSelectWhereLimit(t *testing.T) {
sb := NewContext()
sql := sb.Select("id", "username").
From("t_users").
Where(sb.Eq("password")).
Limit(10).Offset(200).
ToSQL()
checkSQLMatches(sql, "SELECT `id`, `username` FROM `t_users` WHERE `password` = ? LIMIT 10 OFFSET 200;", t)
}
func TestSelectWhereInnerSelect(t *testing.T) {
sb := NewContext()
sql := sb.Select("id", "username").
FromSelect(sb.Select("*").From("t_users_bak").Where(sb.NEq("name"))).
Where(sb.Eq("password")).
Limit(10).Offset(200).
ToSQL()
checkSQLMatches(sql, "SELECT `id`, `username` FROM (SELECT * FROM `t_users_bak` WHERE `name` <> ?) WHERE `password` = ? LIMIT 10 OFFSET 200;", t)
}