Skip to content

Commit 695fd71

Browse files
committed
CSHARP-1437: Refactor FindAndModify WriteConcern tests to handle older servers.
1 parent a8cfbf8 commit 695fd71

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

src/MongoDB.Driver.Legacy.Tests/MongoCollectionTests.cs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -985,12 +985,23 @@ public void TestFindAndModifyReplaceWithWriteConcernError()
985985
VersionReturned = FindAndModifyDocumentVersion.Modified
986986
};
987987

988-
Action action = () => collection.FindAndModify(args);
988+
BsonDocument modifiedDocument;
989+
if (_server.BuildInfo.Version >= new Version(3, 2, 0))
990+
{
991+
Action action = () => collection.FindAndModify(args);
992+
993+
var exception = action.ShouldThrow<MongoWriteConcernException>().Which;
994+
var commandResult = exception.Result;
995+
modifiedDocument = commandResult["value"].AsBsonDocument;
996+
}
997+
else
998+
{
999+
var result = collection.FindAndModify(args);
1000+
1001+
modifiedDocument = result.ModifiedDocument;
1002+
}
9891003

990-
var exception = action.ShouldThrow<MongoWriteConcernException>().Which;
991-
var commandResult = exception.Result;
992-
var result = commandResult["value"].AsBsonDocument;
993-
result.Should().Be("{ _id : 1, x : 2 }");
1004+
modifiedDocument.Should().Be("{ _id : 1, x : 2 }");
9941005
}
9951006

9961007
[Test]
@@ -1011,12 +1022,23 @@ public void TestFindAndModifyUpdateWithWriteConcernError()
10111022
VersionReturned = FindAndModifyDocumentVersion.Modified
10121023
};
10131024

1014-
Action action = () => collection.FindAndModify(args);
1025+
BsonDocument modifiedDocument;
1026+
if (_server.BuildInfo.Version >= new Version(3, 2, 0))
1027+
{
1028+
Action action = () => collection.FindAndModify(args);
1029+
1030+
var exception = action.ShouldThrow<MongoWriteConcernException>().Which;
1031+
var commandResult = exception.Result;
1032+
modifiedDocument = commandResult["value"].AsBsonDocument;
1033+
}
1034+
else
1035+
{
1036+
var result = collection.FindAndModify(args);
1037+
1038+
modifiedDocument = result.ModifiedDocument;
1039+
}
10151040

1016-
var exception = action.ShouldThrow<MongoWriteConcernException>().Which;
1017-
var commandResult = exception.Result;
1018-
var result = commandResult["value"].AsBsonDocument;
1019-
result.Should().Be("{ _id : 1, x : 2 }");
1041+
modifiedDocument.Should().Be("{ _id : 1, x : 2 }");
10201042
}
10211043

10221044
private class FindAndModifyClass
@@ -1120,7 +1142,7 @@ public void TestFindAndRemoveWithMaxTime()
11201142
public void TestFindAndRemoveWithWriteConcernError()
11211143
{
11221144
_collection.RemoveAll();
1123-
_collection.Insert(new BsonDocument("x", 1));
1145+
_collection.Insert(new BsonDocument { { "_id", 1 }, { "x", 1 } });
11241146
var collectionSettings = new MongoCollectionSettings
11251147
{
11261148
WriteConcern = new WriteConcern(9)
@@ -1131,12 +1153,23 @@ public void TestFindAndRemoveWithWriteConcernError()
11311153
Query = Query.EQ("x", 1)
11321154
};
11331155

1134-
Action action = () => collection.FindAndRemove(args);
1156+
BsonDocument modifiedDocument;
1157+
if (_server.BuildInfo.Version >= new Version(3, 2, 0))
1158+
{
1159+
Action action = () => collection.FindAndRemove(args);
11351160

1136-
var exception = action.ShouldThrow<MongoWriteConcernException>().Which;
1137-
var commandResult = exception.Result;
1138-
var result = commandResult["value"].AsBsonDocument;
1139-
result["x"].Should().Be(1);
1161+
var exception = action.ShouldThrow<MongoWriteConcernException>().Which;
1162+
var commandResult = exception.Result;
1163+
modifiedDocument = commandResult["value"].AsBsonDocument;
1164+
}
1165+
else
1166+
{
1167+
var result = collection.FindAndRemove(args);
1168+
1169+
modifiedDocument = result.ModifiedDocument;
1170+
}
1171+
1172+
modifiedDocument.Should().Be("{ _id : 1, x : 1 }");
11401173
_collection.Count().Should().Be(0);
11411174
}
11421175

@@ -2374,7 +2407,7 @@ public void TestInsertDuplicateKey()
23742407
}
23752408

23762409
[Test]
2377-
[RequiresServer(ClusterTypes = ClusterTypes.ReplicaSet)]
2410+
[RequiresServer(MinimumVersion = "3.2.0-rc0", ClusterTypes = ClusterTypes.ReplicaSet)]
23782411
public void TestInsertWithWriteConcernError()
23792412
{
23802413
_collection.RemoveAll();
@@ -2838,7 +2871,7 @@ public void TestRemoveUnacknowledeged()
28382871
}
28392872

28402873
[Test]
2841-
[RequiresServer(ClusterTypes = ClusterTypes.ReplicaSet)]
2874+
[RequiresServer(MinimumVersion = "3.2.0-rc0", ClusterTypes = ClusterTypes.ReplicaSet)]
28422875
public void TestRemoveWithWriteConcernError()
28432876
{
28442877
_collection.RemoveAll();
@@ -3097,7 +3130,7 @@ public void TestUpdateUnacknowledged()
30973130
}
30983131

30993132
[Test]
3100-
[RequiresServer(ClusterTypes = ClusterTypes.ReplicaSet)]
3133+
[RequiresServer(MinimumVersion = "3.2.0-rc0", ClusterTypes = ClusterTypes.ReplicaSet)]
31013134
public void TestUpdateWithWriteConcernError()
31023135
{
31033136
_collection.RemoveAll();

0 commit comments

Comments
 (0)