@@ -36,6 +36,93 @@ public void TestFixtureSetUp()
36
36
_collection = Configuration . TestCollection ;
37
37
}
38
38
39
+ [ Test ]
40
+ public void TestBatchSplittingBySizeWithErrorsOrdered ( )
41
+ {
42
+ using ( _server . RequestStart ( null , ReadPreference . Primary ) )
43
+ {
44
+ var serverInstance = _server . RequestConnection . ServerInstance ;
45
+
46
+ _collection . Drop ( ) ;
47
+
48
+ var documents = new BsonDocument [ 8 ] ;
49
+ for ( var i = 0 ; i < 6 ; i ++ )
50
+ {
51
+ documents [ i ] = new BsonDocument { { "_id" , i } , { "a" , new string ( 'x' , 4 * 1024 * 1024 ) } } ;
52
+ }
53
+ documents [ 6 ] = new BsonDocument ( "_id" , 0 ) ; // will fail
54
+ documents [ 7 ] = new BsonDocument ( "_id" , 100 ) ;
55
+
56
+ var bulk = _collection . InitializeOrderedBulkOperation ( ) ;
57
+ for ( var i = 0 ; i < 8 ; i ++ )
58
+ {
59
+ bulk . Insert ( documents [ i ] ) ;
60
+ }
61
+ var exception = Assert . Throws < BulkWriteException > ( ( ) => { bulk . Execute ( ) ; } ) ;
62
+ var result = exception . Result ;
63
+
64
+ Assert . IsNull ( exception . WriteConcernError ) ;
65
+ Assert . AreEqual ( 1 , exception . WriteErrors . Count ) ;
66
+ var writeError = exception . WriteErrors [ 0 ] ;
67
+ Assert . AreEqual ( 6 , writeError . Index ) ;
68
+ Assert . AreEqual ( 11000 , writeError . Code ) ;
69
+
70
+ var expectedResult = new ExpectedResult
71
+ {
72
+ InsertedCount = 6 ,
73
+ ProcessedRequestsCount = 7 ,
74
+ RequestCount = 8
75
+ } ;
76
+ CheckExpectedResult ( expectedResult , result ) ;
77
+
78
+ var expectedDocuments = documents . Take ( 6 ) ;
79
+ Assert . That ( _collection . FindAll ( ) , Is . EqualTo ( expectedDocuments ) ) ;
80
+ }
81
+ }
82
+
83
+ [ Test ]
84
+ public void TestBatchSplittingBySizeWithErrorsUnordered ( )
85
+ {
86
+ using ( _server . RequestStart ( null , ReadPreference . Primary ) )
87
+ {
88
+ var serverInstance = _server . RequestConnection . ServerInstance ;
89
+
90
+ _collection . Drop ( ) ;
91
+
92
+ var documents = new BsonDocument [ 8 ] ;
93
+ for ( var i = 0 ; i < 6 ; i ++ )
94
+ {
95
+ documents [ i ] = new BsonDocument { { "_id" , i } , { "a" , new string ( 'x' , 4 * 1024 * 1024 ) } } ;
96
+ }
97
+ documents [ 6 ] = new BsonDocument ( "_id" , 0 ) ; // will fail
98
+ documents [ 7 ] = new BsonDocument ( "_id" , 100 ) ;
99
+
100
+ var bulk = _collection . InitializeUnorderedBulkOperation ( ) ;
101
+ for ( var i = 0 ; i < 8 ; i ++ )
102
+ {
103
+ bulk . Insert ( documents [ i ] ) ;
104
+ }
105
+ var exception = Assert . Throws < BulkWriteException > ( ( ) => { bulk . Execute ( ) ; } ) ;
106
+ var result = exception . Result ;
107
+
108
+ Assert . IsNull ( exception . WriteConcernError ) ;
109
+ Assert . AreEqual ( 1 , exception . WriteErrors . Count ) ;
110
+ var writeError = exception . WriteErrors [ 0 ] ;
111
+ Assert . AreEqual ( 6 , writeError . Index ) ;
112
+ Assert . AreEqual ( 11000 , writeError . Code ) ;
113
+
114
+ var expectedResult = new ExpectedResult
115
+ {
116
+ InsertedCount = 7 ,
117
+ RequestCount = 8
118
+ } ;
119
+ CheckExpectedResult ( expectedResult , result ) ;
120
+
121
+ var expectedDocuments = Enumerable . Range ( 0 , 8 ) . Where ( i => i != 6 ) . Select ( i => documents [ i ] ) ;
122
+ Assert . That ( _collection . FindAll ( ) , Is . EquivalentTo ( expectedDocuments ) ) ;
123
+ }
124
+ }
125
+
39
126
[ Test ]
40
127
[ TestCase ( false ) ]
41
128
[ TestCase ( true ) ]
@@ -180,44 +267,91 @@ public void TestInsertOneDocument(bool ordered)
180
267
}
181
268
182
269
[ Test ]
183
- public void TestMixedOrdered ( )
270
+ public void TestMixedOperationsOrdered ( )
271
+ {
272
+ using ( _server . RequestStart ( null , ReadPreference . Primary ) )
273
+ {
274
+ var serverInstance = _server . RequestConnection . ServerInstance ;
275
+
276
+ _collection . Drop ( ) ;
277
+
278
+ var bulk = _collection . InitializeOrderedBulkOperation ( ) ;
279
+ bulk . Insert ( new BsonDocument ( "a" , 1 ) ) ;
280
+ bulk . Find ( Query . EQ ( "a" , 1 ) ) . UpdateOne ( Update . Set ( "b" , 1 ) ) ;
281
+ bulk . Find ( Query . EQ ( "a" , 2 ) ) . Upsert ( ) . UpdateOne ( Update . Set ( "b" , 2 ) ) ;
282
+ bulk . Insert ( new BsonDocument ( "a" , 3 ) ) ;
283
+ bulk . Find ( Query . EQ ( "a" , 3 ) ) . Remove ( ) ;
284
+ var result = bulk . Execute ( ) ;
285
+
286
+ var expectedResult = new ExpectedResult
287
+ {
288
+ DeletedCount = 1 , InsertedCount = 2 , MatchedCount = 1 , ModifiedCount = 1 , RequestCount = 5 , UpsertsCount = 1 ,
289
+ IsModifiedCountAvailable = serverInstance . Supports ( FeatureId . WriteCommands )
290
+ } ;
291
+ CheckExpectedResult ( expectedResult , result ) ;
292
+
293
+ var upserts = result . Upserts ;
294
+ Assert . AreEqual ( 1 , upserts . Count ) ;
295
+ Assert . IsInstanceOf < BsonObjectId > ( upserts [ 0 ] . Id ) ;
296
+ Assert . AreEqual ( 2 , upserts [ 0 ] . Index ) ;
297
+
298
+ var expectedDocuments = new BsonDocument [ ]
299
+ {
300
+ new BsonDocument { { "a" , 1 } , { "b" , 1 } } ,
301
+ new BsonDocument { { "a" , 2 } , { "b" , 2 } }
302
+ } ;
303
+ Assert . That ( _collection . FindAll ( ) . SetFields ( Fields . Exclude ( "_id" ) ) , Is . EquivalentTo ( expectedDocuments ) ) ;
304
+ }
305
+ }
306
+
307
+ [ Test ]
308
+ public void TestMixedOperationsUnordered ( )
184
309
{
185
310
using ( _server . RequestStart ( null , ReadPreference . Primary ) )
186
311
{
187
312
var serverInstance = _server . RequestConnection . ServerInstance ;
188
313
189
314
var documents = new BsonDocument [ ]
190
315
{
191
- new BsonDocument { { "_id" , 1 } , { "x" , 1 } } ,
192
- new BsonDocument { { "_id" , 2 } , { "x" , 2 } } ,
193
- new BsonDocument { { "_id" , 3 } , { "x" , 3 } } ,
194
- new BsonDocument { { "_id" , 4 } , { "x" , 4 } }
316
+ new BsonDocument { { "a" , 1 } } ,
317
+ new BsonDocument { { "a" , 2 } }
195
318
} ;
196
319
197
320
_collection . Drop ( ) ;
198
- var bulk = _collection . InitializeOrderedBulkOperation ( ) ;
199
- bulk . Insert ( documents [ 0 ] ) ;
200
- bulk . Insert ( documents [ 1 ] ) ;
201
- bulk . Insert ( documents [ 2 ] ) ;
202
- bulk . Insert ( documents [ 3 ] ) ;
203
- bulk . Find ( Query . GT ( "x ", 2 ) ) . Update ( Update . Inc ( "x" , 10 ) ) ;
204
- bulk . Find ( Query . EQ ( "x ", 13 ) ) . RemoveOne ( ) ;
205
- bulk . Find ( Query . EQ ( "x " , 14 ) ) . RemoveOne ( ) ;
321
+ _collection . Insert ( documents [ 0 ] ) ;
322
+ _collection . Insert ( documents [ 1 ] ) ;
323
+
324
+ var bulk = _collection . InitializeUnorderedBulkOperation ( ) ;
325
+ bulk . Find ( Query . EQ ( "a" , 1 ) ) . Update ( Update . Set ( "b" , 1 ) ) ;
326
+ bulk . Find ( Query . EQ ( "a ", 2 ) ) . Remove ( ) ;
327
+ bulk . Insert ( new BsonDocument ( "a ", 3 ) ) ;
328
+ bulk . Find ( Query . EQ ( "a " , 4 ) ) . Upsert ( ) . UpdateOne ( Update . Set ( "b" , 4 ) ) ;
206
329
var result = bulk . Execute ( ) ;
207
330
208
331
var expectedResult = new ExpectedResult
209
332
{
210
- DeletedCount = 2 , InsertedCount = 4 , MatchedCount = 2 , ModifiedCount = 2 , RequestCount = 7 ,
333
+ DeletedCount = 1 ,
334
+ InsertedCount = 1 ,
335
+ MatchedCount = 1 ,
336
+ ModifiedCount = 1 ,
337
+ RequestCount = 4 ,
338
+ UpsertsCount = 1 ,
211
339
IsModifiedCountAvailable = serverInstance . Supports ( FeatureId . WriteCommands )
212
340
} ;
213
341
CheckExpectedResult ( expectedResult , result ) ;
214
342
343
+ var upserts = result . Upserts ;
344
+ Assert . AreEqual ( 1 , upserts . Count ) ;
345
+ Assert . IsInstanceOf < BsonObjectId > ( upserts [ 0 ] . Id ) ;
346
+ Assert . AreEqual ( 3 , upserts [ 0 ] . Index ) ;
347
+
215
348
var expectedDocuments = new BsonDocument [ ]
216
349
{
217
- new BsonDocument { { "_id" , 1 } , { "x" , 1 } } ,
218
- new BsonDocument { { "_id" , 2 } , { "x" , 2 } }
350
+ new BsonDocument { { "a" , 1 } , { "b" , 1 } } ,
351
+ new BsonDocument { { "a" , 3 } } ,
352
+ new BsonDocument { { "a" , 4 } , { "b" , 4 } }
219
353
} ;
220
- Assert . That ( _collection . FindAll ( ) , Is . EquivalentTo ( expectedDocuments ) ) ;
354
+ Assert . That ( _collection . FindAll ( ) . SetFields ( Fields . Exclude ( "_id" ) ) , Is . EquivalentTo ( expectedDocuments ) ) ;
221
355
}
222
356
}
223
357
@@ -278,6 +412,54 @@ public void TestMixedUpsertsUnordered()
278
412
}
279
413
}
280
414
415
+ [ Test ]
416
+ public void TestOrderedBatchWithErrors ( )
417
+ {
418
+ using ( _server . RequestStart ( null , ReadPreference . Primary ) )
419
+ {
420
+ var serverInstance = _server . RequestConnection . ServerInstance ;
421
+
422
+ _collection . Drop ( ) ;
423
+ _collection . CreateIndex ( IndexKeys . Ascending ( "a" ) , IndexOptions . SetUnique ( true ) ) ;
424
+
425
+ var bulk = _collection . InitializeOrderedBulkOperation ( ) ;
426
+ bulk . Insert ( new BsonDocument { { "b" , 1 } , { "a" , 1 } } ) ;
427
+ bulk . Find ( Query . EQ ( "b" , 2 ) ) . Upsert ( ) . UpdateOne ( Update . Set ( "a" , 1 ) ) ; // will fail
428
+ bulk . Find ( Query . EQ ( "b" , 3 ) ) . Upsert ( ) . UpdateOne ( Update . Set ( "a" , 2 ) ) ;
429
+ bulk . Find ( Query . EQ ( "b" , 2 ) ) . Upsert ( ) . UpdateOne ( Update . Set ( "a" , 1 ) ) ;
430
+ bulk . Insert ( new BsonDocument { { "b" , 4 } , { "a" , 3 } } ) ;
431
+ bulk . Insert ( new BsonDocument { { "b" , 5 } , { "a" , 1 } } ) ;
432
+ var exception = Assert . Throws < BulkWriteException > ( ( ) => { bulk . Execute ( ) ; } ) ;
433
+ var result = exception . Result ;
434
+
435
+ var expectedResult = new ExpectedResult
436
+ {
437
+ InsertedCount = 1 ,
438
+ ProcessedRequestsCount = 2 ,
439
+ RequestCount = 6 ,
440
+ IsModifiedCountAvailable = serverInstance . Supports ( FeatureId . WriteCommands )
441
+ } ;
442
+ CheckExpectedResult ( expectedResult , result ) ;
443
+
444
+ var upserts = result . Upserts ;
445
+ Assert . AreEqual ( 0 , upserts . Count ) ;
446
+
447
+ Assert . IsNull ( exception . WriteConcernError ) ;
448
+ Assert . AreEqual ( 4 , exception . UnprocessedRequests . Count ) ;
449
+
450
+ var writeErrors = exception . WriteErrors ;
451
+ Assert . AreEqual ( 1 , writeErrors . Count ) ;
452
+ Assert . AreEqual ( 1 , writeErrors [ 0 ] . Index ) ;
453
+ Assert . AreEqual ( 11000 , writeErrors [ 0 ] . Code ) ;
454
+
455
+ var expectedDocuments = new BsonDocument [ ]
456
+ {
457
+ new BsonDocument { { "b" , 1 } , { "a" , 1 } }
458
+ } ;
459
+ Assert . That ( _collection . FindAll ( ) . SetFields ( Fields . Exclude ( "_id" ) ) , Is . EquivalentTo ( expectedDocuments ) ) ;
460
+ }
461
+ }
462
+
281
463
[ Test ]
282
464
[ TestCase ( false ) ]
283
465
[ TestCase ( true ) ]
@@ -312,6 +494,37 @@ public void TestRemoveMultiple(bool ordered)
312
494
}
313
495
}
314
496
497
+ [ Test ]
498
+ [ TestCase ( false ) ]
499
+ [ TestCase ( true ) ]
500
+ public void TestRemoveOneOnlyRemovesOneDocument ( bool ordered )
501
+ {
502
+ using ( _server . RequestStart ( null , ReadPreference . Primary ) )
503
+ {
504
+ var serverInstance = _server . RequestConnection . ServerInstance ;
505
+
506
+ var documents = new BsonDocument [ ]
507
+ {
508
+ new BsonDocument ( "key" , 1 ) ,
509
+ new BsonDocument ( "key" , 1 )
510
+ } ;
511
+
512
+ _collection . Drop ( ) ;
513
+ _collection . Insert ( documents [ 0 ] ) ;
514
+ _collection . Insert ( documents [ 1 ] ) ;
515
+
516
+ var bulk = InitializeBulkOperation ( _collection , ordered ) ;
517
+ bulk . Find ( new QueryDocument ( ) ) . RemoveOne ( ) ;
518
+ var result = bulk . Execute ( ) ;
519
+
520
+ var expectedResult = new ExpectedResult { DeletedCount = 1 } ;
521
+ CheckExpectedResult ( expectedResult , result ) ;
522
+
523
+ var expectedDocuments = new [ ] { new BsonDocument ( "key" , 1 ) } ;
524
+ Assert . That ( _collection . FindAll ( ) . SetFields ( Fields . Exclude ( "_id" ) ) , Is . EquivalentTo ( expectedDocuments ) ) ;
525
+ }
526
+ }
527
+
315
528
[ Test ]
316
529
[ TestCase ( false ) ]
317
530
[ TestCase ( true ) ]
@@ -423,6 +636,61 @@ public void TestReplaceOneWithMultipleMatchingDocuments(bool ordered)
423
636
}
424
637
}
425
638
639
+ [ Test ]
640
+ public void TestUnorderedBatchWithErrors ( )
641
+ {
642
+ using ( _server . RequestStart ( null , ReadPreference . Primary ) )
643
+ {
644
+ var serverInstance = _server . RequestConnection . ServerInstance ;
645
+
646
+ _collection . Drop ( ) ;
647
+ _collection . CreateIndex ( IndexKeys . Ascending ( "a" ) , IndexOptions . SetUnique ( true ) ) ;
648
+
649
+ var bulk = _collection . InitializeUnorderedBulkOperation ( ) ;
650
+ bulk . Insert ( new BsonDocument { { "b" , 1 } , { "a" , 1 } } ) ;
651
+ bulk . Find ( Query . EQ ( "b" , 2 ) ) . Upsert ( ) . UpdateOne ( Update . Set ( "a" , 1 ) ) ;
652
+ bulk . Find ( Query . EQ ( "b" , 3 ) ) . Upsert ( ) . UpdateOne ( Update . Set ( "a" , 2 ) ) ;
653
+ bulk . Find ( Query . EQ ( "b" , 2 ) ) . Upsert ( ) . UpdateOne ( Update . Set ( "a" , 1 ) ) ;
654
+ bulk . Insert ( new BsonDocument { { "b" , 4 } , { "a" , 3 } } ) ;
655
+ bulk . Insert ( new BsonDocument { { "b" , 5 } , { "a" , 1 } } ) ;
656
+ var exception = Assert . Throws < BulkWriteException > ( ( ) => { bulk . Execute ( ) ; } ) ;
657
+ var result = exception . Result ;
658
+
659
+ var expectedResult = new ExpectedResult
660
+ {
661
+ InsertedCount = 2 , RequestCount = 6 , UpsertsCount = 1 ,
662
+ IsModifiedCountAvailable = serverInstance . Supports ( FeatureId . WriteCommands )
663
+ } ;
664
+ CheckExpectedResult ( expectedResult , result ) ;
665
+
666
+ var upserts = result . Upserts ;
667
+ Assert . AreEqual ( 1 , upserts . Count ) ;
668
+ Assert . IsInstanceOf < BsonObjectId > ( upserts [ 0 ] . Id ) ;
669
+ Assert . AreEqual ( 2 , upserts [ 0 ] . Index ) ;
670
+
671
+ Assert . IsNull ( exception . WriteConcernError ) ;
672
+ Assert . AreEqual ( 0 , exception . UnprocessedRequests . Count ) ;
673
+
674
+ var writeErrors = exception . WriteErrors ;
675
+ Assert . AreEqual ( 3 , writeErrors . Count ) ;
676
+ Assert . AreEqual ( 1 , writeErrors [ 0 ] . Index ) ;
677
+ Assert . AreEqual ( 3 , writeErrors [ 1 ] . Index ) ;
678
+ Assert . AreEqual ( 5 , writeErrors [ 2 ] . Index ) ;
679
+ Assert . IsTrue ( writeErrors . All ( e => e . Code == 11000 ) ) ;
680
+
681
+ var expectedDocuments = new BsonDocument [ ]
682
+ {
683
+ new BsonDocument { { "b" , 1 } , { "a" , 1 } } ,
684
+ serverInstance . BuildInfo . Version < new Version ( 2 , 6 , 0 ) ?
685
+ new BsonDocument { { "a" , 2 } , { "b" , 3 } } : // servers prior to 2.6 rewrite field order on update
686
+ new BsonDocument { { "b" , 3 } , { "a" , 2 } } ,
687
+ new BsonDocument { { "b" , 4 } , { "a" , 3 } }
688
+ } ;
689
+
690
+ Assert . That ( _collection . FindAll ( ) . SetFields ( Fields . Exclude ( "_id" ) ) , Is . EquivalentTo ( expectedDocuments ) ) ;
691
+ }
692
+ }
693
+
426
694
[ Ignore ]
427
695
[ Test ]
428
696
[ TestCase ( false ) ]
0 commit comments