func TestSaveAndFind(t *testing.T) { orm := getOrm() randomString := toolkit.RandomString(5) u := new(user) u.Name = randomString err := orm.Fork().Save(u) if err != nil { t.Fatal(err) } if u.Id < 1 { t.Fatal("new id ", u.Id) } newu := new(user) newu.Id = u.Id newerr, exists := orm.Fork().Find(newu) if newerr != nil { t.Fatal(newerr) } if !exists { t.Fatal("save but find ", newu.Id, u.Id) } if newu.Name != randomString { t.Fatal("find diff value") } }
func TestInsertIgnore(t *testing.T) { orm := getOrm() ud := new(UserDetail) ud.UserId = 1 ud.Address = toolkit.RandomString(5) err := orm.Fork().Save(ud) if err != nil { t.Fatal("insert ignore error", err) } }
func TestEx(t *testing.T) { cache := getFileCache() category := "app/info" key := toolkit.RandomString(10) defer cache.Del(category, key) t1 := test_cache{} t1.Id = toolkit.RandInt(0, 10) t1.Name = toolkit.RandomString(10) err := cache.SetEx(category, key, t1) if err != nil { t.Fatal(err) } t2 := test_cache{} err, _ = cache.GetEx(category, key, &t2) if err != nil { t.Fatal(err) } if t1.Id != t2.Id || t2.Name != t2.Name { t.Fatal("error ex") } }
func TestBaseFileCache(t *testing.T) { cache := getFileCache() category := "app/install" key := toolkit.RandomString(5) data := []byte("123") cache.Set(category, key, data) cacheData, err, exists := cache.Get(category, key) if err != nil || !exists { t.Fatal("error cache", err, exists) } if string(cacheData) != string(data) { t.Fatal("error data") } }
func TestSaveTransaction(t *testing.T) { orm := getOrm() u := new(user) //new randomString := toolkit.RandomString(5) u.Name = randomString tx, _ := orm.Transaction() err := tx.TSave(u) if err != nil { t.Fatal(err) } if u.Id < 1 { tx.TRollback() t.Fatal("new id ", u.Id) } else { tx.TCommit() } //check if !checkUserIdAndName(u.Id, randomString) { t.Fatal("tsave error") } else { t.Log("tsave ok") } fmt.Println("after tnew ", u) //update randomString = "uuuuu" tx, _ = orm.Transaction() u.Name = randomString err = tx.TSave(u) fmt.Println("after tupdate ", u) if err != nil { t.Fatal("transaction error ", err) } else { tx.TCommit() } if !checkUserIdAndName(u.Id, randomString) { tx.TRollback() t.Fatal("tupdate error") } else { t.Log("tupdate ok") } }
func TestDelFileCache(t *testing.T) { cache := getFileCache() category := "app/info" key := toolkit.RandomString(10) data := []byte("123") cache.Set(category, key, data) _, _, exists := cache.Get(category, key) if !exists { t.Fatal("error get/set") } cache.Del(category, key) _, _, exists = cache.Get(category, key) if exists { t.Fatal("error del") } }
func TestTransactionInGoroutine(t *testing.T) { orm := getOrm() ch := make(chan int) tx, _ := orm.Transaction() re, _ := tx.TExec("update user_detail set address = ? limit 1", toolkit.RandomString(5)) fmt.Println("main execed") go func(db *Orm, ch chan int, t *testing.T) { time.Sleep(2 * time.Second) tx, _ := db.Transaction() re, _ := tx.TExec("update user set timestamp = now()") fmt.Println("goroutine execed") ch <- 1 time.Sleep(2 * time.Second) d, _ := re.RowsAffected() fmt.Println("goroutine affected:", d) if d <= 1 { t.Fatal("dirty data in go routine") } tx.TCommit() ch <- 1 }(orm, ch, t) select { case <-ch: d, _ := re.RowsAffected() fmt.Println("main affected:", d) if d != 1 { t.Fatal("dirty data in main ") } tx.TCommit() } select { case <-ch: t.Log("Complete Transaction in goroutine") } }
func TestPage(t *testing.T) { orm := getOrm() u := new(user) insert_count := toolkit.RandInt(5, 10) t.Log("insert records", insert_count) for i := 0; i < insert_count; i++ { u := new(user) u.Name = toolkit.RandomString(5) err := orm.Fork().Save(u) FatalError(t, err) } count, err := orm.Fork().Count(u) if count < 1 || err != nil { t.Fatal("can not test page because count error :", count, err) } page := 0 num := 5 total_page := count / num if count%num != 0 { total_page += 1 } for ; page < total_page; page++ { page_err, users := orm.Fork().Page(page, num).All(u) if page_err != nil { t.Fatal("page error:", page, num, page_err) } for _, tmp := range users { t.Log(tmp.(*user)) } t.Log(page, "end") } }