forked from jinzhu/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_test.go
91 lines (73 loc) · 2.71 KB
/
delete_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package gorm_test
import (
"testing"
"time"
)
func TestDelete(t *testing.T) {
user1, user2 := User{Name: "delete1"}, User{Name: "delete2"}
DB.Save(&user1)
DB.Save(&user2)
if err := DB.Delete(&user1).Error; err != nil {
t.Errorf("No error should happen when delete a record, err=%s", err)
}
if !DB.Where("name = ?", user1.Name).First(&User{}).RecordNotFound() {
t.Errorf("User can't be found after delete")
}
if DB.Where("name = ?", user2.Name).First(&User{}).RecordNotFound() {
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"}
DB.Save(&user1)
DB.Save(&user2)
if DB.Delete(&User{}, user1.Id).Error != nil {
t.Errorf("No error should happen when delete a record")
} else if !DB.Where("name = ?", user1.Name).First(&User{}).RecordNotFound() {
t.Errorf("User can't be found after delete")
}
if err := DB.Delete(&User{}, "name = ?", user2.Name).Error; err != nil {
t.Errorf("No error should happen when delete a record, err=%s", err)
} else if !DB.Where("name = ?", user2.Name).First(&User{}).RecordNotFound() {
t.Errorf("User can't be found after delete")
}
}
func TestSoftDelete(t *testing.T) {
type User struct {
Id int64
Name string
DeletedAt *time.Time
}
DB.AutoMigrate(&User{})
user := User{Name: "soft_delete"}
DB.Save(&user)
DB.Delete(&user)
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 be able to find soft deleted record with Unscoped, but err=%s", err)
}
DB.Unscoped().Delete(&user)
if !DB.Unscoped().First(&User{}, "name = ?", user.Name).RecordNotFound() {
t.Errorf("Can't find permanently deleted record")
}
}
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")
}
}