-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete_test.go
169 lines (130 loc) · 4.01 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package gormet
import (
"fmt"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)
type testDelete struct {
gorm.Model
Email string `json:"email" gorm:"unique,not null;default:null"`
Name string `json:"name" gorm:"unique;not null;default:null"`
}
func TestRepository_DeleteById(t *testing.T) {
db := getGormConnection(t, &testDelete{})
repo, err := New[testDelete](db)
assert.Nil(t, err)
t.Run("Delete entity successfully", func(t *testing.T) {
entity := &testDelete{
Name: uuid.NewString(),
Email: fmt.Sprintf("%[email protected]", uuid.NewString()),
}
err := repo.Create(entity)
assert.Nil(t, err)
id := entity.ID
err = repo.DeleteById(id)
assert.Nil(t, err)
var got *testDelete = &testDelete{}
tx := db.First(got, "id = ?", id)
assert.NotNil(t, tx)
assert.NotNil(t, tx.Error)
assert.Equal(t, "record not found", tx.Error.Error())
assert.Equal(t, uint(0), got.ID)
})
t.Run("Error send nil entity", func(t *testing.T) {
err := repo.DeleteById(nil)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "the ID should not be nil")
})
t.Run("Entity with sql injection", func(t *testing.T) {
entity := &testDelete{
Name: uuid.NewString(),
Email: fmt.Sprintf("%[email protected]", uuid.NewString()),
}
err := repo.Create(entity)
assert.Nil(t, err)
id := fmt.Sprintf("%d", entity.ID) + `"; update test_deletes set deleted_at '1970-01-01 00:00:00 --`
err = repo.DeleteById(id)
assert.NotNil(t, err)
assert.Equal(t, "no register found", err.Error())
})
t.Run("Connection Closed", func(t *testing.T) {
sqlDB, _ := db.DB()
err := sqlDB.Close()
assert.Nil(t, err)
err = repo.DeleteById(1)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "sql: database is closed")
})
}
func TestRepository_Delete(t *testing.T) {
db := getGormConnection(t, &testDelete{})
repo, err := New[testDelete](db)
assert.Nil(t, err)
t.Run("Delete entity successfully", func(t *testing.T) {
entity := &testDelete{
Name: uuid.NewString(),
Email: fmt.Sprintf("%[email protected]", uuid.NewString()),
}
err := repo.Create(entity)
assert.Nil(t, err)
id := entity.ID
err = repo.Delete(entity)
assert.Nil(t, err)
var got *testDelete = &testDelete{}
tx := db.First(got, "id = ?", id)
assert.NotNil(t, tx)
assert.NotNil(t, tx.Error)
assert.Equal(t, "record not found", tx.Error.Error())
assert.Equal(t, uint(0), got.ID)
})
t.Run("Error send nil entity", func(t *testing.T) {
err := repo.Delete(nil)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "the entity should not be nil")
})
t.Run("Entity with sql injection", func(t *testing.T) {
entity := &testDelete{
Name: fmt.Sprintf("sql-%s", uuid.NewString()),
Email: fmt.Sprintf("%[email protected]", uuid.NewString()),
}
err := repo.Create(entity)
assert.Nil(t, err)
name := entity.Name + `"; update test_deletes set deleted at '1970-01-01 00:00:00 --`
entity.Name = name
err = repo.Delete(entity)
assert.Nil(t, err)
})
t.Run("Entity with sql injection in string ID", func(t *testing.T) {
type testDeletePkString struct {
Email string `json:"email" gorm:"primaryKey;default:null"`
Name string `json:"name" gorm:"default:null"`
}
db.AutoMigrate(&testDeletePkString{})
repo, err := New[testDeletePkString](db)
assert.Nil(t, err)
entity := &testDeletePkString{}
name := uuid.NewString() + `"; update test_deletes set deleted at '1970-01-01 00:00:00 --`
entity.Email = name
err = repo.Delete(entity)
assert.NotNil(t, err)
assert.Equal(t, "no register found", err.Error())
})
t.Run("Error delete without where", func(t *testing.T) {
repo, err := New[testDelete](db)
assert.Nil(t, err)
entity := &testDelete{}
err = repo.Delete(entity)
assert.NotNil(t, err)
assert.Equal(t, "WHERE conditions required", err.Error())
})
t.Run("Connection Closed", func(t *testing.T) {
sqlDB, _ := db.DB()
err := sqlDB.Close()
assert.Nil(t, err)
err = repo.DeleteById(2)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "sql: database is closed")
})
}