Update select tests

This commit is contained in:
Jinzhu 2022-07-14 14:39:43 +08:00
parent 3262daf8d4
commit 4d40e34734
4 changed files with 26 additions and 3 deletions

View File

@ -80,6 +80,7 @@ func CheckPet(t *testing.T, pet Pet, expect Pet) {
t.Fatalf("errors happened when query: %v", err)
} else {
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
DB.First(&manager, "id = ?", *user.ManagerID)
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 {
t.Errorf("Manager should not be created for zero value, got: %+v", user.ManagerID)

View File

@ -41,4 +41,19 @@ func TestUpdateBelongsTo(t *testing.T) {
var user4 User
DB.Preload("Company").Preload("Manager").Find(&user4, "id = ?", user.ID)
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)
}

View File

@ -90,8 +90,9 @@ func TestUpdateHasOne(t *testing.T) {
t.Run("Restriction", func(t *testing.T) {
type CustomizeAccount struct {
gorm.Model
UserID sql.NullInt64
Number string `gorm:"<-:create"`
UserID sql.NullInt64
Number string `gorm:"<-:create"`
Number2 string
}
type CustomizeUser struct {
@ -114,7 +115,8 @@ func TestUpdateHasOne(t *testing.T) {
cusUser := CustomizeUser{
Name: "update-has-one-associations",
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)
}
cusUser.Account.Number += "-update"
cusUser.Account.Number2 += "-update"
if err := DB.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&cusUser).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
@ -129,5 +132,6 @@ func TestUpdateHasOne(t *testing.T) {
var account2 CustomizeAccount
DB.Find(&account2, "user_id = ?", cusUser.ID)
AssertEqual(t, account2.Number, number)
AssertEqual(t, account2.Number2, cusUser.Account.Number2)
})
}

View File

@ -307,6 +307,8 @@ func TestSelectWithUpdate(t *testing.T) {
if utils.AssertEqual(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) {