From dbc3f8feb0f57d7a277aac51acfaf0df793df683 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 10 Jun 2020 13:42:39 +0800 Subject: [PATCH] Add count soft deleted record test --- tests/go.mod | 2 +- tests/soft_delete_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/go.mod b/tests/go.mod index c184732c..3401bdfe 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -7,7 +7,7 @@ require ( gorm.io/driver/mysql v0.0.0-20200609004954-b8310c61c3f2 gorm.io/driver/postgres v0.0.0-20200602015520-15fcc29eb286 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 ) diff --git a/tests/soft_delete_test.go b/tests/soft_delete_test.go index b6dabe06..40d46fd8 100644 --- a/tests/soft_delete_test.go +++ b/tests/soft_delete_test.go @@ -11,6 +11,12 @@ import ( func TestSoftDelete(t *testing.T) { user := *GetUser("SoftDelete", Config{}) 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 { 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") } + 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 { 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) if err := DB.Unscoped().First(&User{}, "name = ?", user.Name).Error; !errors.Is(err, gorm.ErrRecordNotFound) { t.Errorf("Can't find permanently deleted record")