Skip to content

CSHARP-5370: Test driver-generated '_id' fields are first in documents #1667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 28, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address pr comments
  • Loading branch information
adelinowona committed Apr 24, 2025
commit b4f5bebcbe69a0b15219b97aac7594ef17b17870
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,11 @@ public async Task MongoClient_bulkWrite_unacknowledged_write_concern_uses_w0_all
documentCount.Should().Be(numModels);
}

// https://specifications.readthedocs.io/en/latest/crud/tests/#16-generated-document-identifiers-are-the-first-field-in-their-document
// The next three consecutive tests defined below are part of the prose test linked above.
[Theory]
[ParameterAttributeData]
public async Task Ensure_generated_ids_are_first_fields_in_document([Values(true, false)] bool async)
public async Task Ensure_generated_ids_are_first_fields_in_document_using_insertOne([Values(true, false)] bool async)
{
var eventCapturer = new EventCapturer().Capture<CommandStartedEvent>();
var client = CreateMongoClient(eventCapturer);
Expand All @@ -635,13 +637,21 @@ public async Task Ensure_generated_ids_are_first_fields_in_document([Values(true
testCollection.InsertOne(document);
}

eventCapturer.Next();
eventCapturer.Next(); // skip the hello command that is captured.
eventCapturer.Next().Should().BeOfType<CommandStartedEvent>()
.Subject.Command["documents"][0].AsBsonDocument.Names.First().Should().Be("_id");
document.Names.Should().Contain("_id");
}

[Theory]
[ParameterAttributeData]
public async Task Ensure_generated_ids_are_first_fields_in_document_using_collection_bulkWrite([Values(true, false)] bool async)
{
var eventCapturer = new EventCapturer().Capture<CommandStartedEvent>();
var client = CreateMongoClient(eventCapturer);
var testCollection = client.GetDatabase("test").GetCollection<BsonDocument>("test");

eventCapturer.Clear();
document = new BsonDocument("x", 2);
var document = new BsonDocument("x", 1);
if (async)
{
await testCollection.BulkWriteAsync([new InsertOneModel<BsonDocument>(document)]);
Expand All @@ -651,29 +661,35 @@ public async Task Ensure_generated_ids_are_first_fields_in_document([Values(true
testCollection.BulkWrite([new InsertOneModel<BsonDocument>(document)]);
}

eventCapturer.Count.Should().Be(1);
eventCapturer.Next(); // skip the hello command that is captured.
eventCapturer.Next().Should().BeOfType<CommandStartedEvent>()
.Subject.Command["documents"][0].AsBsonDocument.Names.First().Should().Be("_id");
document.Names.Should().Contain("_id");
}

if (Feature.ClientBulkWrite.IsSupported(CoreTestConfiguration.MaxWireVersion))
{
eventCapturer.Clear();
document = new BsonDocument("x", 3);
if (async)
{
await client.BulkWriteAsync([new BulkWriteInsertOneModel<BsonDocument>("test.test", document)]);
}
else
{
client.BulkWrite([new BulkWriteInsertOneModel<BsonDocument>("test.test", document)]);
}
[Theory]
[ParameterAttributeData]
public async Task Ensure_generated_ids_are_first_fields_in_document_using_client_bulkWrite([Values(true, false)] bool async)
{
RequireServer.Check().Supports(Feature.ClientBulkWrite).Serverless(false);

var eventCapturer = new EventCapturer().Capture<CommandStartedEvent>();
var client = CreateMongoClient(eventCapturer);

eventCapturer.Count.Should().Be(1);
eventCapturer.Next().Should().BeOfType<CommandStartedEvent>()
.Subject.Command["ops"][0]["document"].AsBsonDocument.Names.First().Should().Be("_id");
document.Names.Should().Contain("_id");
var document = new BsonDocument("x", 1);
if (async)
{
await client.BulkWriteAsync([new BulkWriteInsertOneModel<BsonDocument>("test.test", document)]);
}
else
{
client.BulkWrite([new BulkWriteInsertOneModel<BsonDocument>("test.test", document)]);
}

eventCapturer.Next(); // skip the hello command that is captured.
eventCapturer.Next().Should().BeOfType<CommandStartedEvent>()
.Subject.Command["ops"][0]["document"].AsBsonDocument.Names.First().Should().Be("_id");
document.Names.Should().Contain("_id");
}

// private methods
Expand Down