|
25 | 25 | using MongoDB.Driver.Builders;
|
26 | 26 | using MongoDB.Driver.Core;
|
27 | 27 | using MongoDB.Driver.GeoJsonObjectModel;
|
| 28 | +using FluentAssertions; |
28 | 29 | using NUnit.Framework;
|
29 | 30 |
|
30 | 31 | namespace MongoDB.Driver.Tests
|
@@ -952,6 +953,58 @@ public void TestFindAndModifyUpsert()
|
952 | 953 | Assert.AreEqual(1, result.ModifiedDocument["count"].AsInt32);
|
953 | 954 | }
|
954 | 955 |
|
| 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 | + |
955 | 1008 | private class FindAndModifyClass
|
956 | 1009 | {
|
957 | 1010 | public ObjectId Id;
|
@@ -1048,6 +1101,31 @@ public void TestFindAndRemoveWithMaxTime()
|
1048 | 1101 | }
|
1049 | 1102 | }
|
1050 | 1103 |
|
| 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 | + |
1051 | 1129 | [Test]
|
1052 | 1130 | public void TestFindNearSphericalFalse()
|
1053 | 1131 | {
|
@@ -2281,6 +2359,24 @@ public void TestInsertDuplicateKey()
|
2281 | 2359 | CheckExpectedResult(expectedResult, result);
|
2282 | 2360 | }
|
2283 | 2361 |
|
| 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 | + |
2284 | 2380 | [Test]
|
2285 | 2381 | public void TestIsCappedFalse()
|
2286 | 2382 | {
|
@@ -2727,6 +2823,25 @@ public void TestRemoveUnacknowledeged()
|
2727 | 2823 | }
|
2728 | 2824 | }
|
2729 | 2825 |
|
| 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 | + |
2730 | 2845 | [Test]
|
2731 | 2846 | public void TestSetFields()
|
2732 | 2847 | {
|
@@ -2967,6 +3082,26 @@ public void TestUpdateUnacknowledged()
|
2967 | 3082 | }
|
2968 | 3083 | }
|
2969 | 3084 |
|
| 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 | + |
2970 | 3105 | [Test]
|
2971 | 3106 | public void TestUpsertExisting()
|
2972 | 3107 | {
|
|
0 commit comments