forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tag_test.go
396 lines (324 loc) · 8.86 KB
/
tag_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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// 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 (
"context"
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/lingochamp/core"
"github.com/stretchr/testify/assert"
)
type UserCU struct {
Id int64
Name string
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
func TestCreatedAndUpdated(t *testing.T) {
assert.NoError(t, prepareEngine())
u := new(UserCU)
err := testEngine.DropTables(context.Background(), u)
if err != nil {
t.Error(err)
panic(err)
}
err = testEngine.CreateTables(context.Background(), u)
if err != nil {
t.Error(err)
panic(err)
}
u.Name = "sss"
cnt, err := testEngine.Insert(context.Background(), u)
if err != nil {
t.Error(err)
panic(err)
}
if cnt != 1 {
err = errors.New("insert not returned 1")
t.Error(err)
panic(err)
return
}
u.Name = "xxx"
cnt, err = testEngine.ID(u.Id).Update(context.Background(), u)
if err != nil {
t.Error(err)
panic(err)
}
if cnt != 1 {
err = errors.New("update not returned 1")
t.Error(err)
panic(err)
return
}
u.Id = 0
u.Created = time.Now().Add(-time.Hour * 24 * 365)
u.Updated = u.Created
fmt.Println(u)
cnt, err = testEngine.NoAutoTime().Insert(context.Background(), u)
if err != nil {
t.Error(err)
panic(err)
}
if cnt != 1 {
err = errors.New("insert not returned 1")
t.Error(err)
panic(err)
return
}
}
type StrangeName struct {
Id_t int64 `xorm:"pk autoincr"`
Name string
}
func TestStrangeName(t *testing.T) {
assert.NoError(t, prepareEngine())
err := testEngine.DropTables(context.Background(), new(StrangeName))
if err != nil {
t.Error(err)
}
err = testEngine.CreateTables(context.Background(), new(StrangeName))
if err != nil {
t.Error(err)
}
_, err = testEngine.Insert(context.Background(), &StrangeName{Name: "sfsfdsfds"})
if err != nil {
t.Error(err)
}
beans := make([]StrangeName, 0)
err = testEngine.Find(context.Background(), &beans)
if err != nil {
t.Error(err)
}
}
func TestCreatedUpdated(t *testing.T) {
assert.NoError(t, prepareEngine())
type CreatedUpdated struct {
Id int64
Name string
Value float64 `xorm:"numeric"`
Created time.Time `xorm:"created"`
Created2 time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
err := testEngine.Sync2(context.Background(), &CreatedUpdated{})
assert.NoError(t, err)
c := &CreatedUpdated{Name: "test"}
_, err = testEngine.Insert(context.Background(), c)
assert.NoError(t, err)
c2 := new(CreatedUpdated)
has, err := testEngine.ID(c.Id).Get(context.Background(), c2)
assert.NoError(t, err)
assert.True(t, has)
c2.Value -= 1
_, err = testEngine.ID(c2.Id).Update(context.Background(), c2)
assert.NoError(t, err)
}
func TestCreatedUpdatedInt64(t *testing.T) {
assert.NoError(t, prepareEngine())
type CreatedUpdatedInt64 struct {
Id int64
Name string
Value float64 `xorm:"numeric"`
Created int64 `xorm:"created"`
Created2 int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
}
assertSync(t, &CreatedUpdatedInt64{})
c := &CreatedUpdatedInt64{Name: "test"}
_, err := testEngine.Insert(context.Background(), c)
assert.NoError(t, err)
c2 := new(CreatedUpdatedInt64)
has, err := testEngine.ID(c.Id).Get(context.Background(), c2)
assert.NoError(t, err)
assert.True(t, has)
c2.Value -= 1
_, err = testEngine.ID(c2.Id).Update(context.Background(), c2)
assert.NoError(t, err)
}
type Lowercase struct {
Id int64
Name string
ended int64 `xorm:"-"`
}
func TestLowerCase(t *testing.T) {
assert.NoError(t, prepareEngine())
err := testEngine.Sync2(context.Background(), &Lowercase{})
_, err = testEngine.Where("(id) > 0").Delete(context.Background(), &Lowercase{})
if err != nil {
t.Error(err)
panic(err)
}
_, err = testEngine.Insert(context.Background(), &Lowercase{ended: 1})
if err != nil {
t.Error(err)
panic(err)
}
ls := make([]Lowercase, 0)
err = testEngine.Find(context.Background(), &ls)
if err != nil {
t.Error(err)
panic(err)
}
if len(ls) != 1 {
err = errors.New("should be 1")
t.Error(err)
panic(err)
}
}
func TestAutoIncrTag(t *testing.T) {
assert.NoError(t, prepareEngine())
type TestAutoIncr1 struct {
Id int64
}
tb := testEngine.TableInfo(new(TestAutoIncr1))
cols := tb.Columns()
assert.EqualValues(t, 1, len(cols))
assert.True(t, cols[0].IsAutoIncrement)
assert.True(t, cols[0].IsPrimaryKey)
assert.Equal(t, "id", cols[0].Name)
type TestAutoIncr2 struct {
Id int64 `xorm:"id"`
}
tb = testEngine.TableInfo(new(TestAutoIncr2))
cols = tb.Columns()
assert.EqualValues(t, 1, len(cols))
assert.False(t, cols[0].IsAutoIncrement)
assert.False(t, cols[0].IsPrimaryKey)
assert.Equal(t, "id", cols[0].Name)
type TestAutoIncr3 struct {
Id int64 `xorm:"'ID'"`
}
tb = testEngine.TableInfo(new(TestAutoIncr3))
cols = tb.Columns()
assert.EqualValues(t, 1, len(cols))
assert.False(t, cols[0].IsAutoIncrement)
assert.False(t, cols[0].IsPrimaryKey)
assert.Equal(t, "ID", cols[0].Name)
type TestAutoIncr4 struct {
Id int64 `xorm:"pk"`
}
tb = testEngine.TableInfo(new(TestAutoIncr4))
cols = tb.Columns()
assert.EqualValues(t, 1, len(cols))
assert.False(t, cols[0].IsAutoIncrement)
assert.True(t, cols[0].IsPrimaryKey)
assert.Equal(t, "id", cols[0].Name)
}
func TestTagComment(t *testing.T) {
assert.NoError(t, prepareEngine())
// FIXME: only support mysql
if testEngine.Dialect().DriverName() != core.MYSQL {
return
}
type TestComment1 struct {
Id int64 `xorm:"comment(主键)"`
}
assert.NoError(t, testEngine.Sync2(context.Background(), new(TestComment1)))
tables, err := testEngine.DBMetas(context.Background())
assert.NoError(t, err)
assert.EqualValues(t, 1, len(tables))
assert.EqualValues(t, 1, len(tables[0].Columns()))
assert.EqualValues(t, "主键", tables[0].Columns()[0].Comment)
assert.NoError(t, testEngine.DropTables(context.Background(), new(TestComment1)))
type TestComment2 struct {
Id int64 `xorm:"comment('主键')"`
}
assert.NoError(t, testEngine.Sync2(context.Background(), new(TestComment2)))
tables, err = testEngine.DBMetas(context.Background())
assert.NoError(t, err)
assert.EqualValues(t, 1, len(tables))
assert.EqualValues(t, 1, len(tables[0].Columns()))
assert.EqualValues(t, "主键", tables[0].Columns()[0].Comment)
}
func TestTagDefault(t *testing.T) {
assert.NoError(t, prepareEngine())
type DefaultStruct struct {
Id int64
Name string
Age int `xorm:"default(10)"`
}
assertSync(t, new(DefaultStruct))
cnt, err := testEngine.Omit("age").Insert(context.Background(), &DefaultStruct{
Name: "test",
Age: 20,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var s DefaultStruct
has, err := testEngine.ID(1).Get(context.Background(), &s)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, 10, s.Age)
assert.EqualValues(t, "test", s.Name)
}
func TestTagsDirection(t *testing.T) {
assert.NoError(t, prepareEngine())
type OnlyFromDBStruct struct {
Id int64
Name string
Uuid string `xorm:"<- default '1'"`
}
assertSync(t, new(OnlyFromDBStruct))
cnt, err := testEngine.Insert(context.Background(), &OnlyFromDBStruct{
Name: "test",
Uuid: "2",
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var s OnlyFromDBStruct
has, err := testEngine.ID(1).Get(context.Background(), &s)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, "1", s.Uuid)
assert.EqualValues(t, "test", s.Name)
type OnlyToDBStruct struct {
Id int64
Name string
Uuid string `xorm:"->"`
}
assertSync(t, new(OnlyToDBStruct))
cnt, err = testEngine.Insert(context.Background(), &OnlyToDBStruct{
Name: "test",
Uuid: "2",
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var s2 OnlyToDBStruct
has, err = testEngine.ID(1).Get(context.Background(), &s2)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, "", s2.Uuid)
assert.EqualValues(t, "test", s2.Name)
}
func TestTagTime(t *testing.T) {
assert.NoError(t, prepareEngine())
type TagUTCStruct struct {
Id int64
Name string
Created time.Time `xorm:"created utc"`
}
assertSync(t, new(TagUTCStruct))
assert.EqualValues(t, time.Local.String(), testEngine.GetTZLocation().String())
s := TagUTCStruct{
Name: "utc",
}
cnt, err := testEngine.Insert(context.Background(), &s)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var u TagUTCStruct
has, err := testEngine.ID(1).Get(context.Background(), &u)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, s.Created.Format("2006-01-02 15:04:05"), u.Created.Format("2006-01-02 15:04:05"))
var tm string
has, err = testEngine.Table("tag_u_t_c_struct").Cols("created").Get(context.Background(), &tm)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, s.Created.UTC().Format("2006-01-02 15:04:05"),
strings.Replace(strings.Replace(tm, "T", " ", -1), "Z", "", -1))
}