Merge 88721542e1ce1c3aa581a0c3b5a045ae964067f4 into 38a06bfdaae5dde7519faad908373493cec20211

This commit is contained in:
Shuhao Wu 2014-05-24 01:57:12 +00:00
commit f920e000b2

View File

@ -133,6 +133,15 @@ type Animal struct {
From string //test reserverd sql keyword as field name
CreatedAt time.Time
UpdatedAt time.Time
PersonId int64
}
type Person struct {
Id int64
Name string
Pets []Animal
CreatedAt time.Time
UpdatedAt time.Time
}
var (
@ -179,6 +188,7 @@ func init() {
db.Exec("drop table roles")
db.Exec("drop table companies")
db.Exec("drop table animals")
db.Exec("drop table persons")
if err = db.CreateTable(&Animal{}).Error; err != nil {
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
@ -196,6 +206,10 @@ func init() {
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
}
if err = db.CreateTable(Person{}).Error; err != nil {
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
}
if err = db.AutoMigrate(Address{}).Error; err != nil {
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
}
@ -212,6 +226,10 @@ func init() {
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
}
if err = db.AutoMigrate(Person{}).Error; err != nil {
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
}
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")
@ -711,6 +729,22 @@ func TestCreatedAtAndUpdatedAt(t *testing.T) {
}
}
func TestUpdatedAtWhenSelectingWithPrimaryKeyOfAForeignObject(t *testing.T) {
animal := Animal{Name: "Ninja"}
person := Person{Name: "Joe", Pets: []Animal{animal}}
now := time.Now()
db.Save(&person)
db.First(&animal, animal.Counter)
if animal.UpdatedAt.Before(now) {
t.Errorf("Animal's updated time should not be %v", animal.UpdatedAt)
}
if animal.CreatedAt.Before(now) {
t.Errorf("Animal's created time should not be %v", animal.UpdatedAt)
}
}
func (s *Product) BeforeCreate() (err error) {
if s.Code == "Invalid" {
err = errors.New("invalid product")
@ -1097,6 +1131,26 @@ func TestUpdate(t *testing.T) {
}
}
func TestUpdateWithForeignKey(t *testing.T) {
animal1 := Animal{Name: "Ferdinand"}
animal2 := Animal{Name: "nerdz"}
person := Person{Name: "Joe"}
person.Pets = []Animal{animal1, animal2}
if db.Save(&person).Error != nil {
t.Errorf("No error should raise when saving Person")
}
db.First(&animal1, animal1.Counter)
animal1Updated_at1 := animal1.UpdatedAt
db.Model(&person).Update("name", "john")
db.First(&animal1, animal1.Counter)
if animal1.UpdatedAt.After(animal1Updated_at1) {
t.Errorf("Animal should not be updated when only updating an attribute of Person")
}
}
func TestUpdates(t *testing.T) {
product1 := Product{Code: "abc", Price: 10}
product2 := Product{Code: "cde", Price: 20}