Skip to content

Commit aa6fc73

Browse files
author
rstam
committed
CSHARP-553: fix unit tests that weren't passing when connected to mongos (mostly by skipping them, as they test features that mongos doesn't support or supports in a complicated way that doesn't match the simple test).
1 parent 415552d commit aa6fc73

File tree

6 files changed

+222
-149
lines changed

6 files changed

+222
-149
lines changed

Driver/Core/MongoServerInstance.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace MongoDB.Driver
2525
/// <summary>
2626
/// Represents an instance of a MongoDB server host (in the case of a replica set a MongoServer uses multiple MongoServerInstances).
2727
/// </summary>
28-
internal enum MongoServerInstanceType
28+
public enum MongoServerInstanceType
2929
{
3030
/// <summary>
3131
/// The server instance type is unknown. This is the default.
@@ -127,7 +127,7 @@ internal ReplicaSetInformation ReplicaSetInformation
127127
/// <summary>
128128
/// Gets the instance type.
129129
/// </summary>
130-
internal MongoServerInstanceType InstanceType
130+
public MongoServerInstanceType InstanceType
131131
{
132132
get
133133
{

DriverUnitTests/Core/CommandResults/CommandResultTests.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,30 @@ public void TestIsMasterCommand()
142142
[Test]
143143
public void TestInvalidCommand()
144144
{
145-
try
146-
{
147-
var result = _database.RunCommand("invalid");
148-
}
149-
catch (MongoCommandException ex)
145+
using (_database.RequestStart())
150146
{
151-
Assert.IsTrue(ex.Message.StartsWith("Command 'invalid' failed: no such cmd", StringComparison.Ordinal));
147+
try
148+
{
149+
var result = _database.RunCommand("invalid");
150+
}
151+
catch (Exception ex)
152+
{
153+
// when connected to mongod a MongoCommandException is thrown
154+
// but when connected to mongos a MongoQueryException is thrown
155+
// this should be considered a server bug that they don't report the error in the same way
156+
157+
var instance = _server.RequestConnection.ServerInstance;
158+
if (instance.InstanceType == MongoServerInstanceType.ShardRouter)
159+
{
160+
Assert.IsInstanceOf<MongoQueryException>(ex);
161+
Assert.IsTrue(ex.Message.StartsWith("QueryFailure flag was unrecognized command: ", StringComparison.Ordinal));
162+
}
163+
else
164+
{
165+
Assert.IsInstanceOf<MongoCommandException>(ex);
166+
Assert.IsTrue(ex.Message.StartsWith("Command 'invalid' failed: no such cmd", StringComparison.Ordinal));
167+
}
168+
}
152169
}
153170
}
154171
}

DriverUnitTests/Core/CommandResults/DatabaseStatsResultTests.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,27 @@ public void Setup()
4343
[Test]
4444
public void Test()
4545
{
46-
// make sure collection and database exist
47-
_collection.Insert(new BsonDocument());
46+
using (_database.RequestStart())
47+
{
48+
var instance = _server.RequestConnection.ServerInstance;
49+
if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
50+
{
51+
// make sure collection and database exist
52+
_collection.Insert(new BsonDocument());
4853

49-
var result = _database.GetStats();
50-
Assert.IsTrue(result.Ok);
51-
Assert.IsTrue(result.AverageObjectSize > 0);
52-
Assert.IsTrue(result.CollectionCount > 0);
53-
Assert.IsTrue(result.DataSize > 0);
54-
Assert.IsTrue(result.ExtentCount > 0);
55-
Assert.IsTrue(result.FileSize > 0);
56-
Assert.IsTrue(result.IndexCount > 0);
57-
Assert.IsTrue(result.IndexSize > 0);
58-
Assert.IsTrue(result.ObjectCount > 0);
59-
Assert.IsTrue(result.StorageSize > 0);
54+
var result = _database.GetStats();
55+
Assert.IsTrue(result.Ok);
56+
Assert.IsTrue(result.AverageObjectSize > 0);
57+
Assert.IsTrue(result.CollectionCount > 0);
58+
Assert.IsTrue(result.DataSize > 0);
59+
Assert.IsTrue(result.ExtentCount > 0);
60+
Assert.IsTrue(result.FileSize > 0);
61+
Assert.IsTrue(result.IndexCount > 0);
62+
Assert.IsTrue(result.IndexSize > 0);
63+
Assert.IsTrue(result.ObjectCount > 0);
64+
Assert.IsTrue(result.StorageSize > 0);
65+
}
66+
}
6067
}
6168
}
6269
}

DriverUnitTests/Core/CommandResults/ValidateCollectionResultTests.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,20 @@ public void Setup()
4343
[Test]
4444
public void Test()
4545
{
46-
// make sure collection exists and has exactly one document
47-
_collection.RemoveAll();
48-
_collection.Insert(new BsonDocument());
46+
using (_database.RequestStart())
47+
{
48+
var instance = _server.RequestConnection.ServerInstance;
49+
if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
50+
{
51+
// make sure collection exists and has exactly one document
52+
_collection.RemoveAll();
53+
_collection.Insert(new BsonDocument());
4954

50-
var result = _collection.Validate();
51-
Assert.IsTrue(result.Ok);
52-
Assert.AreEqual(_collection.FullName, result.Namespace);
55+
var result = _collection.Validate();
56+
Assert.IsTrue(result.Ok);
57+
Assert.AreEqual(_collection.FullName, result.Namespace);
58+
}
59+
}
5360
}
5461
}
5562
}

DriverUnitTests/Core/MongoCollectionTests.cs

Lines changed: 112 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -652,53 +652,67 @@ private class Place
652652
[Test]
653653
public void TestGeoHaystackSearch()
654654
{
655-
if (_collection.Exists()) { _collection.Drop(); }
656-
_collection.Insert(new Place { Location = new[] { 34.2, 33.3 }, Type = "restaurant" });
657-
_collection.Insert(new Place { Location = new[] { 34.2, 37.3 }, Type = "restaurant" });
658-
_collection.Insert(new Place { Location = new[] { 59.1, 87.2 }, Type = "office" });
659-
_collection.CreateIndex(IndexKeys.GeoSpatialHaystack("Location", "Type"), IndexOptions.SetBucketSize(1));
660-
661-
var options = GeoHaystackSearchOptions
662-
.SetLimit(30)
663-
.SetMaxDistance(6)
664-
.SetQuery("Type", "restaurant");
665-
var result = _collection.GeoHaystackSearchAs<Place>(33, 33, options);
666-
Assert.IsTrue(result.Ok);
667-
Assert.IsTrue(result.Stats.Duration >= TimeSpan.Zero);
668-
Assert.AreEqual(2, result.Stats.BTreeMatches);
669-
Assert.AreEqual(2, result.Stats.NumberOfHits);
670-
Assert.AreEqual(34.2, result.Hits[0].Document.Location[0]);
671-
Assert.AreEqual(33.3, result.Hits[0].Document.Location[1]);
672-
Assert.AreEqual("restaurant", result.Hits[0].Document.Type);
673-
Assert.AreEqual(34.2, result.Hits[1].Document.Location[0]);
674-
Assert.AreEqual(37.3, result.Hits[1].Document.Location[1]);
675-
Assert.AreEqual("restaurant", result.Hits[1].Document.Type);
655+
using (_database.RequestStart())
656+
{
657+
var instance = _server.RequestConnection.ServerInstance;
658+
if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
659+
{
660+
if (_collection.Exists()) { _collection.Drop(); }
661+
_collection.Insert(new Place { Location = new[] { 34.2, 33.3 }, Type = "restaurant" });
662+
_collection.Insert(new Place { Location = new[] { 34.2, 37.3 }, Type = "restaurant" });
663+
_collection.Insert(new Place { Location = new[] { 59.1, 87.2 }, Type = "office" });
664+
_collection.CreateIndex(IndexKeys.GeoSpatialHaystack("Location", "Type"), IndexOptions.SetBucketSize(1));
665+
666+
var options = GeoHaystackSearchOptions
667+
.SetLimit(30)
668+
.SetMaxDistance(6)
669+
.SetQuery("Type", "restaurant");
670+
var result = _collection.GeoHaystackSearchAs<Place>(33, 33, options);
671+
Assert.IsTrue(result.Ok);
672+
Assert.IsTrue(result.Stats.Duration >= TimeSpan.Zero);
673+
Assert.AreEqual(2, result.Stats.BTreeMatches);
674+
Assert.AreEqual(2, result.Stats.NumberOfHits);
675+
Assert.AreEqual(34.2, result.Hits[0].Document.Location[0]);
676+
Assert.AreEqual(33.3, result.Hits[0].Document.Location[1]);
677+
Assert.AreEqual("restaurant", result.Hits[0].Document.Type);
678+
Assert.AreEqual(34.2, result.Hits[1].Document.Location[0]);
679+
Assert.AreEqual(37.3, result.Hits[1].Document.Location[1]);
680+
Assert.AreEqual("restaurant", result.Hits[1].Document.Type);
681+
}
682+
}
676683
}
677684

678685
[Test]
679686
public void TestGeoHaystackSearch_Typed()
680687
{
681-
if (_collection.Exists()) { _collection.Drop(); }
682-
_collection.Insert(new Place { Location = new[] { 34.2, 33.3 }, Type = "restaurant" });
683-
_collection.Insert(new Place { Location = new[] { 34.2, 37.3 }, Type = "restaurant" });
684-
_collection.Insert(new Place { Location = new[] { 59.1, 87.2 }, Type = "office" });
685-
_collection.CreateIndex(IndexKeys<Place>.GeoSpatialHaystack(x => x.Location, x => x.Type), IndexOptions.SetBucketSize(1));
686-
687-
var options = GeoHaystackSearchOptions<Place>
688-
.SetLimit(30)
689-
.SetMaxDistance(6)
690-
.SetQuery(x => x.Type, "restaurant");
691-
var result = _collection.GeoHaystackSearchAs<Place>(33, 33, options);
692-
Assert.IsTrue(result.Ok);
693-
Assert.IsTrue(result.Stats.Duration >= TimeSpan.Zero);
694-
Assert.AreEqual(2, result.Stats.BTreeMatches);
695-
Assert.AreEqual(2, result.Stats.NumberOfHits);
696-
Assert.AreEqual(34.2, result.Hits[0].Document.Location[0]);
697-
Assert.AreEqual(33.3, result.Hits[0].Document.Location[1]);
698-
Assert.AreEqual("restaurant", result.Hits[0].Document.Type);
699-
Assert.AreEqual(34.2, result.Hits[1].Document.Location[0]);
700-
Assert.AreEqual(37.3, result.Hits[1].Document.Location[1]);
701-
Assert.AreEqual("restaurant", result.Hits[1].Document.Type);
688+
using (_database.RequestStart())
689+
{
690+
var instance = _server.RequestConnection.ServerInstance;
691+
if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
692+
{
693+
if (_collection.Exists()) { _collection.Drop(); }
694+
_collection.Insert(new Place { Location = new[] { 34.2, 33.3 }, Type = "restaurant" });
695+
_collection.Insert(new Place { Location = new[] { 34.2, 37.3 }, Type = "restaurant" });
696+
_collection.Insert(new Place { Location = new[] { 59.1, 87.2 }, Type = "office" });
697+
_collection.CreateIndex(IndexKeys<Place>.GeoSpatialHaystack(x => x.Location, x => x.Type), IndexOptions.SetBucketSize(1));
698+
699+
var options = GeoHaystackSearchOptions<Place>
700+
.SetLimit(30)
701+
.SetMaxDistance(6)
702+
.SetQuery(x => x.Type, "restaurant");
703+
var result = _collection.GeoHaystackSearchAs<Place>(33, 33, options);
704+
Assert.IsTrue(result.Ok);
705+
Assert.IsTrue(result.Stats.Duration >= TimeSpan.Zero);
706+
Assert.AreEqual(2, result.Stats.BTreeMatches);
707+
Assert.AreEqual(2, result.Stats.NumberOfHits);
708+
Assert.AreEqual(34.2, result.Hits[0].Document.Location[0]);
709+
Assert.AreEqual(33.3, result.Hits[0].Document.Location[1]);
710+
Assert.AreEqual("restaurant", result.Hits[0].Document.Type);
711+
Assert.AreEqual(34.2, result.Hits[1].Document.Location[0]);
712+
Assert.AreEqual(37.3, result.Hits[1].Document.Location[1]);
713+
Assert.AreEqual("restaurant", result.Hits[1].Document.Type);
714+
}
715+
}
702716
}
703717

704718
[Test]
@@ -1262,21 +1276,28 @@ public void TestMapReduceInlineWithQuery()
12621276
[Test]
12631277
public void TestReIndex()
12641278
{
1265-
_collection.RemoveAll();
1266-
_collection.Insert(new BsonDocument("x", 1));
1267-
_collection.Insert(new BsonDocument("x", 2));
1268-
_collection.DropAllIndexes();
1269-
_collection.CreateIndex("x");
1270-
// note: prior to 1.8.1 the reIndex command was returning duplicate ok elements
1271-
try
1279+
using (_database.RequestStart())
12721280
{
1273-
var result = _collection.ReIndex();
1274-
Assert.AreEqual(2, result.Response["nIndexes"].ToInt32());
1275-
Assert.AreEqual(2, result.Response["nIndexesWas"].ToInt32());
1276-
}
1277-
catch (InvalidOperationException ex)
1278-
{
1279-
Assert.AreEqual("Duplicate element name 'ok'.", ex.Message);
1281+
var instance = _server.RequestConnection.ServerInstance;
1282+
if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
1283+
{
1284+
_collection.RemoveAll();
1285+
_collection.Insert(new BsonDocument("x", 1));
1286+
_collection.Insert(new BsonDocument("x", 2));
1287+
_collection.DropAllIndexes();
1288+
_collection.CreateIndex("x");
1289+
// note: prior to 1.8.1 the reIndex command was returning duplicate ok elements
1290+
try
1291+
{
1292+
var result = _collection.ReIndex();
1293+
Assert.AreEqual(2, result.Response["nIndexes"].ToInt32());
1294+
Assert.AreEqual(2, result.Response["nIndexesWas"].ToInt32());
1295+
}
1296+
catch (InvalidOperationException ex)
1297+
{
1298+
Assert.AreEqual("Duplicate element name 'ok'.", ex.Message);
1299+
}
1300+
}
12801301
}
12811302
}
12821303

@@ -1409,34 +1430,41 @@ public void TestUpdateNullQuery()
14091430
[Test]
14101431
public void TestValidate()
14111432
{
1412-
// ensure collection exists
1413-
_collection.RemoveAll();
1414-
_collection.Insert(new BsonDocument("x", 1));
1415-
1416-
var result = _collection.Validate();
1417-
var ns = result.Namespace;
1418-
var firstExtent = result.FirstExtent;
1419-
var lastExtent = result.LastExtent;
1420-
var extentCount = result.ExtentCount;
1421-
var dataSize = result.DataSize;
1422-
var nrecords = result.RecordCount;
1423-
var lastExtentSize = result.LastExtentSize;
1424-
var padding = result.Padding;
1425-
var firstExtentDetails = result.FirstExtentDetails;
1426-
var loc = firstExtentDetails.Loc;
1427-
var xnext = firstExtentDetails.XNext;
1428-
var xprev = firstExtentDetails.XPrev;
1429-
var nsdiag = firstExtentDetails.NSDiag;
1430-
var size = firstExtentDetails.Size;
1431-
var firstRecord = firstExtentDetails.FirstRecord;
1432-
var lastRecord = firstExtentDetails.LastRecord;
1433-
var deletedCount = result.DeletedCount;
1434-
var deletedSize = result.DeletedSize;
1435-
var nindexes = result.IndexCount;
1436-
var keysPerIndex = result.KeysPerIndex;
1437-
var valid = result.IsValid;
1438-
var errors = result.Errors;
1439-
var warning = result.Warning;
1433+
using (_database.RequestStart())
1434+
{
1435+
var instance = _server.RequestConnection.ServerInstance;
1436+
if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
1437+
{
1438+
// ensure collection exists
1439+
_collection.RemoveAll();
1440+
_collection.Insert(new BsonDocument("x", 1));
1441+
1442+
var result = _collection.Validate();
1443+
var ns = result.Namespace;
1444+
var firstExtent = result.FirstExtent;
1445+
var lastExtent = result.LastExtent;
1446+
var extentCount = result.ExtentCount;
1447+
var dataSize = result.DataSize;
1448+
var nrecords = result.RecordCount;
1449+
var lastExtentSize = result.LastExtentSize;
1450+
var padding = result.Padding;
1451+
var firstExtentDetails = result.FirstExtentDetails;
1452+
var loc = firstExtentDetails.Loc;
1453+
var xnext = firstExtentDetails.XNext;
1454+
var xprev = firstExtentDetails.XPrev;
1455+
var nsdiag = firstExtentDetails.NSDiag;
1456+
var size = firstExtentDetails.Size;
1457+
var firstRecord = firstExtentDetails.FirstRecord;
1458+
var lastRecord = firstExtentDetails.LastRecord;
1459+
var deletedCount = result.DeletedCount;
1460+
var deletedSize = result.DeletedSize;
1461+
var nindexes = result.IndexCount;
1462+
var keysPerIndex = result.KeysPerIndex;
1463+
var valid = result.IsValid;
1464+
var errors = result.Errors;
1465+
var warning = result.Warning;
1466+
}
1467+
}
14401468
}
14411469
}
14421470
}

0 commit comments

Comments
 (0)