Skip to content

Commit b759efa

Browse files
committed
CSHARP-1529: New high level API should respect AssignIdOnInsert.
1 parent 3d1bc8d commit b759efa

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

src/MongoDB.Driver.Tests/MongoCollectionImplTests.cs

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ public void Setup()
4646
_operationExecutor = new MockOperationExecutor();
4747
}
4848

49-
private MongoCollectionImpl<TDocument> CreateSubject<TDocument>()
49+
private MongoCollectionImpl<TDocument> CreateSubject<TDocument>(MongoCollectionSettings settings = null)
5050
{
51-
var settings = new MongoCollectionSettings
52-
{
53-
ReadConcern = _readConcern
54-
};
51+
settings = settings ?? new MongoCollectionSettings();
52+
settings.ReadConcern = _readConcern;
5553
var dbSettings = new MongoDatabaseSettings();
5654
dbSettings.ApplyDefaultValues(new MongoClientSettings());
5755
settings.ApplyDefaultValues(dbSettings);
@@ -1216,6 +1214,35 @@ public void InsertOne_should_throw_a_WriteException_when_an_error_occurs(
12161214
action.ShouldThrow<MongoWriteException>();
12171215
}
12181216

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+
12191246
[Test]
12201247
public void InsertMany_should_execute_the_BulkMixedOperation(
12211248
[Values(null, false, true)] bool? bypassDocumentValidation,
@@ -1252,6 +1279,35 @@ public void InsertMany_should_execute_the_BulkMixedOperation(
12521279
VerifyWrites(expectedRequests, bypassDocumentValidation, isOrdered, call);
12531280
}
12541281

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+
12551311
[Test]
12561312
public void MapReduce_with_inline_output_mode_should_execute_the_MapReduceOperation(
12571313
[Values(false, true)] bool async)

src/MongoDB.Driver/MongoCollectionImpl.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,10 @@ private WriteRequest ConvertWriteModelToWriteRequest(WriteModel<TDocument> model
419419
{
420420
case WriteModelType.InsertOne:
421421
var insertOneModel = (InsertOneModel<TDocument>)model;
422-
AssignId(insertOneModel.Document);
422+
if (_settings.AssignIdOnInsert)
423+
{
424+
AssignId(insertOneModel.Document);
425+
}
423426
return new InsertRequest(new BsonDocumentWrapper(insertOneModel.Document, _documentSerializer))
424427
{
425428
CorrelationId = index

0 commit comments

Comments
 (0)