mirror of https://github.com/go-gorm/gorm.git
Remove RecordNotFound method
This commit is contained in:
parent
31a0553b82
commit
e7b2e92ce3
|
@ -389,7 +389,3 @@ func (db *DB) Exec(sql string, values ...interface{}) (tx *DB) {
|
||||||
tx.callbacks.Raw().Execute(tx)
|
tx.callbacks.Raw().Execute(tx)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) RecordNotFound() bool {
|
|
||||||
return errors.Is(db.Error, ErrRecordNotFound)
|
|
||||||
}
|
|
||||||
|
|
|
@ -52,13 +52,13 @@ func TestInlineCondDelete(t *testing.T) {
|
||||||
|
|
||||||
if DB.Delete(&User{}, user1.ID).Error != nil {
|
if DB.Delete(&User{}, user1.ID).Error != nil {
|
||||||
t.Errorf("No error should happen when delete a record")
|
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")
|
t.Errorf("User can't be found after delete")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := DB.Delete(&User{}, "name = ?", user2.Name).Error; err != nil {
|
if err := DB.Delete(&User{}, "name = ?", user2.Name).Error; err != nil {
|
||||||
t.Errorf("No error should happen when delete a record, err=%s", err)
|
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")
|
t.Errorf("User can't be found after delete")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package tests_test
|
package tests_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
. "gorm.io/gorm/utils/tests"
|
. "gorm.io/gorm/utils/tests"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,7 +24,7 @@ func TestSoftDelete(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
DB.Unscoped().Delete(&user)
|
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")
|
t.Errorf("Can't find permanently deleted record")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"})
|
DB.Table("users").Where("name in ?", []string{users[1].Name}).Updates(User{Name: "updates_02_newname"})
|
||||||
|
|
||||||
var user3 User
|
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")
|
t.Errorf("User2's name should be updated")
|
||||||
}
|
}
|
||||||
AssertEqual(t, user2.UpdatedAt, user3.UpdatedAt)
|
AssertEqual(t, user2.UpdatedAt, user3.UpdatedAt)
|
||||||
|
|
|
@ -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)
|
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")
|
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")
|
t.Errorf("belongs to association should be saved")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue