forked from mirror/gorm
Add count soft deleted record test
This commit is contained in:
parent
0d58d5a3a7
commit
dbc3f8feb0
|
@ -7,7 +7,7 @@ require (
|
||||||
gorm.io/driver/mysql v0.0.0-20200609004954-b8310c61c3f2
|
gorm.io/driver/mysql v0.0.0-20200609004954-b8310c61c3f2
|
||||||
gorm.io/driver/postgres v0.0.0-20200602015520-15fcc29eb286
|
gorm.io/driver/postgres v0.0.0-20200602015520-15fcc29eb286
|
||||||
gorm.io/driver/sqlite v1.0.0
|
gorm.io/driver/sqlite v1.0.0
|
||||||
gorm.io/driver/sqlserver v0.0.0-20200609005334-d550a0be1cfb
|
gorm.io/driver/sqlserver v0.0.0-20200610030356-9c9aea39e1c1
|
||||||
gorm.io/gorm v0.0.0-00010101000000-000000000000
|
gorm.io/gorm v0.0.0-00010101000000-000000000000
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,12 @@ import (
|
||||||
func TestSoftDelete(t *testing.T) {
|
func TestSoftDelete(t *testing.T) {
|
||||||
user := *GetUser("SoftDelete", Config{})
|
user := *GetUser("SoftDelete", Config{})
|
||||||
DB.Save(&user)
|
DB.Save(&user)
|
||||||
|
|
||||||
|
var count int64
|
||||||
|
if DB.Model(&User{}).Where("name = ?", user.Name).Count(&count).Error != nil || count != 1 {
|
||||||
|
t.Errorf("Count soft deleted record, expects: %v, got: %v", 1, count)
|
||||||
|
}
|
||||||
|
|
||||||
if err := DB.Delete(&user).Error; err != nil {
|
if err := DB.Delete(&user).Error; err != nil {
|
||||||
t.Fatalf("No error should happen when soft delete user, but got %v", err)
|
t.Fatalf("No error should happen when soft delete user, but got %v", err)
|
||||||
}
|
}
|
||||||
|
@ -19,10 +25,18 @@ func TestSoftDelete(t *testing.T) {
|
||||||
t.Errorf("Can't find a soft deleted record")
|
t.Errorf("Can't find a soft deleted record")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if DB.Model(&User{}).Where("name = ?", user.Name).Count(&count).Error != nil || count != 0 {
|
||||||
|
t.Errorf("Count soft deleted record, expects: %v, got: %v", 0, count)
|
||||||
|
}
|
||||||
|
|
||||||
if err := DB.Unscoped().First(&User{}, "name = ?", user.Name).Error; err != nil {
|
if err := DB.Unscoped().First(&User{}, "name = ?", user.Name).Error; err != nil {
|
||||||
t.Errorf("Should find soft deleted record with Unscoped, but got err %s", err)
|
t.Errorf("Should find soft deleted record with Unscoped, but got err %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if DB.Unscoped().Model(&User{}).Where("name = ?", user.Name).Count(&count).Error != nil || count != 1 {
|
||||||
|
t.Errorf("Count soft deleted record, expects: %v, count: %v", 1, count)
|
||||||
|
}
|
||||||
|
|
||||||
DB.Unscoped().Delete(&user)
|
DB.Unscoped().Delete(&user)
|
||||||
if err := DB.Unscoped().First(&User{}, "name = ?", user.Name).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
|
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")
|
||||||
|
|
Loading…
Reference in New Issue