From 4d40e34734289137d9ca8fc2b69bf8de98a7448c Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 14 Jul 2022 14:39:43 +0800 Subject: [PATCH] Update select tests --- tests/helper_test.go | 2 ++ tests/update_belongs_to_test.go | 15 +++++++++++++++ tests/update_has_one_test.go | 10 +++++++--- tests/update_test.go | 2 ++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/tests/helper_test.go b/tests/helper_test.go index 7ee2a576..d1af0739 100644 --- a/tests/helper_test.go +++ b/tests/helper_test.go @@ -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) diff --git a/tests/update_belongs_to_test.go b/tests/update_belongs_to_test.go index 8fe0f289..4e94cfd5 100644 --- a/tests/update_belongs_to_test.go +++ b/tests/update_belongs_to_test.go @@ -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) } diff --git a/tests/update_has_one_test.go b/tests/update_has_one_test.go index c926fbcf..40af6ae7 100644 --- a/tests/update_has_one_test.go +++ b/tests/update_has_one_test.go @@ -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) }) } diff --git a/tests/update_test.go b/tests/update_test.go index 0fc89a93..d7634580 100644 --- a/tests/update_test.go +++ b/tests/update_test.go @@ -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) {