Skip to content

Commit 1b12023

Browse files
committed
adding tests for inheritance
1 parent 8f7cde7 commit 1b12023

File tree

5 files changed

+456
-11
lines changed

5 files changed

+456
-11
lines changed
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using NUnit.Framework;
6+
7+
namespace MongoDB.IntegrationTests.Inheritance
8+
{
9+
[TestFixture]
10+
public class TestInheritanceWithAbstractBaseClass : MongoTestBase
11+
{
12+
abstract class Animal
13+
{
14+
public Oid Id { get; set; }
15+
16+
public int Age { get; set; }
17+
18+
public string Name { get; set; }
19+
}
20+
21+
class Bear : Animal
22+
{ }
23+
24+
abstract class Cat : Animal
25+
{ }
26+
27+
class Tiger : Cat
28+
{ }
29+
30+
class Lion : Cat
31+
{ }
32+
33+
public override string TestCollections
34+
{
35+
get { return "Animal"; }
36+
}
37+
38+
[SetUp]
39+
public void TestSetup()
40+
{
41+
CleanDB();
42+
}
43+
44+
protected override Configuration.MongoConfigurationBuilder GetConfiguration()
45+
{
46+
var builder = base.GetConfiguration();
47+
builder.Mapping(mapping =>
48+
{
49+
mapping.DefaultProfile(profile =>
50+
{
51+
profile.SubClassesAre(x => x.IsSubclassOf(typeof(Animal)));
52+
});
53+
54+
mapping.Map<Bear>();
55+
mapping.Map<Cat>();
56+
mapping.Map<Tiger>();
57+
mapping.Map<Lion>();
58+
});
59+
60+
return builder;
61+
}
62+
63+
[Test]
64+
public void Should_persist_discriminator_using_base_class_collection()
65+
{
66+
var animalCollection = DB.GetCollection<Animal>();
67+
animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
68+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
69+
70+
var docCollection = DB.GetCollection<Document>("Animal");
71+
72+
var docs = docCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
73+
74+
Assert.AreEqual(new[] { "Cat", "Tiger" }, (List<string>)docs[0]["_t"]);
75+
Assert.AreEqual("Bear", (string)docs[1]["_t"]);
76+
}
77+
78+
[Test]
79+
public void Should_persist_discriminator_using_concrete_class_collection()
80+
{
81+
var animalCollection = DB.GetCollection<Cat>();
82+
animalCollection.Save(new Lion() { Age = 20, Name = "Jim" });
83+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
84+
85+
var docCollection = DB.GetCollection<Document>("Animal");
86+
87+
var docs = docCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
88+
89+
Assert.AreEqual(new[] { "Cat", "Tiger" }, (List<string>)docs[0]["_t"]);
90+
Assert.AreEqual(new[] { "Cat", "Lion" }, (List<string>)docs[1]["_t"]);
91+
}
92+
93+
[Test]
94+
public void Should_fetch_with_base_class_collection()
95+
{
96+
var animalCollection = DB.GetCollection<Animal>();
97+
animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
98+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
99+
100+
var animals = animalCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
101+
102+
Assert.AreEqual(2, animals.Count);
103+
Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
104+
Assert.AreEqual(19, animals[0].Age);
105+
Assert.AreEqual("Bob", animals[0].Name);
106+
Assert.IsInstanceOfType(typeof(Bear), animals[1]);
107+
Assert.AreEqual(20, animals[1].Age);
108+
Assert.AreEqual("Jim", animals[1].Name);
109+
}
110+
111+
[Test]
112+
public void Should_fetch_with_base_class_collection_through_linq()
113+
{
114+
var animalCollection = DB.GetCollection<Animal>();
115+
animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
116+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
117+
118+
var animals = (from a in animalCollection.Linq()
119+
orderby a.Age ascending
120+
select a).ToList();
121+
122+
Assert.AreEqual(2, animals.Count);
123+
Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
124+
Assert.AreEqual(19, animals[0].Age);
125+
Assert.AreEqual("Bob", animals[0].Name);
126+
Assert.IsInstanceOfType(typeof(Bear), animals[1]);
127+
Assert.AreEqual(20, animals[1].Age);
128+
Assert.AreEqual("Jim", animals[1].Name);
129+
}
130+
131+
[Test]
132+
public void Should_support_projections_with_base_class_collection()
133+
{
134+
var animalCollection = DB.GetCollection<Animal>();
135+
animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
136+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
137+
138+
var animals = animalCollection.FindAll().Fields(new { Age = true }).Sort("Age", IndexOrder.Ascending).Documents.ToList();
139+
140+
Assert.AreEqual(2, animals.Count);
141+
Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
142+
Assert.AreEqual(19, animals[0].Age);
143+
Assert.IsNull(animals[0].Name);
144+
Assert.IsInstanceOfType(typeof(Bear), animals[1]);
145+
Assert.AreEqual(20, animals[1].Age);
146+
Assert.IsNull(animals[1].Name);
147+
}
148+
149+
[Test]
150+
public void Should_support_projections_with_base_class_collections_with_linq()
151+
{
152+
var animalCollection = DB.GetCollection<Animal>();
153+
animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
154+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
155+
156+
var animals = (from a in animalCollection.Linq()
157+
orderby a.Age ascending
158+
select new { a.Name, a.Age }).ToList();
159+
160+
Assert.AreEqual(2, animals.Count);
161+
Assert.AreEqual(19, animals[0].Age);
162+
Assert.AreEqual("Bob", animals[0].Name);
163+
Assert.IsNull(animals[0].Name);
164+
Assert.AreEqual(20, animals[1].Age);
165+
Assert.AreEqual("Jim", animals[1].Name);
166+
}
167+
168+
[Test]
169+
public void Should_fetch_with_concrete_class_collection()
170+
{
171+
var animalCollection = DB.GetCollection<Animal>();
172+
animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
173+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
174+
175+
var catCollection = DB.GetCollection<Cat>();
176+
177+
var cats = catCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
178+
179+
Assert.AreEqual(1, cats.Count);
180+
Assert.IsInstanceOfType(typeof(Tiger), cats[0]);
181+
Assert.AreEqual(19, cats[0].Age);
182+
}
183+
184+
[Test]
185+
public void Should_fetch_with_concrete_class_collection_through_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 animals = (from a in catCollection.Linq()
194+
orderby a.Age ascending
195+
select a).ToList();
196+
197+
Assert.AreEqual(1, animals.Count);
198+
Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
199+
Assert.AreEqual(19, animals[0].Age);
200+
}
201+
202+
[Test]
203+
public void Should_get_correct_count_with_base_class_collection()
204+
{
205+
var animalCollection = DB.GetCollection<Animal>();
206+
animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
207+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
208+
209+
Assert.AreEqual(2, animalCollection.Count());
210+
}
211+
212+
[Test]
213+
public void Should_get_correct_count_with_base_class_collection_using_linq()
214+
{
215+
var animalCollection = DB.GetCollection<Animal>();
216+
animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
217+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
218+
219+
Assert.AreEqual(2, animalCollection.Linq().Count());
220+
}
221+
222+
[Test]
223+
public void Should_get_correct_count_with_concrete_class_collection()
224+
{
225+
var animalCollection = DB.GetCollection<Animal>();
226+
animalCollection.Save(new Bear() { Age = 20 });
227+
animalCollection.Save(new Tiger() { Age = 19 });
228+
229+
var catCollection = DB.GetCollection<Cat>();
230+
231+
Assert.AreEqual(1, catCollection.Count());
232+
}
233+
234+
[Test]
235+
public void Should_get_correct_count_with_concrete_class_collection_using_linq()
236+
{
237+
var animalCollection = DB.GetCollection<Animal>();
238+
animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
239+
animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
240+
241+
var catCollection = DB.GetCollection<Cat>();
242+
243+
Assert.AreEqual(1, catCollection.Linq().Count());
244+
}
245+
}
246+
}

0 commit comments

Comments
 (0)