-
Notifications
You must be signed in to change notification settings - Fork 1
/
tail_test.go
566 lines (482 loc) · 14.5 KB
/
tail_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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package redkeep_test
import (
"bytes"
"fmt"
"io/ioutil"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"text/template"
. "github.com/manyminds/redkeep"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
const sleepDuration = 50 * time.Millisecond
var testConfigurationTemplate = `{
"mongo": {
"connectionURI": "localhost:30000,localhost:30001,localhost:30002"
},
"watches": [
{
"trackCollection": "{{.Database}}.user",
"trackFields": ["username", "gender"],
"targetCollection": "{{.Database}}.comment",
"targetNormalizedField": "meta",
"triggerReference": "user",
"behaviourSettings": {
"cascadeDelete": false
}
},
{
"trackCollection": "{{.Database}}.item",
"trackFields": ["name", "price"],
"targetCollection": "{{.Database}}.order",
"targetNormalizedField": "information",
"triggerReference": "item",
"behaviourSettings": {
"cascadeDelete": false
}
},
{
"trackCollection": "{{.Database}}.user",
"trackFields": ["name", "username"],
"targetCollection": "{{.Database}}.answer",
"targetNormalizedField": "meta",
"triggerReference": "user",
"behaviourSettings": {
"cascadeDelete": false
}
}
]
}`
var _ = Describe("Tail", func() {
var (
running chan bool
database string
answerString string
)
BeforeSuite(func() {
rndDB := func() string {
return fmt.Sprintf("redkeep_tests_%d", time.Now().UnixNano())
}()
database = rndDB
running = make(chan bool)
answerString = "this is my answer"
var data []byte
tmp, err := template.New("config").Parse(testConfigurationTemplate)
Expect(err).ToNot(HaveOccurred())
buffer := bytes.NewBuffer(data)
err = tmp.Execute(buffer, struct{ Database string }{Database: database})
Expect(err).ToNot(HaveOccurred())
data, err = ioutil.ReadAll(buffer)
Expect(err).ToNot(HaveOccurred())
config, err := NewConfiguration(data)
Expect(err).ToNot(HaveOccurred())
agent, err := NewTailAgent(*config)
Expect(err).ToNot(HaveOccurred())
go agent.Tail(running, false)
})
AfterSuite(func() {
running <- false
})
Context("Database testcases", func() {
type comment struct {
Text string
User mgo.DBRef
Meta map[string]interface{}
}
type answer struct {
AnswerText string `bson:"answerText"`
User mgo.DBRef
Meta map[string]interface{}
}
var (
db *mgo.Session
userOneRef mgo.DBRef
userTwoRef mgo.DBRef
userThreeRef mgo.DBRef
)
BeforeEach(func() {
var err error
db, err = mgo.Dial("localhost:30000,localhost:30001,localhost:30002")
Expect(err).ToNot(HaveOccurred())
userID := bson.ObjectIdHex("56a65494b204ccd1edc0b055")
userOneRef = mgo.DBRef{
Database: database,
Id: userID,
Collection: "user",
}
userIDTwo := bson.ObjectIdHex("56a87196ea3265eb5589c9c8")
userTwoRef = mgo.DBRef{
Database: database,
Id: userIDTwo,
Collection: "user",
}
userIDThree := bson.ObjectIdHex("56a87196ea3265eb5589c9dd")
userThreeRef = mgo.DBRef{
Database: database,
Id: userIDThree,
Collection: "user",
}
})
It("Should update nothing with invalid fields", func() {
userRef := mgo.DBRef{
Database: database,
Id: bson.NewObjectId(),
Collection: "user",
}
db.DB(database).C("user").Insert(
bson.M{
"_id": userRef.Id,
"nickname": "captain america",
"contact": bson.M{
"firstName": "Steve",
"lastName": "Rogers",
},
},
)
db.DB(database).C("comment").Insert(
bson.M{
"text": "i am captain of a warmonger",
"user": userRef.Id,
},
)
c := comment{}
time.Sleep(sleepDuration)
db.Copy().DB(database).C("comment").Find(bson.M{"text": "i am captain of a warmonger"}).One(&c)
Expect(c.Text).To(Equal("i am captain of a warmonger"))
Expect(c.Meta).To(BeEmpty())
})
It("Should update nothing with invalid fields", func() {
userRef := mgo.DBRef{
Database: database,
Id: bson.NewObjectId(),
Collection: "user",
}
db.DB(database).C("user").Insert(
bson.M{
"_id": userRef.Id,
"nickname": "captain america",
"contact": bson.M{
"firstName": "Steve",
"lastName": "Rogers",
},
},
)
db.DB(database).C("comment").Insert(
bson.M{
"text": "i am captain of a warmonger",
"user": userRef.Id,
},
)
c := comment{}
time.Sleep(10 * time.Millisecond)
db.Copy().DB(database).C("comment").Find(bson.M{"text": "i am captain of a warmonger"}).One(&c)
Expect(c.Text).To(Equal("i am captain of a warmonger"))
Expect(c.Meta).To(BeEmpty())
})
It("Should update infos on insert correctly", func() {
db.DB(database).C("user").Insert(
bson.M{
"_id": userOneRef.Id,
"username": "naan",
"gender": "male",
"name": bson.M{
"firstName": "Naan",
"lastName": "Waana",
},
},
)
db.DB(database).C("comment").Insert(
bson.M{
"text": "this is my first comment",
"user": userOneRef,
},
)
actual := comment{}
time.Sleep(sleepDuration)
db.Copy().DB(database).C("comment").Find(bson.M{"text": "this is my first comment"}).One(&actual)
Expect(actual.Meta["username"]).To(Equal("naan"))
Expect(actual.Meta["gender"]).To(Equal("male"))
})
It("Can handle user changes", func() {
db.DB(database).C("user").Insert(
bson.M{
"_id": userThreeRef.Id,
"username": "songoku",
"gender": "male",
"name": bson.M{
"firstName": "Songoku",
"lastName": "Kakarotto",
},
},
)
_, err := db.DB(database).C("comment").UpdateAll(
bson.M{
"user": userOneRef,
},
bson.M{
"$set": bson.M{
"user": userThreeRef,
},
},
)
Expect(err).ToNot(HaveOccurred())
actual := comment{}
time.Sleep(sleepDuration)
db.Copy().DB(database).C("comment").Find(bson.M{"text": "this is my first comment"}).One(&actual)
Expect(actual.Meta["username"]).To(Equal("songoku"))
Expect(actual.Meta["gender"]).To(Equal("male"))
_, err = db.DB(database).C("comment").UpdateAll(
bson.M{
"user": userThreeRef,
},
bson.M{
"$set": bson.M{
"user": userOneRef,
},
},
)
Expect(err).ToNot(HaveOccurred())
})
It("will also work with answers and different mapping", func() {
db.DB(database).C("answer").Insert(&answer{AnswerText: answerString, User: userOneRef})
actual := answer{}
time.Sleep(sleepDuration)
db.Copy().DB(database).C("answer").Find(bson.M{"answerText": answerString}).One(&actual)
Expect(actual.Meta["username"]).To(Equal("naan"))
Expect(actual.Meta["name"]).To(Equal(map[string]interface{}{
"firstName": "Naan",
"lastName": "Waana",
}))
})
It("will then update usernames everywhere", func() {
_, err := db.DB(database).C("user").UpdateAll(
bson.M{"username": "naan"},
bson.M{
"$set": bson.M{
"username": "anonym",
"name": bson.M{
"firstName": "Not",
"lastName": "Known",
},
},
},
)
Expect(err).ToNot(HaveOccurred())
time.Sleep(sleepDuration)
actual := answer{}
db.Copy().DB(database).C("answer").Find(bson.M{"answerText": answerString}).One(&actual)
Expect(actual.Meta["username"]).To(Equal("anonym"))
Expect(actual.Meta["name"]).To(HaveKey("firstName"))
Expect(actual.Meta["name"]).To(HaveKey("lastName"))
Expect(actual.Meta["name"].(map[string]interface{})["firstName"]).To(Equal("Not"))
Expect(actual.Meta["name"].(map[string]interface{})["lastName"]).To(Equal("Known"))
})
It("will work with upsert id on the user as well", func() {
cl, err := db.DB(database).C("user").UpsertId(
userOneRef.Id,
bson.M{
"$set": bson.M{
"username": "ironman",
"gender": "male",
"name": bson.M{
"firstName": "Tony",
"lastName": "Stark",
},
},
},
)
Expect(err).ToNot(HaveOccurred())
Expect(cl.Updated).To(Equal(1))
time.Sleep(sleepDuration)
actual := answer{}
db.Copy().DB(database).C("answer").Find(bson.M{"answerText": answerString}).One(&actual)
Expect(actual.Meta["username"]).To(Equal("ironman"))
Expect(actual.Meta["name"]).To(HaveKey("firstName"))
Expect(actual.Meta["name"]).To(HaveKey("lastName"))
Expect(actual.Meta["name"].(map[string]interface{})["firstName"]).To(Equal("Tony"))
Expect(actual.Meta["name"].(map[string]interface{})["lastName"]).To(Equal("Stark"))
actualC := comment{}
time.Sleep(sleepDuration)
db.Copy().DB(database).C("comment").Find(bson.M{"text": "this is my first comment"}).One(&actualC)
Expect(actualC.Meta["username"]).To(Equal("ironman"))
Expect(actualC.Meta["gender"]).To(Equal("male"))
})
It("will work with upserts on the user as well", func() {
cl, err := db.DB(database).C("user").Upsert(
bson.M{
"username": "ironman",
},
bson.M{
"$set": bson.M{
"username": "blackwidow",
"gender": "female",
"name": bson.M{
"firstName": "Natasha",
"lastName": "Romanoff",
},
},
},
)
Expect(err).ToNot(HaveOccurred())
Expect(cl.Updated).To(Equal(1))
time.Sleep(sleepDuration)
actual := answer{}
db.Copy().DB(database).C("answer").Find(bson.M{"answerText": answerString}).One(&actual)
Expect(actual.Meta["username"]).To(Equal("blackwidow"))
Expect(actual.Meta["name"]).To(HaveKey("firstName"))
Expect(actual.Meta["name"]).To(HaveKey("lastName"))
Expect(actual.Meta["name"].(map[string]interface{})["firstName"]).To(Equal("Natasha"))
Expect(actual.Meta["name"].(map[string]interface{})["lastName"]).To(Equal("Romanoff"))
actualC := comment{}
time.Sleep(sleepDuration)
db.Copy().DB(database).C("comment").Find(bson.M{"text": "this is my first comment"}).One(&actualC)
Expect(actualC.Meta["username"]).To(Equal("blackwidow"))
Expect(actualC.Meta["gender"]).To(Equal("female"))
})
It("will also update multiple comments for multiple user updates", func() {
err := db.DB(database).C("user").Insert(
bson.M{
"_id": userTwoRef.Id,
"username": "hawkeye",
"gender": "male",
"name": bson.M{
"firstName": "Clinton Francis",
"lastName": "Barton",
},
},
)
answerString = "I am hawkeye"
db.DB(database).C("answer").Insert(&answer{AnswerText: answerString, User: userTwoRef})
time.Sleep(sleepDuration)
actual := answer{}
db.Copy().DB(database).C("answer").Find(bson.M{"answerText": answerString}).One(&actual)
Expect(actual.Meta["username"]).To(Equal("hawkeye"))
Expect(actual.Meta["name"]).To(HaveKey("firstName"))
Expect(actual.Meta["name"]).To(HaveKey("lastName"))
Expect(actual.Meta["name"].(map[string]interface{})["firstName"]).To(Equal("Clinton Francis"))
Expect(actual.Meta["name"].(map[string]interface{})["lastName"]).To(Equal("Barton"))
Expect(err).ToNot(HaveOccurred())
db.DB(database).C("comment").Insert(
bson.M{
"text": "hawkeye comment",
"user": userTwoRef,
},
)
actualComment := comment{}
time.Sleep(sleepDuration)
db.Copy().DB(database).C("comment").Find(bson.M{"text": "hawkeye comment"}).One(&actualComment)
Expect(actualComment.Meta["username"]).To(Equal("hawkeye"))
Expect(actualComment.Meta["gender"]).To(Equal("male"))
cl, err := db.Copy().DB(database).C("user").UpdateAll(
bson.M{},
bson.M{
"$set": bson.M{
"gender": "confidential",
},
})
Expect(err).ToNot(HaveOccurred())
Expect(cl.Updated).To(BeNumerically(">", 1))
time.Sleep(2 * sleepDuration)
iter := db.Copy().DB(database).C("comment").Find(bson.M{"meta": bson.M{"$exists": true}}).Iter()
for iter.Next(&actualComment) {
Expect(actualComment.Meta["gender"]).To(Equal("confidential"))
}
})
})
Context("test GetValue", func() {
It("will find the first value", func() {
testReference := mgo.DBRef{
Collection: "diff",
Database: "this",
Id: bson.ObjectIdHex("569e787d14b9802c9628b300"),
}
toTest := map[string]interface{}{
"fish": map[string]interface{}{
"dog": "cat",
},
"author": testReference,
"tree": nil,
"$set": map[string]interface{}{
"user": map[string]interface{}{
"$id": "id",
},
},
}
actual := GetValue("fish.dog", toTest)
Expect(actual).To(Equal("cat"))
actual = GetValue("invalid", toTest)
Expect(actual).To(BeNil())
actual = GetValue("$set.user", toTest)
Expect(map[string]interface{}{"$id": "id"}).To(Equal(actual))
actual = GetValue("tree", toTest)
Expect(actual).To(BeNil())
actual = GetValue("author", toTest)
Expect(actual).To(Equal(testReference))
actual = GetValue("author.fisch.baum", toTest)
Expect(actual).To(BeNil())
actual = GetValue("fish", toTest)
Expect(actual).To(Equal(map[string]interface{}{"dog": "cat"}))
})
})
Context("Bulk testcases", func() {
var (
db *mgo.Session
)
type item struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Name string
Price float64
}
type order struct {
Item mgo.DBRef
Name string
Information map[string]interface{}
}
BeforeEach(func() {
var err error
db, err = mgo.Dial("localhost:30000,localhost:30001,localhost:30002")
Expect(err).ToNot(HaveOccurred())
})
It("will work with bulk inserts", func() {
connection := db.Copy()
connection.SetBatch(100)
connection.SetMode(mgo.Strong, true)
collection := connection.DB(database).C("item")
pipeline := collection.Bulk()
pipeline.Unordered()
ids := make([]bson.ObjectId, 1000)
for i := 0; i < 1000; i++ {
ids[i] = bson.NewObjectId()
pipeline.Insert(item{
ID: ids[i],
Name: fmt.Sprintf("Item #%d", i),
Price: float64(i) * 2.3},
)
}
_, err := pipeline.Run()
Expect(err).ToNot(HaveOccurred())
time.Sleep(sleepDuration)
pipeline = connection.DB(database).C("order").Bulk()
pipeline.Unordered()
var itemRef mgo.DBRef
for i, id := range ids {
itemRef = mgo.DBRef{
Database: database,
Id: id,
Collection: "item",
}
pipeline.Insert(order{Name: fmt.Sprintf("Order #%d", i), Item: itemRef})
}
_, err = pipeline.Run()
Expect(err).ToNot(HaveOccurred())
time.Sleep(50 * sleepDuration) // 1000 single updates take long time :-( 2s to be safe on travis
n, err := db.Copy().DB(database).C("order").Find(
bson.M{"information.price": bson.M{"$exists": true}},
).Count()
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(1000))
})
})
})