@@ -231,18 +231,30 @@ func TestCollection(t *testing.T) {
231
231
mt .Run (tc .name , func (mt * mtest.T ) {
232
232
res , err := mt .Coll .InsertMany (context .Background (), docs , options .InsertMany ().SetOrdered (tc .ordered ))
233
233
234
- assert .Equal (mt , tc .numInserted , len (res .InsertedIDs ), "expected %v inserted IDs, got %v" , tc .numInserted , len (res .InsertedIDs ))
235
- assert .Equal (mt , id , res .InsertedIDs [0 ], "expected inserted ID %v, got %v" , id , res .InsertedIDs [0 ])
236
- if tc .numInserted > 1 {
237
- assert .NotNil (mt , res .InsertedIDs [1 ], "expected ID but got nil" )
238
- }
239
-
240
234
we , ok := err .(mongo.BulkWriteException )
241
235
assert .True (mt , ok , "expected error type %T, got %T" , mongo.BulkWriteException {}, err )
242
236
numErrors := len (we .WriteErrors )
243
237
assert .Equal (mt , tc .numErrors , numErrors , "expected %v write errors, got %v" , tc .numErrors , numErrors )
244
238
gotCode := we .WriteErrors [0 ].Code
245
239
assert .Equal (mt , errorDuplicateKey , gotCode , "expected error code %v, got %v" , errorDuplicateKey , gotCode )
240
+
241
+ if len (res .InsertedIDs ) == 0 {
242
+ mt .Fatal ("Expected at least one inserted ID" )
243
+ }
244
+
245
+ assert .Equal (mt ,
246
+ tc .numInserted ,
247
+ len (res .InsertedIDs ),
248
+ "expected %v inserted IDs, got %v" ,
249
+ tc .numInserted ,
250
+ len (res .InsertedIDs ))
251
+ assert .Equal (mt ,
252
+ id ,
253
+ res .InsertedIDs [0 ],
254
+ "expected ID to match" )
255
+ if tc .numInserted > 1 {
256
+ assert .NotNil (mt , res .InsertedIDs [1 ], "expected ID but got nil" )
257
+ }
246
258
})
247
259
}
248
260
})
0 commit comments