forked from mirror/gorm
Update select tests
This commit is contained in:
parent
3262daf8d4
commit
4d40e34734
|
@ -80,6 +80,7 @@ func CheckPet(t *testing.T, pet Pet, expect Pet) {
|
||||||
t.Fatalf("errors happened when query: %v", err)
|
t.Fatalf("errors happened when query: %v", err)
|
||||||
} else {
|
} else {
|
||||||
AssertObjEqual(t, newPet, pet, "ID", "CreatedAt", "UpdatedAt", "DeletedAt", "UserID", "Name")
|
AssertObjEqual(t, newPet, pet, "ID", "CreatedAt", "UpdatedAt", "DeletedAt", "UserID", "Name")
|
||||||
|
AssertObjEqual(t, newPet, expect, "ID", "CreatedAt", "UpdatedAt", "DeletedAt", "UserID", "Name")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,6 +175,7 @@ func CheckUser(t *testing.T, user User, expect User) {
|
||||||
var manager User
|
var manager User
|
||||||
DB.First(&manager, "id = ?", *user.ManagerID)
|
DB.First(&manager, "id = ?", *user.ManagerID)
|
||||||
AssertObjEqual(t, manager, user.Manager, "ID", "CreatedAt", "UpdatedAt", "DeletedAt", "Name", "Age", "Birthday", "CompanyID", "ManagerID", "Active")
|
AssertObjEqual(t, manager, user.Manager, "ID", "CreatedAt", "UpdatedAt", "DeletedAt", "Name", "Age", "Birthday", "CompanyID", "ManagerID", "Active")
|
||||||
|
AssertObjEqual(t, manager, expect.Manager, "ID", "CreatedAt", "UpdatedAt", "DeletedAt", "Name", "Age", "Birthday", "CompanyID", "ManagerID", "Active")
|
||||||
}
|
}
|
||||||
} else if user.ManagerID != nil {
|
} else if user.ManagerID != nil {
|
||||||
t.Errorf("Manager should not be created for zero value, got: %+v", user.ManagerID)
|
t.Errorf("Manager should not be created for zero value, got: %+v", user.ManagerID)
|
||||||
|
|
|
@ -41,4 +41,19 @@ func TestUpdateBelongsTo(t *testing.T) {
|
||||||
var user4 User
|
var user4 User
|
||||||
DB.Preload("Company").Preload("Manager").Find(&user4, "id = ?", user.ID)
|
DB.Preload("Company").Preload("Manager").Find(&user4, "id = ?", user.ID)
|
||||||
CheckUser(t, user4, user)
|
CheckUser(t, user4, user)
|
||||||
|
|
||||||
|
user.Company.Name += "new2"
|
||||||
|
user.Manager.Name += "new2"
|
||||||
|
if err := DB.Session(&gorm.Session{FullSaveAssociations: true}).Select("`Company`").Save(&user).Error; err != nil {
|
||||||
|
t.Fatalf("errors happened when update: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var user5 User
|
||||||
|
DB.Preload("Company").Preload("Manager").Find(&user5, "id = ?", user.ID)
|
||||||
|
if user5.Manager.Name != user4.Manager.Name {
|
||||||
|
t.Errorf("should not update user's manager")
|
||||||
|
} else {
|
||||||
|
user.Manager.Name = user4.Manager.Name
|
||||||
|
}
|
||||||
|
CheckUser(t, user, user5)
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,8 +90,9 @@ func TestUpdateHasOne(t *testing.T) {
|
||||||
t.Run("Restriction", func(t *testing.T) {
|
t.Run("Restriction", func(t *testing.T) {
|
||||||
type CustomizeAccount struct {
|
type CustomizeAccount struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
UserID sql.NullInt64
|
UserID sql.NullInt64
|
||||||
Number string `gorm:"<-:create"`
|
Number string `gorm:"<-:create"`
|
||||||
|
Number2 string
|
||||||
}
|
}
|
||||||
|
|
||||||
type CustomizeUser struct {
|
type CustomizeUser struct {
|
||||||
|
@ -114,7 +115,8 @@ func TestUpdateHasOne(t *testing.T) {
|
||||||
cusUser := CustomizeUser{
|
cusUser := CustomizeUser{
|
||||||
Name: "update-has-one-associations",
|
Name: "update-has-one-associations",
|
||||||
Account: CustomizeAccount{
|
Account: CustomizeAccount{
|
||||||
Number: number,
|
Number: number,
|
||||||
|
Number2: number,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,6 +124,7 @@ func TestUpdateHasOne(t *testing.T) {
|
||||||
t.Fatalf("errors happened when create: %v", err)
|
t.Fatalf("errors happened when create: %v", err)
|
||||||
}
|
}
|
||||||
cusUser.Account.Number += "-update"
|
cusUser.Account.Number += "-update"
|
||||||
|
cusUser.Account.Number2 += "-update"
|
||||||
if err := DB.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&cusUser).Error; err != nil {
|
if err := DB.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&cusUser).Error; err != nil {
|
||||||
t.Fatalf("errors happened when create: %v", err)
|
t.Fatalf("errors happened when create: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -129,5 +132,6 @@ func TestUpdateHasOne(t *testing.T) {
|
||||||
var account2 CustomizeAccount
|
var account2 CustomizeAccount
|
||||||
DB.Find(&account2, "user_id = ?", cusUser.ID)
|
DB.Find(&account2, "user_id = ?", cusUser.ID)
|
||||||
AssertEqual(t, account2.Number, number)
|
AssertEqual(t, account2.Number, number)
|
||||||
|
AssertEqual(t, account2.Number2, cusUser.Account.Number2)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -307,6 +307,8 @@ func TestSelectWithUpdate(t *testing.T) {
|
||||||
if utils.AssertEqual(result.UpdatedAt, user.UpdatedAt) {
|
if utils.AssertEqual(result.UpdatedAt, user.UpdatedAt) {
|
||||||
t.Fatalf("Update struct should update UpdatedAt, was %+v, got %+v", result.UpdatedAt, user.UpdatedAt)
|
t.Fatalf("Update struct should update UpdatedAt, was %+v, got %+v", result.UpdatedAt, user.UpdatedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AssertObjEqual(t, result, User{Name: "update_with_select"}, "Name", "Age")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSelectWithUpdateWithMap(t *testing.T) {
|
func TestSelectWithUpdateWithMap(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue