forked from go-bongo/bongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cascade_test.go
279 lines (228 loc) · 6.7 KB
/
cascade_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
package bongo
import (
. "github.com/smartystreets/goconvey/convey"
"github.com/globalsign/mgo/bson"
"reflect"
"testing"
"time"
)
type Parent struct {
DocumentBase `bson:",inline"`
Bar string
Number int
FooBar string
Children []ChildRef
Child ChildRef
ChildProp string `bson:"childProp"`
diffTracker *DiffTracker
}
func (f *Parent) GetDiffTracker() *DiffTracker {
v := reflect.ValueOf(f.diffTracker)
if !v.IsValid() || v.IsNil() {
f.diffTracker = NewDiffTracker(f)
}
return f.diffTracker
}
type Child struct {
DocumentBase `bson:",inline"`
ParentId bson.ObjectId `bson:",omitempty"`
Name string
SubChild SubChildRef `bson:"subChild"`
ChildProp string
diffTracker *DiffTracker
}
func (c *Child) GetCascade(collection *Collection) []*CascadeConfig {
ref := ChildRef{
Id: c.Id,
Name: c.Name,
SubChild: c.SubChild,
}
connection := collection.Connection
cascadeSingle := &CascadeConfig{
Collection: connection.Collection("parents"),
Properties: []string{"_id", "name", "subChild.foo", "subChild._id"},
Data: ref,
ThroughProp: "child",
RelType: REL_ONE,
Query: bson.M{
"_id": c.ParentId,
},
}
cascadeCopy := &CascadeConfig{
Collection: connection.Collection("parents"),
Properties: []string{"childProp"},
Data: map[string]interface{}{
"childProp": c.ChildProp,
},
RelType: REL_ONE,
Query: bson.M{
"_id": c.ParentId,
},
}
cascadeMulti := &CascadeConfig{
Collection: connection.Collection("parents"),
Properties: []string{"_id", "name", "subChild.foo", "subChild._id"},
Data: ref,
ThroughProp: "children",
RelType: REL_MANY,
Query: bson.M{
"_id": c.ParentId,
},
}
if c.GetDiffTracker().Modified("ParentId") {
origId, _ := c.diffTracker.GetOriginalValue("ParentId")
if origId != nil {
oldQuery := bson.M{
"_id": origId,
}
cascadeSingle.OldQuery = oldQuery
cascadeCopy.OldQuery = oldQuery
cascadeMulti.OldQuery = oldQuery
}
}
return []*CascadeConfig{cascadeSingle, cascadeMulti, cascadeCopy}
}
func (f *Child) GetDiffTracker() *DiffTracker {
v := reflect.ValueOf(f.diffTracker)
if !v.IsValid() || v.IsNil() {
f.diffTracker = NewDiffTracker(f)
}
return f.diffTracker
}
type SubChild struct {
DocumentBase `bson:",inline"`
Foo string
ChildId bson.ObjectId
}
func (c *SubChild) GetCascade(collection *Collection) []*CascadeConfig {
ref := SubChildRef{
Id: c.Id,
Foo: c.Foo,
}
connection := collection.Connection
cascadeSingle := &CascadeConfig{
Collection: connection.Collection("children"),
Properties: []string{"_id", "foo"},
Data: ref,
ThroughProp: "subChild",
RelType: REL_ONE,
Query: bson.M{
"_id": c.ChildId,
},
Nest: true,
Instance: &Child{},
}
return []*CascadeConfig{cascadeSingle}
}
type SubChildRef struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Foo string
}
type ChildRef struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Name string
SubChild SubChildRef
}
func TestCascade(t *testing.T) {
connection := getConnection()
// defer connection.Session.Close()
Convey("Cascade Save/Delete - full runthrough", t, func() {
connection.Session.DB("bongotest").DropDatabase()
collection := connection.Collection("parents")
childCollection := connection.Collection("children")
subchildCollection := connection.Collection("subchildren")
parent := &Parent{
Bar: "Testy McGee",
Number: 5,
}
parent2 := &Parent{
Bar: "Other Parent",
Number: 10,
}
err := collection.Save(parent)
So(err, ShouldEqual, nil)
err = collection.Save(parent2)
So(err, ShouldEqual, nil)
child := &Child{
ParentId: parent.Id,
Name: "Foo McGoo",
ChildProp: "Doop McGoop",
}
err = childCollection.Save(child)
// Wait a sec for the go routine to finish.
time.Sleep(100 * time.Millisecond)
So(err, ShouldEqual, nil)
child.GetDiffTracker().Reset()
newParent := &Parent{}
collection.FindById(parent.Id, newParent)
So(newParent.Child.Name, ShouldEqual, "Foo McGoo")
So(newParent.Child.Id.Hex(), ShouldEqual, child.Id.Hex())
So(newParent.Children[0].Name, ShouldEqual, "Foo McGoo")
So(newParent.Children[0].Id.Hex(), ShouldEqual, child.Id.Hex())
// No through prop should populate directly o the parent
So(newParent.ChildProp, ShouldEqual, "Doop McGoop")
// Now change the child parent Id...
child.ParentId = parent2.Id
So(child.GetDiffTracker().Modified("ParentId"), ShouldEqual, true)
err = childCollection.Save(child)
So(err, ShouldEqual, nil)
// Wait a sec for the go routine to finish.
time.Sleep(100 * time.Millisecond)
child.diffTracker.Reset()
// Now make sure it says the parent id DIDNT change, because we just reset the tracker
So(child.GetDiffTracker().Modified("ParentId"), ShouldEqual, false)
newParent1 := &Parent{}
collection.FindById(parent.Id, newParent1)
So(newParent1.Child.Name, ShouldEqual, "")
So(newParent1.ChildProp, ShouldEqual, "")
So(len(newParent1.Children), ShouldEqual, 0)
newParent2 := &Parent{}
collection.FindById(parent2.Id, newParent2)
So(newParent2.ChildProp, ShouldEqual, "Doop McGoop")
So(newParent2.Child.Name, ShouldEqual, "Foo McGoo")
So(newParent2.Child.Id.Hex(), ShouldEqual, child.Id.Hex())
So(newParent2.Children[0].Name, ShouldEqual, "Foo McGoo")
So(newParent2.Children[0].Id.Hex(), ShouldEqual, child.Id.Hex())
// Make a new sub child, save it, and it should cascade to the child AND the parent
subChild := &SubChild{
Foo: "MySubChild",
ChildId: child.Id,
}
err = subchildCollection.Save(subChild)
So(err, ShouldEqual, nil)
// Wait a sec for the go routine to finish.
time.Sleep(100 * time.Millisecond)
// Fetch the parent
newParent3 := &Parent{}
collection.FindById(parent2.Id, newParent3)
So(newParent3.Child.SubChild.Foo, ShouldEqual, "MySubChild")
So(newParent3.Child.SubChild.Id.Hex(), ShouldEqual, subChild.Id.Hex())
newParent4 := &Parent{}
err = childCollection.DeleteDocument(child)
// Wait a sec for the go routine to finish.
time.Sleep(100 * time.Millisecond)
So(err, ShouldEqual, nil)
collection.FindById(parent2.Id, newParent4)
So(newParent4.Child.Name, ShouldEqual, "")
So(newParent4.ChildProp, ShouldEqual, "")
So(len(newParent4.Children), ShouldEqual, 0)
})
Convey("MapFromCascadeProperties", t, func() {
parent := &Parent{
Bar: "bar",
Child: ChildRef{
Name: "child",
SubChild: SubChildRef{
Foo: "foo",
},
},
Number: 5,
}
props := []string{"bar", "child.name"}
mp := MapFromCascadeProperties(props, parent)
So(len(mp), ShouldEqual, 2)
So(mp["bar"], ShouldEqual, "bar")
submp := mp["child"].(map[string]interface{})
So(submp["name"], ShouldEqual, "child")
})
}