Skip to content

Commit 20e289d

Browse files
author
rstam
committed
Fixed some unit tests that were failing against server 2.5.2.
1 parent 48aae83 commit 20e289d

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

MongoDB.DriverUnitTests/Configuration.cs

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ static Configuration()
4747
__testServer = __testClient.GetServer();
4848
__testDatabase = __testServer.GetDatabase("csharpdriverunittests");
4949
__testCollection = __testDatabase.GetCollection("testcollection");
50+
51+
// connect early so BuildInfo will be populated
52+
__testServer.Connect();
5053
}
5154

5255
// public static properties

MongoDB.DriverUnitTests/Jira/CSharp111Tests.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.Collections.Generic;
1718
using MongoDB.Bson;
1819
using MongoDB.Driver.Builders;
@@ -62,7 +63,16 @@ public void TestAddToSetEach()
6263

6364
var document = collection.FindOneAs<BsonDocument>();
6465
var json = document.ToJson();
65-
var expected = "{ 'InnerObjects' : [1, { 'X' : 1 }, { 'X' : 2 }, { 'X' : 3 }], '_id' : ObjectId('#ID') }"; // server put _id at end?
66+
string expected;
67+
if (server.BuildInfo.Version >= new Version(2, 5, 2))
68+
{
69+
expected = "{ '_id' : ObjectId('#ID'), 'InnerObjects' : [1, { 'X' : 1 }, { 'X' : 2 }, { 'X' : 3 }] }";
70+
}
71+
else
72+
{
73+
// prior to version 2.5.2 the server would reorder the elements when Update was used
74+
expected = "{ 'InnerObjects' : [1, { 'X' : 1 }, { 'X' : 2 }, { 'X' : 3 }], '_id' : ObjectId('#ID') }";
75+
}
6676
expected = expected.Replace("#ID", id.ToString());
6777
expected = expected.Replace("'", "\"");
6878
Assert.AreEqual(expected, json);

MongoDB.DriverUnitTests/Jira/CSharp253Tests.cs

+14-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using MongoDB.Bson;
1718
using MongoDB.Driver;
1819
using NUnit.Framework;
@@ -73,20 +74,25 @@ public void TestLegacyDollar()
7374
{
7475
{ "_id", ObjectId.GenerateNewId() },
7576
{ "BsonNull", new BsonDocument("_csharpnull", true) },
76-
{ "Code", new BsonDocument
77-
{
78-
{ "$code", "code" },
79-
{ "$scope", "scope" }
80-
}
81-
},
8277
{ "DBRef", new BsonDocument
8378
{
84-
{ "$db", "db" },
79+
// starting with server version 2.5.2 the order of the fields must be exactly as below
80+
{ "$ref", "ref" },
8581
{ "$id", "id" },
86-
{ "$ref", "ref" }
82+
{ "$db", "db" }
8783
}
8884
}
8985
};
86+
if (_server.BuildInfo.Version < new Version(2, 5, 2))
87+
{
88+
// starting with version 2.5.2 the server got stricter about dollars in element names
89+
// so the Code element below can only be added when testing against older servers
90+
document["Code"] = new BsonDocument
91+
{
92+
{ "$code", "code" },
93+
{ "$scope", "scope" }
94+
};
95+
}
9096
_collection.Insert(document);
9197
}
9298

MongoDB.DriverUnitTests/Jira/CSharp358Tests.cs

+29-23
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using MongoDB.Bson;
1718
using MongoDB.Driver;
1819
using MongoDB.Driver.Builders;
@@ -26,33 +27,38 @@ public class CSharp358Tests
2627
[Test]
2728
public void TestInsertUpdateAndSaveWithElementNameStartingWithDollarSign()
2829
{
30+
// starting with version 2.5.2 the server got stricter about dollars in element names
31+
// so this test should only be run when testing against older servers
2932
var server = Configuration.TestServer;
30-
var database = Configuration.TestDatabase;
31-
var collection = Configuration.TestCollection;
32-
collection.Drop();
33-
34-
var document = new BsonDocument
33+
if (server.BuildInfo.Version < new Version(2, 5, 2))
3534
{
36-
{ "_id", 1 },
37-
{ "v", new BsonDocument("$x", 1) } // server doesn't allow "$" at top level
38-
};
39-
var insertOptions = new MongoInsertOptions { CheckElementNames = false };
40-
collection.Insert(document, insertOptions);
41-
document = collection.FindOne();
42-
Assert.AreEqual(1, document["v"]["$x"].AsInt32);
35+
var database = Configuration.TestDatabase;
36+
var collection = Configuration.TestCollection;
37+
collection.Drop();
38+
39+
var document = new BsonDocument
40+
{
41+
{ "_id", 1 },
42+
{ "v", new BsonDocument("$x", 1) } // server doesn't allow "$" at top level
43+
};
44+
var insertOptions = new MongoInsertOptions { CheckElementNames = false };
45+
collection.Insert(document, insertOptions);
46+
document = collection.FindOne();
47+
Assert.AreEqual(1, document["v"]["$x"].AsInt32);
4348

44-
document["v"]["$x"] = 2;
45-
var query = Query.EQ("_id", 1);
46-
var update = Update.Replace(document);
47-
var updateOptions = new MongoUpdateOptions { CheckElementNames = false };
48-
collection.Update(query, update, updateOptions);
49-
document = collection.FindOne();
50-
Assert.AreEqual(2, document["v"]["$x"].AsInt32);
49+
document["v"]["$x"] = 2;
50+
var query = Query.EQ("_id", 1);
51+
var update = Update.Replace(document);
52+
var updateOptions = new MongoUpdateOptions { CheckElementNames = false };
53+
collection.Update(query, update, updateOptions);
54+
document = collection.FindOne();
55+
Assert.AreEqual(2, document["v"]["$x"].AsInt32);
5156

52-
document["v"]["$x"] = 3;
53-
collection.Save(document, insertOptions);
54-
document = collection.FindOne();
55-
Assert.AreEqual(3, document["v"]["$x"].AsInt32);
57+
document["v"]["$x"] = 3;
58+
collection.Save(document, insertOptions);
59+
document = collection.FindOne();
60+
Assert.AreEqual(3, document["v"]["$x"].AsInt32);
61+
}
5662
}
5763
}
5864
}

0 commit comments

Comments
 (0)