Skip to content

Commit e543727

Browse files
committed
cleaning up a little
1 parent 1ff7ff6 commit e543727

File tree

9 files changed

+315
-203
lines changed

9 files changed

+315
-203
lines changed

source/MongoDB.Tests/IntegrationTests/Inheritance/TestInheritanceWithAbstractBaseClass.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,45 @@ orderby a.Age ascending
160160
Assert.AreEqual(2, animals.Count);
161161
Assert.AreEqual(19, animals[0].Age);
162162
Assert.AreEqual("Bob", animals[0].Name);
163-
Assert.IsNull(animals[0].Name);
164163
Assert.AreEqual(20, animals[1].Age);
165164
Assert.AreEqual("Jim", animals[1].Name);
166165
}
167166

167+
[Test]
168+
public void Should_support_projections_with_concrete_class_collection()
169+
{
170+
var animalCollection = DB.GetCollection<Animal>();
171+
animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
172+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
173+
174+
var catCollection = DB.GetCollection<Cat>();
175+
176+
var cats = catCollection.FindAll().Fields(new { Age = true }).Sort("Age", IndexOrder.Ascending).Documents.ToList();
177+
178+
Assert.AreEqual(1, cats.Count);
179+
Assert.IsInstanceOfType(typeof(Tiger), cats[0]);
180+
Assert.AreEqual(19, cats[0].Age);
181+
Assert.IsNull(cats[0].Name);
182+
}
183+
184+
[Test]
185+
public void Should_support_projections_with_concrete_class_collections_with_linq()
186+
{
187+
var animalCollection = DB.GetCollection<Animal>();
188+
animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
189+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
190+
191+
var catCollection = DB.GetCollection<Cat>();
192+
193+
var cats = (from a in catCollection.Linq()
194+
orderby a.Age ascending
195+
select new { a.Name, a.Age }).ToList();
196+
197+
Assert.AreEqual(1, cats.Count);
198+
Assert.AreEqual(19, cats[0].Age);
199+
Assert.AreEqual("Bob", cats[0].Name);
200+
}
201+
168202
[Test]
169203
public void Should_fetch_with_concrete_class_collection()
170204
{

source/MongoDB.Tests/IntegrationTests/Inheritance/TestInheritanceWithConcreteBaseClass.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Animal
1414
public Oid Id { get; set; }
1515

1616
public int Age { get; set; }
17+
18+
public string Name { get; set; }
1719
}
1820

1921
class Bear : Animal
@@ -156,6 +158,77 @@ orderby a.Age ascending
156158
Assert.AreEqual(19, animals[0].Age);
157159
}
158160

161+
[Test]
162+
public void Should_support_projections_with_base_class_collection()
163+
{
164+
var animalCollection = DB.GetCollection<Animal>();
165+
animalCollection.Save(new Animal() { Age = 20, Name = "Jim" });
166+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
167+
168+
var animals = animalCollection.FindAll().Fields(new { Age = true }).Sort("Age", IndexOrder.Ascending).Documents.ToList();
169+
170+
Assert.AreEqual(2, animals.Count);
171+
Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
172+
Assert.AreEqual(19, animals[0].Age);
173+
Assert.IsNull(animals[0].Name);
174+
Assert.IsInstanceOfType(typeof(Animal), animals[1]);
175+
Assert.AreEqual(20, animals[1].Age);
176+
Assert.IsNull(animals[1].Name);
177+
}
178+
179+
[Test]
180+
public void Should_support_projections_with_base_class_collections_with_linq()
181+
{
182+
var animalCollection = DB.GetCollection<Animal>();
183+
animalCollection.Save(new Animal() { Age = 20, Name = "Jim" });
184+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
185+
186+
var animals = (from a in animalCollection.Linq()
187+
orderby a.Age ascending
188+
select new { a.Name, a.Age }).ToList();
189+
190+
Assert.AreEqual(2, animals.Count);
191+
Assert.AreEqual(19, animals[0].Age);
192+
Assert.AreEqual("Bob", animals[0].Name);
193+
Assert.AreEqual(20, animals[1].Age);
194+
Assert.AreEqual("Jim", animals[1].Name);
195+
}
196+
197+
[Test]
198+
public void Should_support_projections_with_inherited_class_collection()
199+
{
200+
var animalCollection = DB.GetCollection<Animal>();
201+
animalCollection.Save(new Animal() { Age = 20, Name = "Jim" });
202+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
203+
204+
var catCollection = DB.GetCollection<Cat>();
205+
206+
var cats = catCollection.FindAll().Fields(new { Age = true }).Sort("Age", IndexOrder.Ascending).Documents.ToList();
207+
208+
Assert.AreEqual(1, cats.Count);
209+
Assert.IsInstanceOfType(typeof(Tiger), cats[0]);
210+
Assert.AreEqual(19, cats[0].Age);
211+
Assert.IsNull(cats[0].Name);
212+
}
213+
214+
[Test]
215+
public void Should_support_projections_with_inherited_class_collections_with_linq()
216+
{
217+
var animalCollection = DB.GetCollection<Animal>();
218+
animalCollection.Save(new Animal() { Age = 20, Name = "Jim" });
219+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
220+
221+
var catCollection = DB.GetCollection<Cat>();
222+
223+
var cats = (from a in catCollection.Linq()
224+
orderby a.Age ascending
225+
select new { a.Name, a.Age }).ToList();
226+
227+
Assert.AreEqual(1, cats.Count);
228+
Assert.AreEqual(19, cats[0].Age);
229+
Assert.AreEqual("Bob", cats[0].Name);
230+
}
231+
159232
[Test]
160233
public void Should_get_correct_count_with_base_class_collection()
161234
{

source/MongoDB/Connections/Connection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using MongoDB.Serialization;
99
using MongoDB.Util;
1010

11-
namespace MongoDB.Connections
11+
namespace MongoDB.Connections
1212
{
1313
/// <summary>
1414
/// Connection is a managment unit which uses a RawConnection from connection pool
@@ -18,7 +18,7 @@ namespace MongoDB.Connections
1818
/// by a new fresh connection.
1919
/// </remarks>
2020
/// </summary>
21-
internal class Connection : IDisposable
21+
internal class Connection : IDisposable
2222
{
2323
private readonly IConnectionFactory _factory;
2424
private RawConnection _connection;

0 commit comments

Comments
 (0)