Skip to content

Commit e6cd330

Browse files
committed
CSHARP-1437: Added write concern tests for legacy CRUD and FindAndModify methods.
1 parent ae461fa commit e6cd330

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

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

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using MongoDB.Driver.Builders;
2626
using MongoDB.Driver.Core;
2727
using MongoDB.Driver.GeoJsonObjectModel;
28+
using FluentAssertions;
2829
using NUnit.Framework;
2930

3031
namespace MongoDB.Driver.Tests
@@ -952,6 +953,58 @@ public void TestFindAndModifyUpsert()
952953
Assert.AreEqual(1, result.ModifiedDocument["count"].AsInt32);
953954
}
954955

956+
[Test]
957+
[RequiresServer(ClusterTypes = ClusterTypes.ReplicaSet)]
958+
public void TestFindAndModifyReplaceWithWriteConcernError()
959+
{
960+
_collection.RemoveAll();
961+
_collection.Insert(new BsonDocument { { "_id", 1 }, { "x", 1 } });
962+
var collectionSettings = new MongoCollectionSettings
963+
{
964+
WriteConcern = new WriteConcern(9)
965+
};
966+
var collection = _database.GetCollection(_collection.Name, collectionSettings);
967+
var args = new FindAndModifyArgs
968+
{
969+
Query = Query.EQ("_id", 1),
970+
Update = Update.Replace(new BsonDocument { { "_id", 1 }, { "x", 2 } }),
971+
VersionReturned = FindAndModifyDocumentVersion.Modified
972+
};
973+
974+
Action action = () => collection.FindAndModify(args);
975+
976+
var exception = action.ShouldThrow<MongoWriteConcernException>().Which;
977+
var commandResult = exception.Result;
978+
var result = commandResult["value"].AsBsonDocument;
979+
result.Should().Be("{ _id : 1, x : 2 }");
980+
}
981+
982+
[Test]
983+
[RequiresServer(ClusterTypes = ClusterTypes.ReplicaSet)]
984+
public void TestFindAndModifyUpdateWithWriteConcernError()
985+
{
986+
_collection.RemoveAll();
987+
_collection.Insert(new BsonDocument { { "_id", 1 }, { "x", 1 } });
988+
var collectionSettings = new MongoCollectionSettings
989+
{
990+
WriteConcern = new WriteConcern(9)
991+
};
992+
var collection = _database.GetCollection(_collection.Name, collectionSettings);
993+
var args = new FindAndModifyArgs
994+
{
995+
Query = Query.EQ("x", 1),
996+
Update = Update.Set("x", 2),
997+
VersionReturned = FindAndModifyDocumentVersion.Modified
998+
};
999+
1000+
Action action = () => collection.FindAndModify(args);
1001+
1002+
var exception = action.ShouldThrow<MongoWriteConcernException>().Which;
1003+
var commandResult = exception.Result;
1004+
var result = commandResult["value"].AsBsonDocument;
1005+
result.Should().Be("{ _id : 1, x : 2 }");
1006+
}
1007+
9551008
private class FindAndModifyClass
9561009
{
9571010
public ObjectId Id;
@@ -1048,6 +1101,31 @@ public void TestFindAndRemoveWithMaxTime()
10481101
}
10491102
}
10501103

1104+
[Test]
1105+
[RequiresServer(ClusterTypes = ClusterTypes.ReplicaSet)]
1106+
public void TestFindAndRemoveWithWriteConcernError()
1107+
{
1108+
_collection.RemoveAll();
1109+
_collection.Insert(new BsonDocument("x", 1));
1110+
var collectionSettings = new MongoCollectionSettings
1111+
{
1112+
WriteConcern = new WriteConcern(9)
1113+
};
1114+
var collection = _database.GetCollection(_collection.Name, collectionSettings);
1115+
var args = new FindAndRemoveArgs
1116+
{
1117+
Query = Query.EQ("x", 1)
1118+
};
1119+
1120+
Action action = () => collection.FindAndRemove(args);
1121+
1122+
var exception = action.ShouldThrow<MongoWriteConcernException>().Which;
1123+
var commandResult = exception.Result;
1124+
var result = commandResult["value"].AsBsonDocument;
1125+
result["x"].Should().Be(1);
1126+
_collection.Count().Should().Be(0);
1127+
}
1128+
10511129
[Test]
10521130
public void TestFindNearSphericalFalse()
10531131
{
@@ -2281,6 +2359,24 @@ public void TestInsertDuplicateKey()
22812359
CheckExpectedResult(expectedResult, result);
22822360
}
22832361

2362+
[Test]
2363+
[RequiresServer(ClusterTypes = ClusterTypes.ReplicaSet)]
2364+
public void TestInsertWithWriteConcernError()
2365+
{
2366+
_collection.RemoveAll();
2367+
var document = new BsonDocument { { "_id", 1 }, { "x", 1 } };
2368+
var collectionSettings = new MongoCollectionSettings
2369+
{
2370+
WriteConcern = new WriteConcern(9)
2371+
};
2372+
var collection = _database.GetCollection(_collection.Name, collectionSettings);
2373+
2374+
Action action = () => collection.Insert(document);
2375+
2376+
action.ShouldThrow<MongoWriteConcernException>();
2377+
_collection.FindOne().Should().Be(document);
2378+
}
2379+
22842380
[Test]
22852381
public void TestIsCappedFalse()
22862382
{
@@ -2727,6 +2823,25 @@ public void TestRemoveUnacknowledeged()
27272823
}
27282824
}
27292825

2826+
[Test]
2827+
[RequiresServer(ClusterTypes = ClusterTypes.ReplicaSet)]
2828+
public void TestRemoveWithWriteConcernError()
2829+
{
2830+
_collection.RemoveAll();
2831+
_collection.Insert(new BsonDocument { { "_id", 1 }, { "x", 1 } });
2832+
var collectionSettings = new MongoCollectionSettings
2833+
{
2834+
WriteConcern = new WriteConcern(9)
2835+
};
2836+
var collection = _database.GetCollection(_collection.Name, collectionSettings);
2837+
var query = Query.EQ("x", 1);
2838+
2839+
Action action = () => collection.Remove(query);
2840+
2841+
action.ShouldThrow<MongoWriteConcernException>();
2842+
_collection.Count().Should().Be(0);
2843+
}
2844+
27302845
[Test]
27312846
public void TestSetFields()
27322847
{
@@ -2967,6 +3082,26 @@ public void TestUpdateUnacknowledged()
29673082
}
29683083
}
29693084

3085+
[Test]
3086+
[RequiresServer(ClusterTypes = ClusterTypes.ReplicaSet)]
3087+
public void TestUpdateWithWriteConcernError()
3088+
{
3089+
_collection.RemoveAll();
3090+
_collection.Insert(new BsonDocument { { "_id", 1 }, { "x", 1 } });
3091+
var collectionSettings = new MongoCollectionSettings
3092+
{
3093+
WriteConcern = new WriteConcern(9)
3094+
};
3095+
var collection = _database.GetCollection(_collection.Name, collectionSettings);
3096+
var query = Query.EQ("x", 1);
3097+
var update = Update.Set("x", 2);
3098+
3099+
Action action = () => collection.Update(query, update);
3100+
3101+
action.ShouldThrow<MongoWriteConcernException>();
3102+
_collection.FindOne().Should().Be("{ _id : 1, x : 2 }");
3103+
}
3104+
29703105
[Test]
29713106
public void TestUpsertExisting()
29723107
{

0 commit comments

Comments
 (0)