Skip to content

Commit 4e7de4f

Browse files
author
rstam
committed
Added a unit test to make sure that LINQ queries comparing a property that is stored as an embeded document work. Implemented ToString method in MongoDBRef.
1 parent d285834 commit 4e7de4f

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

Change Log v1.4-Driver.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ MongoDBRef.cs
9292
better argument checking
9393
new implementation of !=, ==, Equals and GetHashCode
9494
new MongoDBRefSerializer class makes DBRefs queryable from LINQ queries
95+
implemented ToString
9596

9697
MongoInsertOptions.cs
9798
constructor no longer takes the collection as an argument

Driver/Core/MongoDBRef.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ public override int GetHashCode()
172172
hash = 37 * hash + _id.GetHashCode();
173173
return hash;
174174
}
175+
176+
/// <summary>
177+
/// Returns a string representation of the value.
178+
/// </summary>
179+
/// <returns>A string representation of the value.</returns>
180+
public override string ToString()
181+
{
182+
if (_databaseName == null)
183+
{
184+
return string.Format("new MongoDBRef(\"{0}\", {1})", _collectionName, _id);
185+
}
186+
else
187+
{
188+
return string.Format("new MongoDBRef(\"{0}\", \"{1}\", {2})", _databaseName, _collectionName, _id);
189+
}
190+
}
175191
}
176192

177193
/// <summary>

DriverOnlineTests/Linq/SelectQueryTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2740,6 +2740,29 @@ public void TestWhereDBRefDatabaseNameEqualsDb()
27402740
Assert.AreEqual(1, Consume(query));
27412741
}
27422742

2743+
[Test]
2744+
public void TestWhereDBRefEquals()
2745+
{
2746+
var query = from c in _collection.AsQueryable<C>()
2747+
where c.DBRef == new MongoDBRef("db", "c", 1)
2748+
select c;
2749+
2750+
var translatedQuery = MongoQueryTranslator.Translate(query);
2751+
Assert.IsInstanceOf<SelectQuery>(translatedQuery);
2752+
Assert.AreSame(_collection, translatedQuery.Collection);
2753+
Assert.AreSame(typeof(C), translatedQuery.DocumentType);
2754+
2755+
var selectQuery = (SelectQuery)translatedQuery;
2756+
Assert.AreEqual("(C c) => (c.DBRef == new MongoDBRef(\"db\", \"c\", 1))", ExpressionFormatter.ToString(selectQuery.Where));
2757+
Assert.IsNull(selectQuery.OrderBy);
2758+
Assert.IsNull(selectQuery.Projection);
2759+
Assert.IsNull(selectQuery.Skip);
2760+
Assert.IsNull(selectQuery.Take);
2761+
2762+
Assert.AreEqual("{ \"dbref\" : { \"$ref\" : \"c\", \"$id\" : 1, \"$db\" : \"db\" } }", selectQuery.BuildQuery().ToJson());
2763+
Assert.AreEqual(1, Consume(query));
2764+
}
2765+
27432766
[Test]
27442767
public void TestWhereDBRefIdEquals1()
27452768
{

0 commit comments

Comments
 (0)