-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk_test.go
311 lines (276 loc) · 8.32 KB
/
bulk_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
package mgo
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestBulk_InsertError(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
session := ctx.mongo
coll := session.DB("mydb").C("mycoll")
bulk := coll.Bulk()
bulk.Insert(M{"_id": 1}, M{"_id": 2}, M{"_id": 2}, M{"_id": 3})
_, err = bulk.Run()
So(err, ShouldBeError, "{E11000 duplicate key error collection: mydb.mycoll index: _id_ dup key: { _id: 2 }}")
So(IsDup(err), ShouldBeTrue)
type doc struct {
N int `bson:"_id"`
}
var res []doc
err = coll.Find(nil).Sort("_id").All(&res)
So(err, ShouldBeNil)
So(res, ShouldResemble, []doc{{1}, {2}})
})
}
func TestBulk_InsertErrorUnordered(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
session := ctx.mongo
coll := session.DB("mydb").C("mycoll")
bulk := coll.Bulk()
bulk.Unordered()
bulk.Insert(M{"_id": 1}, M{"_id": 2}, M{"_id": 2}, M{"_id": 3})
_, err = bulk.Run()
So(err, ShouldBeError, "{E11000 duplicate key error collection: mydb.mycoll index: _id_ dup key: { _id: 2 }}")
type doc struct {
N int `bson:"_id"`
}
var res []doc
err = coll.Find(nil).Sort("_id").All(&res)
So(err, ShouldBeNil)
So(res, ShouldResemble, []doc{{1}, {2}, {3}})
})
}
func TestBulk_InsertErrorUnorderedSplitBatch(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
session := ctx.mongo
coll := session.DB("mydb").C("mycoll")
bulk := coll.Bulk()
bulk.Unordered()
const total = 4096
type doc struct {
Id int `bson:"_id"`
}
docs := make([]interface{}, total)
for i := 0; i < total; i++ {
docs[i] = doc{i}
}
docs[1] = doc{0}
bulk.Insert(docs...)
_, err = bulk.Run()
So(err, ShouldBeError, "{E11000 duplicate key error collection: mydb.mycoll index: _id_ dup key: { _id: 0 }}")
n, err := coll.Count()
So(err, ShouldBeNil)
So(n, ShouldEqual, total-1)
var res doc
err = coll.FindId(1500).One(&res)
So(err, ShouldBeNil)
So(res.Id, ShouldEqual, 1500)
})
}
func TestBulk_InsertErrorString(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
session := ctx.mongo
coll := session.DB("mydb").C("mycoll")
// If it's just the same string multiple times, join it into a single message.
bulk := coll.Bulk()
bulk.Unordered()
bulk.Insert(M{"_id": 1}, M{"_id": 2}, M{"_id": 2})
_, err = bulk.Run()
So(err, ShouldHaveSameTypeAs, &BulkError{})
So(IsDup(err), ShouldEqual, true)
// With matching errors but different messages, present them all.
bulk = coll.Bulk()
bulk.Unordered()
bulk.Insert(M{"_id": "dupone"}, M{"_id": "dupone"}, M{"_id": "duptwo"}, M{"_id": "duptwo"})
_, err = bulk.Run()
So(err, ShouldHaveSameTypeAs, &BulkError{})
So(IsDup(err), ShouldEqual, true)
// With mixed errors, present them all.
bulk = coll.Bulk()
bulk.Unordered()
bulk.Insert(M{"_id": 1}, M{"_id": []int{2}})
_, err = bulk.Run()
So(IsDup(err), ShouldEqual, false)
})
}
func Test_BulkErrorCases(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
session := ctx.mongo
coll := session.DB("mydb").C("mycoll")
bulk := coll.Bulk()
// There's a limit of 1000 operations per command, so
// this forces the more complex indexing logic to act.
for i := 0; i < 20; i++ {
switch i {
case 3, 14:
bulk.Insert(M{"_id": "dupone"})
case 7, 17:
bulk.Insert(M{"_id": "duptwo"})
default:
bulk.Insert(M{"_id": i})
}
}
_, err = bulk.Run()
So(err, ShouldNotHaveSameTypeAs, BulkError{})
})
}
func TestBulk_Update(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
session := ctx.mongo
coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"n": 1}, M{"n": 2}, M{"n": 3})
So(err, ShouldBeNil)
bulk := coll.Bulk()
bulk.Update(M{"n": 1}, M{"$set": M{"n": 1}})
bulk.Update(M{"n": 2}, M{"$set": M{"n": 20}})
bulk.Update(M{"n": 5}, M{"$set": M{"n": 50}}) // Won't match.
bulk.Update(M{"n": 1}, M{"$set": M{"n": 10}}, M{"n": 3}, M{"$set": M{"n": 30}})
r, err := bulk.Run()
So(err, ShouldBeNil)
So(r.Matched, ShouldEqual, 4)
So(r.Modified, ShouldEqual, 3)
type doc struct{ N int }
var res []doc
err = coll.Find(nil).Sort("n").All(&res)
So(err, ShouldBeNil)
So(res, ShouldResemble, []doc{{10}, {20}, {30}})
})
}
func TestBulk_UpdateError(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
var session = ctx.mongo
coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"n": 1}, M{"n": 2}, M{"n": 3})
So(err, ShouldBeNil)
bulk := coll.Bulk()
bulk.Update(
M{"n": 1}, M{"$set": M{"n": 10}},
M{"n": 2}, M{"$set": M{"n": 20, "_id": 20}},
M{"n": 3}, M{"$set": M{"n": 30}},
)
r, err := bulk.Run()
So(err, ShouldBeError, "{Performing an update on the path '_id' would modify the immutable field '_id'}")
So(r, ShouldHaveSameTypeAs, &BulkResult{})
type doc struct{ N int }
var res []doc
err = coll.Find(nil).Sort("n").All(&res)
So(err, ShouldBeNil)
So(res, ShouldResemble, []doc{{2}, {3}, {10}})
})
}
func TestBulk_UpdateAll(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
var session = ctx.mongo
coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"n": 1}, M{"n": 2}, M{"n": 3})
So(err, ShouldBeNil)
bulk := coll.Bulk()
bulk.UpdateAll(M{"n": 1}, M{"$set": M{"n": 10}})
bulk.UpdateAll(M{"n": 2}, M{"$set": M{"n": 2}}) // Won't change.
bulk.UpdateAll(M{"n": 5}, M{"$set": M{"n": 50}}) // Won't match.
bulk.UpdateAll(M{}, M{"$inc": M{"n": 1}}, M{"n": 11}, M{"$set": M{"n": 5}})
r, err := bulk.Run()
So(err, ShouldBeNil)
So(r.Matched, ShouldEqual, 6)
So(r.Modified, ShouldEqual, 5)
type doc struct{ N int }
var res []doc
err = coll.Find(nil).Sort("n").All(&res)
So(err, ShouldBeNil)
So(res, ShouldResemble, []doc{{3}, {4}, {5}})
})
}
func TestBulk_MixedUnordered(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
var session = ctx.mongo
coll := session.DB("mydb").C("mycoll")
// Abuse undefined behavior to ensure the desired implementation is in place.
bulk := coll.Bulk()
bulk.Unordered()
bulk.Insert(M{"n": 1})
bulk.Update(M{"n": 2}, M{"$inc": M{"n": 1}})
bulk.Insert(M{"n": 2})
bulk.Update(M{"n": 3}, M{"$inc": M{"n": 1}})
bulk.Update(M{"n": 1}, M{"$inc": M{"n": 1}})
bulk.Insert(M{"n": 3})
r, err := bulk.Run()
So(err, ShouldBeNil)
So(r.Matched, ShouldEqual, 3)
So(r.Modified, ShouldEqual, 3)
type doc struct{ N int }
var res []doc
err = coll.Find(nil).Sort("n").All(&res)
So(err, ShouldBeNil)
So(res, ShouldResemble, []doc{{2}, {3}, {4}})
})
}
func TestBulk_Upsert(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
var session = ctx.mongo
coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"n": 1}, M{"n": 2}, M{"n": 3})
So(err, ShouldBeNil)
bulk := coll.Bulk()
bulk.Upsert(M{"n": 2}, M{"$set": M{"n": 20}})
bulk.Upsert(M{"n": 4}, M{"$set": M{"n": 40}}, M{"n": 3}, M{"$set": M{"n": 30}})
r, err := bulk.Run()
So(err, ShouldBeNil)
So(r, ShouldHaveSameTypeAs, &BulkResult{})
type doc struct{ N int }
var res []doc
err = coll.Find(nil).Sort("n").All(&res)
So(err, ShouldBeNil)
So(res, ShouldResemble, []doc{{1}, {20}, {30}, {40}})
})
}
func TestBulk_Remove(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
var session = ctx.mongo
coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"n": 1}, M{"n": 2}, M{"n": 3}, M{"n": 4}, M{"n": 4})
So(err, ShouldBeNil)
bulk := coll.Bulk()
bulk.Remove(M{"n": 1})
bulk.Remove(M{"n": 2}, M{"n": 4})
r, err := bulk.Run()
So(err, ShouldBeNil)
So(r.Matched, ShouldEqual, 0)
So(r.Deleted, ShouldEqual, 3)
type doc struct{ N int }
var res []doc
err = coll.Find(nil).Sort("n").All(&res)
So(err, ShouldBeNil)
So(res, ShouldResemble, []doc{{3}, {4}})
})
}
func TestBulk_RemoveAll(t *testing.T) {
MongoTest(t, func(ctx *TestContext) {
var err error
var session = ctx.mongo
coll := session.DB("mydb").C("mycoll")
err = coll.Insert(M{"n": 1}, M{"n": 2}, M{"n": 3}, M{"n": 4}, M{"n": 4})
So(err, ShouldBeNil)
bulk := coll.Bulk()
bulk.RemoveAll(M{"n": 1})
bulk.RemoveAll(M{"n": 2}, M{"n": 4})
r, err := bulk.Run()
So(err, ShouldBeNil)
So(r.Matched, ShouldEqual, 0)
So(r.Deleted, ShouldEqual, 4)
type doc struct{ N int }
var res []doc
err = coll.Find(nil).Sort("n").All(&res)
So(err, ShouldBeNil)
So(res, ShouldResemble, []doc{{3}})
})
}