123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911 |
- // 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 TestInsertOne2(t *testing.T) {
- assert.NoError(t, prepareEngine())
- type Test2 struct {
- Id int64 `xorm:"autoincr pk"`
- Msg string `xorm:"varchar(255)"`
- Created time.Time `xorm:"datetime created"`
- }
- assert.NoError(t, testEngine.Sync2(new(Test2)))
- data := Test2{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)
- }
- /*INSERT INTO `issue` (`repo_id`, `poster_id`, ... ,`name`, `content`, ... ,`index`)
- SELECT $1, $2, ..., $14, $15, ..., MAX(`index`) + 1 FROM `issue` WHERE `repo_id` = $1;
- */
- func TestInsertWhere(t *testing.T) {
- type InsertWhere struct {
- Id int64
- Index int `xorm:"unique(s) notnull"`
- RepoId int64 `xorm:"unique(s)"`
- Width uint32
- Height uint32
- Name string
- }
- assert.NoError(t, prepareEngine())
- assertSync(t, new(InsertWhere))
- var i = InsertWhere{
- RepoId: 1,
- Width: 10,
- Height: 20,
- Name: "trest",
- }
- inserted, err := testEngine.SetExpr("`index`", "coalesce(MAX(`index`),0)+1").
- Where("repo_id=?", 1).
- Insert(&i)
- assert.NoError(t, err)
- assert.EqualValues(t, 1, inserted)
- assert.EqualValues(t, 1, i.Id)
- var j InsertWhere
- has, err := testEngine.ID(i.Id).Get(&j)
- assert.NoError(t, err)
- assert.True(t, has)
- i.Index = 1
- assert.EqualValues(t, i, j)
- inserted, err = testEngine.Table(new(InsertWhere)).Where("repo_id=?", 1).
- SetExpr("`index`", "coalesce(MAX(`index`),0)+1").
- Insert(map[string]interface{}{
- "repo_id": 1,
- "width": 20,
- "height": 40,
- "name": "trest2",
- })
- assert.NoError(t, err)
- assert.EqualValues(t, 1, inserted)
- var j2 InsertWhere
- has, err = testEngine.ID(2).Get(&j2)
- assert.NoError(t, err)
- assert.True(t, has)
- assert.EqualValues(t, 1, j2.RepoId)
- assert.EqualValues(t, 20, j2.Width)
- assert.EqualValues(t, 40, j2.Height)
- assert.EqualValues(t, "trest2", j2.Name)
- assert.EqualValues(t, 2, j2.Index)
- }
|