Failing test cases.
The second one added is not failing (#121) because of #123. Both time the UpdatedAt time is initialized to be the same (0). Therefore it is passing. However, when #123 is fixed, it will fail the second test case.
This commit is contained in:
parent
2f8b1842d8
commit
88721542e1
54
main_test.go
54
main_test.go
@ -133,6 +133,15 @@ type Animal struct {
|
|||||||
From string //test reserverd sql keyword as field name
|
From string //test reserverd sql keyword as field name
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
|
PersonId int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
Id int64
|
||||||
|
Name string
|
||||||
|
Pets []Animal
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -179,6 +188,7 @@ func init() {
|
|||||||
db.Exec("drop table roles")
|
db.Exec("drop table roles")
|
||||||
db.Exec("drop table companies")
|
db.Exec("drop table companies")
|
||||||
db.Exec("drop table animals")
|
db.Exec("drop table animals")
|
||||||
|
db.Exec("drop table persons")
|
||||||
|
|
||||||
if err = db.CreateTable(&Animal{}).Error; err != nil {
|
if err = db.CreateTable(&Animal{}).Error; err != nil {
|
||||||
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
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))
|
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 {
|
if err = db.AutoMigrate(Address{}).Error; err != nil {
|
||||||
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
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))
|
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"
|
var shortForm = "2006-01-02 15:04:05"
|
||||||
t1, _ = time.Parse(shortForm, "2000-10-27 12:02:40")
|
t1, _ = time.Parse(shortForm, "2000-10-27 12:02:40")
|
||||||
t2, _ = time.Parse(shortForm, "2002-01-01 00:00:00")
|
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) {
|
func (s *Product) BeforeCreate() (err error) {
|
||||||
if s.Code == "Invalid" {
|
if s.Code == "Invalid" {
|
||||||
err = errors.New("invalid product")
|
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) {
|
func TestUpdates(t *testing.T) {
|
||||||
product1 := Product{Code: "abc", Price: 10}
|
product1 := Product{Code: "abc", Price: 10}
|
||||||
product2 := Product{Code: "cde", Price: 20}
|
product2 := Product{Code: "cde", Price: 20}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user