forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_get_test.go
321 lines (262 loc) · 8.17 KB
/
session_get_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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// 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 (
"database/sql"
"fmt"
"testing"
"time"
"github.com/go-xorm/core"
"github.com/stretchr/testify/assert"
)
func TestGetVar(t *testing.T) {
assert.NoError(t, prepareEngine())
type GetVar struct {
Id int64 `xorm:"autoincr pk"`
Msg string `xorm:"varchar(255)"`
Age int
Money float32
Created time.Time `xorm:"created"`
}
assert.NoError(t, testEngine.Sync2(new(GetVar)))
var data = GetVar{
Msg: "hi",
Age: 28,
Money: 1.5,
}
_, err := testEngine.InsertOne(&data)
assert.NoError(t, err)
var msg string
has, err := testEngine.Table("get_var").Cols("msg").Get(&msg)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, "hi", msg)
var age int
has, err = testEngine.Table("get_var").Cols("age").Get(&age)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, 28, age)
var age2 int64
has, err = testEngine.Table("get_var").Cols("age").
Where("age > ?", 20).
And("age < ?", 30).
Get(&age2)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.EqualValues(t, 28, age2)
var id sql.NullInt64
has, err = testEngine.Table("get_var").Cols("id").Get(&id)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, true, id.Valid)
assert.EqualValues(t, data.Id, id.Int64)
var msgNull sql.NullString
has, err = testEngine.Table("get_var").Cols("msg").Get(&msgNull)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, true, msgNull.Valid)
assert.EqualValues(t, data.Msg, msgNull.String)
var nullMoney sql.NullFloat64
has, err = testEngine.Table("get_var").Cols("money").Get(&nullMoney)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, true, nullMoney.Valid)
assert.EqualValues(t, data.Money, nullMoney.Float64)
var money float64
has, err = testEngine.Table("get_var").Cols("money").Get(&money)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, "1.5", fmt.Sprintf("%.1f", money))
var money2 float64
has, err = testEngine.SQL("SELECT money FROM " + testEngine.TableName("get_var", true) + " LIMIT 1").Get(&money2)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, "1.5", fmt.Sprintf("%.1f", money2))
var money3 float64
has, err = testEngine.SQL("SELECT money FROM " + testEngine.TableName("get_var", true) + " WHERE money > 20").Get(&money3)
assert.NoError(t, err)
assert.Equal(t, false, has)
var valuesString = make(map[string]string)
has, err = testEngine.Table("get_var").Get(&valuesString)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, 5, len(valuesString))
assert.Equal(t, "1", valuesString["id"])
assert.Equal(t, "hi", valuesString["msg"])
assert.Equal(t, "28", valuesString["age"])
assert.Equal(t, "1.5", valuesString["money"])
// for mymysql driver, interface{} will be []byte, so ignore it currently
if testEngine.Dialect().DriverName() != "mymysql" {
var valuesInter = make(map[string]interface{})
has, err = testEngine.Table("get_var").Where("id = ?", 1).Select("*").Get(&valuesInter)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, 5, len(valuesInter))
assert.EqualValues(t, 1, valuesInter["id"])
assert.Equal(t, "hi", fmt.Sprintf("%s", valuesInter["msg"]))
assert.EqualValues(t, 28, valuesInter["age"])
assert.Equal(t, "1.5", fmt.Sprintf("%v", valuesInter["money"]))
}
var valuesSliceString = make([]string, 5)
has, err = testEngine.Table("get_var").Get(&valuesSliceString)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, "1", valuesSliceString[0])
assert.Equal(t, "hi", valuesSliceString[1])
assert.Equal(t, "28", valuesSliceString[2])
assert.Equal(t, "1.5", valuesSliceString[3])
var valuesSliceInter = make([]interface{}, 5)
has, err = testEngine.Table("get_var").Get(&valuesSliceInter)
assert.NoError(t, err)
assert.Equal(t, true, has)
v1, err := convertInt(valuesSliceInter[0])
assert.NoError(t, err)
assert.EqualValues(t, 1, v1)
assert.Equal(t, "hi", fmt.Sprintf("%s", valuesSliceInter[1]))
v3, err := convertInt(valuesSliceInter[2])
assert.NoError(t, err)
assert.EqualValues(t, 28, v3)
v4, err := convertFloat(valuesSliceInter[3])
assert.NoError(t, err)
assert.Equal(t, "1.5", fmt.Sprintf("%v", v4))
}
func TestGetStruct(t *testing.T) {
assert.NoError(t, prepareEngine())
type UserinfoGet struct {
Uid int `xorm:"pk autoincr"`
IsMan bool
}
assert.NoError(t, testEngine.Sync2(new(UserinfoGet)))
var err error
if testEngine.Dialect().DBType() == core.MSSQL {
_, err = testEngine.Exec("SET IDENTITY_INSERT userinfo_get ON")
assert.NoError(t, err)
}
cnt, err := testEngine.Insert(&UserinfoGet{Uid: 2})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
user := UserinfoGet{Uid: 2}
has, err := testEngine.Get(&user)
assert.NoError(t, err)
assert.True(t, has)
type NoIdUser struct {
User string `xorm:"unique"`
Remain int64
Total int64
}
assert.NoError(t, testEngine.Sync2(&NoIdUser{}))
userCol := testEngine.GetColumnMapper().Obj2Table("User")
_, err = testEngine.Where("`"+userCol+"` = ?", "xlw").Delete(&NoIdUser{})
assert.NoError(t, err)
cnt, err = testEngine.Insert(&NoIdUser{"xlw", 20, 100})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
noIdUser := new(NoIdUser)
has, err = testEngine.Where("`"+userCol+"` = ?", "xlw").Get(noIdUser)
assert.NoError(t, err)
assert.True(t, has)
}
func TestGetSlice(t *testing.T) {
assert.NoError(t, prepareEngine())
type UserinfoSlice struct {
Uid int `xorm:"pk autoincr"`
IsMan bool
}
assertSync(t, new(UserinfoSlice))
var users []UserinfoSlice
has, err := testEngine.Get(&users)
assert.False(t, has)
assert.Error(t, err)
}
func TestGetError(t *testing.T) {
assert.NoError(t, prepareEngine())
type GetError struct {
Uid int `xorm:"pk autoincr"`
IsMan bool
}
assertSync(t, new(GetError))
var info = new(GetError)
has, err := testEngine.Get(&info)
assert.False(t, has)
assert.Error(t, err)
has, err = testEngine.Get(info)
assert.False(t, has)
assert.NoError(t, err)
}
func TestJSONString(t *testing.T) {
assert.NoError(t, prepareEngine())
type JsonString struct {
Id int64
Content string `xorm:"json"`
}
type JsonJson struct {
Id int64
Content []string `xorm:"json"`
}
assertSync(t, new(JsonJson))
_, err := testEngine.Insert(&JsonJson{
Content: []string{"1", "2"},
})
assert.NoError(t, err)
var js JsonString
has, err := testEngine.Table("json_json").Get(&js)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, 1, js.Id)
assert.EqualValues(t, `["1","2"]`, js.Content)
var jss []JsonString
err = testEngine.Table("json_json").Find(&jss)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(jss))
assert.EqualValues(t, `["1","2"]`, jss[0].Content)
}
func TestGetActionMapping(t *testing.T) {
assert.NoError(t, prepareEngine())
type ActionMapping struct {
ActionId string `xorm:"pk"`
ActionName string `xorm:"index"`
ScriptId string `xorm:"unique"`
RollbackId string `xorm:"unique"`
Env string
Tags string
Description string
UpdateTime time.Time `xorm:"updated"`
DeleteTime time.Time `xorm:"deleted"`
}
assertSync(t, new(ActionMapping))
_, err := testEngine.Insert(&ActionMapping{
ActionId: "1",
ScriptId: "2",
})
assert.NoError(t, err)
var valuesSlice = make([]string, 2)
has, err := testEngine.Table(new(ActionMapping)).
Cols("script_id", "rollback_id").
ID("1").Get(&valuesSlice)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, "2", valuesSlice[0])
assert.EqualValues(t, "", valuesSlice[1])
}
func TestGetStructId(t *testing.T) {
type TestGetStruct struct {
Id int64
}
assert.NoError(t, prepareEngine())
assertSync(t, new(TestGetStruct))
_, err := testEngine.Insert(&TestGetStruct{})
assert.NoError(t, err)
_, err = testEngine.Insert(&TestGetStruct{})
assert.NoError(t, err)
type maxidst struct {
Id int64
}
//var id int64
var maxid maxidst
sql := "select max(id) as id from " + testEngine.TableName(&TestGetStruct{}, true)
has, err := testEngine.SQL(sql).Get(&maxid)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, 2, maxid.Id)
}