From 978799e1981642699ab14929bc6f1421abfbd646 Mon Sep 17 00:00:00 2001 From: adelinowona Date: Tue, 8 Apr 2025 02:34:40 -0400 Subject: [PATCH 1/4] CSHARP-5370: Test driver-generated '_id' fields are first in documents --- .../crud/prose-tests/CrudProseTests.cs | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs b/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs index bb9ecbbd8e3..f86b394734f 100644 --- a/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs +++ b/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs @@ -617,6 +617,65 @@ public async Task MongoClient_bulkWrite_unacknowledged_write_concern_uses_w0_all documentCount.Should().Be(numModels); } + [Theory] + [ParameterAttributeData] + public async Task Ensure_generated_ids_are_first_fields_in_document([Values(true, false)] bool async) + { + var eventCapturer = new EventCapturer().Capture(); + var client = CreateMongoClient(eventCapturer); + var testCollection = client.GetDatabase("test").GetCollection("test"); + + var document = new BsonDocument("x", 1); + if (async) + { + await testCollection.InsertOneAsync(document); + } + else + { + testCollection.InsertOne(document); + } + + eventCapturer.Next(); + eventCapturer.Next().Should().BeOfType() + .Subject.Command["documents"][0].AsBsonDocument.Names.First().Should().Be("_id"); + document.Names.Should().Contain("_id"); + + eventCapturer.Clear(); + document = new BsonDocument("x", 2); + if (async) + { + await testCollection.BulkWriteAsync([new InsertOneModel(document)]); + } + else + { + testCollection.BulkWrite([new InsertOneModel(document)]); + } + + eventCapturer.Count.Should().Be(1); + eventCapturer.Next().Should().BeOfType() + .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("test.test", document)]); + } + else + { + client.BulkWrite([new BulkWriteInsertOneModel("test.test", document)]); + } + + eventCapturer.Count.Should().Be(1); + eventCapturer.Next().Should().BeOfType() + .Subject.Command["ops"][0]["document"].AsBsonDocument.Names.First().Should().Be("_id"); + document.Names.Should().Contain("_id"); + } + } + // private methods private FailPoint ConfigureFailPoint(string failpointCommand) { From b4f5bebcbe69a0b15219b97aac7594ef17b17870 Mon Sep 17 00:00:00 2001 From: adelinowona Date: Fri, 11 Apr 2025 04:49:59 -0400 Subject: [PATCH 2/4] address pr comments --- .../crud/prose-tests/CrudProseTests.cs | 58 ++++++++++++------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs b/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs index f86b394734f..4a9ab09b649 100644 --- a/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs +++ b/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs @@ -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(); var client = CreateMongoClient(eventCapturer); @@ -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() .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(); + var client = CreateMongoClient(eventCapturer); + var testCollection = client.GetDatabase("test").GetCollection("test"); - eventCapturer.Clear(); - document = new BsonDocument("x", 2); + var document = new BsonDocument("x", 1); if (async) { await testCollection.BulkWriteAsync([new InsertOneModel(document)]); @@ -651,29 +661,35 @@ public async Task Ensure_generated_ids_are_first_fields_in_document([Values(true testCollection.BulkWrite([new InsertOneModel(document)]); } - eventCapturer.Count.Should().Be(1); + eventCapturer.Next(); // skip the hello command that is captured. eventCapturer.Next().Should().BeOfType() .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("test.test", document)]); - } - else - { - client.BulkWrite([new BulkWriteInsertOneModel("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(); + var client = CreateMongoClient(eventCapturer); - eventCapturer.Count.Should().Be(1); - eventCapturer.Next().Should().BeOfType() - .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("test.test", document)]); + } + else + { + client.BulkWrite([new BulkWriteInsertOneModel("test.test", document)]); } + + eventCapturer.Next(); // skip the hello command that is captured. + eventCapturer.Next().Should().BeOfType() + .Subject.Command["ops"][0]["document"].AsBsonDocument.Names.First().Should().Be("_id"); + document.Names.Should().Contain("_id"); } // private methods From 03b65da4772c90494fe2f6c5f8152b8a7dd766ea Mon Sep 17 00:00:00 2001 From: adelinowona Date: Fri, 11 Apr 2025 05:27:26 -0400 Subject: [PATCH 3/4] fix EG failures --- .../crud/prose-tests/CrudProseTests.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs b/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs index 4a9ab09b649..92ee3132013 100644 --- a/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs +++ b/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs @@ -637,9 +637,9 @@ public async Task Ensure_generated_ids_are_first_fields_in_document_using_insert testCollection.InsertOne(document); } - eventCapturer.Next(); // skip the hello command that is captured. - eventCapturer.Next().Should().BeOfType() - .Subject.Command["documents"][0].AsBsonDocument.Names.First().Should().Be("_id"); + var insertEvents = eventCapturer.Events.OfType().Where(e => e.CommandName == "insert").ToArray(); + insertEvents.Length.Should().Be(1); + insertEvents[0].Command["documents"][0].AsBsonDocument.Names.First().Should().Be("_id"); document.Names.Should().Contain("_id"); } @@ -661,9 +661,9 @@ public async Task Ensure_generated_ids_are_first_fields_in_document_using_collec testCollection.BulkWrite([new InsertOneModel(document)]); } - eventCapturer.Next(); // skip the hello command that is captured. - eventCapturer.Next().Should().BeOfType() - .Subject.Command["documents"][0].AsBsonDocument.Names.First().Should().Be("_id"); + var insertEvents = eventCapturer.Events.OfType().Where(e => e.CommandName == "insert").ToArray(); + insertEvents.Length.Should().Be(1); + insertEvents[0].Command["documents"][0].AsBsonDocument.Names.First().Should().Be("_id"); document.Names.Should().Contain("_id"); } @@ -686,9 +686,9 @@ public async Task Ensure_generated_ids_are_first_fields_in_document_using_client client.BulkWrite([new BulkWriteInsertOneModel("test.test", document)]); } - eventCapturer.Next(); // skip the hello command that is captured. - eventCapturer.Next().Should().BeOfType() - .Subject.Command["ops"][0]["document"].AsBsonDocument.Names.First().Should().Be("_id"); + var bulkEvents = eventCapturer.Events.OfType().Where(e => e.CommandName == "bulkWrite").ToArray(); + bulkEvents.Length.Should().Be(1); + bulkEvents[0].Command["ops"][0]["document"].AsBsonDocument.Names.First().Should().Be("_id"); document.Names.Should().Contain("_id"); } From 9c7f3cfdc1b6f6ad33ebb6d23138a0a7bfd8cef4 Mon Sep 17 00:00:00 2001 From: adelinowona Date: Fri, 25 Apr 2025 11:41:17 -0400 Subject: [PATCH 4/4] dispose of mongoClient --- .../Specifications/crud/prose-tests/CrudProseTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs b/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs index 92ee3132013..41987fcfc3e 100644 --- a/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs +++ b/tests/MongoDB.Driver.Tests/Specifications/crud/prose-tests/CrudProseTests.cs @@ -624,7 +624,7 @@ public async Task MongoClient_bulkWrite_unacknowledged_write_concern_uses_w0_all public async Task Ensure_generated_ids_are_first_fields_in_document_using_insertOne([Values(true, false)] bool async) { var eventCapturer = new EventCapturer().Capture(); - var client = CreateMongoClient(eventCapturer); + using var client = CreateMongoClient(eventCapturer); var testCollection = client.GetDatabase("test").GetCollection("test"); var document = new BsonDocument("x", 1); @@ -648,7 +648,7 @@ public async Task Ensure_generated_ids_are_first_fields_in_document_using_insert public async Task Ensure_generated_ids_are_first_fields_in_document_using_collection_bulkWrite([Values(true, false)] bool async) { var eventCapturer = new EventCapturer().Capture(); - var client = CreateMongoClient(eventCapturer); + using var client = CreateMongoClient(eventCapturer); var testCollection = client.GetDatabase("test").GetCollection("test"); var document = new BsonDocument("x", 1); @@ -674,7 +674,7 @@ public async Task Ensure_generated_ids_are_first_fields_in_document_using_client RequireServer.Check().Supports(Feature.ClientBulkWrite).Serverless(false); var eventCapturer = new EventCapturer().Capture(); - var client = CreateMongoClient(eventCapturer); + using var client = CreateMongoClient(eventCapturer); var document = new BsonDocument("x", 1); if (async)