Skip to content

Commit f832ea9

Browse files
CSHARP-2841: Improve testing around default writeConcern.
1 parent 082f1d7 commit f832ea9

17 files changed

+1676
-12
lines changed

tests/MongoDB.Driver.Core.TestHelpers/JsonDrivenTests/CommandStartedEventAsserter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private void AssertCommandAspect(BsonDocument actualCommand, string name, BsonVa
150150
AdaptExpectedUpdateModels(actualValue.AsBsonArray.Cast<BsonDocument>().ToList(), expectedValue.AsBsonArray.Cast<BsonDocument>().ToList());
151151
}
152152

153-
var namesToUseOrderInsensitiveComparisonWith = new[] { "writeConcern", "maxTimeMS", "updates" };
153+
var namesToUseOrderInsensitiveComparisonWith = new[] { "writeConcern", "maxTimeMS", "updates", "indexes" };
154154
var useOrderInsensitiveComparison = namesToUseOrderInsensitiveComparisonWith.Contains(name);
155155

156156
if (!(useOrderInsensitiveComparison ? BsonValueEquivalencyComparer.Compare(actualValue, expectedValue) : actualValue.Equals(expectedValue)))

tests/MongoDB.Driver.Tests/JsonDrivenTests/JsonDrivenAggregateTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public JsonDrivenAggregateTest(IMongoCollection<BsonDocument> collection, Dictio
4141
// public methods
4242
public override void Arrange(BsonDocument document)
4343
{
44-
JsonDrivenHelper.EnsureAllFieldsAreValid(document, "name", "object", "collectionOptions", "arguments", "result", "error");
44+
JsonDrivenHelper.EnsureAllFieldsAreValid(document, "name", "object", "collectionOptions", "databaseOptions", "arguments", "result", "error");
4545
base.Arrange(document);
4646
}
4747

@@ -59,7 +59,7 @@ protected override void CallMethod(CancellationToken cancellationToken)
5959
cursor = _collection.Aggregate(_pipeline, _options, cancellationToken);
6060
}
6161
else
62-
{
62+
{
6363
cursor = _collection.Aggregate(_session, _pipeline, _options, cancellationToken);
6464
}
6565
_result = cursor.ToList();

tests/MongoDB.Driver.Tests/JsonDrivenTests/JsonDrivenCollectionTest.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ public override void Arrange(BsonDocument document)
3636
{
3737
JsonDrivenHelper.EnsureFieldEquals(document, "object", "collection");
3838

39-
if (document.Contains("collectionOptions"))
39+
if (document.TryGetValue("databaseOptions", out var databaseOptions))
4040
{
41-
ParseCollectionOptions(document["collectionOptions"].AsBsonDocument);
41+
ParseDatabaseOptions(databaseOptions.AsBsonDocument);
42+
}
43+
44+
if (document.TryGetValue("collectionOptions", out var collectionOptions))
45+
{
46+
ParseCollectionOptions(collectionOptions.AsBsonDocument);
4247
}
4348

4449
base.Arrange(document);
@@ -67,5 +72,34 @@ private void ParseCollectionOptions(BsonDocument document)
6772
_collection = _collection.WithWriteConcern(writeConcern);
6873
}
6974
}
75+
76+
private void ParseDatabaseOptions(BsonDocument document)
77+
{
78+
JsonDrivenHelper.EnsureAllFieldsAreValid(document, "readConcern", "readPreference", "writeConcern");
79+
80+
var database = _collection.Database;
81+
if (document.Contains("readConcern"))
82+
{
83+
var readConcern = ReadConcern.FromBsonDocument(document["readConcern"].AsBsonDocument);
84+
database = database.WithReadConcern(readConcern);
85+
}
86+
87+
if (document.Contains("readPreference"))
88+
{
89+
var readPreference = ReadPreference.FromBsonDocument(document["readPreference"].AsBsonDocument);
90+
database = database.WithReadPreference(readPreference);
91+
}
92+
93+
if (document.Contains("writeConcern"))
94+
{
95+
var writeConcern = WriteConcern.FromBsonDocument(document["writeConcern"].AsBsonDocument);
96+
database = database.WithWriteConcern(writeConcern);
97+
}
98+
99+
// update _collection to be associated with the reconfigured database
100+
_collection = database.GetCollection<BsonDocument>(
101+
_collection.CollectionNamespace.CollectionName,
102+
_collection.Settings);
103+
}
70104
}
71105
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Copyright 2020-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Collections.Generic;
17+
using System.Threading;
18+
using System.Threading.Tasks;
19+
using MongoDB.Bson;
20+
using MongoDB.Bson.TestHelpers.JsonDrivenTests;
21+
22+
namespace MongoDB.Driver.Tests.JsonDrivenTests
23+
{
24+
public sealed class JsonDrivenCreateIndexTest : JsonDrivenCollectionTest
25+
{
26+
// private fields
27+
private CreateIndexModel<BsonDocument> _createIndexModel;
28+
private IClientSessionHandle _session;
29+
30+
// public constructors
31+
public JsonDrivenCreateIndexTest(IMongoCollection<BsonDocument> collection, Dictionary<string, object> objectMap)
32+
: base(collection, objectMap)
33+
{
34+
}
35+
36+
protected override void AssertResult()
37+
{
38+
}
39+
40+
// public methods
41+
public override void Arrange(BsonDocument document)
42+
{
43+
JsonDrivenHelper.EnsureAllFieldsAreValid(document, "name", "object", "collectionOptions", "arguments");
44+
base.Arrange(document);
45+
}
46+
47+
protected override void CallMethod(CancellationToken cancellationToken)
48+
{
49+
if (_session == null)
50+
{
51+
_collection.Indexes.CreateOne(_createIndexModel, new CreateOneIndexOptions(), cancellationToken);
52+
}
53+
else
54+
{
55+
_collection.Indexes.CreateOne(_session, _createIndexModel, new CreateOneIndexOptions(), cancellationToken);
56+
}
57+
}
58+
59+
protected override async Task CallMethodAsync(CancellationToken cancellationToken)
60+
{
61+
if (_session == null)
62+
{
63+
await _collection.Indexes.CreateOneAsync(_createIndexModel, new CreateOneIndexOptions(), cancellationToken);
64+
}
65+
else
66+
{
67+
await _collection.Indexes.CreateOneAsync(_session, _createIndexModel, new CreateOneIndexOptions(), cancellationToken);
68+
}
69+
}
70+
71+
protected override void SetArgument(string name, BsonValue value)
72+
{
73+
switch (name)
74+
{
75+
case "session":
76+
_session = (IClientSessionHandle)_objectMap[value.AsString];
77+
return;
78+
case "keys":
79+
_createIndexModel = new CreateIndexModel<BsonDocument>(value.AsBsonDocument);
80+
return;
81+
}
82+
83+
base.SetArgument(name, value);
84+
}
85+
}
86+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Copyright 2020-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Collections.Generic;
17+
using System.Threading;
18+
using System.Threading.Tasks;
19+
using MongoDB.Bson;
20+
using MongoDB.Bson.TestHelpers.JsonDrivenTests;
21+
22+
namespace MongoDB.Driver.Tests.JsonDrivenTests
23+
{
24+
public sealed class JsonDrivenDropIndexTest : JsonDrivenCollectionTest
25+
{
26+
// private fields
27+
private string _indexName;
28+
private IClientSessionHandle _session;
29+
30+
// public constructors
31+
public JsonDrivenDropIndexTest(IMongoCollection<BsonDocument> collection, Dictionary<string, object> objectMap)
32+
: base(collection, objectMap)
33+
{
34+
}
35+
36+
protected override void AssertResult()
37+
{
38+
}
39+
40+
// public methods
41+
public override void Arrange(BsonDocument document)
42+
{
43+
JsonDrivenHelper.EnsureAllFieldsAreValid(document, "name", "object", "collectionOptions", "arguments");
44+
base.Arrange(document);
45+
}
46+
47+
protected override void CallMethod(CancellationToken cancellationToken)
48+
{
49+
if (_session == null)
50+
{
51+
_collection.Indexes.DropOne(_indexName, cancellationToken);
52+
}
53+
else
54+
{
55+
_collection.Indexes.DropOne(_session, _indexName, cancellationToken);
56+
}
57+
}
58+
59+
protected override async Task CallMethodAsync(CancellationToken cancellationToken)
60+
{
61+
if (_session == null)
62+
{
63+
await _collection.Indexes.DropOneAsync(_indexName, cancellationToken);
64+
}
65+
else
66+
{
67+
await _collection.Indexes.DropOneAsync(_session, _indexName, cancellationToken);
68+
}
69+
}
70+
71+
protected override void SetArgument(string name, BsonValue value)
72+
{
73+
switch (name)
74+
{
75+
case "name":
76+
_indexName = value.ToString();
77+
return;
78+
case "session":
79+
_session = (IClientSessionHandle)_objectMap[value.AsString];
80+
return;
81+
}
82+
83+
base.SetArgument(name, value);
84+
}
85+
}
86+
}

tests/MongoDB.Driver.Tests/JsonDrivenTests/JsonDrivenTestFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ public JsonDrivenTest CreateTest(string receiver, string name)
114114
case "bulkWrite": return new JsonDrivenBulkWriteTest(collection, _objectMap);
115115
case "count": return new JsonDrivenCountTest(collection, _objectMap);
116116
case "countDocuments": return new JsonDrivenCountDocumentsTest(collection, _objectMap);
117+
case "createIndex": return new JsonDrivenCreateIndexTest(collection, _objectMap);
117118
case "deleteMany": return new JsonDrivenDeleteManyTest(collection, _objectMap);
118119
case "deleteOne": return new JsonDrivenDeleteOneTest(collection, _objectMap);
119120
case "distinct": return new JsonDrivenDistinctTest(collection, _objectMap);
121+
case "dropIndex": return new JsonDrivenDropIndexTest(collection, _objectMap);
120122
case "estimatedDocumentCount": return new JsonDrivenEstimatedCountTest(collection, _objectMap);
121123
case "find":
122124
case "findOne":

tests/MongoDB.Driver.Tests/Specifications/Runner/MongoClientJsonDrivenTestRunnerBase.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,17 +301,28 @@ protected virtual bool TryConfigureClientOption(MongoClientSettings settings, Bs
301301

302302
protected virtual void VerifyCollectionData(IEnumerable<BsonDocument> expectedDocuments)
303303
{
304-
VerifyCollectionData(expectedDocuments, null);
304+
VerifyCollectionData(expectedDocuments, DatabaseName, CollectionName);
305+
}
306+
307+
protected virtual void VerifyCollectionData(IEnumerable<BsonDocument> expectedDocuments, string databaseName, string collectionName)
308+
{
309+
VerifyCollectionData(expectedDocuments, databaseName, collectionName, prepareExpectedResult: null);
305310
}
306311

307312
protected virtual void VerifyCollectionOutcome(BsonDocument outcome)
308313
{
314+
var collectionName = CollectionName;
315+
309316
foreach (var aspect in outcome)
310317
{
311318
switch (aspect.Name)
312319
{
320+
case "name":
321+
collectionName = aspect.Value.AsString;
322+
break;
323+
313324
case "data":
314-
VerifyCollectionData(aspect.Value.AsBsonArray.Cast<BsonDocument>());
325+
VerifyCollectionData(aspect.Value.AsBsonArray.Cast<BsonDocument>(), DatabaseName, collectionName);
315326
break;
316327

317328
default:
@@ -356,10 +367,14 @@ protected void SetupAndRunTest(JsonDrivenTestCase testCase)
356367
SetupAndRunTest(testCase.Shared, testCase.Test);
357368
}
358369

359-
protected void VerifyCollectionData(IEnumerable<BsonDocument> expectedDocuments, Action<BsonDocument, BsonDocument> prepareExpectedResult)
370+
protected void VerifyCollectionData(
371+
IEnumerable<BsonDocument> expectedDocuments,
372+
string databaseName,
373+
string collectionName,
374+
Action<BsonDocument, BsonDocument> prepareExpectedResult)
360375
{
361-
var database = DriverTestConfiguration.Client.GetDatabase(DatabaseName).WithReadConcern(ReadConcern.Local);
362-
var collection = database.GetCollection<BsonDocument>(CollectionName);
376+
var database = DriverTestConfiguration.Client.GetDatabase(databaseName).WithReadConcern(ReadConcern.Default);
377+
var collection = database.GetCollection<BsonDocument>(collectionName);
363378
var actualDocuments = collection.Find("{}").ToList();
364379
if (prepareExpectedResult != null)
365380
{

tests/MongoDB.Driver.Tests/Specifications/client-side-encryption/ClientSideEncryptionTestRunner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ protected override bool TryConfigureClientOption(MongoClientSettings settings, B
151151
return true;
152152
}
153153

154-
protected override void VerifyCollectionData(IEnumerable<BsonDocument> expectedDocuments)
154+
protected override void VerifyCollectionData(IEnumerable<BsonDocument> expectedDocuments, string databaseName, string collectionName)
155155
{
156-
base.VerifyCollectionData(expectedDocuments, ReplaceTypeAssertionWithActual);
156+
base.VerifyCollectionData(expectedDocuments, databaseName, collectionName, ReplaceTypeAssertionWithActual);
157157
}
158158

159159
// private methods
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Copyright 2020-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using FluentAssertions;
20+
using MongoDB.Bson;
21+
using MongoDB.Bson.TestHelpers.JsonDrivenTests;
22+
using MongoDB.Driver.Tests.Specifications.Runner;
23+
using Xunit;
24+
25+
namespace MongoDB.Driver.Tests.Specifications.read_write_concern
26+
{
27+
public class OperationTestRunner : MongoClientJsonDrivenTestRunnerBase
28+
{
29+
protected override string[] ExpectedTestColumns => new[] { "description", "operations", "outcome", "expectations", "async" };
30+
31+
public OperationTestRunner()
32+
{
33+
DefaultCommandsToNotCapture.Add("find");
34+
}
35+
36+
// public methods
37+
[SkippableTheory]
38+
[ClassData(typeof(TestCaseFactory))]
39+
public void Run(JsonDrivenTestCase testCase)
40+
{
41+
SetupAndRunTest(testCase);
42+
}
43+
44+
// nested types
45+
public class TestCaseFactory : JsonDrivenTestCaseFactory
46+
{
47+
// protected properties
48+
protected override string PathPrefix => "MongoDB.Driver.Tests.Specifications.read_write_concern.tests.";
49+
50+
// protected methods
51+
protected override IEnumerable<JsonDrivenTestCase> CreateTestCases(BsonDocument document)
52+
{
53+
foreach (var testCase in base.CreateTestCases(document))
54+
{
55+
foreach (var async in new[] { false, true })
56+
{
57+
var name = $"{testCase.Name}:async={async}";
58+
var test = testCase.Test.DeepClone().AsBsonDocument.Add("async", async);
59+
yield return new JsonDrivenTestCase(name, testCase.Shared, test);
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)