@@ -164,6 +164,74 @@ func TestCollection(t *testing.T) {
164
164
})
165
165
}
166
166
})
167
+ mt .Run ("return only inserted ids" , func (mt * mtest.T ) {
168
+ id := int32 (11 )
169
+ docs := []interface {}{
170
+ bson.D {{"_id" , id }},
171
+ bson.D {{"_id" , id }},
172
+ bson.D {{"x" , 6 }},
173
+ bson.D {{"_id" , id }},
174
+ }
175
+
176
+ testCases := []struct {
177
+ name string
178
+ ordered bool
179
+ numInserted int
180
+ numErrors int
181
+ }{
182
+ {"unordered" , false , 2 , 2 },
183
+ {"ordered" , true , 1 , 1 },
184
+ }
185
+ for _ , tc := range testCases {
186
+ mt .Run (tc .name , func (mt * mtest.T ) {
187
+ res , err := mt .Coll .InsertMany (mtest .Background , docs , options .InsertMany ().SetOrdered (tc .ordered ))
188
+
189
+ assert .Equal (mt , tc .numInserted , len (res .InsertedIDs ), "expected %v inserted IDs, got %v" , tc .numInserted , len (res .InsertedIDs ))
190
+ assert .Equal (mt , id , res .InsertedIDs [0 ], "expected inserted ID %v, got %v" , id , res .InsertedIDs [0 ])
191
+ if tc .numInserted > 1 {
192
+ assert .NotNil (mt , res .InsertedIDs [1 ], "expected ID but got nil" )
193
+ }
194
+
195
+ we , ok := err .(mongo.BulkWriteException )
196
+ assert .True (mt , ok , "expected error type %T, got %T" , mongo.BulkWriteException {}, err )
197
+ numErrors := len (we .WriteErrors )
198
+ assert .Equal (mt , tc .numErrors , numErrors , "expected %v write errors, got %v" , tc .numErrors , numErrors )
199
+ gotCode := we .WriteErrors [0 ].Code
200
+ assert .Equal (mt , errorDuplicateKey , gotCode , "expected error code %v, got %v" , errorDuplicateKey , gotCode )
201
+ })
202
+ }
203
+ })
204
+ mt .Run ("writeError index" , func (mt * mtest.T ) {
205
+ // TODO(GODRIVER-425): remove this as part a larger project to
206
+ // refactor integration and other longrunning tasks.
207
+ if os .Getenv ("EVR_TASK_ID" ) == "" {
208
+ mt .Skip ("skipping long running integration test outside of evergreen" )
209
+ }
210
+
211
+ // force multiple batches
212
+ numDocs := 700000
213
+ var docs []interface {}
214
+ for i := 0 ; i < numDocs ; i ++ {
215
+ d := bson.D {
216
+ {"a" , int32 (i )},
217
+ {"b" , int32 (i * 2 )},
218
+ {"c" , int32 (i * 3 )},
219
+ }
220
+ docs = append (docs , d )
221
+ }
222
+ repeated := bson.D {{"_id" , int32 (11 )}}
223
+ docs = append (docs , repeated , repeated )
224
+
225
+ _ , err := mt .Coll .InsertMany (context .Background (), docs )
226
+ assert .NotNil (mt , err , "expected InsertMany error, got nil" )
227
+
228
+ we , ok := err .(mongo.BulkWriteException )
229
+ assert .True (mt , ok , "expected error type %T, got %T" , mongo.BulkWriteException {}, err )
230
+ numErrors := len (we .WriteErrors )
231
+ assert .Equal (mt , 1 , numErrors , "expected 1 write error, got %v" , numErrors )
232
+ gotIndex := we .WriteErrors [0 ].Index
233
+ assert .Equal (mt , numDocs + 1 , gotIndex , "expected index %v, got %v" , numDocs + 1 , gotIndex )
234
+ })
167
235
wcCollOpts := options .Collection ().SetWriteConcern (impossibleWc )
168
236
wcTestOpts := mtest .NewOptions ().CollectionOptions (wcCollOpts ).Topologies (mtest .ReplicaSet )
169
237
mt .RunOpts ("write concern error" , wcTestOpts , func (mt * mtest.T ) {
0 commit comments