123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197 |
- // 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"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/xormplus/core"
- )
- type IntId struct {
- Id int `xorm:"pk autoincr"`
- Name string
- }
- type Int16Id struct {
- Id int16 `xorm:"pk autoincr"`
- Name string
- }
- type Int32Id struct {
- Id int32 `xorm:"pk autoincr"`
- Name string
- }
- type UintId struct {
- Id uint `xorm:"pk autoincr"`
- Name string
- }
- type Uint16Id struct {
- Id uint16 `xorm:"pk autoincr"`
- Name string
- }
- type Uint32Id struct {
- Id uint32 `xorm:"pk autoincr"`
- Name string
- }
- type Uint64Id struct {
- Id uint64 `xorm:"pk autoincr"`
- Name string
- }
- type StringPK struct {
- Id string `xorm:"pk notnull"`
- Name string
- }
- type ID int64
- type MyIntPK struct {
- ID ID `xorm:"pk autoincr"`
- Name string
- }
- type StrID string
- type MyStringPK struct {
- ID StrID `xorm:"pk notnull"`
- Name string
- }
- func TestIntId(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&IntId{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&IntId{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- cnt, err := testEngine.Insert(&IntId{Name: "test"})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- bean := new(IntId)
- has, err := testEngine.Get(bean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if !has {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans := make([]IntId, 0)
- err = testEngine.Find(&beans)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans2 := make(map[int]IntId)
- err = testEngine.Find(&beans2)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans2) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- cnt, err = testEngine.ID(bean.Id).Delete(&IntId{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- }
- func TestInt16Id(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&Int16Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&Int16Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- cnt, err := testEngine.Insert(&Int16Id{Name: "test"})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- bean := new(Int16Id)
- has, err := testEngine.Get(bean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if !has {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans := make([]Int16Id, 0)
- err = testEngine.Find(&beans)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans2 := make(map[int16]Int16Id, 0)
- err = testEngine.Find(&beans2)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans2) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- cnt, err = testEngine.ID(bean.Id).Delete(&Int16Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- }
- func TestInt32Id(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&Int32Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&Int32Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- cnt, err := testEngine.Insert(&Int32Id{Name: "test"})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- bean := new(Int32Id)
- has, err := testEngine.Get(bean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if !has {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans := make([]Int32Id, 0)
- err = testEngine.Find(&beans)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans2 := make(map[int32]Int32Id, 0)
- err = testEngine.Find(&beans2)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans2) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- cnt, err = testEngine.ID(bean.Id).Delete(&Int32Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- }
- func TestUintId(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&UintId{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&UintId{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- cnt, err := testEngine.Insert(&UintId{Name: "test"})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- var inserts = []UintId{
- {Name: "test1"},
- {Name: "test2"},
- }
- cnt, err = testEngine.Insert(&inserts)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 2 {
- err = errors.New("insert count should be two")
- t.Error(err)
- panic(err)
- }
- bean := new(UintId)
- has, err := testEngine.Get(bean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if !has {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans := make([]UintId, 0)
- err = testEngine.Find(&beans)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans) != 3 {
- err = errors.New("get count should be three")
- t.Error(err)
- panic(err)
- }
- beans2 := make(map[uint]UintId, 0)
- err = testEngine.Find(&beans2)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans2) != 3 {
- err = errors.New("get count should be three")
- t.Error(err)
- panic(err)
- }
- cnt, err = testEngine.ID(bean.Id).Delete(&UintId{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- }
- func TestUint16Id(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&Uint16Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&Uint16Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- cnt, err := testEngine.Insert(&Uint16Id{Name: "test"})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- bean := new(Uint16Id)
- has, err := testEngine.Get(bean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if !has {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans := make([]Uint16Id, 0)
- err = testEngine.Find(&beans)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans2 := make(map[uint16]Uint16Id, 0)
- err = testEngine.Find(&beans2)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans2) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- cnt, err = testEngine.ID(bean.Id).Delete(&Uint16Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- }
- func TestUint32Id(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&Uint32Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&Uint32Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- cnt, err := testEngine.Insert(&Uint32Id{Name: "test"})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- bean := new(Uint32Id)
- has, err := testEngine.Get(bean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if !has {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans := make([]Uint32Id, 0)
- err = testEngine.Find(&beans)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans2 := make(map[uint32]Uint32Id, 0)
- err = testEngine.Find(&beans2)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans2) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- cnt, err = testEngine.ID(bean.Id).Delete(&Uint32Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- }
- func TestUint64Id(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&Uint64Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&Uint64Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- idbean := &Uint64Id{Name: "test"}
- cnt, err := testEngine.Insert(idbean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- bean := new(Uint64Id)
- has, err := testEngine.Get(bean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if !has {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- if bean.Id != idbean.Id {
- panic(errors.New("should be equal"))
- }
- beans := make([]Uint64Id, 0)
- err = testEngine.Find(&beans)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- if *bean != beans[0] {
- panic(errors.New("should be equal"))
- }
- beans2 := make(map[uint64]Uint64Id, 0)
- err = testEngine.Find(&beans2)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans2) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- if *bean != beans2[bean.Id] {
- panic(errors.New("should be equal"))
- }
- cnt, err = testEngine.ID(bean.Id).Delete(&Uint64Id{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- }
- func TestStringPK(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&StringPK{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&StringPK{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- cnt, err := testEngine.Insert(&StringPK{Id: "1-1-2", Name: "test"})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- bean := new(StringPK)
- has, err := testEngine.Get(bean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if !has {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans := make([]StringPK, 0)
- err = testEngine.Find(&beans)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- beans2 := make(map[string]StringPK)
- err = testEngine.Find(&beans2)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans2) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- cnt, err = testEngine.ID(bean.Id).Delete(&StringPK{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- }
- type CompositeKey struct {
- Id1 int64 `xorm:"id1 pk"`
- Id2 int64 `xorm:"id2 pk"`
- UpdateStr string
- }
- func TestCompositeKey(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&CompositeKey{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&CompositeKey{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- cnt, err := testEngine.Insert(&CompositeKey{11, 22, ""})
- if err != nil {
- t.Error(err)
- } else if cnt != 1 {
- t.Error(errors.New("failed to insert CompositeKey{11, 22}"))
- }
- cnt, err = testEngine.Insert(&CompositeKey{11, 22, ""})
- if err == nil || cnt == 1 {
- t.Error(errors.New("inserted CompositeKey{11, 22}"))
- }
- var compositeKeyVal CompositeKey
- has, err := testEngine.ID(core.PK{11, 22}).Get(&compositeKeyVal)
- if err != nil {
- t.Error(err)
- } else if !has {
- t.Error(errors.New("can't get CompositeKey{11, 22}"))
- }
- var compositeKeyVal2 CompositeKey
- // test passing PK ptr, this test seem failed withCache
- has, err = testEngine.ID(&core.PK{11, 22}).Get(&compositeKeyVal2)
- if err != nil {
- t.Error(err)
- } else if !has {
- t.Error(errors.New("can't get CompositeKey{11, 22}"))
- }
- if compositeKeyVal != compositeKeyVal2 {
- t.Error(errors.New("should be equal"))
- }
- var cps = make([]CompositeKey, 0)
- err = testEngine.Find(&cps)
- if err != nil {
- t.Error(err)
- }
- if len(cps) != 1 {
- t.Error(errors.New("should has one record"))
- }
- if cps[0] != compositeKeyVal {
- t.Error(errors.New("should be equal"))
- }
- cnt, err = testEngine.Insert(&CompositeKey{22, 22, ""})
- if err != nil {
- t.Error(err)
- } else if cnt != 1 {
- t.Error(errors.New("failed to insert CompositeKey{22, 22}"))
- }
- cps = make([]CompositeKey, 0)
- err = testEngine.Find(&cps)
- assert.NoError(t, err)
- assert.EqualValues(t, 2, len(cps), "should has two record")
- assert.EqualValues(t, compositeKeyVal, cps[0], "should be equeal")
- compositeKeyVal = CompositeKey{UpdateStr: "test1"}
- cnt, err = testEngine.ID(core.PK{11, 22}).Update(&compositeKeyVal)
- if err != nil {
- t.Error(err)
- } else if cnt != 1 {
- t.Error(errors.New("can't update CompositeKey{11, 22}"))
- }
- cnt, err = testEngine.ID(core.PK{11, 22}).Delete(&CompositeKey{})
- if err != nil {
- t.Error(err)
- } else if cnt != 1 {
- t.Error(errors.New("can't delete CompositeKey{11, 22}"))
- }
- }
- func TestCompositeKey2(t *testing.T) {
- assert.NoError(t, prepareEngine())
- type User struct {
- UserId string `xorm:"varchar(19) not null pk"`
- NickName string `xorm:"varchar(19) not null"`
- GameId uint32 `xorm:"integer pk"`
- Score int32 `xorm:"integer"`
- }
- err := testEngine.DropTables(&User{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&User{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- cnt, err := testEngine.Insert(&User{"11", "nick", 22, 5})
- if err != nil {
- t.Error(err)
- } else if cnt != 1 {
- t.Error(errors.New("failed to insert User{11, 22}"))
- }
- cnt, err = testEngine.Insert(&User{"11", "nick", 22, 6})
- if err == nil || cnt == 1 {
- t.Error(errors.New("inserted User{11, 22}"))
- }
- var user User
- has, err := testEngine.ID(core.PK{"11", 22}).Get(&user)
- if err != nil {
- t.Error(err)
- } else if !has {
- t.Error(errors.New("can't get User{11, 22}"))
- }
- // test passing PK ptr, this test seem failed withCache
- has, err = testEngine.ID(&core.PK{"11", 22}).Get(&user)
- if err != nil {
- t.Error(err)
- } else if !has {
- t.Error(errors.New("can't get User{11, 22}"))
- }
- user = User{NickName: "test1"}
- cnt, err = testEngine.ID(core.PK{"11", 22}).Update(&user)
- if err != nil {
- t.Error(err)
- } else if cnt != 1 {
- t.Error(errors.New("can't update User{11, 22}"))
- }
- cnt, err = testEngine.ID(core.PK{"11", 22}).Delete(&User{})
- if err != nil {
- t.Error(err)
- } else if cnt != 1 {
- t.Error(errors.New("can't delete CompositeKey{11, 22}"))
- }
- }
- type MyString string
- type UserPK2 struct {
- UserId MyString `xorm:"varchar(19) not null pk"`
- NickName string `xorm:"varchar(19) not null"`
- GameId uint32 `xorm:"integer pk"`
- Score int32 `xorm:"integer"`
- }
- func TestCompositeKey3(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&UserPK2{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&UserPK2{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- cnt, err := testEngine.Insert(&UserPK2{"11", "nick", 22, 5})
- if err != nil {
- t.Error(err)
- } else if cnt != 1 {
- t.Error(errors.New("failed to insert User{11, 22}"))
- }
- cnt, err = testEngine.Insert(&UserPK2{"11", "nick", 22, 6})
- if err == nil || cnt == 1 {
- t.Error(errors.New("inserted User{11, 22}"))
- }
- var user UserPK2
- has, err := testEngine.ID(core.PK{"11", 22}).Get(&user)
- if err != nil {
- t.Error(err)
- } else if !has {
- t.Error(errors.New("can't get User{11, 22}"))
- }
- // test passing PK ptr, this test seem failed withCache
- has, err = testEngine.ID(&core.PK{"11", 22}).Get(&user)
- if err != nil {
- t.Error(err)
- } else if !has {
- t.Error(errors.New("can't get User{11, 22}"))
- }
- user = UserPK2{NickName: "test1"}
- cnt, err = testEngine.ID(core.PK{"11", 22}).Update(&user)
- if err != nil {
- t.Error(err)
- } else if cnt != 1 {
- t.Error(errors.New("can't update User{11, 22}"))
- }
- cnt, err = testEngine.ID(core.PK{"11", 22}).Delete(&UserPK2{})
- if err != nil {
- t.Error(err)
- } else if cnt != 1 {
- t.Error(errors.New("can't delete CompositeKey{11, 22}"))
- }
- }
- func TestMyIntId(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&MyIntPK{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&MyIntPK{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- idbean := &MyIntPK{Name: "test"}
- cnt, err := testEngine.Insert(idbean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- bean := new(MyIntPK)
- has, err := testEngine.Get(bean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if !has {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- if bean.ID != idbean.ID {
- panic(errors.New("should be equal"))
- }
- var beans []MyIntPK
- err = testEngine.Find(&beans)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- if *bean != beans[0] {
- panic(errors.New("should be equal"))
- }
- beans2 := make(map[ID]MyIntPK, 0)
- err = testEngine.Find(&beans2)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans2) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- if *bean != beans2[bean.ID] {
- panic(errors.New("should be equal"))
- }
- cnt, err = testEngine.ID(bean.ID).Delete(&MyIntPK{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- }
- func TestMyStringId(t *testing.T) {
- assert.NoError(t, prepareEngine())
- err := testEngine.DropTables(&MyStringPK{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- err = testEngine.CreateTables(&MyStringPK{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- idbean := &MyStringPK{ID: "1111", Name: "test"}
- cnt, err := testEngine.Insert(idbean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- bean := new(MyStringPK)
- has, err := testEngine.Get(bean)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if !has {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- if bean.ID != idbean.ID {
- panic(errors.New("should be equal"))
- }
- var beans []MyStringPK
- err = testEngine.Find(&beans)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- if *bean != beans[0] {
- panic(errors.New("should be equal"))
- }
- beans2 := make(map[StrID]MyStringPK, 0)
- err = testEngine.Find(&beans2)
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if len(beans2) != 1 {
- err = errors.New("get count should be one")
- t.Error(err)
- panic(err)
- }
- if *bean != beans2[bean.ID] {
- panic(errors.New("should be equal"))
- }
- cnt, err = testEngine.ID(bean.ID).Delete(&MyStringPK{})
- if err != nil {
- t.Error(err)
- panic(err)
- }
- if cnt != 1 {
- err = errors.New("insert count should be one")
- t.Error(err)
- panic(err)
- }
- }
- func TestSingleAutoIncrColumn(t *testing.T) {
- type Account struct {
- Id int64 `xorm:"pk autoincr"`
- }
- assert.NoError(t, prepareEngine())
- assertSync(t, new(Account))
- _, err := testEngine.Insert(&Account{})
- assert.NoError(t, err)
- }
- func TestCompositePK(t *testing.T) {
- type TaskSolution struct {
- UID string `xorm:"notnull pk UUID 'uid'"`
- TID string `xorm:"notnull pk UUID 'tid'"`
- Created time.Time `xorm:"created"`
- Updated time.Time `xorm:"updated"`
- }
- assert.NoError(t, prepareEngine())
- tables1, err := testEngine.DBMetas()
- assert.NoError(t, err)
- assertSync(t, new(TaskSolution))
- assert.NoError(t, testEngine.Sync2(new(TaskSolution)))
- tables2, err := testEngine.DBMetas()
- assert.NoError(t, err)
- assert.EqualValues(t, 1+len(tables1), len(tables2))
- var table *core.Table
- for _, t := range tables2 {
- if t.Name == testEngine.GetTableMapper().Obj2Table("TaskSolution") {
- table = t
- break
- }
- }
- assert.NotEqual(t, nil, table)
- pkCols := table.PKColumns()
- assert.EqualValues(t, 2, len(pkCols))
- assert.EqualValues(t, "uid", pkCols[0].Name)
- assert.EqualValues(t, "tid", pkCols[1].Name)
- }
- func TestNoPKIdQueryUpdate(t *testing.T) {
- type NoPKTable struct {
- Username string
- }
- assert.NoError(t, prepareEngine())
- assertSync(t, new(NoPKTable))
- cnt, err := testEngine.Insert(&NoPKTable{
- Username: "test",
- })
- assert.NoError(t, err)
- assert.EqualValues(t, 1, cnt)
- var res NoPKTable
- has, err := testEngine.ID("test").Get(&res)
- assert.Error(t, err)
- assert.False(t, has)
- cnt, err = testEngine.ID("test").Update(&NoPKTable{
- Username: "test1",
- })
- assert.Error(t, err)
- assert.EqualValues(t, 0, cnt)
- type UnvalidPKTable struct {
- ID int `xorm:"id"`
- Username string
- }
- assertSync(t, new(UnvalidPKTable))
- cnt, err = testEngine.Insert(&UnvalidPKTable{
- ID: 1,
- Username: "test",
- })
- assert.NoError(t, err)
- assert.EqualValues(t, 1, cnt)
- var res2 UnvalidPKTable
- has, err = testEngine.ID(1).Get(&res2)
- assert.Error(t, err)
- assert.False(t, has)
- cnt, err = testEngine.ID(1).Update(&UnvalidPKTable{
- Username: "test1",
- })
- assert.Error(t, err)
- assert.EqualValues(t, 0, cnt)
- }
|