gorm/gorm_test.go

619 lines
17 KiB
Go
Raw Normal View History

2013-10-25 18:31:56 +04:00
package gorm
2013-10-26 20:36:56 +04:00
import (
2013-10-27 12:06:45 +04:00
"errors"
2013-10-28 16:27:25 +04:00
_ "github.com/lib/pq"
2013-10-27 11:24:01 +04:00
"reflect"
2013-10-27 16:54:23 +04:00
"strconv"
2013-10-28 16:27:25 +04:00
2013-10-26 20:36:56 +04:00
"testing"
"time"
)
2013-10-25 18:31:56 +04:00
2013-10-26 03:14:57 +04:00
type User struct {
2013-10-27 10:34:34 +04:00
Id int64
Age int64
Birthday time.Time
Name string
CreatedAt time.Time
UpdatedAt time.Time
2013-10-26 17:05:54 +04:00
}
2013-10-27 11:24:01 +04:00
type Product struct {
Id int64
Code string
Price int64
CreatedAt time.Time
UpdatedAt time.Time
BeforeCreateCallTimes int64
AfterCreateCallTimes int64
BeforeUpdateCallTimes int64
AfterUpdateCallTimes int64
BeforeSaveCallTimes int64
AfterSaveCallTimes int64
2013-10-27 12:06:45 +04:00
BeforeDeleteCallTimes int64
AfterDeleteCallTimes int64
2013-10-27 11:24:01 +04:00
}
2013-10-27 03:38:08 +04:00
var (
db DB
t1, t2, t3, t4, t5 time.Time
)
2013-10-26 17:05:54 +04:00
func init() {
db, _ = Open("postgres", "user=gorm dbname=gorm sslmode=disable")
2013-10-28 06:09:44 +04:00
db.SetPool(10)
2013-10-26 17:05:54 +04:00
db.Exec("drop table users;")
2013-10-27 11:24:01 +04:00
db.Exec("drop table products;")
2013-10-26 03:14:57 +04:00
2013-10-26 17:05:54 +04:00
orm := db.CreateTable(&User{})
if orm.Error != nil {
2013-10-27 03:38:08 +04:00
panic("No error should raise when create table")
2013-10-26 17:05:54 +04:00
}
2013-10-27 15:41:58 +04:00
2013-10-27 11:24:01 +04:00
db.CreateTable(&Product{})
2013-10-27 03:38:08 +04:00
var shortForm = "2006-01-02 15:04:05"
t1, _ = time.Parse(shortForm, "2000-10-27 12:02:40")
t2, _ = time.Parse(shortForm, "2002-01-01 00:00:00")
t3, _ = time.Parse(shortForm, "2005-01-01 00:00:00")
t4, _ = time.Parse(shortForm, "2010-01-01 00:00:00")
t5, _ = time.Parse(shortForm, "2020-01-01 00:00:00")
db.Save(&User{Name: "1", Age: 18, Birthday: t1})
db.Save(&User{Name: "2", Age: 20, Birthday: t2})
db.Save(&User{Name: "3", Age: 22, Birthday: t3})
db.Save(&User{Name: "3", Age: 24, Birthday: t4})
db.Save(&User{Name: "5", Age: 26, Birthday: t4})
2013-10-26 05:49:40 +04:00
}
2013-10-27 17:37:31 +04:00
func TestInitlineCondition(t *testing.T) {
2013-10-27 16:54:23 +04:00
var u1, u2, u3, u4, u5, u6, u7 User
2013-10-27 16:07:13 +04:00
db.Where("name = ?", "3").Order("age desc").First(&u1).First(&u2)
2013-10-27 16:54:23 +04:00
db.Where("name = ?", "3").First(&u3, "age = 22").First(&u4, "age = ?", 24).First(&u5, "age = ?", 26)
if !((u5.Id == 0) && (u3.Age == 22 && u3.Name == "3") && (u4.Age == 24 && u4.Name == "3")) {
t.Errorf("Inline where condition for first when search")
}
var us1, us2, us3, us4 []User
db.Find(&us1, "age = 22").Find(&us2, "name = ?", "3").Find(&us3, "age > ?", 20)
if !(len(us1) == 1 && len(us2) == 2 && len(us3) == 3) {
t.Errorf("Inline where condition for find when search")
}
db.Find(&us4, "name = ? and age > ?", "3", "22")
if len(us4) != 1 {
t.Errorf("More complex inline where condition for find, %v", us4)
}
db.First(&u6, u1.Id)
if !(u6.Id == u1.Id && u6.Id != 0) {
t.Errorf("Should find out user with int id")
}
db.First(&u7, strconv.Itoa(int(u1.Id)))
if !(u6.Id == u1.Id && u6.Id != 0) {
t.Errorf("Should find out user with string id")
}
2013-10-27 12:47:02 +04:00
}
2013-10-26 17:05:54 +04:00
func TestSaveAndFind(t *testing.T) {
name := "save_and_find"
2013-10-27 06:28:47 +04:00
u := &User{Name: name, Age: 1}
2013-10-26 06:15:09 +04:00
db.Save(u)
2013-10-26 16:20:49 +04:00
if u.Id == 0 {
t.Errorf("Should have ID after create record")
}
2013-10-26 05:49:40 +04:00
user := &User{}
2013-10-27 18:18:06 +04:00
db.First(user, "name = ?", name)
2013-10-26 17:05:54 +04:00
if user.Name != name {
2013-10-26 05:49:40 +04:00
t.Errorf("User should be saved and fetched correctly")
2013-10-25 18:31:56 +04:00
}
2013-10-26 08:33:05 +04:00
users := []User{}
db.Find(&users)
2013-10-26 05:49:40 +04:00
}
2013-10-26 03:14:57 +04:00
2013-10-26 17:37:42 +04:00
func TestUpdate(t *testing.T) {
2013-10-26 18:00:38 +04:00
name, name2, new_name := "update", "update2", "new_update"
2013-10-27 06:28:47 +04:00
user := User{Name: name, Age: 1}
2013-10-26 17:37:42 +04:00
db.Save(&user)
2013-10-27 06:28:47 +04:00
db.Save(&User{Name: name2, Age: 1})
2013-10-26 17:37:42 +04:00
2013-10-26 18:00:38 +04:00
if user.Id == 0 {
2013-10-26 17:37:42 +04:00
t.Errorf("User Id should exist after create")
}
2013-10-26 18:00:38 +04:00
user.Name = new_name
2013-10-26 17:37:42 +04:00
db.Save(&user)
2013-10-26 18:00:38 +04:00
orm := db.Where("name = ?", name).First(&User{})
2013-10-26 17:37:42 +04:00
if orm.Error == nil {
t.Errorf("Should raise error when looking for a existing user with an outdated name")
}
2013-10-26 18:00:38 +04:00
orm = db.Where("name = ?", new_name).First(&User{})
2013-10-26 17:37:42 +04:00
if orm.Error != nil {
t.Errorf("Shouldn't raise error when looking for a existing user with the new name")
}
2013-10-26 18:00:38 +04:00
orm = db.Where("name = ?", name2).First(&User{})
if orm.Error != nil {
t.Errorf("Shouldn't update other users")
}
2013-10-26 17:37:42 +04:00
}
2013-10-26 17:51:44 +04:00
func TestDelete(t *testing.T) {
name, name2 := "delete", "delete2"
2013-10-27 06:28:47 +04:00
user := User{Name: name, Age: 1}
2013-10-26 17:51:44 +04:00
db.Save(&user)
2013-10-27 06:28:47 +04:00
db.Save(&User{Name: name2, Age: 1})
2013-10-26 17:51:44 +04:00
orm := db.Delete(&user)
orm = db.Where("name = ?", name).First(&User{})
if orm.Error == nil {
t.Errorf("User should be deleted successfully")
}
orm = db.Where("name = ?", name2).First(&User{})
if orm.Error != nil {
t.Errorf("User2 should not be deleted")
}
}
2013-10-26 05:49:40 +04:00
func TestWhere(t *testing.T) {
2013-10-26 17:05:54 +04:00
name := "where"
2013-10-27 06:28:47 +04:00
db.Save(&User{Name: name, Age: 1})
2013-10-26 10:10:47 +04:00
2013-10-26 03:14:57 +04:00
user := &User{}
2013-10-28 05:05:44 +04:00
db.Where("name = ?", name).First(user)
2013-10-26 17:05:54 +04:00
if user.Name != name {
t.Errorf("Should found out user with name '%v'", name)
}
2013-10-26 10:10:47 +04:00
user = &User{}
2013-10-28 05:05:44 +04:00
orm := db.Where("name LIKE ?", "%nonono%").First(user)
if orm.Error == nil {
t.Errorf("Should return error when searching for none existing record, %+v", user)
}
user = &User{}
orm = db.Where("name LIKE ?", "%whe%").First(user)
if orm.Error != nil {
t.Errorf("Should not return error when searching for existing record, %+v", user)
}
user = &User{}
orm = db.Where("name = ?", "noexisting-user").First(user)
if orm.Error == nil {
2013-10-26 17:05:54 +04:00
t.Errorf("Should return error when looking for none existing record, %+v", user)
}
2013-10-26 17:05:54 +04:00
users := []User{}
2013-10-28 05:05:44 +04:00
orm = db.Where("name = ?", "none-noexisting").Find(&users)
if orm.Error != nil {
2013-10-26 17:05:54 +04:00
t.Errorf("Shouldn't return error when looking for none existing records, %+v", users)
}
2013-10-26 17:05:54 +04:00
if len(users) != 0 {
t.Errorf("Shouldn't find anything when looking for none existing records, %+v", users)
2013-10-26 13:28:52 +04:00
}
}
2013-10-26 20:36:56 +04:00
func TestComplexWhere(t *testing.T) {
var users []User
db.Where("age > ?", 20).Find(&users)
2013-10-27 03:38:08 +04:00
if len(users) != 3 {
t.Errorf("Should only found 3 users that age > 20, but have %v", len(users))
2013-10-26 21:02:57 +04:00
}
users = []User{}
db.Where("age >= ?", 20).Find(&users)
2013-10-27 03:38:08 +04:00
if len(users) != 4 {
t.Errorf("Should only found 4 users that age >= 20, but have %v", len(users))
2013-10-26 21:02:57 +04:00
}
users = []User{}
db.Where("age = ?", 20).Find(&users)
if len(users) != 1 {
t.Errorf("Should only found 1 users age == 20, but have %v", len(users))
}
users = []User{}
db.Where("age <> ?", 20).Find(&users)
if len(users) < 3 {
t.Errorf("Should have more than 3 users age != 20, but have %v", len(users))
}
users = []User{}
db.Where("name = ? and age >= ?", "3", 20).Find(&users)
if len(users) != 2 {
t.Errorf("Should only found 2 users that age >= 20 with name 3, but have %v", len(users))
}
users = []User{}
db.Where("name = ?", "3").Where("age >= ?", 20).Find(&users)
if len(users) != 2 {
t.Errorf("Should only found 2 users that age >= 20 with name 3, but have %v", len(users))
2013-10-26 20:36:56 +04:00
}
2013-10-26 21:41:29 +04:00
users = []User{}
db.Where("birthday > ?", t2).Find(&users)
2013-10-27 03:38:08 +04:00
if len(users) != 3 {
t.Errorf("Should only found 3 users's birthday >= t2", len(users))
2013-10-26 21:41:29 +04:00
}
users = []User{}
db.Where("birthday >= ?", t1).Where("birthday < ?", t2).Find(&users)
if len(users) != 1 {
t.Errorf("Should only found 1 users's birthday <= t2, but have %v", len(users))
}
users = []User{}
db.Where("birthday >= ? and birthday <= ?", t1, t2).Find(&users)
if len(users) != 2 {
t.Errorf("Should only found 2 users's birthday <= t2, but have %v", len(users))
}
2013-10-26 22:31:38 +04:00
users = []User{}
db.Where("name in (?)", []string{"1", "3"}).Find(&users)
2013-10-26 22:31:38 +04:00
if len(users) != 3 {
t.Errorf("Should only found 3 users's name in (1, 3), but have %v", len(users))
}
var user_ids []int64
for _, user := range users {
user_ids = append(user_ids, user.Id)
}
users = []User{}
db.Where("id in (?)", user_ids).Find(&users)
if len(users) != 3 {
t.Errorf("Should only found 3 users's name in (1, 3) - search by id, but have %v", len(users))
}
users = []User{}
db.Where("name in (?)", []string{"1", "2"}).Find(&users)
if len(users) != 2 {
t.Errorf("Should only found 2 users's name in (1, 2), but have %v", len(users))
}
user_ids = []int64{}
for _, user := range users {
user_ids = append(user_ids, user.Id)
}
users = []User{}
db.Where("id in (?)", user_ids).Find(&users)
if len(users) != 2 {
t.Errorf("Should only found 2 users's name in (1, 2) - search by id, but have %v", len(users))
}
users = []User{}
db.Where("id in (?)", user_ids[0]).Find(&users)
if len(users) != 1 {
t.Errorf("Should only found 1 users's name in (1, 2) - search by the first id, but have %v", len(users))
}
2013-10-26 20:36:56 +04:00
}
2013-10-27 03:38:08 +04:00
2013-10-27 05:50:11 +04:00
func TestSelect(t *testing.T) {
var user User
db.Where("name = ?", "3").Select("name").Find(&user)
if user.Id != 0 {
t.Errorf("Should not got ID because I am only looking for age, %+v", user.Id)
}
if user.Name != "3" {
t.Errorf("Should got Name = 3 when searching it, %+v", user.Id)
}
query := db.Where("name = ?", "3").Select("nam;e")
if query.Error == nil {
t.Errorf("Should got error with invalid select string")
}
2013-10-27 05:50:11 +04:00
}
2013-10-27 07:21:33 +04:00
func TestOrderAndPluck(t *testing.T) {
2013-10-27 05:32:49 +04:00
var ages []int64
db.Model(&[]User{}).Order("age desc").Pluck("age", &ages)
2013-10-27 07:21:33 +04:00
if ages[0] != 26 {
t.Errorf("The first age should be 26 because of ordered by")
}
2013-10-28 05:18:34 +04:00
var ages1, ages2 []int64
db.Model(&[]User{}).Order("age desc").Pluck("age", &ages1).Order("age").Pluck("age", &ages2)
if !reflect.DeepEqual(ages1, ages2) {
t.Errorf("The first order is the primary order")
}
var ages3, ages4 []int64
db.Model(&[]User{}).Order("age desc").Pluck("age", &ages3).Order("age", true).Pluck("age", &ages4)
if reflect.DeepEqual(ages3, ages4) {
t.Errorf("Reorder should works well")
}
2013-10-27 07:21:33 +04:00
ages = []int64{}
var names []string
db.Model(&User{}).Order("name").Order("age desc").Pluck("age", &ages).Pluck("name", &names)
if !(names[0] == "1" && names[2] == "3" && names[3] == "3" && ages[2] == 24 && ages[3] == 22) {
t.Errorf("Should be ordered correctly with multiple orders")
}
2013-10-27 03:38:08 +04:00
}
2013-10-27 07:38:05 +04:00
func TestLimit(t *testing.T) {
var users1, users2, users3 []User
db.Order("age desc").Limit(3).Find(&users1).Limit(5).Find(&users2).Limit(-1).Find(&users3)
if !(len(users1) == 3 && len(users2) == 5 && len(users3) > 5) {
t.Errorf("Limit should works perfectly")
}
}
2013-10-27 07:44:47 +04:00
func TestOffset(t *testing.T) {
var users1, users2, users3, users4 []User
db.Order("age desc").Find(&users1).Offset(3).Find(&users2).Offset(5).Find(&users3).Offset(-1).Find(&users4)
if !((len(users1) == len(users4)) && (len(users1)-len(users2) == 3) && (len(users1)-len(users3) == 5)) {
t.Errorf("Offset should works perfectly")
}
}
2013-10-27 08:00:39 +04:00
2013-10-27 16:07:13 +04:00
func TestOr(t *testing.T) {
2013-10-27 08:31:51 +04:00
var users []User
db.Where("name = ?", "1").Or("name = ?", "3").Find(&users)
if len(users) != 3 {
t.Errorf("Should find three users with name 1 and 3")
}
2013-10-27 08:00:39 +04:00
}
2013-10-27 10:34:34 +04:00
2013-10-27 16:07:13 +04:00
func TestCount(t *testing.T) {
var count, count1, count2 int64
var users []User
db.Where("name = ?", "1").Or("name = ?", "3").Find(&users).Count(&count)
if count != int64(len(users)) {
t.Errorf("Count() method should get same value of users count")
}
db.Model(&User{}).Where("name = ?", "1").Count(&count1).Or("name = ?", "3").Count(&count2)
if !(count1 == 1 && count2 == 3) {
t.Errorf("Multiple count should works well also")
}
}
2013-10-27 10:34:34 +04:00
func TestCreatedAtAndUpdatedAt(t *testing.T) {
name := "check_created_at_and_updated_at"
u := User{Name: name, Age: 1}
db.Save(&u)
created_at := u.CreatedAt
updated_at := u.UpdatedAt
if created_at.IsZero() {
t.Errorf("Should have created_at after create")
}
if updated_at.IsZero() {
t.Errorf("Should have updated_at after create")
}
u.Name = "check_created_at_and_updated_at_2"
db.Save(&u)
created_at2 := u.CreatedAt
updated_at2 := u.UpdatedAt
if created_at != created_at2 {
t.Errorf("Created At should not changed after update")
}
if updated_at == updated_at2 {
t.Errorf("Updated At should be changed after update")
}
}
2013-10-27 11:24:01 +04:00
2013-10-27 12:06:45 +04:00
func (s *Product) BeforeCreate() (err error) {
if s.Code == "Invalid" {
err = errors.New("invalid product")
}
2013-10-27 11:24:01 +04:00
s.BeforeCreateCallTimes = s.BeforeCreateCallTimes + 1
2013-10-27 12:06:45 +04:00
return
2013-10-27 11:24:01 +04:00
}
2013-10-27 12:06:45 +04:00
func (s *Product) BeforeUpdate() (err error) {
if s.Code == "dont_update" {
err = errors.New("Can't update")
}
2013-10-27 11:24:01 +04:00
s.BeforeUpdateCallTimes = s.BeforeUpdateCallTimes + 1
2013-10-27 12:06:45 +04:00
return
2013-10-27 11:24:01 +04:00
}
2013-10-27 12:06:45 +04:00
func (s *Product) BeforeSave() (err error) {
if s.Code == "dont_save" {
err = errors.New("Can't save")
}
2013-10-27 11:24:01 +04:00
s.BeforeSaveCallTimes = s.BeforeSaveCallTimes + 1
2013-10-27 12:06:45 +04:00
return
2013-10-27 11:24:01 +04:00
}
func (s *Product) AfterCreate() {
s.AfterCreateCallTimes = s.AfterCreateCallTimes + 1
}
func (s *Product) AfterUpdate() {
s.AfterUpdateCallTimes = s.AfterUpdateCallTimes + 1
}
func (s *Product) AfterSave() {
s.AfterSaveCallTimes = s.AfterSaveCallTimes + 1
}
2013-10-27 12:06:45 +04:00
func (s *Product) BeforeDelete() (err error) {
if s.Code == "dont_delete" {
err = errors.New("Can't delete")
}
s.BeforeDeleteCallTimes = s.BeforeDeleteCallTimes + 1
return
}
func (s *Product) AfterDelete() {
s.AfterDeleteCallTimes = s.AfterDeleteCallTimes + 1
}
2013-10-27 11:24:01 +04:00
func (p *Product) GetCallTimes() []int64 {
2013-10-27 12:06:45 +04:00
return []int64{p.BeforeCreateCallTimes, p.BeforeSaveCallTimes, p.BeforeUpdateCallTimes, p.AfterCreateCallTimes, p.AfterSaveCallTimes, p.AfterUpdateCallTimes, p.BeforeDeleteCallTimes, p.AfterDeleteCallTimes}
2013-10-27 11:24:01 +04:00
}
func TestRunCallbacks(t *testing.T) {
p := Product{Code: "unique_code", Price: 100}
db.Save(&p)
2013-10-27 12:06:45 +04:00
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 1, 0, 1, 1, 0, 0, 0}) {
2013-10-27 11:24:01 +04:00
t.Errorf("Some errors happened when run create callbacks, %v", p.GetCallTimes())
}
db.Where("Code = ?", "unique_code").First(&p)
2013-10-27 12:06:45 +04:00
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 1, 0, 0, 0, 0, 0, 0}) {
2013-10-27 11:24:01 +04:00
t.Errorf("Should be able to query about saved values in before filters, %v", p.GetCallTimes())
}
p.Price = 200
db.Save(&p)
2013-10-27 12:06:45 +04:00
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 0, 1, 1, 0, 0}) {
2013-10-27 11:24:01 +04:00
t.Errorf("Some errors happened when run update callbacks, %v", p.GetCallTimes())
}
db.Where("Code = ?", "unique_code").First(&p)
2013-10-27 12:06:45 +04:00
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 0, 0, 0, 0, 0}) {
2013-10-27 11:24:01 +04:00
t.Errorf("Some errors happened when run update callbacks, %v", p.GetCallTimes())
}
2013-10-27 12:06:45 +04:00
db.Delete(&p)
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 0, 0, 0, 1, 1}) {
t.Errorf("Some errors happened when run update callbacks, %v", p.GetCallTimes())
}
if db.Where("Code = ?", "unique_code").First(&p).Error == nil {
t.Errorf("Should get error when find an deleted record")
}
}
func TestRunCallbacksAndGetErrors(t *testing.T) {
p := Product{Code: "Invalid", Price: 100}
if db.Save(&p).Error == nil {
t.Errorf("An error from create callbacks expected when create")
}
if db.Where("code = ?", "Invalid").First(&Product{}).Error == nil {
t.Errorf("Should not save records that have errors")
}
if db.Save(&Product{Code: "dont_save", Price: 100}).Error == nil {
t.Errorf("An error from create callbacks expected when create")
}
p2 := Product{Code: "update_callback", Price: 100}
db.Save(&p2)
p2.Code = "dont_update"
if db.Save(&p2).Error == nil {
t.Errorf("An error from callbacks expected when update")
}
if db.Where("code = ?", "update_callback").First(&Product{}).Error != nil {
t.Errorf("Record Should not be updated due to errors happened in callback")
}
if db.Where("code = ?", "dont_update").First(&Product{}).Error == nil {
t.Errorf("Record Should not be updated due to errors happened in callback")
}
p2.Code = "dont_save"
if db.Save(&p2).Error == nil {
t.Errorf("An error from before save callbacks expected when update")
}
p3 := Product{Code: "dont_delete", Price: 100}
db.Save(&p3)
if db.Delete(&p3).Error == nil {
t.Errorf("An error from before delete callbacks expected when delete")
}
if db.Where("Code = ?", "dont_delete").First(&p3).Error != nil {
t.Errorf("Should not delete record due to errors happened in callback")
}
2013-10-27 11:24:01 +04:00
}
2013-10-28 08:12:12 +04:00
func TestNoUnExpectedHappenWithInvalidSql(t *testing.T) {
2013-10-28 08:12:12 +04:00
var columns []string
if db.Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {
t.Errorf("Should got error with invalid SQL")
}
if db.Model(&User{}).Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {
t.Errorf("Should got error with invalid SQL")
}
2013-10-28 08:12:12 +04:00
type Article struct {
Name string
}
db.Where("sdsd.zaaa = ?", "sd;;;aa").Find(&Article{})
db.Where("name = ?", "3").Find(&[]User{})
var count1, count2 int64
db.Model(&User{}).Count(&count1)
if count1 <= 0 {
t.Errorf("Should find some users")
}
q := db.Where("name = ?", "jinzhu; delete * from users").First(&User{})
if q.Error == nil {
t.Errorf("Can't find user")
}
db.Model(&User{}).Count(&count2)
if count1 != count2 {
t.Errorf("Users should not be deleted by invalid SQL")
}
2013-10-28 08:12:12 +04:00
db.Where("unexisting = ?", "3").Find(&[]User{})
}
2013-10-28 16:27:25 +04:00
func TestSetTableDirectly(t *testing.T) {
var ages []int64
if db.Table("users").Pluck("age", &ages).Error != nil {
t.Errorf("No errors should happen if only set table")
}
if len(ages) == 0 {
t.Errorf("Should find out some records")
}
var users []User
if db.Table("users").Find(&users).Error != nil {
t.Errorf("No errors should happen if set table to an existing table")
}
if db.Table("unexisting_users_table").Find(&users).Error == nil {
t.Errorf("Should got some errors if set table to an unexisting table")
}
if db.Table("products").Find(&users).Error == nil {
t.Errorf("Should got some errors if set table to an unexisting table")
}
db.Exec("drop table deleted_users;")
if db.Table("deleted_users").CreateTable(&User{}).Error != nil {
t.Errorf("Should create deleted_users table")
}
db.Table("deleted_users").Save(&User{Name: "DeletedUser"})
var deleted_users []User
db.Table("deleted_users").Find(&deleted_users)
if len(deleted_users) != 1 {
t.Errorf("Should query from deleted_users table")
}
var deleted_user User
db.Table("deleted_users").Find(&deleted_user)
if deleted_user.Name != "DeletedUser" {
t.Errorf("Should query from deleted_users table")
}
var user1, user2, user3 User
db.First(&user1).Table("deleted_users").First(&user2).Table("").First(&user3)
if !((user1.Name != user2.Name) && (user1.Name == user3.Name)) {
t.Errorf("Set Table Chain Should works well")
}
}