Skip to content

Commit 4ca5355

Browse files
committed
CSHARP-2342: Test only the initial command in a transaction includes readConcern
1 parent add2c25 commit 4ca5355

File tree

10 files changed

+1291
-6
lines changed

10 files changed

+1291
-6
lines changed

tests/MongoDB.Bson.TestHelpers/JsonDrivenTests/JsonDrivenTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ public virtual void Assert()
8080
{
8181
throw new Exception("Unexpected exception was thrown.", _actualException);
8282
}
83-
AssertResult();
83+
84+
if (_expectedResult != null)
85+
{
86+
AssertResult();
87+
}
8488
}
8589
else
8690
{
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* Copyright 2019-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 FluentAssertions;
20+
using MongoDB.Bson;
21+
using MongoDB.Bson.TestHelpers.JsonDrivenTests;
22+
23+
namespace MongoDB.Driver.Tests.JsonDrivenTests
24+
{
25+
public sealed class JsonDrivenCountDocumentsTest : JsonDrivenCollectionTest
26+
{
27+
// private fields
28+
private FilterDefinition<BsonDocument> _filter = new BsonDocument();
29+
private CountOptions _options = new CountOptions();
30+
private long _result;
31+
private IClientSessionHandle _session;
32+
33+
// public constructors
34+
public JsonDrivenCountDocumentsTest(IMongoCollection<BsonDocument> collection, Dictionary<string, object> objectMap)
35+
: base(collection, objectMap)
36+
{
37+
}
38+
39+
// public methods
40+
public override void Arrange(BsonDocument document)
41+
{
42+
JsonDrivenHelper.EnsureAllFieldsAreValid(document, "name", "object", "collectionOptions", "arguments", "result", "error");
43+
base.Arrange(document);
44+
}
45+
46+
// protected methods
47+
protected override void AssertResult()
48+
{
49+
_result.Should().Be(_expectedResult.ToInt64());
50+
}
51+
52+
protected override void CallMethod(CancellationToken cancellationToken)
53+
{
54+
if (_session == null)
55+
{
56+
#pragma warning disable 618
57+
_result = _collection.CountDocuments(_filter, _options, cancellationToken);
58+
#pragma warning restore
59+
}
60+
else
61+
{
62+
#pragma warning disable 618
63+
_result = _collection.CountDocuments(_session, _filter, _options, cancellationToken);
64+
#pragma warning restore
65+
}
66+
}
67+
68+
protected override async Task CallMethodAsync(CancellationToken cancellationToken)
69+
{
70+
if (_session == null)
71+
{
72+
#pragma warning disable 618
73+
_result = await _collection.CountDocumentsAsync(_filter, _options, cancellationToken).ConfigureAwait(false);
74+
#pragma warning restore
75+
}
76+
else
77+
{
78+
#pragma warning disable 618
79+
_result = await _collection.CountDocumentsAsync(_session, _filter, _options, cancellationToken).ConfigureAwait(false);
80+
#pragma warning restore
81+
}
82+
}
83+
84+
protected override void SetArgument(string name, BsonValue value)
85+
{
86+
switch (name)
87+
{
88+
case "filter":
89+
_filter = new BsonDocumentFilterDefinition<BsonDocument>(value.AsBsonDocument);
90+
return;
91+
92+
case "session":
93+
_session = (IClientSessionHandle)_objectMap[value.AsString];
94+
return;
95+
}
96+
97+
base.SetArgument(name, value);
98+
}
99+
}
100+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public JsonDrivenTest CreateTest(string receiver, string name)
7070
case "aggregate": return new JsonDrivenAggregateTest(collection, _objectMap);
7171
case "bulkWrite": return new JsonDrivenBulkWriteTest(collection, _objectMap);
7272
case "count": return new JsonDrivenCountTest(collection, _objectMap);
73+
case "countDocuments": return new JsonDrivenCountDocumentsTest(collection, _objectMap);
7374
case "deleteMany": return new JsonDrivenDeleteManyTest(collection, _objectMap);
7475
case "deleteOne": return new JsonDrivenDeleteOneTest(collection, _objectMap);
7576
case "distinct": return new JsonDrivenDistinctTest(collection, _objectMap);

tests/MongoDB.Driver.Tests/Specifications/transactions/tests/abort.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,6 @@
441441
}
442442
},
443443
"result": {
444-
"errorCodeName": "DuplicateKey",
445444
"errorLabelsOmit": [
446445
"TransientTransactionError",
447446
"UnknownTransactionCommitResult"

tests/MongoDB.Driver.Tests/Specifications/transactions/tests/abort.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ tests:
297297
document:
298298
_id: 1
299299
result:
300-
errorCodeName: DuplicateKey
300+
# Don't assert on errorCodeName because (after SERVER-38583) the
301+
# DuplicateKey is reported in writeErrors, not as a top-level
302+
# command error.
301303
errorLabelsOmit: ["TransientTransactionError", "UnknownTransactionCommitResult"]
302304
# Make sure the server aborted the transaction.
303305
- name: insertOne

tests/MongoDB.Driver.Tests/Specifications/transactions/tests/error-labels.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
]
2626
},
2727
"result": {
28-
"errorCodeName": "DuplicateKey",
2928
"errorLabelsOmit": [
3029
"TransientTransactionError",
3130
"UnknownTransactionCommitResult"

tests/MongoDB.Driver.Tests/Specifications/transactions/tests/error-labels.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ tests:
1717
- _id: 1
1818
- _id: 1
1919
result:
20-
errorCodeName: DuplicateKey
20+
# Don't assert on errorCodeName because (after SERVER-38583) the
21+
# DuplicateKey is reported in writeErrors, not as a top-level
22+
# command error.
2123
errorLabelsOmit: ["TransientTransactionError", "UnknownTransactionCommitResult"]
2224
- name: abortTransaction
2325
object: session0

0 commit comments

Comments
 (0)