@@ -258,6 +258,167 @@ func TestCollection_database_accessor(t *testing.T) {
258
258
require .Equal (t , coll .Database ().Name (), dbName )
259
259
}
260
260
261
+ func TestCollection_BulkWrite_writeErrors_Insert (t * testing.T ) {
262
+ if testing .Short () {
263
+ t .Skip ("skipping integration test in short mode" )
264
+ }
265
+
266
+ want := WriteError {Code : 11000 }
267
+ doc1 := NewInsertOneModel ().SetDocument (bson.D {{"_id" , "x" }})
268
+ doc2 := NewInsertOneModel ().SetDocument (bson.D {{"_id" , "y" }})
269
+ models := []WriteModel {doc1 , doc1 , doc2 , doc2 }
270
+
271
+ t .Run ("unordered" , func (t * testing.T ) {
272
+ coll := createTestCollection (t , nil , nil )
273
+ res , err := coll .BulkWrite (context .Background (), models , options .BulkWrite ().SetOrdered (false ))
274
+ require .Equal (t , res .InsertedCount , int64 (2 ))
275
+
276
+ got , ok := err .(BulkWriteException )
277
+ if ! ok {
278
+ t .Errorf ("Did not receive correct type of error. got %T; want %T" , err , WriteErrors {})
279
+ t .FailNow ()
280
+ }
281
+ if len (got .WriteErrors ) != 2 {
282
+ t .Errorf ("Incorrect number of errors receieved. got %d; want %d" , len (got .WriteErrors ), 2 )
283
+ t .FailNow ()
284
+ }
285
+ if got .WriteErrors [0 ].Code != want .Code {
286
+ t .Errorf ("Did not receive the correct error code. got %d; want %d" , got .WriteErrors [0 ].Code , want .Code )
287
+ }
288
+ })
289
+
290
+ t .Run ("ordered" , func (t * testing.T ) {
291
+ coll := createTestCollection (t , nil , nil )
292
+ res , err := coll .BulkWrite (context .Background (), models , options .BulkWrite ())
293
+ require .Equal (t , res .InsertedCount , int64 (1 ))
294
+
295
+ got , ok := err .(BulkWriteException )
296
+ if ! ok {
297
+ t .Errorf ("Did not receive correct type of error. got %T; want %T" , err , WriteErrors {})
298
+ t .FailNow ()
299
+ }
300
+ if len (got .WriteErrors ) != 1 {
301
+ t .Errorf ("Incorrect number of errors receieved. got %d; want %d" , len (got .WriteErrors ), 1 )
302
+ t .FailNow ()
303
+ }
304
+ if got .WriteErrors [0 ].Code != want .Code {
305
+ t .Errorf ("Did not receive the correct error code. got %d; want %d" , got .WriteErrors [0 ].Code , want .Code )
306
+ }
307
+ })
308
+ }
309
+
310
+ func TestCollection_BulkWrite_writeErrors_Delete (t * testing.T ) {
311
+ if testing .Short () {
312
+ t .Skip ("skipping integration test in short mode" )
313
+ }
314
+
315
+ doc := NewDeleteOneModel ().SetFilter (bson.D {{"x" , 1 }})
316
+ models := []WriteModel {doc , doc }
317
+
318
+ db := createTestDatabase (t , nil )
319
+ collName := testutil .ColName (t )
320
+ err := db .RunCommand (
321
+ context .Background (),
322
+ bsonx.Doc {
323
+ {"create" , bsonx .String (collName )},
324
+ {"capped" , bsonx .Boolean (true )},
325
+ {"size" , bsonx .Int32 (64 * 1024 )},
326
+ },
327
+ ).Err ()
328
+ require .NoError (t , err )
329
+
330
+ t .Run ("unordered" , func (t * testing.T ) {
331
+ coll := db .Collection (collName )
332
+ _ , err = coll .BulkWrite (context .Background (), models , options .BulkWrite ().SetOrdered (false ))
333
+
334
+ got , ok := err .(BulkWriteException )
335
+ if ! ok {
336
+ t .Errorf ("Did not receive correct type of error. got %T; want %T" , err , WriteErrors {})
337
+ t .FailNow ()
338
+ }
339
+ if len (got .WriteErrors ) != 2 {
340
+ t .Errorf ("Incorrect number of errors receieved. got %d; want %d" , len (got .WriteErrors ), 2 )
341
+ t .FailNow ()
342
+ }
343
+ if got .WriteErrors [0 ].Code != 20 && got .WriteErrors [0 ].Code != 10101 {
344
+ t .Errorf ("Did not receive the correct error code. got %d; want 20 or 10101" , got .WriteErrors [0 ].Code )
345
+ }
346
+ })
347
+
348
+ t .Run ("ordered" , func (t * testing.T ) {
349
+ coll := db .Collection (collName )
350
+ _ , err = coll .BulkWrite (context .Background (), models , options .BulkWrite ())
351
+
352
+ got , ok := err .(BulkWriteException )
353
+ if ! ok {
354
+ t .Errorf ("Did not receive correct type of error. got %T; want %T" , err , WriteErrors {})
355
+ t .FailNow ()
356
+ }
357
+ if len (got .WriteErrors ) != 1 {
358
+ t .Errorf ("Incorrect number of errors receieved. got %d; want %d" , len (got .WriteErrors ), 1 )
359
+ t .FailNow ()
360
+ }
361
+ if got .WriteErrors [0 ].Code != 20 && got .WriteErrors [0 ].Code != 10101 {
362
+ t .Errorf ("Did not receive the correct error code. got %d; want 20 or 10101" , got .WriteErrors [0 ].Code )
363
+ }
364
+ })
365
+ }
366
+
367
+ func TestCollection_BulkWrite_writeErrors_Update (t * testing.T ) {
368
+ if testing .Short () {
369
+ t .Skip ("skipping integration test in short mode" )
370
+ }
371
+
372
+ doc1 := NewUpdateOneModel ().SetFilter (bson.D {{"_id" , "foo" }}).SetUpdate (bson.D {{"$set" , bson.D {{"_id" , 3.14159 }}}})
373
+ doc2 := NewUpdateOneModel ().SetFilter (bson.D {{"_id" , "foo" }}).SetUpdate (bson.D {{"$set" , bson.D {{"x" , "fa" }}}})
374
+ models := []WriteModel {doc1 , doc1 , doc2 }
375
+ want := WriteError {Code : 66 }
376
+
377
+ t .Run ("unordered" , func (t * testing.T ) {
378
+ coll := createTestCollection (t , nil , nil )
379
+ _ , err := coll .InsertOne (context .Background (), bsonx.Doc {{"_id" , bsonx .String ("foo" )}})
380
+ require .NoError (t , err )
381
+
382
+ res , err := coll .BulkWrite (context .Background (), models , options .BulkWrite ().SetOrdered (false ))
383
+ require .Equal (t , res .ModifiedCount , int64 (1 ))
384
+
385
+ got , ok := err .(BulkWriteException )
386
+ if ! ok {
387
+ t .Errorf ("Did not receive correct type of error. got %T; want %T" , err , WriteErrors {})
388
+ t .FailNow ()
389
+ }
390
+ if len (got .WriteErrors ) != 2 {
391
+ t .Errorf ("Incorrect number of errors receieved. got %d; want %d" , len (got .WriteErrors ), 2 )
392
+ t .FailNow ()
393
+ }
394
+ if got .WriteErrors [0 ].Code != want .Code {
395
+ t .Errorf ("Did not receive the correct error code. got %d; want %d" , got .WriteErrors [0 ].Code , want .Code )
396
+ }
397
+ })
398
+
399
+ t .Run ("ordered" , func (t * testing.T ) {
400
+ coll := createTestCollection (t , nil , nil )
401
+ _ , err := coll .InsertOne (context .Background (), bsonx.Doc {{"_id" , bsonx .String ("foo" )}})
402
+ require .NoError (t , err )
403
+
404
+ res , err := coll .BulkWrite (context .Background (), models , options .BulkWrite ())
405
+ require .Equal (t , res .ModifiedCount , int64 (0 ))
406
+
407
+ got , ok := err .(BulkWriteException )
408
+ if ! ok {
409
+ t .Errorf ("Did not receive correct type of error. got %T; want %T" , err , WriteErrors {})
410
+ t .FailNow ()
411
+ }
412
+ if len (got .WriteErrors ) != 1 {
413
+ t .Errorf ("Incorrect number of errors receieved. got %d; want %d" , len (got .WriteErrors ), 1 )
414
+ t .FailNow ()
415
+ }
416
+ if got .WriteErrors [0 ].Code != want .Code {
417
+ t .Errorf ("Did not receive the correct error code. got %d; want %d" , got .WriteErrors [0 ].Code , want .Code )
418
+ }
419
+ })
420
+ }
421
+
261
422
func TestCollection_InsertOne (t * testing.T ) {
262
423
if testing .Short () {
263
424
t .Skip ("skipping integration test in short mode" )
0 commit comments