-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandle_test.go
413 lines (347 loc) · 11.6 KB
/
handle_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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// +build !acceptance
package mongo
import (
"fmt"
"testing"
"time"
"github.com/ddspog/bdd"
)
// TestMain setup the testable mongo connecter to run a temp database.
func TestMain(m *testing.M) {
PrepareTestMongoAndRun(m)
}
// Feature Create Handle with functional Getters
// - As a developer,
// - I want to be able to create a Handle, and access data with its getters,
// - So that I could use these getters to manipulate and read data.
func Test_Create_Handle_with_functional_Getters(t *testing.T) {
given, like, s := bdd.Sentences().All()
given(t, "a ProductHandle p with ID '%[1]v'", func(when bdd.When, args ...interface{}) {
p := newProductHandle().SetDocument(&product{
IDV: ObjectIdHex(args[0].(string)),
})
defer p.Close()
when("p.Name() is called", func(it bdd.It) {
it("should return 'products'", func(assert bdd.Assert) {
assert.Equal("products", p.Name())
})
})
when("p.Document().ID().Hex() is called", func(it bdd.It) {
it("should return '%[1]v'", func(assert bdd.Assert) {
assert.Equal(args[0].(string), p.Document().ID().Hex())
})
})
}, like(
s(id1), s(id2), s(id3),
))
}
// Feature Count documents with Handle
// - As a developer,
// - I want to count documents with Handle,
// - So that I can use this to perform verifications and such.
func Test_Count_documents_with_Handle(t *testing.T) {
given, like, s := bdd.Sentences().All()
given(t, "a empty ProductHandle and products collection has %[1]v documents", func(when bdd.When, args ...interface{}) {
when("p.Count() is called", func(it bdd.It) {
n, err := newProductHandle().Safely().Count()
it("should return no errors", func(assert bdd.Assert) {
assert.Nil(err)
})
it("should return %[1]v", func(assert bdd.Assert) {
assert.Equal(args[0].(int), n)
})
})
}, like(
s(len(fixtures)),
))
given(t, "a ProductHandle with a nil document", func(when bdd.When) {
when("p.Count() is called", func(it bdd.It) {
n, err := newProductHandle().SetDocument(nil).Safely().Count()
it("should an error", func(assert bdd.Assert) {
assert.Error(err)
assert.Equal(DocNotDefined.Error(), err.Error())
})
it("should return 0", func(assert bdd.Assert) {
assert.Equal(0, n)
})
})
})
}
// Feature Clean documents with Handle
// - As a developer,
// - I want to Clean documents on Handle,
// - So that I can reset Handle after use.
func Test_Clean_documents_with_Handle(t *testing.T) {
given, like, s := bdd.Sentences().All()
given(t, "a ProductHandle p with Document with ID '%[1]v'", func(when bdd.When, args ...interface{}) {
p := newProductHandle().SetDocument(&product{
IDV: ObjectIdHex(args[0].(string)),
})
defer p.Close()
when("h.Clean() is called", func(it bdd.It) {
p.Clean()
it("should have p.Document().ID() return empty", func(assert bdd.Assert) {
assert.Empty(p.Document().ID().Hex())
})
})
}, like(
s(id1), s(id2), s(id3),
))
}
// Feature Find documents with Handle
// - As a developer,
// - I want to Find documents using Handle,
// - So that I can user Handler to search on database.
func Test_Find_documents_with_Handle(t *testing.T) {
defer cleanChanges()
given, like, s := bdd.Sentences().All()
given(t, "a linked ProductHandle p and products collection with documents "+colFixtures, func(when bdd.When, args ...interface{}) {
p := newProductHandle()
VerifyFind := func(it bdd.It, args ...interface{}) {
d, err := p.Safely().Find()
if args[1].(bool) {
it("should return no errors", func(assert bdd.Assert) {
assert.Nil(err)
})
it("d.ID().Hex() should return %[1]v", func(assert bdd.Assert) {
assert.Equal(args[0].(string), d.ID().Hex())
})
it("d.CreatedOn() should return %[3]v", func(assert bdd.Assert) {
assert.Equal(args[2].(int64), d.CreatedOn())
})
} else {
it("should return an error", func(assert bdd.Assert) {
assert.Error(err)
})
}
}
when("d, err := p.Find() is called with Document id '%[1]v'", func(it bdd.It) {
p.Document().IDV = ObjectIdHex(args[0].(string))
VerifyFind(it, args...)
})
p.Clean()
when("d, err := p.Find() is called with Search '_id' equal '%[1]v'", func(it bdd.It) {
p.SearchFor(M{
"_id": ObjectIdHex(args[0].(string)),
})
VerifyFind(it, args...)
})
}, like(
s(fixture(1).ID().Hex(), true, fixture(1).CreatedOn()),
s(fixture(2).ID().Hex(), true, fixture(2).CreatedOn()),
s(fixture(3).ID().Hex(), true, fixture(3).CreatedOn()),
s(idE, false),
))
}
// Feature Find various documents with Handle
// - As a developer,
// - I want to Find various documents using Handle,
// - So that I can use Handler to iterate through data.
func Test_Find_various_documents_with_Handle(t *testing.T) {
defer cleanChanges()
given, like, s := bdd.Sentences().All()
given(t, "a linked ProductHandle p with products collection with documents "+colFixtures, func(when bdd.When, args ...interface{}) {
p := newProductHandle()
VerifyFindAll := func(it bdd.It, args ...interface{}) {
da, err := p.Safely().FindAll(QueryOptions{
Sort: []string{"_id"},
})
it("should return no errors", func(assert bdd.Assert) {
assert.Nil(err)
})
if args[1].(bool) {
it("should return some documents", func(assert bdd.Assert) {
assert.NotEqual(0, len(da))
})
for i := range da {
dstr := fmt.Sprintf("da[%d]", i)
aID := args[(2*i)+2].(string)
it(dstr+".ID().Hex() should return "+aID, func(assert bdd.Assert) {
assert.Equal(aID, da[i].ID().Hex())
})
aCreatedOn := args[(2*i)+3].(int64)
sCreatedOn := fmt.Sprintf("%v", aCreatedOn)
it(dstr+".CreatedOn() should return "+sCreatedOn, func(assert bdd.Assert) {
assert.Equal(aCreatedOn, da[i].CreatedOn())
})
}
} else {
it("should return no documents", func(assert bdd.Assert) {
assert.Equal(0, len(da))
})
}
}
when("da, err := p.FindAll() is called with document id '%[1]v'", func(it bdd.It) {
if args[0].(string) != "" {
p.Document().IDV = ObjectIdHex(args[0].(string))
}
VerifyFindAll(it, args...)
})
p.Clean()
when("da, err := h.FindAll() is called with Search '_id' equal '%[1]v'", func(it bdd.It) {
if args[0].(string) != "" {
p.SearchMap()["_id"] = ObjectIdHex(args[0].(string))
}
VerifyFindAll(it, args...)
})
}, like(
s(fixture(1).ID().Hex(), true, fixture(1).ID().Hex(), fixture(1).CreatedOn()),
s(fixture(2).ID().Hex(), true, fixture(2).ID().Hex(), fixture(2).CreatedOn()),
s(fixture(3).ID().Hex(), true, fixture(3).ID().Hex(), fixture(3).CreatedOn()),
s(
"", true,
fixture(1).ID().Hex(), fixture(1).CreatedOn(),
fixture(2).ID().Hex(), fixture(2).CreatedOn(),
fixture(3).ID().Hex(), fixture(3).CreatedOn(),
),
s(idE, false),
))
}
// Feature Insert documents with Handle
// - As a developer,
// - I want to Insert documents using Handle,
// - So that I can use Handler to insert data.
func Test_Insert_documents_with_Handle(t *testing.T) {
defer cleanChanges()
given, like, s := bdd.Sentences().All()
given(t, "a linked ProductHandle p with ID '%[1]v'", func(when bdd.When, args ...interface{}) {
p := newProductHandle()
if args[0].(string) != "" {
p.Document().IDV = ObjectIdHex(args[0].(string))
}
when("p.Insert() is called", func(it bdd.It) {
now = func() (t time.Time) {
t = args[1].(time.Time)
return
}
defer resetUtils()
err := p.Safely().Insert()
if args[2].(bool) {
it("should return no errors", func(assert bdd.Assert) {
assert.Nil(err)
})
it("p.Document().CreatedOn() should return %[2]v", func(assert bdd.Assert) {
assert.Equal(expectedNowInMilli(args[1].(time.Time)), p.Document().CreatedOn())
})
} else {
it("should return an error", func(assert bdd.Assert) {
assert.Error(err)
})
it("p.Document().CreatedOn() should return %[2]v", func(assert bdd.Assert) {
assert.Equal(expectedNowInMilli(args[1].(time.Time)), p.Document().CreatedOn())
})
}
})
}, like(
s("", timeFmt("01-01-2000 00:00:01"), true),
s("", timeFmt("02-05-2014 13:36:42"), true),
s("", timeFmt("19-12-2017 22:59:00"), true),
s(fixture(1).ID().Hex(), timeFmt("11-11-2011 11:11:11"), false),
s(fixture(2).ID().Hex(), timeFmt("12-12-2012 00:00:00"), false),
s(fixture(3).ID().Hex(), timeFmt("01-01-0001 02:14:16"), false),
))
}
// Feature Remove documents with Handle
// - As a developer,
// - I want to Remove documents using Handle,
// - So that I can use Handler to remove data.
func Test_Remove_documents_with_Handle(t *testing.T) {
defer cleanChanges()
given, like, s := bdd.Sentences().All()
given(t, "a linked ProductHandle p with ID '%[1]v'", func(when bdd.When, args ...interface{}) {
p := newProductHandle()
when("p.Remove('%[1]v') is called", func(it bdd.It) {
err := p.Safely().Remove(args[0].(ObjectId))
if args[0].(ObjectId) != "" {
it("should return no errors", func(assert bdd.Assert) {
assert.Nil(err)
})
} else {
it("should return an error", func(assert bdd.Assert) {
assert.Equal(ErrIDNotDefined, err)
})
}
})
}, like(
s(fixture(1).ID()),
s(fixture(2).ID()),
s(fixture(3).ID()),
s(ObjectId("")),
))
}
// Feature Remove various documents with Handle
// - As a developer,
// - I want to Remove various documents using Handle,
// - So that I can use Handler to remove lots of data.
func Test_Remove_various_documents_with_Handle(t *testing.T) {
defer cleanChanges()
given, like, s := bdd.Sentences().All()
given(t, "a linked ProductHandle p with ID '%[1]v'", func(when bdd.When, args ...interface{}) {
p := newProductHandle()
VerifyRemoveAll := func(it bdd.It, args ...interface{}) {
info, err := p.Safely().RemoveAll()
it("should return no errors", func(assert bdd.Assert) {
assert.Nil(err)
})
it("should have removed %[2]v documents", func(assert bdd.Assert) {
assert.Equal(args[1].(int), info.Removed)
})
}
cleanChanges()
when("_, err := p.RemoveAll() is called with document id '%[1]v'", func(it bdd.It) {
if args[0].(string) != "" {
p.Document().IDV = ObjectIdHex(args[0].(string))
}
VerifyRemoveAll(it, args...)
})
p.Clean()
cleanChanges()
when("_, err := p.RemoveAll() is called with Search '_id' equal '%[1]v'", func(it bdd.It) {
if args[0].(string) != "" {
p.SearchMap()["_id"] = ObjectIdHex(args[0].(string))
}
VerifyRemoveAll(it, args...)
})
}, like(
s(fixture(1).ID().Hex(), 1),
s(fixture(2).ID().Hex(), 1),
s(fixture(3).ID().Hex(), 1),
s("", 3),
))
}
// Feature Update documents with Handle
// - As a developer,
// - I want to Update documents using Handle,
// - So that I can use Handler to update data.
func Test_Update_documents_with_Handle(t *testing.T) {
defer cleanChanges()
given, like, s := bdd.Sentences().All()
given(t, "a linked empty ProductHandle p", func(when bdd.When, args ...interface{}) {
p := newProductHandle()
when("p.Update('%[1]v') is called", func(it bdd.It) {
now = func() (t time.Time) {
t = args[1].(time.Time)
return
}
defer resetUtils()
err := p.Safely().Update(args[0].(ObjectId))
if args[0].(ObjectId) != "" {
it("should return no errors", func(assert bdd.Assert) {
assert.Nil(err)
})
it("should have p.Document().UpdatedOn() return %[2]v", func(assert bdd.Assert) {
assert.Equal(expectedNowInMilli(args[1].(time.Time)), p.Document().UpdatedOn())
})
} else {
it("should return an error", func(assert bdd.Assert) {
assert.Equal(ErrIDNotDefined, err)
})
}
})
}, like(
s(fixture(1).ID(), timeFmt("14-03-1998 12:15:06")),
s(fixture(2).ID(), timeFmt("22-10-1974 03:11:02")),
s(fixture(3).ID(), timeFmt("07-12-2007 02:48:59")),
s(ObjectId(""), timeFmt("11-2-2037 01:53:21")),
))
}