1
1
package redkeep_test
2
2
3
3
import (
4
+ "bytes"
5
+ "fmt"
4
6
"io/ioutil"
5
7
"log"
6
8
"time"
7
9
8
10
"gopkg.in/mgo.v2"
9
11
"gopkg.in/mgo.v2/bson"
10
12
13
+ "text/template"
14
+
11
15
. "github.com/manyminds/redkeep"
12
16
13
17
. "github.com/onsi/ginkgo"
14
18
. "github.com/onsi/gomega"
15
19
)
16
20
21
+ var testConfigurationTemplate = `{
22
+ "mongo": {
23
+ "connectionURI": "localhost:30000,localhost:30001,localhost:30002"
24
+ },
25
+ "watches": [
26
+ {
27
+ "trackCollection": "{{.Database}}.user",
28
+ "trackFields": ["username", "gender"],
29
+ "targetCollection": "{{.Database}}.comment",
30
+ "targetNormalizedField": "meta",
31
+ "triggerReference": "user",
32
+ "behaviourSettings": {
33
+ "cascadeDelete": false
34
+ }
35
+ },
36
+ {
37
+ "trackCollection": "{{.Database}}.user",
38
+ "trackFields": ["name", "username"],
39
+ "targetCollection": "{{.Database}}.answer",
40
+ "targetNormalizedField": "meta",
41
+ "triggerReference": "user",
42
+ "behaviourSettings": {
43
+ "cascadeDelete": false
44
+ }
45
+ }
46
+ ]
47
+ }`
48
+
17
49
var _ = Describe ("Tail" , func () {
18
50
var (
19
- running chan bool
51
+ running chan bool
52
+ database string
20
53
)
21
54
22
55
BeforeSuite (func () {
23
- file , err := ioutil .ReadFile (testConfiguration )
56
+ randomDB := func () string {
57
+ return fmt .Sprintf ("redkeep_tests_%d" , time .Now ().UnixNano ())
58
+ }()
59
+ tmp , err := template .New ("config" ).Parse (testConfigurationTemplate )
60
+ Expect (err ).ToNot (HaveOccurred ())
61
+ var data []byte
62
+ buffer := bytes .NewBuffer (data )
63
+ err = tmp .Execute (buffer , struct { Database string }{Database : randomDB })
24
64
Expect (err ).ToNot (HaveOccurred ())
25
- config , err := NewConfiguration ( file )
65
+ data , err = ioutil . ReadAll ( buffer )
26
66
Expect (err ).ToNot (HaveOccurred ())
67
+ config , err := NewConfiguration (data )
68
+ Expect (err ).ToNot (HaveOccurred ())
69
+ database = randomDB
27
70
agent , err := NewTailAgent (* config )
28
71
if err != nil {
29
72
log .Fatal (err )
@@ -60,14 +103,14 @@ var _ = Describe("Tail", func() {
60
103
Expect (err ).ToNot (HaveOccurred ())
61
104
userID := bson .ObjectIdHex ("56a65494b204ccd1edc0b055" )
62
105
userOneRef = mgo.DBRef {
63
- Database : "testing" ,
106
+ Database : database ,
64
107
Id : userID ,
65
108
Collection : "user" ,
66
109
}
67
110
})
68
111
69
112
It ("Should update infos on insert correctly" , func () {
70
- db .DB ("testing" ).C ("user" ).Insert (
113
+ db .DB (database ).C ("user" ).Insert (
71
114
bson.M {
72
115
"_id" : userOneRef .Id ,
73
116
"username" : "naan" ,
@@ -79,7 +122,7 @@ var _ = Describe("Tail", func() {
79
122
},
80
123
)
81
124
82
- db .DB ("testing" ).C ("comment" ).Insert (
125
+ db .DB (database ).C ("comment" ).Insert (
83
126
bson.M {
84
127
"text" : "this is my first comment" ,
85
128
"user" : userOneRef ,
@@ -88,18 +131,18 @@ var _ = Describe("Tail", func() {
88
131
89
132
actual := comment {}
90
133
time .Sleep (10 * time .Millisecond )
91
- db .Copy ().DB ("testing" ).C ("comment" ).Find (bson.M {}).One (& actual )
134
+ db .Copy ().DB (database ).C ("comment" ).Find (bson.M {}).One (& actual )
92
135
93
136
Expect (actual .Meta ["username" ]).To (Equal ("naan" ))
94
137
Expect (actual .Meta ["gender" ]).To (Equal ("male" ))
95
138
})
96
139
97
140
It ("will also work with answers and different mapping" , func () {
98
- db .DB ("testing" ).C ("answer" ).Insert (& answer {AnswerText : "this is my answer" , User : userOneRef })
141
+ db .DB (database ).C ("answer" ).Insert (& answer {AnswerText : "this is my answer" , User : userOneRef })
99
142
100
143
actual := answer {}
101
144
time .Sleep (10 * time .Millisecond )
102
- db .Copy ().DB ("testing" ).C ("answer" ).Find (bson.M {"answerText" : "this is my answer" }).One (& actual )
145
+ db .Copy ().DB (database ).C ("answer" ).Find (bson.M {"answerText" : "this is my answer" }).One (& actual )
103
146
104
147
Expect (actual .Meta ["username" ]).To (Equal ("naan" ))
105
148
Expect (actual .Meta ["name" ]).To (Equal (map [string ]interface {}{
@@ -109,7 +152,7 @@ var _ = Describe("Tail", func() {
109
152
})
110
153
111
154
It ("will then update usernames everywhere" , func () {
112
- _ , err := db .DB ("testing" ).C ("user" ).UpdateAll (
155
+ _ , err := db .DB (database ).C ("user" ).UpdateAll (
113
156
bson.M {"username" : "naan" },
114
157
bson.M {
115
158
"$set" : bson.M {
@@ -125,13 +168,11 @@ var _ = Describe("Tail", func() {
125
168
Expect (err ).ToNot (HaveOccurred ())
126
169
time .Sleep (10 * time .Millisecond )
127
170
actual := answer {}
128
- db .Copy ().DB ("testing" ).C ("answer" ).Find (bson.M {"answerText" : "this is my answer" }).One (& actual )
171
+ db .Copy ().DB (database ).C ("answer" ).Find (bson.M {"answerText" : "this is my answer" }).One (& actual )
129
172
130
173
Expect (actual .Meta ["username" ]).To (Equal ("anonym" ))
131
- Expect (actual .Meta ["name" ]).To (Equal (map [string ]interface {}{
132
- "firstName" : "Not" ,
133
- "lastName" : "Known" ,
134
- }))
174
+ Expect (actual .Meta ["name" ]).To (HaveKey ("firstName" ))
175
+ Expect (actual .Meta ["name" ]).To (HaveKey ("lastName" ))
135
176
})
136
177
})
137
178
0 commit comments