@@ -46,12 +46,10 @@ public void Setup()
46
46
_operationExecutor = new MockOperationExecutor ( ) ;
47
47
}
48
48
49
- private MongoCollectionImpl < TDocument > CreateSubject < TDocument > ( )
49
+ private MongoCollectionImpl < TDocument > CreateSubject < TDocument > ( MongoCollectionSettings settings = null )
50
50
{
51
- var settings = new MongoCollectionSettings
52
- {
53
- ReadConcern = _readConcern
54
- } ;
51
+ settings = settings ?? new MongoCollectionSettings ( ) ;
52
+ settings . ReadConcern = _readConcern ;
55
53
var dbSettings = new MongoDatabaseSettings ( ) ;
56
54
dbSettings . ApplyDefaultValues ( new MongoClientSettings ( ) ) ;
57
55
settings . ApplyDefaultValues ( dbSettings ) ;
@@ -1216,6 +1214,35 @@ public void InsertOne_should_throw_a_WriteException_when_an_error_occurs(
1216
1214
action. ShouldThrow< MongoWriteException> ( ) ;
1217
1215
}
1218
1216
1217
+ [ Test]
1218
+ public void InsertOne_should_respect_AssignIdOnInsert(
1219
+ [ Values( false, true) ] bool assignIdOnInsert,
1220
+ [ Values( false, true) ] bool async)
1221
+ {
1222
+ var document = BsonDocument. Parse( "{ a : 1 } ") ;
1223
+ var expectedRequest = new InsertRequest( document) { CorrelationId = 0 } ;
1224
+ var operationResult = new BulkWriteOperationResult. Unacknowledged( 1 , new [ ] { expectedRequest } ) ;
1225
+ _operationExecutor. EnqueueResult< BulkWriteOperationResult> ( operationResult) ;
1226
+
1227
+ var settings = new MongoCollectionSettings { AssignIdOnInsert = assignIdOnInsert } ;
1228
+ var subject = CreateSubject< BsonDocument> ( settings) ;
1229
+
1230
+ if ( async)
1231
+ {
1232
+ subject. InsertOneAsync( document, cancellationToken: CancellationToken. None) . GetAwaiter( ) . GetResult( ) ;
1233
+ }
1234
+ else
1235
+ {
1236
+ subject. InsertOne( document, cancellationToken: CancellationToken. None) ;
1237
+ }
1238
+
1239
+ var call = _operationExecutor. GetWriteCall< BulkWriteOperationResult> ( ) ;
1240
+ var operation = ( BulkMixedWriteOperation) call. Operation;
1241
+ var requests = operation. Requests. ToList( ) ; // call ToList to force evaluation
1242
+ document. Contains( "_id") . Should( ) . Be( assignIdOnInsert) ;
1243
+ VerifySingleWrite( expectedRequest, null , true, call) ;
1244
+ }
1245
+
1219
1246
[ Test]
1220
1247
public void InsertMany_should_execute_the_BulkMixedOperation(
1221
1248
[ Values( null , false, true) ] bool ? bypassDocumentValidation,
@@ -1252,6 +1279,35 @@ public void InsertMany_should_execute_the_BulkMixedOperation(
1252
1279
VerifyWrites( expectedRequests, bypassDocumentValidation, isOrdered, call) ;
1253
1280
}
1254
1281
1282
+ [ Test]
1283
+ public void InsertMany_should_respect_AssignIdOnInsert(
1284
+ [ Values( false, true) ] bool assignIdOnInsert,
1285
+ [ Values( false, true) ] bool async)
1286
+ {
1287
+ var document = BsonDocument. Parse( "{ a : 1 } ") ;
1288
+ var expectedRequest = new InsertRequest( document) { CorrelationId = 0 } ;
1289
+ var operationResult = new BulkWriteOperationResult. Unacknowledged( 1 , new [ ] { expectedRequest } ) ;
1290
+ _operationExecutor. EnqueueResult< BulkWriteOperationResult> ( operationResult) ;
1291
+
1292
+ var settings = new MongoCollectionSettings { AssignIdOnInsert = assignIdOnInsert } ;
1293
+ var subject = CreateSubject< BsonDocument> ( settings) ;
1294
+
1295
+ if ( async)
1296
+ {
1297
+ subject. InsertManyAsync( new [ ] { document } , cancellationToken: CancellationToken. None) . GetAwaiter( ) . GetResult( ) ;
1298
+ }
1299
+ else
1300
+ {
1301
+ subject. InsertMany( new [ ] { document } , cancellationToken: CancellationToken. None) ;
1302
+ }
1303
+
1304
+ var call = _operationExecutor. GetWriteCall< BulkWriteOperationResult> ( ) ;
1305
+ var operation = ( BulkMixedWriteOperation) call. Operation;
1306
+ var requests = operation. Requests. ToList( ) ; // call ToList to force evaluation
1307
+ document. Contains( "_id") . Should( ) . Be( assignIdOnInsert) ;
1308
+ VerifySingleWrite( expectedRequest, null , true, call) ;
1309
+ }
1310
+
1255
1311
[ Test]
1256
1312
public void MapReduce_with_inline_output_mode_should_execute_the_MapReduceOperation(
1257
1313
[ Values( false, true) ] bool async)
0 commit comments