forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_cond_test.go
231 lines (190 loc) · 5.94 KB
/
session_cond_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
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xorm
import (
"errors"
"fmt"
"testing"
"github.com/go-xorm/builder"
"github.com/stretchr/testify/assert"
)
func TestBuilder(t *testing.T) {
assert.NoError(t, prepareEngine())
const (
OpEqual int = iota
OpGreatThan
OpLessThan
)
type Condition struct {
Id int64
TableName string
ColName string
Op int
Value string
}
err := testEngine.CreateTables(&Condition{})
assert.NoError(t, err)
_, err = testEngine.Insert(&Condition{TableName: "table1", ColName: "col1", Op: OpEqual, Value: "1"})
assert.NoError(t, err)
var cond Condition
has, err := testEngine.Where(builder.Eq{"col_name": "col1"}).Get(&cond)
assert.NoError(t, err)
assert.Equal(t, true, has, "records should exist")
has, err = testEngine.Where(builder.Eq{"col_name": "col1"}.
And(builder.Eq{"op": OpEqual})).
NoAutoCondition().
Get(&cond)
assert.NoError(t, err)
assert.Equal(t, true, has, "records should exist")
has, err = testEngine.Where(builder.Eq{"col_name": "col1", "op": OpEqual, "value": "1"}).
NoAutoCondition().
Get(&cond)
assert.NoError(t, err)
assert.Equal(t, true, has, "records should exist")
has, err = testEngine.Where(builder.Eq{"col_name": "col1"}.
And(builder.Neq{"op": OpEqual})).
NoAutoCondition().
Get(&cond)
assert.NoError(t, err)
assert.Equal(t, false, has, "records should not exist")
var conds []Condition
err = testEngine.Where(builder.Eq{"col_name": "col1"}.
And(builder.Eq{"op": OpEqual})).
Find(&conds)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist")
conds = make([]Condition, 0)
err = testEngine.Where(builder.Like{"col_name", "col"}).Find(&conds)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist")
conds = make([]Condition, 0)
err = testEngine.Where(builder.Expr("col_name = ?", "col1")).Find(&conds)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist")
conds = make([]Condition, 0)
err = testEngine.Where(builder.In("col_name", "col1", "col2")).Find(&conds)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist")
conds = make([]Condition, 0)
err = testEngine.NotIn("col_name", "col1", "col2").Find(&conds)
assert.NoError(t, err)
assert.EqualValues(t, 0, len(conds), "records should not exist")
// complex condtions
var where = builder.NewCond()
if true {
where = where.And(builder.Eq{"col_name": "col1"})
where = where.Or(builder.And(builder.In("col_name", "col1", "col2"), builder.Expr("col_name = ?", "col1")))
}
conds = make([]Condition, 0)
err = testEngine.Where(where).Find(&conds)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(conds), "records should exist")
}
func TestIn(t *testing.T) {
assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.Sync2(new(Userinfo)))
cnt, err := testEngine.Insert([]Userinfo{
{
Username: "user1",
Departname: "dev",
},
{
Username: "user2",
Departname: "dev",
},
{
Username: "user3",
Departname: "dev",
},
})
assert.NoError(t, err)
assert.EqualValues(t, 3, cnt)
department := "`" + testEngine.GetColumnMapper().Obj2Table("Departname") + "`"
var usrs []Userinfo
err = testEngine.Where(department+" = ?", "dev").Limit(3).Find(&usrs)
assert.NoError(t, err)
assert.EqualValues(t, 3, len(usrs))
var ids []int64
var idsStr string
for _, u := range usrs {
ids = append(ids, u.Uid)
idsStr = fmt.Sprintf("%d,", u.Uid)
}
idsStr = idsStr[:len(idsStr)-1]
users := make([]Userinfo, 0)
err = testEngine.In("(id)", ids[0], ids[1], ids[2]).Find(&users)
assert.NoError(t, err)
fmt.Println(users)
assert.EqualValues(t, 3, len(users))
users = make([]Userinfo, 0)
err = testEngine.In("(id)", ids).Find(&users)
assert.NoError(t, err)
fmt.Println(users)
assert.EqualValues(t, 3, len(users))
for _, user := range users {
if user.Uid != ids[0] && user.Uid != ids[1] && user.Uid != ids[2] {
err = errors.New("in uses should be " + idsStr + " total 3")
assert.NoError(t, err)
}
}
users = make([]Userinfo, 0)
var idsInterface []interface{}
for _, id := range ids {
idsInterface = append(idsInterface, id)
}
err = testEngine.Where(department+" = ?", "dev").In("(id)", idsInterface...).Find(&users)
assert.NoError(t, err)
fmt.Println(users)
assert.EqualValues(t, 3, len(users))
for _, user := range users {
if user.Uid != ids[0] && user.Uid != ids[1] && user.Uid != ids[2] {
err = errors.New("in uses should be " + idsStr + " total 3")
assert.NoError(t, err)
}
}
dev := testEngine.GetColumnMapper().Obj2Table("Dev")
err = testEngine.In("(id)", 1).In("(id)", 2).In(department, dev).Find(&users)
assert.NoError(t, err)
fmt.Println(users)
cnt, err = testEngine.In("(id)", ids[0]).Update(&Userinfo{Departname: "dev-"})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
user := new(Userinfo)
has, err := testEngine.ID(ids[0]).Get(user)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, "dev-", user.Departname)
cnt, err = testEngine.In("(id)", ids[0]).Update(&Userinfo{Departname: "dev"})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
cnt, err = testEngine.In("(id)", ids[1]).Delete(&Userinfo{})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
}
func TestFindAndCount(t *testing.T) {
assert.NoError(t, prepareEngine())
type FindAndCount struct {
Id int64
Name string
}
assert.NoError(t, testEngine.Sync2(new(FindAndCount)))
_, err := testEngine.Insert([]FindAndCount{
{
Name: "test1",
},
{
Name: "test2",
},
})
assert.NoError(t, err)
var results []FindAndCount
sess := testEngine.Where("name = ?", "test1")
conds := sess.Conds()
err = sess.Find(&results)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(results))
total, err := testEngine.Where(conds).Count(new(FindAndCount))
assert.NoError(t, err)
assert.EqualValues(t, 1, total)
}