forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_insert_test.go
836 lines (720 loc) · 19.8 KB
/
session_insert_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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// 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"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestInsertOne(t *testing.T) {
assert.NoError(t, prepareEngine())
type Test struct {
Id int64 `xorm:"autoincr pk"`
Msg string `xorm:"varchar(255)"`
Created time.Time `xorm:"created"`
}
assert.NoError(t, testEngine.Sync2(new(Test)))
data := Test{Msg: "hi"}
_, err := testEngine.InsertOne(data)
assert.NoError(t, err)
}
func TestInsertMulti(t *testing.T) {
assert.NoError(t, prepareEngine())
type TestMulti struct {
Id int64 `xorm:"int(11) pk"`
Name string `xorm:"varchar(255)"`
}
assert.NoError(t, testEngine.Sync2(new(TestMulti)))
num, err := insertMultiDatas(1,
append([]TestMulti{}, TestMulti{1, "test1"}, TestMulti{2, "test2"}, TestMulti{3, "test3"}))
assert.NoError(t, err)
assert.EqualValues(t, 3, num)
}
func insertMultiDatas(step int, datas interface{}) (num int64, err error) {
sliceValue := reflect.Indirect(reflect.ValueOf(datas))
var iLen int64
if sliceValue.Kind() != reflect.Slice {
return 0, fmt.Errorf("not silce")
}
iLen = int64(sliceValue.Len())
if iLen == 0 {
return
}
session := testEngine.NewSession()
defer session.Close()
if err = callbackLooper(datas, step,
func(innerDatas interface{}) error {
n, e := session.InsertMulti(innerDatas)
if e != nil {
return e
}
num += n
return nil
}); err != nil {
return 0, err
} else if num != iLen {
return 0, fmt.Errorf("num error: %d - %d", num, iLen)
}
return
}
func callbackLooper(datas interface{}, step int, actionFunc func(interface{}) error) (err error) {
sliceValue := reflect.Indirect(reflect.ValueOf(datas))
if sliceValue.Kind() != reflect.Slice {
return fmt.Errorf("not slice")
}
if sliceValue.Len() <= 0 {
return
}
tempLen := 0
processedLen := sliceValue.Len()
for i := 0; i < sliceValue.Len(); i += step {
if processedLen > step {
tempLen = i + step
} else {
tempLen = sliceValue.Len()
}
var tempInterface []interface{}
for j := i; j < tempLen; j++ {
tempInterface = append(tempInterface, sliceValue.Index(j).Interface())
}
if err = actionFunc(tempInterface); err != nil {
return
}
processedLen = processedLen - step
}
return
}
func TestInsertOneIfPkIsPoint(t *testing.T) {
assert.NoError(t, prepareEngine())
type TestPoint struct {
Id *int64 `xorm:"autoincr pk notnull 'id'"`
Msg *string `xorm:"varchar(255)"`
Created *time.Time `xorm:"created"`
}
assert.NoError(t, testEngine.Sync2(new(TestPoint)))
msg := "hi"
data := TestPoint{Msg: &msg}
_, err := testEngine.InsertOne(&data)
assert.NoError(t, err)
}
func TestInsertOneIfPkIsPointRename(t *testing.T) {
assert.NoError(t, prepareEngine())
type ID *int64
type TestPoint2 struct {
Id ID `xorm:"autoincr pk notnull 'id'"`
Msg *string `xorm:"varchar(255)"`
Created *time.Time `xorm:"created"`
}
assert.NoError(t, testEngine.Sync2(new(TestPoint2)))
msg := "hi"
data := TestPoint2{Msg: &msg}
_, err := testEngine.InsertOne(&data)
assert.NoError(t, err)
}
func TestInsert(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))
user := Userinfo{0, "xiaolunwen", "dev", "lunny", time.Now(),
Userdetail{Id: 1}, 1.78, []byte{1, 2, 3}, true}
cnt, err := testEngine.Insert(&user)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt, "insert not returned 1")
assert.True(t, user.Uid > 0, "not return id error")
user.Uid = 0
cnt, err = testEngine.Insert(&user)
// Username is unique, so this should return error
assert.Error(t, err, "insert should fail but no error returned")
assert.EqualValues(t, 0, cnt, "insert not returned 1")
if err == nil {
panic("should return err")
}
}
func TestInsertAutoIncr(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))
// auto increment insert
user := Userinfo{Username: "xiaolunwen2", Departname: "dev", Alias: "lunny", Created: time.Now(),
Detail: Userdetail{Id: 1}, Height: 1.78, Avatar: []byte{1, 2, 3}, IsMan: true}
cnt, err := testEngine.Insert(&user)
fmt.Println(user.Uid)
if err != nil {
t.Error(err)
panic(err)
}
if cnt != 1 {
err = errors.New("insert not returned 1")
t.Error(err)
panic(err)
}
if user.Uid <= 0 {
t.Error(errors.New("not return id error"))
}
}
type DefaultInsert struct {
Id int64
Status int `xorm:"default -1"`
Name string
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
func TestInsertDefault(t *testing.T) {
assert.NoError(t, prepareEngine())
di := new(DefaultInsert)
err := testEngine.Sync2(di)
assert.NoError(t, err)
var di2 = DefaultInsert{Name: "test"}
_, err = testEngine.Omit(testEngine.GetColumnMapper().Obj2Table("Status")).Insert(&di2)
assert.NoError(t, err)
has, err := testEngine.Desc("(id)").Get(di)
assert.NoError(t, err)
if !has {
err = errors.New("error with no data")
t.Error(err)
panic(err)
}
if di.Status != -1 {
err = errors.New("inserted error data")
t.Error(err)
panic(err)
}
if di2.Updated.Unix() != di.Updated.Unix() {
err = errors.New("updated should equal")
t.Error(err, di.Updated, di2.Updated)
panic(err)
}
if di2.Created.Unix() != di.Created.Unix() {
err = errors.New("created should equal")
t.Error(err, di.Created, di2.Created)
panic(err)
}
}
type DefaultInsert2 struct {
Id int64
Name string
Url string `xorm:"text"`
CheckTime time.Time `xorm:"not null default '2000-01-01 00:00:00' TIMESTAMP"`
}
func TestInsertDefault2(t *testing.T) {
assert.NoError(t, prepareEngine())
di := new(DefaultInsert2)
err := testEngine.Sync2(di)
if err != nil {
t.Error(err)
}
var di2 = DefaultInsert2{Name: "test"}
_, err = testEngine.Omit(testEngine.GetColumnMapper().Obj2Table("CheckTime")).Insert(&di2)
if err != nil {
t.Error(err)
}
has, err := testEngine.Desc("(id)").Get(di)
if err != nil {
t.Error(err)
}
if !has {
err = errors.New("error with no data")
t.Error(err)
panic(err)
}
has, err = testEngine.NoAutoCondition().Desc("(id)").Get(&di2)
if err != nil {
t.Error(err)
}
if !has {
err = errors.New("error with no data")
t.Error(err)
panic(err)
}
if *di != di2 {
err = fmt.Errorf("%v is not equal to %v", di, di2)
t.Error(err)
panic(err)
}
/*if di2.Updated.Unix() != di.Updated.Unix() {
err = errors.New("updated should equal")
t.Error(err, di.Updated, di2.Updated)
panic(err)
}
if di2.Created.Unix() != di.Created.Unix() {
err = errors.New("created should equal")
t.Error(err, di.Created, di2.Created)
panic(err)
}*/
}
type CreatedInsert struct {
Id int64
Created time.Time `xorm:"created"`
}
type CreatedInsert2 struct {
Id int64
Created int64 `xorm:"created"`
}
type CreatedInsert3 struct {
Id int64
Created int `xorm:"created bigint"`
}
type CreatedInsert4 struct {
Id int64
Created int `xorm:"created"`
}
type CreatedInsert5 struct {
Id int64
Created time.Time `xorm:"created bigint"`
}
type CreatedInsert6 struct {
Id int64
Created time.Time `xorm:"created bigint"`
}
func TestInsertCreated(t *testing.T) {
assert.NoError(t, prepareEngine())
di := new(CreatedInsert)
err := testEngine.Sync2(di)
if err != nil {
t.Fatal(err)
}
ci := &CreatedInsert{}
_, err = testEngine.Insert(ci)
if err != nil {
t.Fatal(err)
}
has, err := testEngine.Desc("(id)").Get(di)
if err != nil {
t.Fatal(err)
}
if !has {
t.Fatal(ErrNotExist)
}
if ci.Created.Unix() != di.Created.Unix() {
t.Fatal("should equal:", ci, di)
}
fmt.Println("ci:", ci, "di:", di)
di2 := new(CreatedInsert2)
err = testEngine.Sync2(di2)
if err != nil {
t.Fatal(err)
}
ci2 := &CreatedInsert2{}
_, err = testEngine.Insert(ci2)
if err != nil {
t.Fatal(err)
}
has, err = testEngine.Desc("(id)").Get(di2)
if err != nil {
t.Fatal(err)
}
if !has {
t.Fatal(ErrNotExist)
}
if ci2.Created != di2.Created {
t.Fatal("should equal:", ci2, di2)
}
fmt.Println("ci2:", ci2, "di2:", di2)
di3 := new(CreatedInsert3)
err = testEngine.Sync2(di3)
if err != nil {
t.Fatal(err)
}
ci3 := &CreatedInsert3{}
_, err = testEngine.Insert(ci3)
if err != nil {
t.Fatal(err)
}
has, err = testEngine.Desc("(id)").Get(di3)
if err != nil {
t.Fatal(err)
}
if !has {
t.Fatal(ErrNotExist)
}
if ci3.Created != di3.Created {
t.Fatal("should equal:", ci3, di3)
}
fmt.Println("ci3:", ci3, "di3:", di3)
di4 := new(CreatedInsert4)
err = testEngine.Sync2(di4)
if err != nil {
t.Fatal(err)
}
ci4 := &CreatedInsert4{}
_, err = testEngine.Insert(ci4)
if err != nil {
t.Fatal(err)
}
has, err = testEngine.Desc("(id)").Get(di4)
if err != nil {
t.Fatal(err)
}
if !has {
t.Fatal(ErrNotExist)
}
if ci4.Created != di4.Created {
t.Fatal("should equal:", ci4, di4)
}
fmt.Println("ci4:", ci4, "di4:", di4)
di5 := new(CreatedInsert5)
err = testEngine.Sync2(di5)
if err != nil {
t.Fatal(err)
}
ci5 := &CreatedInsert5{}
_, err = testEngine.Insert(ci5)
if err != nil {
t.Fatal(err)
}
has, err = testEngine.Desc("(id)").Get(di5)
if err != nil {
t.Fatal(err)
}
if !has {
t.Fatal(ErrNotExist)
}
if ci5.Created.Unix() != di5.Created.Unix() {
t.Fatal("should equal:", ci5, di5)
}
fmt.Println("ci5:", ci5, "di5:", di5)
di6 := new(CreatedInsert6)
err = testEngine.Sync2(di6)
if err != nil {
t.Fatal(err)
}
oldTime := time.Now().Add(-time.Hour)
ci6 := &CreatedInsert6{Created: oldTime}
_, err = testEngine.Insert(ci6)
if err != nil {
t.Fatal(err)
}
has, err = testEngine.Desc("(id)").Get(di6)
if err != nil {
t.Fatal(err)
}
if !has {
t.Fatal(ErrNotExist)
}
if ci6.Created.Unix() != di6.Created.Unix() {
t.Fatal("should equal:", ci6, di6)
}
fmt.Println("ci6:", ci6, "di6:", di6)
}
type JsonTime time.Time
func (j JsonTime) format() string {
t := time.Time(j)
if t.IsZero() {
return ""
}
return t.Format("2006-01-02")
}
func (j JsonTime) MarshalText() ([]byte, error) {
return []byte(j.format()), nil
}
func (j JsonTime) MarshalJSON() ([]byte, error) {
return []byte(`"` + j.format() + `"`), nil
}
func TestDefaultTime3(t *testing.T) {
type PrepareTask struct {
Id int `xorm:"not null pk autoincr INT(11)" json:"id"`
// ...
StartTime JsonTime `xorm:"not null default '2006-01-02 15:04:05' TIMESTAMP index" json:"start_time"`
EndTime JsonTime `xorm:"not null default '2006-01-02 15:04:05' TIMESTAMP" json:"end_time"`
Cuser string `xorm:"not null default '' VARCHAR(64) index" json:"cuser"`
Muser string `xorm:"not null default '' VARCHAR(64)" json:"muser"`
Ctime JsonTime `xorm:"not null default CURRENT_TIMESTAMP TIMESTAMP created" json:"ctime"`
Mtime JsonTime `xorm:"not null default CURRENT_TIMESTAMP TIMESTAMP updated" json:"mtime"`
}
assert.NoError(t, prepareEngine())
assertSync(t, new(PrepareTask))
prepareTask := &PrepareTask{
StartTime: JsonTime(time.Now()),
Cuser: "userId",
Muser: "userId",
}
cnt, err := testEngine.Omit("end_time").InsertOne(prepareTask)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
}
type MyJsonTime struct {
Id int64 `json:"id"`
Created JsonTime `xorm:"created" json:"created_at"`
}
func TestCreatedJsonTime(t *testing.T) {
assert.NoError(t, prepareEngine())
di5 := new(MyJsonTime)
err := testEngine.Sync2(di5)
if err != nil {
t.Fatal(err)
}
ci5 := &MyJsonTime{}
_, err = testEngine.Insert(ci5)
if err != nil {
t.Fatal(err)
}
has, err := testEngine.Desc("(id)").Get(di5)
if err != nil {
t.Fatal(err)
}
if !has {
t.Fatal(ErrNotExist)
}
if time.Time(ci5.Created).Unix() != time.Time(di5.Created).Unix() {
t.Fatal("should equal:", time.Time(ci5.Created).Unix(), time.Time(di5.Created).Unix())
}
fmt.Println("ci5:", ci5, "di5:", di5)
var dis = make([]MyJsonTime, 0)
err = testEngine.Find(&dis)
if err != nil {
t.Fatal(err)
}
}
func TestInsertMulti2(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))
users := []Userinfo{
{Username: "xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
{Username: "xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
{Username: "xlw11", Departname: "dev", Alias: "lunny2", Created: time.Now()},
{Username: "xlw22", Departname: "dev", Alias: "lunny3", Created: time.Now()},
}
cnt, err := testEngine.Insert(&users)
if err != nil {
t.Error(err)
panic(err)
}
if cnt != int64(len(users)) {
err = errors.New("insert not returned 1")
t.Error(err)
panic(err)
return
}
users2 := []*Userinfo{
&Userinfo{Username: "1xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
&Userinfo{Username: "1xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
&Userinfo{Username: "1xlw11", Departname: "dev", Alias: "lunny2", Created: time.Now()},
&Userinfo{Username: "1xlw22", Departname: "dev", Alias: "lunny3", Created: time.Now()},
}
cnt, err = testEngine.Insert(&users2)
if err != nil {
t.Error(err)
panic(err)
}
if cnt != int64(len(users2)) {
err = errors.New(fmt.Sprintf("insert not returned %v", len(users2)))
t.Error(err)
panic(err)
}
}
func TestInsertTwoTable(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo), new(Userdetail))
userdetail := Userdetail{ /*Id: 1, */ Intro: "I'm a very beautiful women.", Profile: "sfsaf"}
userinfo := Userinfo{Username: "xlw3", Departname: "dev", Alias: "lunny4", Created: time.Now(), Detail: userdetail}
cnt, err := testEngine.Insert(&userinfo, &userdetail)
if err != nil {
t.Error(err)
panic(err)
}
if userinfo.Uid <= 0 {
err = errors.New("not return id error")
t.Error(err)
panic(err)
}
if userdetail.Id <= 0 {
err = errors.New("not return id error")
t.Error(err)
panic(err)
}
if cnt != 2 {
err = errors.New("insert not returned 2")
t.Error(err)
panic(err)
}
}
func TestInsertCreatedInt64(t *testing.T) {
assert.NoError(t, prepareEngine())
type TestCreatedInt64 struct {
Id int64 `xorm:"autoincr pk"`
Msg string `xorm:"varchar(255)"`
Created int64 `xorm:"created"`
}
assert.NoError(t, testEngine.Sync2(new(TestCreatedInt64)))
data := TestCreatedInt64{Msg: "hi"}
now := time.Now()
cnt, err := testEngine.Insert(&data)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
assert.True(t, now.Unix() <= data.Created)
var data2 TestCreatedInt64
has, err := testEngine.Get(&data2)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, data.Created, data2.Created)
}
type MyUserinfo Userinfo
func (MyUserinfo) TableName() string {
return "user_info"
}
func TestInsertMulti3(t *testing.T) {
assert.NoError(t, prepareEngine())
testEngine.ShowSQL(true)
assertSync(t, new(MyUserinfo))
users := []MyUserinfo{
{Username: "xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
{Username: "xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
{Username: "xlw11", Departname: "dev", Alias: "lunny2", Created: time.Now()},
{Username: "xlw22", Departname: "dev", Alias: "lunny3", Created: time.Now()},
}
cnt, err := testEngine.Insert(&users)
assert.NoError(t, err)
assert.EqualValues(t, len(users), cnt)
users2 := []*MyUserinfo{
&MyUserinfo{Username: "1xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
&MyUserinfo{Username: "1xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
&MyUserinfo{Username: "1xlw11", Departname: "dev", Alias: "lunny2", Created: time.Now()},
&MyUserinfo{Username: "1xlw22", Departname: "dev", Alias: "lunny3", Created: time.Now()},
}
cnt, err = testEngine.Insert(&users2)
assert.NoError(t, err)
assert.EqualValues(t, len(users), cnt)
}
type MyUserinfo2 struct {
Uid int64 `xorm:"id pk not null autoincr"`
Username string `xorm:"unique"`
Departname string
Alias string `xorm:"-"`
Created time.Time
Detail Userdetail `xorm:"detail_id int(11)"`
Height float64
Avatar []byte
IsMan bool
}
func (MyUserinfo2) TableName() string {
return "user_info"
}
func TestInsertMulti4(t *testing.T) {
assert.NoError(t, prepareEngine())
testEngine.ShowSQL(false)
assertSync(t, new(MyUserinfo2))
testEngine.ShowSQL(true)
users := []MyUserinfo2{
{Username: "xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
{Username: "xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
{Username: "xlw11", Departname: "dev", Alias: "lunny2", Created: time.Now()},
{Username: "xlw22", Departname: "dev", Alias: "lunny3", Created: time.Now()},
}
cnt, err := testEngine.Insert(&users)
assert.NoError(t, err)
assert.EqualValues(t, len(users), cnt)
users2 := []*MyUserinfo2{
&MyUserinfo2{Username: "1xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
&MyUserinfo2{Username: "1xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
&MyUserinfo2{Username: "1xlw11", Departname: "dev", Alias: "lunny2", Created: time.Now()},
&MyUserinfo2{Username: "1xlw22", Departname: "dev", Alias: "lunny3", Created: time.Now()},
}
cnt, err = testEngine.Insert(&users2)
assert.NoError(t, err)
assert.EqualValues(t, len(users), cnt)
}
func TestAnonymousStruct(t *testing.T) {
type PlainObject struct {
ID uint64 `json:"id,string" xorm:"'ID' pk autoincr"`
Desc string `json:"desc" xorm:"'DESC' notnull"`
}
type PlainFoo struct {
PlainObject `xorm:"extends"` // primary key defined in extends struct
Width uint32 `json:"width" xorm:"'WIDTH' notnull"`
Height uint32 `json:"height" xorm:"'HEIGHT' notnull"`
Ext struct {
F1 uint32 `json:"f1,omitempty"`
F2 uint32 `json:"f2,omitempty"`
} `json:"ext" xorm:"'EXT' json notnull"`
}
assert.NoError(t, prepareEngine())
assertSync(t, new(PlainFoo))
_, err := testEngine.Insert(&PlainFoo{
PlainObject: PlainObject{
Desc: "test",
},
Width: 10,
Height: 20,
Ext: struct {
F1 uint32 `json:"f1,omitempty"`
F2 uint32 `json:"f2,omitempty"`
}{
F1: 11,
F2: 12,
},
})
assert.NoError(t, err)
}
func TestInsertMap(t *testing.T) {
type InsertMap struct {
Id int64
Width uint32
Height uint32
Name string
}
assert.NoError(t, prepareEngine())
assertSync(t, new(InsertMap))
cnt, err := testEngine.Table(new(InsertMap)).Insert(map[string]interface{}{
"width": 20,
"height": 10,
"name": "lunny",
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var im InsertMap
has, err := testEngine.Get(&im)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, 20, im.Width)
assert.EqualValues(t, 10, im.Height)
assert.EqualValues(t, "lunny", im.Name)
cnt, err = testEngine.Table("insert_map").Insert(map[string]interface{}{
"width": 30,
"height": 10,
"name": "lunny",
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var ims []InsertMap
err = testEngine.Find(&ims)
assert.NoError(t, err)
assert.EqualValues(t, 2, len(ims))
assert.EqualValues(t, 20, ims[0].Width)
assert.EqualValues(t, 10, ims[0].Height)
assert.EqualValues(t, "lunny", ims[0].Name)
assert.EqualValues(t, 30, ims[1].Width)
assert.EqualValues(t, 10, ims[1].Height)
assert.EqualValues(t, "lunny", ims[1].Name)
cnt, err = testEngine.Table("insert_map").Insert([]map[string]interface{}{
{
"width": 40,
"height": 10,
"name": "lunny",
},
{
"width": 50,
"height": 10,
"name": "lunny",
},
})
assert.NoError(t, err)
assert.EqualValues(t, 2, cnt)
ims = make([]InsertMap, 0, 4)
err = testEngine.Find(&ims)
assert.NoError(t, err)
assert.EqualValues(t, 4, len(ims))
assert.EqualValues(t, 20, ims[0].Width)
assert.EqualValues(t, 10, ims[0].Height)
assert.EqualValues(t, "lunny", ims[1].Name)
assert.EqualValues(t, 30, ims[1].Width)
assert.EqualValues(t, 10, ims[1].Height)
assert.EqualValues(t, "lunny", ims[1].Name)
assert.EqualValues(t, 40, ims[2].Width)
assert.EqualValues(t, 10, ims[2].Height)
assert.EqualValues(t, "lunny", ims[2].Name)
assert.EqualValues(t, 50, ims[3].Width)
assert.EqualValues(t, 10, ims[3].Height)
assert.EqualValues(t, "lunny", ims[3].Name)
}