Remove RecordNotFound method

This commit is contained in:
Jinzhu 2020-06-07 22:03:45 +08:00
parent 31a0553b82
commit e7b2e92ce3
5 changed files with 8 additions and 10 deletions

View File

@ -389,7 +389,3 @@ func (db *DB) Exec(sql string, values ...interface{}) (tx *DB) {
tx.callbacks.Raw().Execute(tx)
return
}
func (db *DB) RecordNotFound() bool {
return errors.Is(db.Error, ErrRecordNotFound)
}

View File

@ -52,13 +52,13 @@ func TestInlineCondDelete(t *testing.T) {
if DB.Delete(&User{}, user1.ID).Error != nil {
t.Errorf("No error should happen when delete a record")
} else if !DB.Where("name = ?", user1.Name).First(&User{}).RecordNotFound() {
} else if err := DB.Where("name = ?", user1.Name).First(&User{}).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
t.Errorf("User can't be found after delete")
}
if err := DB.Delete(&User{}, "name = ?", user2.Name).Error; err != nil {
t.Errorf("No error should happen when delete a record, err=%s", err)
} else if !DB.Where("name = ?", user2.Name).First(&User{}).RecordNotFound() {
} else if err := DB.Where("name = ?", user2.Name).First(&User{}).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
t.Errorf("User can't be found after delete")
}
}

View File

@ -1,8 +1,10 @@
package tests_test
import (
"errors"
"testing"
"gorm.io/gorm"
. "gorm.io/gorm/utils/tests"
)
@ -22,7 +24,7 @@ func TestSoftDelete(t *testing.T) {
}
DB.Unscoped().Delete(&user)
if !DB.Unscoped().First(&User{}, "name = ?", user.Name).RecordNotFound() {
if err := DB.Unscoped().First(&User{}, "name = ?", user.Name).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
t.Errorf("Can't find permanently deleted record")
}
}

View File

@ -149,7 +149,7 @@ func TestUpdates(t *testing.T) {
DB.Table("users").Where("name in ?", []string{users[1].Name}).Updates(User{Name: "updates_02_newname"})
var user3 User
if DB.First(&user3, "name = ?", "updates_02_newname").RecordNotFound() {
if err := DB.First(&user3, "name = ?", "updates_02_newname").Error; err != nil {
t.Errorf("User2's name should be updated")
}
AssertEqual(t, user2.UpdatedAt, user3.UpdatedAt)

View File

@ -171,11 +171,11 @@ func TestFindOrCreate(t *testing.T) {
}
DB.Where(&User{Name: "find or create embedded struct"}).Assign(User{Age: 44, Account: Account{Number: "1231231231"}, Pets: []*Pet{{Name: "first_or_create_pet1"}, {Name: "first_or_create_pet2"}}}).FirstOrCreate(&user8)
if DB.Where("name = ?", "first_or_create_pet1").First(&Pet{}).RecordNotFound() {
if err := DB.Where("name = ?", "first_or_create_pet1").First(&Pet{}).Error; err != nil {
t.Errorf("has many association should be saved")
}
if DB.Where("number = ?", "1231231231").First(&Account{}).RecordNotFound() {
if err := DB.Where("number = ?", "1231231231").First(&Account{}).Error; err != nil {
t.Errorf("belongs to association should be saved")
}
}