This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
subquery_test.go
150 lines (128 loc) · 6.82 KB
/
subquery_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
package reiner
import (
"testing"
"github.com/stretchr/testify/assert"
)
var subQuery *SubQuery
func TestSubQueryMain(t *testing.T) {
assert := assert.New(t)
var err error
builder, err = New()
assert.NoError(err)
}
func TestSubQueryGetx(t *testing.T) {
assert := assert.New(t)
subQuery = builder.SubQuery().Table("Users").Get()
assertEqual(assert, "SELECT * FROM Users", subQuery.builder.Query())
subQuery = builder.SubQuery().Table("Users").Get("Username", "Password")
assertEqual(assert, "SELECT Username, Password FROM Users", subQuery.builder.Query())
}
func TestSubQueryPaginate(t *testing.T) {
assert := assert.New(t)
subQuery = builder.SubQuery().Table("Users").Paginate(1)
assertEqual(assert, "SELECT SQL_CALC_FOUND_ROWS * FROM Users LIMIT 0, 20", subQuery.builder.Query())
}
func TestSubQueryWhere(t *testing.T) {
assert := assert.New(t)
subQuery = builder.SubQuery().Table("Users").Where("ID", 1).Where("Username", "admin").Get()
assertEqual(assert, "SELECT * FROM Users WHERE ID = ? AND Username = ?", subQuery.builder.Query())
}
func TestSubQueryOrWhere(t *testing.T) {
assert := assert.New(t)
subQuery = builder.SubQuery().Table("Users").Where("FirstName", "John").OrWhere("FirstName", "Peter").Get()
assertEqual(assert, "SELECT * FROM Users WHERE FirstName = ? OR FirstName = ?", subQuery.builder.Query())
subQuery = builder.SubQuery().Table("Users").Where("A = B").OrWhere("(A = C OR A = D)").Get()
assertEqual(assert, "SELECT * FROM Users WHERE A = B OR (A = C OR A = D)", subQuery.builder.Query())
}
func TestSubQueryWhereHaving(t *testing.T) {
assert := assert.New(t)
subQuery = builder.SubQuery().Table("Users").Where("ID", 1).Having("Username", "admin").Get()
assertEqual(assert, "SELECT * FROM Users WHERE ID = ? HAVING Username = ?", subQuery.builder.Query())
subQuery = builder.SubQuery().Table("Users").Where("ID", 1).Having("Username", "admin").OrHaving("Password", "test").Get()
assertEqual(assert, "SELECT * FROM Users WHERE ID = ? HAVING Username = ? OR Password = ?", subQuery.builder.Query())
}
func TestSubQueryLimit(t *testing.T) {
assert := assert.New(t)
subQuery = builder.SubQuery().Table("Users").Limit(10).Get()
assertEqual(assert, "SELECT * FROM Users LIMIT 10", subQuery.builder.Query())
}
func TestSubQueryOrderBy(t *testing.T) {
assert := assert.New(t)
subQuery = builder.SubQuery().Table("Users").OrderBy("ID", "ASC").OrderBy("Login", "DESC").OrderBy("RAND()").Get()
assertEqual(assert, "SELECT * FROM Users ORDER BY ID ASC, Login DESC, RAND()", subQuery.builder.Query())
}
func TestSubQueryOrderByField(t *testing.T) {
assert := assert.New(t)
subQuery = builder.SubQuery().Table("Users").OrderBy("UserGroup", "ASC", "SuperUser", "Admin", "Users").Get()
assertEqual(assert, "SELECT * FROM Users ORDER BY FIELD (UserGroup, ?, ?, ?) ASC", subQuery.builder.Query())
}
func TestSubQueryGroupBy(t *testing.T) {
assert := assert.New(t)
subQuery = builder.SubQuery().Table("Users").GroupBy("Name").Get()
assertEqual(assert, "SELECT * FROM Users GROUP BY Name", subQuery.builder.Query())
subQuery = builder.SubQuery().Table("Users").GroupBy("Name", "ID").Get()
assertEqual(assert, "SELECT * FROM Users GROUP BY Name, ID", subQuery.builder.Query())
}
func TestSubQueryJoinx(t *testing.T) {
assert := assert.New(t)
subQuery = builder.SubQuery().
Table("Products").
LeftJoin("Users", "Products.TenantID = Users.TenantID").
Where("Users.ID", 6).
Get("Users.Name", "Products.ProductName")
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM Products LEFT JOIN Users ON (Products.TenantID = Users.TenantID) WHERE Users.ID = ?", subQuery.builder.Query())
builder, _ = builder.
Table("Products").
LeftJoin("Users", "Products.TenantID = Users.TenantID").
Where("Users.ID", 6).
Get("Users.Name", "Products.ProductName")
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM Products LEFT JOIN Users ON (Products.TenantID = Users.TenantID) WHERE Users.ID = ?", builder.Query())
subQuery = builder.SubQuery().
Table("Products").
RightJoin("Users", "Products.TenantID = Users.TenantID").
Where("Users.ID", 6).
Get("Users.Name", "Products.ProductName")
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM Products RIGHT JOIN Users ON (Products.TenantID = Users.TenantID) WHERE Users.ID = ?", subQuery.builder.Query())
subQuery = builder.SubQuery().
Table("Products").
InnerJoin("Users", "Products.TenantID = Users.TenantID").
Where("Users.ID", 6).
Get("Users.Name", "Products.ProductName")
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM Products INNER JOIN Users ON (Products.TenantID = Users.TenantID) WHERE Users.ID = ?", subQuery.builder.Query())
subQuery = builder.SubQuery().
Table("Products").
NaturalJoin("Users", "Products.TenantID = Users.TenantID").
Where("Users.ID", 6).
Get("Users.Name", "Products.ProductName")
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM Products NATURAL JOIN Users ON (Products.TenantID = Users.TenantID) WHERE Users.ID = ?", subQuery.builder.Query())
subQuery = builder.SubQuery().
Table("Products").
LeftJoin("Users", "Products.TenantID = Users.TenantID").
RightJoin("Posts", "Products.TenantID = Posts.TenantID").
Where("Users.ID", 6).
Get("Users.Name", "Products.ProductName")
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM Products RIGHT JOIN Posts ON (Products.TenantID = Posts.TenantID) LEFT JOIN Users ON (Products.TenantID = Users.TenantID) WHERE Users.ID = ?", subQuery.builder.Query())
}
func TestSubQueryJoinWhere(t *testing.T) {
assert := assert.New(t)
subQuery = builder.SubQuery().
Table("Products").
LeftJoin("Users", "Products.TenantID = Users.TenantID").
JoinOrWhere("Users", "Users.TenantID", 5).
Get("Users.Name", "Products.ProductName")
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM Products LEFT JOIN Users ON (Products.TenantID = Users.TenantID OR Users.TenantID = ?)", subQuery.builder.Query())
subQuery = builder.SubQuery().
Table("Products").
LeftJoin("Users", "Products.TenantID = Users.TenantID").
JoinWhere("Users", "Users.Username", "Wow").
Get("Users.Name", "Products.ProductName")
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM Products LEFT JOIN Users ON (Products.TenantID = Users.TenantID AND Users.Username = ?)", subQuery.builder.Query())
subQuery = builder.SubQuery().
Table("Products").
LeftJoin("Users", "Products.TenantID = Users.TenantID").
RightJoin("Posts", "Products.TenantID = Posts.TenantID").
JoinWhere("Posts", "Posts.Username", "Wow").
JoinWhere("Users", "Users.Username", "Wow").
Get("Users.Name", "Products.ProductName")
assertEqual(assert, "SELECT Users.Name, Products.ProductName FROM Products LEFT JOIN Users ON (Products.TenantID = Users.TenantID AND Users.Username = ?) RIGHT JOIN Posts ON (Products.TenantID = Posts.TenantID AND Posts.Username = ?)", subQuery.builder.Query())
}