2014-07-29 06:59:13 +04:00
|
|
|
package gorm_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
user1, user2 := User{Name: "delete1"}, User{Name: "delete2"}
|
2014-08-28 11:33:43 +04:00
|
|
|
DB.Save(&user1)
|
|
|
|
DB.Save(&user2)
|
2014-07-29 06:59:13 +04:00
|
|
|
|
2015-08-09 00:08:25 +03:00
|
|
|
if err := DB.Delete(&user1).Error; err != nil {
|
|
|
|
t.Errorf("No error should happen when delete a record, err=%s", err)
|
2014-07-29 06:59:13 +04:00
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
if !DB.Where("name = ?", user1.Name).First(&User{}).RecordNotFound() {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("User can't be found after delete")
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
if DB.Where("name = ?", user2.Name).First(&User{}).RecordNotFound() {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("Other users that not deleted should be found-able")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInlineDelete(t *testing.T) {
|
|
|
|
user1, user2 := User{Name: "inline_delete1"}, User{Name: "inline_delete2"}
|
2014-08-28 11:33:43 +04:00
|
|
|
DB.Save(&user1)
|
|
|
|
DB.Save(&user2)
|
2014-07-29 06:59:13 +04:00
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
if DB.Delete(&User{}, user1.Id).Error != nil {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("No error should happen when delete a record")
|
2014-08-28 11:33:43 +04:00
|
|
|
} else if !DB.Where("name = ?", user1.Name).First(&User{}).RecordNotFound() {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("User can't be found after delete")
|
|
|
|
}
|
|
|
|
|
2015-08-09 00:08:25 +03:00
|
|
|
if err := DB.Delete(&User{}, "name = ?", user2.Name).Error; err != nil {
|
|
|
|
t.Errorf("No error should happen when delete a record, err=%s", err)
|
2014-08-28 11:33:43 +04:00
|
|
|
} else if !DB.Where("name = ?", user2.Name).First(&User{}).RecordNotFound() {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("User can't be found after delete")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSoftDelete(t *testing.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int64
|
|
|
|
Name string
|
2016-01-10 16:33:15 +03:00
|
|
|
DeletedAt *time.Time
|
2014-07-29 06:59:13 +04:00
|
|
|
}
|
2014-08-28 11:33:43 +04:00
|
|
|
DB.AutoMigrate(&User{})
|
2014-07-29 06:59:13 +04:00
|
|
|
|
|
|
|
user := User{Name: "soft_delete"}
|
2014-08-28 11:33:43 +04:00
|
|
|
DB.Save(&user)
|
|
|
|
DB.Delete(&user)
|
2014-07-29 06:59:13 +04:00
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
if DB.First(&User{}, "name = ?", user.Name).Error == nil {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("Can't find a soft deleted record")
|
|
|
|
}
|
|
|
|
|
2015-08-09 00:08:25 +03:00
|
|
|
if err := DB.Unscoped().First(&User{}, "name = ?", user.Name).Error; err != nil {
|
|
|
|
t.Errorf("Should be able to find soft deleted record with Unscoped, but err=%s", err)
|
2014-07-29 06:59:13 +04:00
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
DB.Unscoped().Delete(&user)
|
|
|
|
if !DB.Unscoped().First(&User{}, "name = ?", user.Name).RecordNotFound() {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("Can't find permanently deleted record")
|
|
|
|
}
|
|
|
|
}
|
2017-02-01 16:33:36 +03:00
|
|
|
|
|
|
|
func TestSoftDeleteWithCustomizedDeletedAtColumnName(t *testing.T) {
|
|
|
|
creditCard := CreditCard{Number: "411111111234567"}
|
|
|
|
DB.Save(&creditCard)
|
|
|
|
DB.Delete(&creditCard)
|
|
|
|
|
|
|
|
if deletedAtField, ok := DB.NewScope(&CreditCard{}).FieldByName("DeletedAt"); !ok || deletedAtField.DBName != "deleted_time" {
|
|
|
|
t.Errorf("CreditCard's DeletedAt's column name should be `deleted_time`")
|
|
|
|
}
|
|
|
|
|
|
|
|
if DB.First(&CreditCard{}, "number = ?", creditCard.Number).Error == nil {
|
|
|
|
t.Errorf("Can't find a soft deleted record")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Unscoped().First(&CreditCard{}, "number = ?", creditCard.Number).Error; err != nil {
|
|
|
|
t.Errorf("Should be able to find soft deleted record with Unscoped, but err=%s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
DB.Unscoped().Delete(&creditCard)
|
|
|
|
if !DB.Unscoped().First(&CreditCard{}, "number = ?", creditCard.Number).RecordNotFound() {
|
|
|
|
t.Errorf("Can't find permanently deleted record")
|
|
|
|
}
|
|
|
|
}
|