Skip to content

Commit 3e65585

Browse files
authored
CSHARP-5370: Test driver-generated '_id' fields are first in documents (#1667)
1 parent 20c3590 commit 3e65585

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,81 @@ public async Task MongoClient_bulkWrite_unacknowledged_write_concern_uses_w0_all
617617
documentCount.Should().Be(numModels);
618618
}
619619

620+
// https://specifications.readthedocs.io/en/latest/crud/tests/#16-generated-document-identifiers-are-the-first-field-in-their-document
621+
// The next three consecutive tests defined below are part of the prose test linked above.
622+
[Theory]
623+
[ParameterAttributeData]
624+
public async Task Ensure_generated_ids_are_first_fields_in_document_using_insertOne([Values(true, false)] bool async)
625+
{
626+
var eventCapturer = new EventCapturer().Capture<CommandStartedEvent>();
627+
using var client = CreateMongoClient(eventCapturer);
628+
var testCollection = client.GetDatabase("test").GetCollection<BsonDocument>("test");
629+
630+
var document = new BsonDocument("x", 1);
631+
if (async)
632+
{
633+
await testCollection.InsertOneAsync(document);
634+
}
635+
else
636+
{
637+
testCollection.InsertOne(document);
638+
}
639+
640+
var insertEvents = eventCapturer.Events.OfType<CommandStartedEvent>().Where(e => e.CommandName == "insert").ToArray();
641+
insertEvents.Length.Should().Be(1);
642+
insertEvents[0].Command["documents"][0].AsBsonDocument.Names.First().Should().Be("_id");
643+
document.Names.Should().Contain("_id");
644+
}
645+
646+
[Theory]
647+
[ParameterAttributeData]
648+
public async Task Ensure_generated_ids_are_first_fields_in_document_using_collection_bulkWrite([Values(true, false)] bool async)
649+
{
650+
var eventCapturer = new EventCapturer().Capture<CommandStartedEvent>();
651+
using var client = CreateMongoClient(eventCapturer);
652+
var testCollection = client.GetDatabase("test").GetCollection<BsonDocument>("test");
653+
654+
var document = new BsonDocument("x", 1);
655+
if (async)
656+
{
657+
await testCollection.BulkWriteAsync([new InsertOneModel<BsonDocument>(document)]);
658+
}
659+
else
660+
{
661+
testCollection.BulkWrite([new InsertOneModel<BsonDocument>(document)]);
662+
}
663+
664+
var insertEvents = eventCapturer.Events.OfType<CommandStartedEvent>().Where(e => e.CommandName == "insert").ToArray();
665+
insertEvents.Length.Should().Be(1);
666+
insertEvents[0].Command["documents"][0].AsBsonDocument.Names.First().Should().Be("_id");
667+
document.Names.Should().Contain("_id");
668+
}
669+
670+
[Theory]
671+
[ParameterAttributeData]
672+
public async Task Ensure_generated_ids_are_first_fields_in_document_using_client_bulkWrite([Values(true, false)] bool async)
673+
{
674+
RequireServer.Check().Supports(Feature.ClientBulkWrite).Serverless(false);
675+
676+
var eventCapturer = new EventCapturer().Capture<CommandStartedEvent>();
677+
using var client = CreateMongoClient(eventCapturer);
678+
679+
var document = new BsonDocument("x", 1);
680+
if (async)
681+
{
682+
await client.BulkWriteAsync([new BulkWriteInsertOneModel<BsonDocument>("test.test", document)]);
683+
}
684+
else
685+
{
686+
client.BulkWrite([new BulkWriteInsertOneModel<BsonDocument>("test.test", document)]);
687+
}
688+
689+
var bulkEvents = eventCapturer.Events.OfType<CommandStartedEvent>().Where(e => e.CommandName == "bulkWrite").ToArray();
690+
bulkEvents.Length.Should().Be(1);
691+
bulkEvents[0].Command["ops"][0]["document"].AsBsonDocument.Names.First().Should().Be("_id");
692+
document.Names.Should().Contain("_id");
693+
}
694+
620695
// private methods
621696
private FailPoint ConfigureFailPoint(string failpointCommand)
622697
{

0 commit comments

Comments
 (0)