-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks_test.go
244 lines (184 loc) · 5.83 KB
/
hooks_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package mgm_test
import (
"errors"
"github.com/kamva/mgm/v3"
"github.com/kamva/mgm/v3/internal/util"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"testing"
)
type Person struct {
mock.Mock `bson:"-"`
mgm.DefaultModel `bson:",inline"`
Name string `bson:"name"`
Age int `bson:"age"`
}
func (d *Person) CollectionName() string {
return "persons"
}
func NewPerson(name string, age int) *Person {
return &Person{Name: name, Age: age}
}
func insertPerson(person *Person) {
// Set listeners to mocked hooks:
person.On("Creating").Return(nil)
person.On("Created").Return(nil)
person.On("Saving").Return(nil)
person.On("Saved").Return(nil)
util.PanicErr(mgm.Coll(person).Create(person))
}
//--------------------------------
// Mock hooks
//--------------------------------
func (d *Person) Creating() error {
args := d.Called()
return args.Error(0)
}
func (d *Person) Created() error {
args := d.Called()
return args.Error(0)
}
func (d *Person) Updating() error {
args := d.Called()
return args.Error(0)
}
func (d *Person) Updated(result *mongo.UpdateResult) error {
args := d.Called(result.MatchedCount, result.ModifiedCount)
return args.Error(0)
}
func (d *Person) Saving() error {
args := d.Called()
return args.Error(0)
}
func (d *Person) Saved() error {
args := d.Called()
return args.Error(0)
}
func (d *Person) Deleting() error {
args := d.Called()
return args.Error(0)
}
func (d *Person) Deleted(result *mongo.DeleteResult) error {
args := d.Called(result.DeletedCount)
return args.Error(0)
}
func TestReturnErrorInCreatingHook(t *testing.T) {
setupDefConnection()
resetCollection()
creatingErr := errors.New("test error")
person := NewPerson("Ali", 24)
// Set listeners to mocked hooks:
person.On("Creating").Return(creatingErr)
err := mgm.Coll(person).Create(person)
require.Equal(t, creatingErr, err, "Expected returning hook's error")
person.AssertExpectations(t)
// Expected do not inserting this model:
count, _ := mgm.Coll(person).CountDocuments(mgm.Ctx(), bson.M{})
require.Equal(t, count, int64(0))
}
func TestCreatingDocHooks(t *testing.T) {
setupDefConnection()
resetCollection()
person := NewPerson("Ali", 24)
// Set listeners to mocked hooks:
person.On("Creating").Return(nil)
person.On("Created").Return(nil)
person.On("Saving").Return(nil)
person.On("Saved").Return(nil)
util.AssertErrIsNil(t, mgm.Coll(person).Create(person))
person.AssertExpectations(t)
}
func TestReturnErrorInSavingHook(t *testing.T) {
setupDefConnection()
resetCollection()
savingErr := errors.New("test error")
person := NewPerson("Ali", 24)
// Set listeners to mocked hooks:
person.On("Creating").Return(nil)
person.On("Saving").Return(savingErr)
err := mgm.Coll(person).Create(person)
require.Equal(t, savingErr, err, "Expected returning hook's error")
person.AssertExpectations(t)
// Expected do not inserting this model:
count, _ := mgm.Coll(person).CountDocuments(mgm.Ctx(), bson.M{})
require.Equal(t, count, int64(0))
}
func TestSavingDocHooks(t *testing.T) {
setupDefConnection()
resetCollection()
person := NewPerson("Ali", 24)
// Set listeners to mocked hooks:
person.On("Creating").Return(nil)
person.On("Created").Return(nil)
person.On("Saving").Return(nil)
person.On("Saved").Return(nil)
util.AssertErrIsNil(t, mgm.Coll(person).Create(person))
person.AssertExpectations(t)
}
func TestReturnErrorInUpdatingHook(t *testing.T) {
setupDefConnection()
resetCollection()
oldName := "Ali"
updatingErr := errors.New("test error")
person := NewPerson(oldName, 24)
insertPerson(person)
person.Name = "Mehran"
// Set listeners to mocked hooks:
person.On("Updating").Return(updatingErr)
err := mgm.Coll(person).Update(person)
require.Equal(t, updatingErr, err, "Expected returning hook's error")
person.AssertExpectations(t)
// Expected do not update this model:
oldPerson := &Person{}
util.PanicErr(mgm.Coll(person).FindByID(person.ID, oldPerson))
require.Equal(t, oldName, oldPerson.Name, "Expected person's name be %s name, but is %s", oldName, person.Name)
}
func TestUpdatingDocHooks(t *testing.T) {
setupDefConnection()
resetCollection()
newName := "Mehran"
person := NewPerson("Ali", 24)
insertPerson(person)
person.Name = newName
// Set listeners to mocked hooks:
person.On("Updating").Return(nil)
person.On("Updated", int64(1), int64(1)).Return(nil)
err := mgm.Coll(person).Update(person)
util.AssertErrIsNil(t, err)
person.AssertExpectations(t)
// Expected do not update this model:
newPerson := &Person{}
util.PanicErr(mgm.Coll(person).FindByID(person.ID, newPerson))
require.Equal(t, newName, newPerson.Name, "Expected person's name be %s , but is %s", newName, person.Name)
}
func TestReturnErrorInDeletingHook(t *testing.T) {
setupDefConnection()
resetCollection()
deletingErr := errors.New("test error")
person := NewPerson("Ali", 24)
insertPerson(person)
// Set listeners to mocked hooks:
person.On("Deleting").Return(deletingErr)
err := mgm.Coll(person).Delete(person)
require.Equal(t, deletingErr, err, "Expected returning hook's error")
person.AssertExpectations(t)
// Expected do not delete this model:
count, _ := mgm.Coll(person).CountDocuments(mgm.Ctx(), bson.M{})
require.Equal(t, count, int64(1), "Expected having one document,got ", count)
}
func TestDeletingDocHooks(t *testing.T) {
setupDefConnection()
resetCollection()
person := NewPerson("Ali", 24)
insertPerson(person)
// Set listeners to mocked hooks:
person.On("Deleting").Return(nil)
person.On("Deleted", int64(1)).Return(nil)
util.AssertErrIsNil(t, mgm.Coll(person).Delete(person))
person.AssertExpectations(t)
// Expected do not delete this model:
count, _ := mgm.Coll(person).CountDocuments(mgm.Ctx(), bson.M{})
require.Equal(t, count, int64(0), "Expected having no documents,got ", count)
}