forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_schema_test.go
332 lines (252 loc) · 8.13 KB
/
session_schema_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
// 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 (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestStoreEngine(t *testing.T) {
assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.DropTables("user_store_engine"))
type UserinfoStoreEngine struct {
Id int64
Name string
}
assert.NoError(t, testEngine.StoreEngine("InnoDB").Table("user_store_engine").CreateTable(&UserinfoStoreEngine{}))
}
func TestCreateTable(t *testing.T) {
assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.DropTables("user_user"))
type UserinfoCreateTable struct {
Id int64
Name string
}
assert.NoError(t, testEngine.Table("user_user").CreateTable(&UserinfoCreateTable{}))
}
func TestCreateMultiTables(t *testing.T) {
assert.NoError(t, prepareEngine())
session := testEngine.NewSession()
defer session.Close()
type UserinfoMultiTable struct {
Id int64
Name string
}
user := &UserinfoMultiTable{}
assert.NoError(t, session.Begin())
for i := 0; i < 10; i++ {
tableName := fmt.Sprintf("user_%v", i)
assert.NoError(t, session.DropTable(tableName))
assert.NoError(t, session.Table(tableName).CreateTable(user))
}
assert.NoError(t, session.Commit())
}
type SyncTable1 struct {
Id int64
Name string
Dev int `xorm:"index"`
}
type SyncTable2 struct {
Id int64
Name string `xorm:"unique"`
Number string `xorm:"index"`
Dev int
Age int
}
func (SyncTable2) TableName() string {
return "sync_table1"
}
type SyncTable3 struct {
Id int64
Name string `xorm:"unique"`
Number string `xorm:"index"`
Dev int
Age int
}
func (s *SyncTable3) TableName() string {
return "sync_table1"
}
func TestSyncTable(t *testing.T) {
assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.Sync2(new(SyncTable1)))
tables, err := testEngine.DBMetas()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(tables))
assert.EqualValues(t, "sync_table1", tables[0].Name)
assert.NoError(t, testEngine.Sync2(new(SyncTable2)))
tables, err = testEngine.DBMetas()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(tables))
assert.EqualValues(t, "sync_table1", tables[0].Name)
assert.NoError(t, testEngine.Sync2(new(SyncTable3)))
tables, err = testEngine.DBMetas()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(tables))
assert.EqualValues(t, "sync_table1", tables[0].Name)
}
func TestSyncTable2(t *testing.T) {
assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.Table("sync_tablex").Sync2(new(SyncTable1)))
tables, err := testEngine.DBMetas()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(tables))
assert.EqualValues(t, "sync_tablex", tables[0].Name)
assert.EqualValues(t, 3, len(tables[0].Columns()))
type SyncTable4 struct {
SyncTable1 `xorm:"extends"`
NewCol string
}
assert.NoError(t, testEngine.Table("sync_tablex").Sync2(new(SyncTable4)))
tables, err = testEngine.DBMetas()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(tables))
assert.EqualValues(t, "sync_tablex", tables[0].Name)
assert.EqualValues(t, 4, len(tables[0].Columns()))
assert.EqualValues(t, colMapper.Obj2Table("NewCol"), tables[0].Columns()[3].Name)
}
func TestIsTableExist(t *testing.T) {
assert.NoError(t, prepareEngine())
exist, err := testEngine.IsTableExist(new(CustomTableName))
assert.NoError(t, err)
assert.False(t, exist)
assert.NoError(t, testEngine.CreateTables(new(CustomTableName)))
exist, err = testEngine.IsTableExist(new(CustomTableName))
assert.NoError(t, err)
assert.True(t, exist)
}
func TestIsTableEmpty(t *testing.T) {
assert.NoError(t, prepareEngine())
type NumericEmpty struct {
Numeric float64 `xorm:"numeric(26,2)"`
}
type PictureEmpty struct {
Id int64
Url string `xorm:"unique"` //image's url
Title string
Description string
Created time.Time `xorm:"created"`
ILike int
PageView int
From_url string
Pre_url string `xorm:"unique"` //pre view image's url
Uid int64
}
assert.NoError(t, testEngine.DropTables(&PictureEmpty{}, &NumericEmpty{}))
assert.NoError(t, testEngine.Sync2(new(PictureEmpty), new(NumericEmpty)))
isEmpty, err := testEngine.IsTableEmpty(&PictureEmpty{})
assert.NoError(t, err)
assert.True(t, isEmpty)
tbName := testEngine.GetTableMapper().Obj2Table("PictureEmpty")
isEmpty, err = testEngine.IsTableEmpty(tbName)
assert.NoError(t, err)
assert.True(t, isEmpty)
}
type CustomTableName struct {
Id int64
Name string
}
func (c *CustomTableName) TableName() string {
return "customtablename"
}
func TestCustomTableName(t *testing.T) {
assert.NoError(t, prepareEngine())
c := new(CustomTableName)
assert.NoError(t, testEngine.DropTables(c))
assert.NoError(t, testEngine.CreateTables(c))
}
type IndexOrUnique struct {
Id int64
Index int `xorm:"index"`
Unique int `xorm:"unique"`
Group1 int `xorm:"index(ttt)"`
Group2 int `xorm:"index(ttt)"`
UniGroup1 int `xorm:"unique(lll)"`
UniGroup2 int `xorm:"unique(lll)"`
}
func TestIndexAndUnique(t *testing.T) {
assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.CreateTables(&IndexOrUnique{}))
assert.NoError(t, testEngine.DropTables(&IndexOrUnique{}))
assert.NoError(t, testEngine.CreateTables(&IndexOrUnique{}))
assert.NoError(t, testEngine.CreateIndexes(&IndexOrUnique{}))
assert.NoError(t, testEngine.CreateUniques(&IndexOrUnique{}))
assert.NoError(t, testEngine.DropIndexes(&IndexOrUnique{}))
}
func TestMetaInfo(t *testing.T) {
assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.Sync2(new(CustomTableName), new(IndexOrUnique)))
tables, err := testEngine.DBMetas()
assert.NoError(t, err)
assert.EqualValues(t, 2, len(tables))
tableNames := []string{tables[0].Name, tables[1].Name}
assert.Contains(t, tableNames, "customtablename")
assert.Contains(t, tableNames, "index_or_unique")
}
func TestCharst(t *testing.T) {
assert.NoError(t, prepareEngine())
err := testEngine.DropTables("user_charset")
assert.NoError(t, err)
err = testEngine.Charset("utf8").Table("user_charset").CreateTable(&Userinfo{})
assert.NoError(t, err)
}
func TestSync2_1(t *testing.T) {
type WxTest struct {
Id int `xorm:"not null pk autoincr INT(64)"`
Passport_user_type int16 `xorm:"null int"`
Id_delete int8 `xorm:"null int default 1"`
}
assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.DropTables("wx_test"))
assert.NoError(t, testEngine.Sync2(new(WxTest)))
assert.NoError(t, testEngine.Sync2(new(WxTest)))
}
func TestUnique_1(t *testing.T) {
type UserUnique struct {
Id int64
UserName string `xorm:"unique varchar(25) not null"`
Password string `xorm:"varchar(255) not null"`
Admin bool `xorm:"not null"`
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
}
assert.NoError(t, prepareEngine())
assert.NoError(t, testEngine.DropTables("user_unique"))
assert.NoError(t, testEngine.Sync2(new(UserUnique)))
assert.NoError(t, testEngine.DropTables("user_unique"))
assert.NoError(t, testEngine.CreateTables(new(UserUnique)))
assert.NoError(t, testEngine.CreateUniques(new(UserUnique)))
}
func TestSync2_2(t *testing.T) {
type TestSync2Index struct {
Id int64
UserId int64 `xorm:"index"`
}
assert.NoError(t, prepareEngine())
var tableNames = make(map[string]bool)
for i := 0; i < 10; i++ {
tableName := fmt.Sprintf("test_sync2_index_%d", i)
tableNames[tableName] = true
assert.NoError(t, testEngine.Table(tableName).Sync2(new(TestSync2Index)))
exist, err := testEngine.IsTableExist(tableName)
assert.NoError(t, err)
assert.True(t, exist)
}
tables, err := testEngine.DBMetas()
assert.NoError(t, err)
for _, table := range tables {
assert.True(t, tableNames[table.Name])
}
}
func TestSync2_Default(t *testing.T) {
type TestSync2Default struct {
Id int64
UserId int64 `xorm:"default(1)"`
IsMember bool `xorm:"default(true)"`
Name string `xorm:"default('my_name')"`
}
assert.NoError(t, prepareEngine())
assertSync(t, new(TestSync2Default))
assert.NoError(t, testEngine.Sync2(new(TestSync2Default)))
}