test: add testcase in TestSave

This commit is contained in:
riverchu 2021-09-05 11:10:48 +08:00 committed by Jinzhu
parent 1d9e563023
commit c898622791
1 changed files with 30 additions and 0 deletions

View File

@ -642,6 +642,36 @@ func TestSave(t *testing.T) {
if !regexp.MustCompile("WHERE .id. = [^ ]+$").MatchString(stmt.SQL.String()) { if !regexp.MustCompile("WHERE .id. = [^ ]+$").MatchString(stmt.SQL.String()) {
t.Fatalf("invalid updating SQL, got %v", stmt.SQL.String()) t.Fatalf("invalid updating SQL, got %v", stmt.SQL.String())
} }
user3 := *GetUser("save3", Config{})
DB.Create(&user3)
if err := DB.First(&User{}, "name = ?", "save3").Error; err != nil {
t.Fatalf("failed to find created user")
}
user3.Name = "save3_"
DB.Model(User{}).Save(&user3)
var result2 User
if err := DB.First(&result2, "name = ?", "save3_").Error; err != nil || result2.ID != user3.ID {
t.Fatalf("failed to find updated user")
}
DB.Model(User{}).Save(&struct {
gorm.Model
Placeholder string
Name string
}{
Model: user3.Model,
Placeholder: "placeholder",
Name: "save3__",
})
var result3 User
if err := DB.First(&result3, "name = ?", "save3__").Error; err != nil || result3.ID != user3.ID {
t.Fatalf("failed to find updated user")
}
} }
func TestSaveWithPrimaryValue(t *testing.T) { func TestSaveWithPrimaryValue(t *testing.T) {