Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

CSHARP-553: fix unit tests that weren't passing when connected to mon…

…gos (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).
  • Loading branch information...
commit aa6fc73e5defbe027b60996e9960206046cf6138 1 parent 415552d
rstam authored
View
4 Driver/Core/MongoServerInstance.cs
@@ -25,7 +25,7 @@ namespace MongoDB.Driver
/// <summary>
/// Represents an instance of a MongoDB server host (in the case of a replica set a MongoServer uses multiple MongoServerInstances).
/// </summary>
- internal enum MongoServerInstanceType
+ public enum MongoServerInstanceType
{
/// <summary>
/// The server instance type is unknown. This is the default.
@@ -127,7 +127,7 @@ internal ReplicaSetInformation ReplicaSetInformation
/// <summary>
/// Gets the instance type.
/// </summary>
- internal MongoServerInstanceType InstanceType
+ public MongoServerInstanceType InstanceType
{
get
{
View
29 DriverUnitTests/Core/CommandResults/CommandResultTests.cs
@@ -142,13 +142,30 @@ public void TestIsMasterCommand()
[Test]
public void TestInvalidCommand()
{
- try
- {
- var result = _database.RunCommand("invalid");
- }
- catch (MongoCommandException ex)
+ using (_database.RequestStart())
{
- Assert.IsTrue(ex.Message.StartsWith("Command 'invalid' failed: no such cmd", StringComparison.Ordinal));
+ try
+ {
+ var result = _database.RunCommand("invalid");
+ }
+ catch (Exception ex)
+ {
+ // when connected to mongod a MongoCommandException is thrown
+ // but when connected to mongos a MongoQueryException is thrown
+ // this should be considered a server bug that they don't report the error in the same way
+
+ var instance = _server.RequestConnection.ServerInstance;
+ if (instance.InstanceType == MongoServerInstanceType.ShardRouter)
+ {
+ Assert.IsInstanceOf<MongoQueryException>(ex);
+ Assert.IsTrue(ex.Message.StartsWith("QueryFailure flag was unrecognized command: ", StringComparison.Ordinal));
+ }
+ else
+ {
+ Assert.IsInstanceOf<MongoCommandException>(ex);
+ Assert.IsTrue(ex.Message.StartsWith("Command 'invalid' failed: no such cmd", StringComparison.Ordinal));
+ }
+ }
}
}
}
View
33 DriverUnitTests/Core/CommandResults/DatabaseStatsResultTests.cs
@@ -43,20 +43,27 @@ public void Setup()
[Test]
public void Test()
{
- // make sure collection and database exist
- _collection.Insert(new BsonDocument());
+ using (_database.RequestStart())
+ {
+ var instance = _server.RequestConnection.ServerInstance;
+ if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
+ {
+ // make sure collection and database exist
+ _collection.Insert(new BsonDocument());
- var result = _database.GetStats();
- Assert.IsTrue(result.Ok);
- Assert.IsTrue(result.AverageObjectSize > 0);
- Assert.IsTrue(result.CollectionCount > 0);
- Assert.IsTrue(result.DataSize > 0);
- Assert.IsTrue(result.ExtentCount > 0);
- Assert.IsTrue(result.FileSize > 0);
- Assert.IsTrue(result.IndexCount > 0);
- Assert.IsTrue(result.IndexSize > 0);
- Assert.IsTrue(result.ObjectCount > 0);
- Assert.IsTrue(result.StorageSize > 0);
+ var result = _database.GetStats();
+ Assert.IsTrue(result.Ok);
+ Assert.IsTrue(result.AverageObjectSize > 0);
+ Assert.IsTrue(result.CollectionCount > 0);
+ Assert.IsTrue(result.DataSize > 0);
+ Assert.IsTrue(result.ExtentCount > 0);
+ Assert.IsTrue(result.FileSize > 0);
+ Assert.IsTrue(result.IndexCount > 0);
+ Assert.IsTrue(result.IndexSize > 0);
+ Assert.IsTrue(result.ObjectCount > 0);
+ Assert.IsTrue(result.StorageSize > 0);
+ }
+ }
}
}
}
View
19 DriverUnitTests/Core/CommandResults/ValidateCollectionResultTests.cs
@@ -43,13 +43,20 @@ public void Setup()
[Test]
public void Test()
{
- // make sure collection exists and has exactly one document
- _collection.RemoveAll();
- _collection.Insert(new BsonDocument());
+ using (_database.RequestStart())
+ {
+ var instance = _server.RequestConnection.ServerInstance;
+ if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
+ {
+ // make sure collection exists and has exactly one document
+ _collection.RemoveAll();
+ _collection.Insert(new BsonDocument());
- var result = _collection.Validate();
- Assert.IsTrue(result.Ok);
- Assert.AreEqual(_collection.FullName, result.Namespace);
+ var result = _collection.Validate();
+ Assert.IsTrue(result.Ok);
+ Assert.AreEqual(_collection.FullName, result.Namespace);
+ }
+ }
}
}
}
View
196 DriverUnitTests/Core/MongoCollectionTests.cs
@@ -652,53 +652,67 @@ private class Place
[Test]
public void TestGeoHaystackSearch()
{
- if (_collection.Exists()) { _collection.Drop(); }
- _collection.Insert(new Place { Location = new[] { 34.2, 33.3 }, Type = "restaurant" });
- _collection.Insert(new Place { Location = new[] { 34.2, 37.3 }, Type = "restaurant" });
- _collection.Insert(new Place { Location = new[] { 59.1, 87.2 }, Type = "office" });
- _collection.CreateIndex(IndexKeys.GeoSpatialHaystack("Location", "Type"), IndexOptions.SetBucketSize(1));
-
- var options = GeoHaystackSearchOptions
- .SetLimit(30)
- .SetMaxDistance(6)
- .SetQuery("Type", "restaurant");
- var result = _collection.GeoHaystackSearchAs<Place>(33, 33, options);
- Assert.IsTrue(result.Ok);
- Assert.IsTrue(result.Stats.Duration >= TimeSpan.Zero);
- Assert.AreEqual(2, result.Stats.BTreeMatches);
- Assert.AreEqual(2, result.Stats.NumberOfHits);
- Assert.AreEqual(34.2, result.Hits[0].Document.Location[0]);
- Assert.AreEqual(33.3, result.Hits[0].Document.Location[1]);
- Assert.AreEqual("restaurant", result.Hits[0].Document.Type);
- Assert.AreEqual(34.2, result.Hits[1].Document.Location[0]);
- Assert.AreEqual(37.3, result.Hits[1].Document.Location[1]);
- Assert.AreEqual("restaurant", result.Hits[1].Document.Type);
+ using (_database.RequestStart())
+ {
+ var instance = _server.RequestConnection.ServerInstance;
+ if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
+ {
+ if (_collection.Exists()) { _collection.Drop(); }
+ _collection.Insert(new Place { Location = new[] { 34.2, 33.3 }, Type = "restaurant" });
+ _collection.Insert(new Place { Location = new[] { 34.2, 37.3 }, Type = "restaurant" });
+ _collection.Insert(new Place { Location = new[] { 59.1, 87.2 }, Type = "office" });
+ _collection.CreateIndex(IndexKeys.GeoSpatialHaystack("Location", "Type"), IndexOptions.SetBucketSize(1));
+
+ var options = GeoHaystackSearchOptions
+ .SetLimit(30)
+ .SetMaxDistance(6)
+ .SetQuery("Type", "restaurant");
+ var result = _collection.GeoHaystackSearchAs<Place>(33, 33, options);
+ Assert.IsTrue(result.Ok);
+ Assert.IsTrue(result.Stats.Duration >= TimeSpan.Zero);
+ Assert.AreEqual(2, result.Stats.BTreeMatches);
+ Assert.AreEqual(2, result.Stats.NumberOfHits);
+ Assert.AreEqual(34.2, result.Hits[0].Document.Location[0]);
+ Assert.AreEqual(33.3, result.Hits[0].Document.Location[1]);
+ Assert.AreEqual("restaurant", result.Hits[0].Document.Type);
+ Assert.AreEqual(34.2, result.Hits[1].Document.Location[0]);
+ Assert.AreEqual(37.3, result.Hits[1].Document.Location[1]);
+ Assert.AreEqual("restaurant", result.Hits[1].Document.Type);
+ }
+ }
}
[Test]
public void TestGeoHaystackSearch_Typed()
{
- if (_collection.Exists()) { _collection.Drop(); }
- _collection.Insert(new Place { Location = new[] { 34.2, 33.3 }, Type = "restaurant" });
- _collection.Insert(new Place { Location = new[] { 34.2, 37.3 }, Type = "restaurant" });
- _collection.Insert(new Place { Location = new[] { 59.1, 87.2 }, Type = "office" });
- _collection.CreateIndex(IndexKeys<Place>.GeoSpatialHaystack(x => x.Location, x => x.Type), IndexOptions.SetBucketSize(1));
-
- var options = GeoHaystackSearchOptions<Place>
- .SetLimit(30)
- .SetMaxDistance(6)
- .SetQuery(x => x.Type, "restaurant");
- var result = _collection.GeoHaystackSearchAs<Place>(33, 33, options);
- Assert.IsTrue(result.Ok);
- Assert.IsTrue(result.Stats.Duration >= TimeSpan.Zero);
- Assert.AreEqual(2, result.Stats.BTreeMatches);
- Assert.AreEqual(2, result.Stats.NumberOfHits);
- Assert.AreEqual(34.2, result.Hits[0].Document.Location[0]);
- Assert.AreEqual(33.3, result.Hits[0].Document.Location[1]);
- Assert.AreEqual("restaurant", result.Hits[0].Document.Type);
- Assert.AreEqual(34.2, result.Hits[1].Document.Location[0]);
- Assert.AreEqual(37.3, result.Hits[1].Document.Location[1]);
- Assert.AreEqual("restaurant", result.Hits[1].Document.Type);
+ using (_database.RequestStart())
+ {
+ var instance = _server.RequestConnection.ServerInstance;
+ if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
+ {
+ if (_collection.Exists()) { _collection.Drop(); }
+ _collection.Insert(new Place { Location = new[] { 34.2, 33.3 }, Type = "restaurant" });
+ _collection.Insert(new Place { Location = new[] { 34.2, 37.3 }, Type = "restaurant" });
+ _collection.Insert(new Place { Location = new[] { 59.1, 87.2 }, Type = "office" });
+ _collection.CreateIndex(IndexKeys<Place>.GeoSpatialHaystack(x => x.Location, x => x.Type), IndexOptions.SetBucketSize(1));
+
+ var options = GeoHaystackSearchOptions<Place>
+ .SetLimit(30)
+ .SetMaxDistance(6)
+ .SetQuery(x => x.Type, "restaurant");
+ var result = _collection.GeoHaystackSearchAs<Place>(33, 33, options);
+ Assert.IsTrue(result.Ok);
+ Assert.IsTrue(result.Stats.Duration >= TimeSpan.Zero);
+ Assert.AreEqual(2, result.Stats.BTreeMatches);
+ Assert.AreEqual(2, result.Stats.NumberOfHits);
+ Assert.AreEqual(34.2, result.Hits[0].Document.Location[0]);
+ Assert.AreEqual(33.3, result.Hits[0].Document.Location[1]);
+ Assert.AreEqual("restaurant", result.Hits[0].Document.Type);
+ Assert.AreEqual(34.2, result.Hits[1].Document.Location[0]);
+ Assert.AreEqual(37.3, result.Hits[1].Document.Location[1]);
+ Assert.AreEqual("restaurant", result.Hits[1].Document.Type);
+ }
+ }
}
[Test]
@@ -1262,21 +1276,28 @@ public void TestMapReduceInlineWithQuery()
[Test]
public void TestReIndex()
{
- _collection.RemoveAll();
- _collection.Insert(new BsonDocument("x", 1));
- _collection.Insert(new BsonDocument("x", 2));
- _collection.DropAllIndexes();
- _collection.CreateIndex("x");
- // note: prior to 1.8.1 the reIndex command was returning duplicate ok elements
- try
+ using (_database.RequestStart())
{
- var result = _collection.ReIndex();
- Assert.AreEqual(2, result.Response["nIndexes"].ToInt32());
- Assert.AreEqual(2, result.Response["nIndexesWas"].ToInt32());
- }
- catch (InvalidOperationException ex)
- {
- Assert.AreEqual("Duplicate element name 'ok'.", ex.Message);
+ var instance = _server.RequestConnection.ServerInstance;
+ if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
+ {
+ _collection.RemoveAll();
+ _collection.Insert(new BsonDocument("x", 1));
+ _collection.Insert(new BsonDocument("x", 2));
+ _collection.DropAllIndexes();
+ _collection.CreateIndex("x");
+ // note: prior to 1.8.1 the reIndex command was returning duplicate ok elements
+ try
+ {
+ var result = _collection.ReIndex();
+ Assert.AreEqual(2, result.Response["nIndexes"].ToInt32());
+ Assert.AreEqual(2, result.Response["nIndexesWas"].ToInt32());
+ }
+ catch (InvalidOperationException ex)
+ {
+ Assert.AreEqual("Duplicate element name 'ok'.", ex.Message);
+ }
+ }
}
}
@@ -1409,34 +1430,41 @@ public void TestUpdateNullQuery()
[Test]
public void TestValidate()
{
- // ensure collection exists
- _collection.RemoveAll();
- _collection.Insert(new BsonDocument("x", 1));
-
- var result = _collection.Validate();
- var ns = result.Namespace;
- var firstExtent = result.FirstExtent;
- var lastExtent = result.LastExtent;
- var extentCount = result.ExtentCount;
- var dataSize = result.DataSize;
- var nrecords = result.RecordCount;
- var lastExtentSize = result.LastExtentSize;
- var padding = result.Padding;
- var firstExtentDetails = result.FirstExtentDetails;
- var loc = firstExtentDetails.Loc;
- var xnext = firstExtentDetails.XNext;
- var xprev = firstExtentDetails.XPrev;
- var nsdiag = firstExtentDetails.NSDiag;
- var size = firstExtentDetails.Size;
- var firstRecord = firstExtentDetails.FirstRecord;
- var lastRecord = firstExtentDetails.LastRecord;
- var deletedCount = result.DeletedCount;
- var deletedSize = result.DeletedSize;
- var nindexes = result.IndexCount;
- var keysPerIndex = result.KeysPerIndex;
- var valid = result.IsValid;
- var errors = result.Errors;
- var warning = result.Warning;
+ using (_database.RequestStart())
+ {
+ var instance = _server.RequestConnection.ServerInstance;
+ if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
+ {
+ // ensure collection exists
+ _collection.RemoveAll();
+ _collection.Insert(new BsonDocument("x", 1));
+
+ var result = _collection.Validate();
+ var ns = result.Namespace;
+ var firstExtent = result.FirstExtent;
+ var lastExtent = result.LastExtent;
+ var extentCount = result.ExtentCount;
+ var dataSize = result.DataSize;
+ var nrecords = result.RecordCount;
+ var lastExtentSize = result.LastExtentSize;
+ var padding = result.Padding;
+ var firstExtentDetails = result.FirstExtentDetails;
+ var loc = firstExtentDetails.Loc;
+ var xnext = firstExtentDetails.XNext;
+ var xprev = firstExtentDetails.XPrev;
+ var nsdiag = firstExtentDetails.NSDiag;
+ var size = firstExtentDetails.Size;
+ var firstRecord = firstExtentDetails.FirstRecord;
+ var lastRecord = firstExtentDetails.LastRecord;
+ var deletedCount = result.DeletedCount;
+ var deletedSize = result.DeletedSize;
+ var nindexes = result.IndexCount;
+ var keysPerIndex = result.KeysPerIndex;
+ var valid = result.IsValid;
+ var errors = result.Errors;
+ var warning = result.Warning;
+ }
+ }
}
}
}
View
90 DriverUnitTests/Core/MongoDatabaseTests.cs
@@ -172,15 +172,22 @@ public void TestGetCollectionNames()
[Test]
public void TestGetProfilingInfo()
{
- var collection = Configuration.TestCollection;
- if (collection.Exists()) { collection.Drop(); }
- collection.Insert(new BsonDocument("x", 1));
- _database.SetProfilingLevel(ProfilingLevel.All);
- var count = collection.Count();
- _database.SetProfilingLevel(ProfilingLevel.None);
- var info = _database.GetProfilingInfo(Query.Null).SetSortOrder(SortBy.Descending("$natural")).SetLimit(1).First();
- Assert.IsTrue(info.Timestamp >= new DateTime(2011, 10, 6, 0, 0, 0, DateTimeKind.Utc));
- Assert.IsTrue(info.Duration >= TimeSpan.Zero);
+ using (_database.RequestStart())
+ {
+ var instance = _server.RequestConnection.ServerInstance;
+ if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
+ {
+ var collection = Configuration.TestCollection;
+ if (collection.Exists()) { collection.Drop(); }
+ collection.Insert(new BsonDocument("x", 1));
+ _database.SetProfilingLevel(ProfilingLevel.All);
+ var count = collection.Count();
+ _database.SetProfilingLevel(ProfilingLevel.None);
+ var info = _database.GetProfilingInfo(Query.Null).SetSortOrder(SortBy.Descending("$natural")).SetLimit(1).First();
+ Assert.IsTrue(info.Timestamp >= new DateTime(2011, 10, 6, 0, 0, 0, DateTimeKind.Utc));
+ Assert.IsTrue(info.Duration >= TimeSpan.Zero);
+ }
+ }
}
[Test]
@@ -240,35 +247,42 @@ public void TestRenameCollectionDropTarget()
[Test]
public void TestSetProfilingLevel()
{
- _database.SetProfilingLevel(ProfilingLevel.None, TimeSpan.FromMilliseconds(100));
- var result = _database.GetProfilingLevel();
- Assert.AreEqual(ProfilingLevel.None, result.Level);
- Assert.AreEqual(TimeSpan.FromMilliseconds(100), result.Slow);
-
- _database.SetProfilingLevel(ProfilingLevel.Slow);
- result = _database.GetProfilingLevel();
- Assert.AreEqual(ProfilingLevel.Slow, result.Level);
- Assert.AreEqual(TimeSpan.FromMilliseconds(100), result.Slow);
-
- _database.SetProfilingLevel(ProfilingLevel.Slow, TimeSpan.FromMilliseconds(200));
- result = _database.GetProfilingLevel();
- Assert.AreEqual(ProfilingLevel.Slow, result.Level);
- Assert.AreEqual(TimeSpan.FromMilliseconds(200), result.Slow);
-
- _database.SetProfilingLevel(ProfilingLevel.Slow, TimeSpan.FromMilliseconds(100));
- result = _database.GetProfilingLevel();
- Assert.AreEqual(ProfilingLevel.Slow, result.Level);
- Assert.AreEqual(TimeSpan.FromMilliseconds(100), result.Slow);
-
- _database.SetProfilingLevel(ProfilingLevel.All);
- result = _database.GetProfilingLevel();
- Assert.AreEqual(ProfilingLevel.All, result.Level);
- Assert.AreEqual(TimeSpan.FromMilliseconds(100), result.Slow);
-
- _database.SetProfilingLevel(ProfilingLevel.None);
- result = _database.GetProfilingLevel();
- Assert.AreEqual(ProfilingLevel.None, result.Level);
- Assert.AreEqual(TimeSpan.FromMilliseconds(100), result.Slow);
+ using (_database.RequestStart())
+ {
+ var instance = _server.RequestConnection.ServerInstance;
+ if (instance.InstanceType != MongoServerInstanceType.ShardRouter)
+ {
+ _database.SetProfilingLevel(ProfilingLevel.None, TimeSpan.FromMilliseconds(100));
+ var result = _database.GetProfilingLevel();
+ Assert.AreEqual(ProfilingLevel.None, result.Level);
+ Assert.AreEqual(TimeSpan.FromMilliseconds(100), result.Slow);
+
+ _database.SetProfilingLevel(ProfilingLevel.Slow);
+ result = _database.GetProfilingLevel();
+ Assert.AreEqual(ProfilingLevel.Slow, result.Level);
+ Assert.AreEqual(TimeSpan.FromMilliseconds(100), result.Slow);
+
+ _database.SetProfilingLevel(ProfilingLevel.Slow, TimeSpan.FromMilliseconds(200));
+ result = _database.GetProfilingLevel();
+ Assert.AreEqual(ProfilingLevel.Slow, result.Level);
+ Assert.AreEqual(TimeSpan.FromMilliseconds(200), result.Slow);
+
+ _database.SetProfilingLevel(ProfilingLevel.Slow, TimeSpan.FromMilliseconds(100));
+ result = _database.GetProfilingLevel();
+ Assert.AreEqual(ProfilingLevel.Slow, result.Level);
+ Assert.AreEqual(TimeSpan.FromMilliseconds(100), result.Slow);
+
+ _database.SetProfilingLevel(ProfilingLevel.All);
+ result = _database.GetProfilingLevel();
+ Assert.AreEqual(ProfilingLevel.All, result.Level);
+ Assert.AreEqual(TimeSpan.FromMilliseconds(100), result.Slow);
+
+ _database.SetProfilingLevel(ProfilingLevel.None);
+ result = _database.GetProfilingLevel();
+ Assert.AreEqual(ProfilingLevel.None, result.Level);
+ Assert.AreEqual(TimeSpan.FromMilliseconds(100), result.Slow);
+ }
+ }
}
[Test]
Please sign in to comment.
Something went wrong with that request. Please try again.