func testLowerCase(engine *xorm.Engine, t *testing.T) { err := engine.Sync(&Lowercase{}) _, err = engine.Where("(id) > 0").Delete(&Lowercase{}) if err != nil { t.Error(err) panic(err) } _, err = engine.Insert(&Lowercase{ended: 1}) if err != nil { t.Error(err) panic(err) } ls := make([]Lowercase, 0) err = engine.Find(&ls) if err != nil { t.Error(err) panic(err) } if len(ls) != 1 { err = errors.New("should be 1") t.Error(err) panic(err) } }
func testCreatedJsonTime(engine *xorm.Engine, t *testing.T) { di5 := new(MyJsonTime) err := engine.Sync2(di5) if err != nil { t.Fatal(err) } ci5 := &MyJsonTime{} _, err = engine.Insert(ci5) if err != nil { t.Fatal(err) } has, err := engine.Desc("(id)").Get(di5) if err != nil { t.Fatal(err) } if !has { t.Fatal(xorm.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 = engine.Find(&dis) if err != nil { t.Fatal(err) } }
func test(engine *xorm.Engine) { err := engine.CreateTables(u) if err != nil { fmt.Println(err) return } engine.ShowSQL = true engine.SetMaxOpenConns(5) size := 1000 queue := make(chan int, size) for i := 0; i < size; i++ { go func(x int) { //x := i err := engine.Ping() if err != nil { fmt.Println(err) } else { /*err = engine.Map(u) if err != nil { fmt.Println("Map user failed") } else {*/ for j := 0; j < 10; j++ { if x+j < 2 { _, err = engine.Get(u) } else if x+j < 4 { users := make([]User, 0) err = engine.Find(&users) } else if x+j < 8 { _, err = engine.Count(u) } else if x+j < 16 { _, err = engine.Insert(&User{Name: "xlw"}) } else if x+j < 32 { _, err = engine.Id(1).Delete(u) } if err != nil { fmt.Println(err) queue <- x return } } fmt.Printf("%v success!\n", x) //} } queue <- x }(i) } for i := 0; i < size; i++ { <-queue } fmt.Println("end") }
func test(engine *xorm.Engine) { err := engine.CreateTables(u) if err != nil { fmt.Println(err) return } size := 100 queue := make(chan int, size) for i := 0; i < size; i++ { go func(x int) { //x := i err := engine.Ping() if err != nil { fmt.Println(err) } else { /*err = engine.(u) if err != nil { fmt.Println("Map user failed") } else {*/ for j := 0; j < 10; j++ { if x+j < 2 { _, err = engine.Get(u) } else if x+j < 4 { users := make([]User, 0) err = engine.Find(&users) } else if x+j < 8 { _, err = engine.Count(u) } else if x+j < 16 { _, err = engine.Insert(&User{Name: "xlw"}) } else if x+j < 32 { _, err = engine.Id(1).Delete(u) } if err != nil { fmt.Println(err) queue <- x return } } fmt.Printf("%v success!\n", x) //} } queue <- x }(i) } for i := 0; i < size; i++ { <-queue } //conns := atomic.LoadInt32(&xorm.ConnectionNum) //fmt.Println("connection number:", conns) fmt.Println("end") }
func testJsonField(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&JsonField{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&JsonField{}) if err != nil { t.Error(err) panic(err) } js := &JsonField{ Name: map[string]string{ "test": "test", "test2": "test2", }, } _, err = engine.Insert(js) if err != nil { t.Error(err) panic(err) } var j JsonField has, err := engine.Id(js.Id).Get(&j) if err != nil { t.Error(err) panic(err) } fmt.Println("j:", j) if !has { err = errors.New("not exist") t.Error(err) panic(err) } var jss = make([]JsonField, 0) err = engine.Find(&jss) if err != nil { t.Error(err) panic(err) } if len(jss) == 0 { err = errors.New("not exist") t.Error(err) panic(err) } fmt.Println("jss:", jss) }
func findMap2(engine *xorm.Engine, t *testing.T) { users := make(map[int64]*Userinfo) err := engine.Find(&users) if err != nil { t.Error(err) panic(err) } for id, user := range users { fmt.Println(id, user) } }
func find2(engine *xorm.Engine, t *testing.T) { users := make([]*Userinfo, 0) err := engine.Find(&users) if err != nil { t.Error(err) panic(err) } for _, user := range users { fmt.Println(user) } }
func testTime(engine *xorm.Engine, t *testing.T) { err := engine.Sync(&TTime{}) if err != nil { t.Error(err) panic(err) } tt := &TTime{} println("b4 Insert tt:", tt.String()) _, err = engine.Insert(tt) println("after Insert tt:", tt.String()) if err != nil { t.Error(err) panic(err) } tt2 := &TTime{Id: tt.Id} println("b4 Get tt2:", tt2.String()) has, err := engine.Get(tt2) println("after Get tt2:", tt2.String()) if err != nil { t.Error(err) panic(err) } if !has { err = errors.New("no record error") t.Error(err) panic(err) } tt3 := &TTime{T: time.Now(), Tz: time.Now()} println("b4 Insert tt3:", tt3.String()) _, err = engine.Insert(tt3) println("after Insert tt3:", tt3.String()) if err != nil { t.Error(err) panic(err) } tt4s := make([]TTime, 0) println("b4 Insert tt4s:", tt4s) err = engine.Find(&tt4s) println("after Insert tt4s:", tt4s) if err != nil { t.Error(err) panic(err) } fmt.Println("=======\n", tt4s, "=======\n") }
func find(engine *xorm.Engine, t *testing.T) { users := make([]Userinfo, 0) err := engine.Find(&users) if err != nil { t.Error(err) panic(err) } for _, user := range users { fmt.Println(user) } users2 := make([]Userinfo, 0) userinfo := engine.TableMapper.Obj2Table("Userinfo") err = engine.Sql("select * from " + engine.Quote(userinfo)).Find(&users2) if err != nil { t.Error(err) panic(err) } }
func testBool(engine *xorm.Engine, t *testing.T) { _, err := engine.UseBool().Update(&Userinfo{IsMan: true}) if err != nil { t.Error(err) panic(err) } users := make([]Userinfo, 0) err = engine.Find(&users) if err != nil { t.Error(err) panic(err) } for _, user := range users { if !user.IsMan { err = errors.New("update bool or find bool error") t.Error(err) panic(err) } } _, err = engine.UseBool().Update(&Userinfo{IsMan: false}) if err != nil { t.Error(err) panic(err) } users = make([]Userinfo, 0) err = engine.Find(&users) if err != nil { t.Error(err) panic(err) } for _, user := range users { if user.IsMan { err = errors.New("update bool or find bool error") t.Error(err) panic(err) } } }
func testUseBool(engine *xorm.Engine, t *testing.T) { cnt1, err := engine.Count(&Userinfo{}) if err != nil { t.Error(err) panic(err) } users := make([]Userinfo, 0) err = engine.Find(&users) if err != nil { t.Error(err) panic(err) } var fNumber int64 for _, u := range users { if u.IsMan == false { fNumber += 1 } } cnt2, err := engine.UseBool().Update(&Userinfo{IsMan: true}) if err != nil { t.Error(err) panic(err) } if fNumber != cnt2 { fmt.Println("cnt1", cnt1, "fNumber", fNumber, "cnt2", cnt2) /*err = errors.New("Updated number is not corrected.") t.Error(err) panic(err)*/ } _, err = engine.Update(&Userinfo{IsMan: true}) if err == nil { err = errors.New("error condition") t.Error(err) panic(err) } }
func testStrangeName(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(new(StrangeName)) if err != nil { t.Error(err) } err = engine.CreateTables(new(StrangeName)) if err != nil { t.Error(err) } _, err = engine.Insert(&StrangeName{Name: "sfsfdsfds"}) if err != nil { t.Error(err) } beans := make([]StrangeName, 0) err = engine.Find(&beans) if err != nil { t.Error(err) } }
func testUintId(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&UintId{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&UintId{}) if err != nil { t.Error(err) panic(err) } cnt, err := engine.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 = engine.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 := engine.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 = engine.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 = engine.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 = engine.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 testStringPK(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&StringPK{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&StringPK{}) if err != nil { t.Error(err) panic(err) } cnt, err := engine.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 := engine.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 = engine.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 = engine.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 = engine.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) } }
func testExtends(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&tempUser2{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&tempUser2{}) if err != nil { t.Error(err) panic(err) } tu := &tempUser2{tempUser{0, "extends"}, "dev depart"} _, err = engine.Insert(tu) if err != nil { t.Error(err) panic(err) } tu2 := &tempUser2{} _, err = engine.Get(tu2) if err != nil { t.Error(err) panic(err) } tu3 := &tempUser2{tempUser{0, "extends update"}, ""} _, err = engine.Id(tu2.Id).Update(tu3) if err != nil { t.Error(err) panic(err) } err = engine.DropTables(&tempUser3{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&tempUser3{}) if err != nil { t.Error(err) panic(err) } tu4 := &tempUser3{&tempUser{0, "extends"}, "dev depart"} _, err = engine.Insert(tu4) if err != nil { t.Error(err) panic(err) } tu5 := &tempUser3{} _, err = engine.Get(tu5) if err != nil { t.Error(err) panic(err) } if tu5.Temp == nil { err = errors.New("error get data extends") t.Error(err) panic(err) } if tu5.Temp.Id != 1 || tu5.Temp.Username != "extends" || tu5.Departname != "dev depart" { err = errors.New("error get data extends") t.Error(err) panic(err) } tu6 := &tempUser3{&tempUser{0, "extends update"}, ""} _, err = engine.Id(tu5.Temp.Id).Update(tu6) if err != nil { t.Error(err) panic(err) } users := make([]tempUser3, 0) err = engine.Find(&users) if err != nil { t.Error(err) panic(err) } if len(users) != 1 { err = errors.New("error get data not 1") t.Error(err) panic(err) } var info UserAndDetail qt := engine.Quote engine.Update(&Userinfo{Detail: Userdetail{Id: 1}}) ui := engine.TableMapper.Obj2Table("Userinfo") ud := engine.TableMapper.Obj2Table("Userdetail") uiid := engine.TableMapper.Obj2Table("Id") udid := "detail_id" sql := fmt.Sprintf("select * from %s, %s where %s.%s = %s.%s", qt(ui), qt(ud), qt(ui), qt(udid), qt(ud), qt(uiid)) b, err := engine.Sql(sql).Get(&info) if err != nil { t.Error(err) panic(err) } if !b { err = errors.New("should has lest one record") t.Error(err) panic(err) } if info.Userinfo.Uid == 0 || info.Userdetail.Id == 0 { err = errors.New("all of the id should has value") t.Error(err) panic(err) } fmt.Println(info) fmt.Println("----join--info2") var info2 UserAndDetail b, err = engine.Table(&Userinfo{}).Join("LEFT", qt(ud), qt(ui)+"."+qt("detail_id")+" = "+qt(ud)+"."+qt(uiid)).Get(&info2) if err != nil { t.Error(err) panic(err) } if !b { err = errors.New("should has lest one record") t.Error(err) panic(err) } if info2.Userinfo.Uid == 0 || info2.Userdetail.Id == 0 { err = errors.New("all of the id should has value") t.Error(err) panic(err) } fmt.Println(info2) fmt.Println("----join--infos2") var infos2 = make([]UserAndDetail, 0) err = engine.Table(&Userinfo{}).Join("LEFT", qt(ud), qt(ui)+"."+qt("detail_id")+" = "+qt(ud)+"."+qt(uiid)).Find(&infos2) if err != nil { t.Error(err) panic(err) } fmt.Println(infos2) }
func testUint64Id(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&Uint64Id{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&Uint64Id{}) if err != nil { t.Error(err) panic(err) } idbean := &Uint64Id{Name: "test"} cnt, err := engine.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 := engine.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 = engine.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 = engine.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 = engine.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 testIntId(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&IntId{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&IntId{}) if err != nil { t.Error(err) panic(err) } cnt, err := engine.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 := engine.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 = engine.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 = engine.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 = engine.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 testCustomType1(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&MyStruct{}) if err != nil { t.Error(err) panic(err) return } err = engine.CreateTables(&MyStruct{}) 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 := engine.Insert(&i) if err != nil { t.Error(err) panic(err) return } if cnt != 1 { err = errors.New("insert not returned 1") t.Error(err) panic(err) return } fmt.Println(i) i.NameArray = []string{} i.MSS = map[string]string{} i.F = 0 has, err := engine.Get(&i) if err != nil { t.Error(err) panic(err) } else if !has { t.Error(errors.New("should get one record")) panic(err) } ss := []MyStruct{} err = engine.Find(&ss) if err != nil { t.Error(err) panic(err) } fmt.Println(ss) sss := MyStruct{} has, err = engine.Get(&sss) if err != nil { t.Error(err) panic(err) } fmt.Println(sss) if has { sss.NameArray = []string{} sss.MSS = map[string]string{} cnt, err := engine.Delete(&sss) if err != nil { t.Error(err) panic(err) } if cnt != 1 { t.Error(errors.New("delete error")) panic(err) } } }
func testCompositeKey(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&CompositeKey{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&CompositeKey{}) if err != nil { t.Error(err) panic(err) } cnt, err := engine.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 = engine.Insert(&CompositeKey{11, 22, ""}) if err == nil || cnt == 1 { t.Error(errors.New("inserted CompositeKey{11, 22}")) } var compositeKeyVal CompositeKey has, err := engine.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 = engine.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 = engine.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 = engine.Insert(&CompositeKey{22, 22, ""}) if err != nil { t.Error(err) } else if cnt != 1 { t.Error(errors.New("failed to insert CompositeKey{22, 22}")) } if engine.Cacher != nil { engine.Cacher.ClearBeans(engine.TableInfo(compositeKeyVal).Name) } cps = make([]CompositeKey, 0) err = engine.Find(&cps) if err != nil { t.Error(err) } if len(cps) != 2 { t.Error(errors.New("should has two record")) } if cps[0] != compositeKeyVal { t.Error(errors.New("should be equeal")) } compositeKeyVal = CompositeKey{UpdateStr: "test1"} cnt, err = engine.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 = engine.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 testJsonField(engine *xorm.Engine, t *testing.T) { err := engine.DropTables(&JsonField{}) if err != nil { t.Error(err) panic(err) } err = engine.CreateTables(&JsonField{}) if err != nil { t.Error(err) panic(err) } js := &JsonField{ Name: map[string]string{ "test": "test", "test2": "test2", }, Indexes: []int{1, 3, 5}, Profile: JsonProfile{ Name: "lll", Age: 12, }, } _, err = engine.Insert(js) if err != nil { t.Error(err) panic(err) } var j JsonField has, err := engine.Id(js.Id).Get(&j) if err != nil { t.Error(err) panic(err) } fmt.Println("j:", j) if !has { err = errors.New("not exist") t.Error(err) panic(err) } if j.Profile.Name != "lll" || j.Profile.Age != 12 { err = errors.New("json unmarshal error") t.Error(err) panic(err) } var jss = make([]JsonField, 0) err = engine.Find(&jss) if err != nil { t.Error(err) panic(err) } if len(jss) == 0 { err = errors.New("not exist") t.Error(err) panic(err) } fmt.Println("jss:", jss) }