2020-05-29 02:35:45 +03:00
|
|
|
package tests_test
|
|
|
|
|
|
|
|
import (
|
2020-06-07 17:03:45 +03:00
|
|
|
"errors"
|
2020-05-29 02:35:45 +03:00
|
|
|
"testing"
|
|
|
|
|
2020-06-07 17:03:45 +03:00
|
|
|
"gorm.io/gorm"
|
2020-06-02 05:34:50 +03:00
|
|
|
. "gorm.io/gorm/utils/tests"
|
2020-05-29 02:35:45 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSoftDelete(t *testing.T) {
|
|
|
|
user := *GetUser("SoftDelete", Config{})
|
|
|
|
DB.Save(&user)
|
|
|
|
if err := DB.Delete(&user).Error; err != nil {
|
|
|
|
t.Fatalf("No error should happen when soft delete user, but got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if DB.First(&User{}, "name = ?", user.Name).Error == nil {
|
|
|
|
t.Errorf("Can't find a soft deleted record")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
DB.Unscoped().Delete(&user)
|
2020-06-07 17:03:45 +03:00
|
|
|
if err := DB.Unscoped().First(&User{}, "name = ?", user.Name).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
|
2020-05-29 02:35:45 +03:00
|
|
|
t.Errorf("Can't find permanently deleted record")
|
|
|
|
}
|
|
|
|
}
|