forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
336 lines (276 loc) · 7.2 KB
/
types_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
// 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 (
"encoding/json"
"errors"
"fmt"
"testing"
"github.com/go-xorm/core"
"github.com/stretchr/testify/assert"
)
func TestArrayField(t *testing.T) {
assert.NoError(t, prepareEngine())
type ArrayStruct struct {
Id int64
Name [20]byte `xorm:"char(80)"`
}
assert.NoError(t, testEngine.Sync2(new(ArrayStruct)))
var as = ArrayStruct{
Name: [20]byte{
96, 96, 96, 96, 96,
96, 96, 96, 96, 96,
96, 96, 96, 96, 96,
96, 96, 96, 96, 96,
},
}
cnt, err := testEngine.Insert(&as)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var arr ArrayStruct
has, err := testEngine.ID(1).Get(&arr)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, as.Name, arr.Name)
var arrs []ArrayStruct
err = testEngine.Find(&arrs)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(arrs))
assert.Equal(t, as.Name, arrs[0].Name)
var newName = [20]byte{
90, 96, 96, 96, 96,
96, 96, 96, 96, 96,
96, 96, 96, 96, 96,
96, 96, 96, 96, 96,
}
cnt, err = testEngine.ID(1).Update(&ArrayStruct{
Name: newName,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var newArr ArrayStruct
has, err = testEngine.ID(1).Get(&newArr)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, newName, newArr.Name)
cnt, err = testEngine.ID(1).Delete(new(ArrayStruct))
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var cfgArr ArrayStruct
has, err = testEngine.ID(1).Get(&cfgArr)
assert.NoError(t, err)
assert.Equal(t, false, has)
}
func TestGetBytes(t *testing.T) {
assert.NoError(t, prepareEngine())
type Varbinary struct {
Data []byte `xorm:"VARBINARY(250)"`
}
err := testEngine.Sync2(new(Varbinary))
assert.NoError(t, err)
cnt, err := testEngine.Insert(&Varbinary{
Data: []byte("test"),
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var b Varbinary
has, err := testEngine.Get(&b)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, "test", string(b.Data))
}
type ConvString string
func (s *ConvString) FromDB(data []byte) error {
*s = ConvString("prefix---" + string(data))
return nil
}
func (s *ConvString) ToDB() ([]byte, error) {
return []byte(string(*s)), nil
}
type ConvConfig struct {
Name string
Id int64
}
func (s *ConvConfig) FromDB(data []byte) error {
return json.Unmarshal(data, s)
}
func (s *ConvConfig) ToDB() ([]byte, error) {
return json.Marshal(s)
}
type SliceType []*ConvConfig
func (s *SliceType) FromDB(data []byte) error {
return json.Unmarshal(data, s)
}
func (s *SliceType) ToDB() ([]byte, error) {
return json.Marshal(s)
}
type ConvStruct struct {
Conv ConvString
Conv2 *ConvString
Cfg1 ConvConfig
Cfg2 *ConvConfig `xorm:"TEXT"`
Cfg3 core.Conversion `xorm:"BLOB"`
Slice SliceType
}
func (c *ConvStruct) BeforeSet(name string, cell Cell) {
if name == "cfg3" || name == "Cfg3" {
c.Cfg3 = new(ConvConfig)
}
}
func TestConversion(t *testing.T) {
assert.NoError(t, prepareEngine())
c := new(ConvStruct)
assert.NoError(t, testEngine.DropTables(c))
assert.NoError(t, testEngine.Sync2(c))
var s ConvString = "sssss"
c.Conv = "tttt"
c.Conv2 = &s
c.Cfg1 = ConvConfig{"mm", 1}
c.Cfg2 = &ConvConfig{"xx", 2}
c.Cfg3 = &ConvConfig{"zz", 3}
c.Slice = []*ConvConfig{{"yy", 4}, {"ff", 5}}
_, err := testEngine.Insert(c)
assert.NoError(t, err)
c1 := new(ConvStruct)
has, err := testEngine.Get(c1)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, "prefix---tttt", string(c1.Conv))
assert.NotNil(t, c1.Conv2)
assert.EqualValues(t, "prefix---"+s, *c1.Conv2)
assert.EqualValues(t, c.Cfg1, c1.Cfg1)
assert.NotNil(t, c1.Cfg2)
assert.EqualValues(t, *c.Cfg2, *c1.Cfg2)
assert.NotNil(t, c1.Cfg3)
assert.EqualValues(t, *c.Cfg3.(*ConvConfig), *c1.Cfg3.(*ConvConfig))
assert.EqualValues(t, 2, len(c1.Slice))
assert.EqualValues(t, *c.Slice[0], *c1.Slice[0])
assert.EqualValues(t, *c.Slice[1], *c1.Slice[1])
}
type MyInt int
type MyUInt uint
type MyFloat float64
type MyStruct struct {
Type MyInt
U MyUInt
F MyFloat
S MyString
IA []MyInt
UA []MyUInt
FA []MyFloat
SA []MyString
NameArray []string
Name string
UIA []uint
UIA8 []uint8
UIA16 []uint16
UIA32 []uint32
UIA64 []uint64
UI uint
//C64 complex64
MSS map[string]string
}
func TestCustomType1(t *testing.T) {
assert.NoError(t, prepareEngine())
err := testEngine.DropTables(&MyStruct{})
assert.NoError(t, err)
err = testEngine.CreateTables(&MyStruct{})
assert.NoError(t, err)
i := MyStruct{Name: "Test", Type: MyInt(1)}
i.U = 23
i.F = 1.34
i.S = "fafdsafdsaf"
i.UI = 2
i.IA = []MyInt{1, 3, 5}
i.UIA = []uint{1, 3}
i.UIA16 = []uint16{2}
i.UIA32 = []uint32{4, 5}
i.UIA64 = []uint64{6, 7, 9}
i.UIA8 = []uint8{1, 2, 3, 4}
i.NameArray = []string{"ssss", "fsdf", "lllll, ss"}
i.MSS = map[string]string{"s": "sfds,ss", "x": "lfjljsl"}
cnt, err := testEngine.Insert(&i)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
fmt.Println(i)
i.NameArray = []string{}
i.MSS = map[string]string{}
i.F = 0
has, err := testEngine.Get(&i)
assert.NoError(t, err)
assert.True(t, has)
ss := []MyStruct{}
err = testEngine.Find(&ss)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(ss))
assert.EqualValues(t, i, ss[0])
sss := MyStruct{}
has, err = testEngine.Get(&sss)
assert.NoError(t, err)
assert.True(t, has)
sss.NameArray = []string{}
sss.MSS = map[string]string{}
cnt, err = testEngine.Delete(&sss)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
}
type Status struct {
Name string
Color string
}
var (
_ core.Conversion = &Status{}
Registed Status = Status{"Registed", "white"}
Approved Status = Status{"Approved", "green"}
Removed Status = Status{"Removed", "red"}
Statuses map[string]Status = map[string]Status{
Registed.Name: Registed,
Approved.Name: Approved,
Removed.Name: Removed,
}
)
func (s *Status) FromDB(bytes []byte) error {
if r, ok := Statuses[string(bytes)]; ok {
*s = r
return nil
} else {
return errors.New("no this data")
}
}
func (s *Status) ToDB() ([]byte, error) {
return []byte(s.Name), nil
}
type UserCus struct {
Id int64
Name string
Status Status `xorm:"varchar(40)"`
}
func TestCustomType2(t *testing.T) {
assert.NoError(t, prepareEngine())
var uc UserCus
err := testEngine.CreateTables(&uc)
assert.NoError(t, err)
tableName := testEngine.TableName(&uc, true)
_, err = testEngine.Exec("delete from " + testEngine.Quote(tableName))
assert.NoError(t, err)
if testEngine.Dialect().DBType() == core.MSSQL {
return
/*_, err = engine.Exec("set IDENTITY_INSERT " + tableName + " on")
if err != nil {
t.Fatal(err)
}*/
}
_, err = testEngine.Insert(&UserCus{1, "xlw", Registed})
assert.NoError(t, err)
user := UserCus{}
exist, err := testEngine.ID(1).Get(&user)
assert.NoError(t, err)
assert.True(t, exist)
fmt.Println(user)
users := make([]UserCus, 0)
err = testEngine.Where("`"+testEngine.GetColumnMapper().Obj2Table("Status")+"` = ?", "Registed").Find(&users)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(users))
fmt.Println(users)
}