/source/MongoDB.Tests/IntegrationTests/Inheritance/TestInheritanceWithConcreteBaseClass.cs
C# | 276 lines | 218 code | 58 blank | 0 comment | 0 complexity | cb801b3218211454065b6969fd05cedd MD5 | raw file
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using NUnit.Framework;
6
7namespace MongoDB.IntegrationTests.Inheritance
8{
9 [TestFixture]
10 public class TestInheritanceWithConcreteBaseClass : MongoTestBase
11 {
12 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 Animal() { Age = 20 });
68 animalCollection.Save(new Tiger() { Age = 19 });
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.IsNull(docs[1]["_t"]);
76 }
77
78 [Test]
79 public void Should_persist_discriminator_using_inherited_class_collection()
80 {
81 var animalCollection = DB.GetCollection<Cat>();
82 animalCollection.Save(new Lion() { Age = 20 });
83 animalCollection.Save(new Tiger() { Age = 19 });
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 Animal() { Age = 20 });
98 animalCollection.Save(new Tiger() { Age = 19 });
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.IsInstanceOfType(typeof(Animal), animals[1]);
106 Assert.AreEqual(20, animals[1].Age);
107 }
108
109 [Test]
110 public void Should_fetch_with_base_class_collection_through_linq()
111 {
112 var animalCollection = DB.GetCollection<Animal>();
113 animalCollection.Save(new Animal() { Age = 20 });
114 animalCollection.Save(new Tiger() { Age = 19 });
115
116 var animals = (from a in animalCollection.Linq()
117 orderby a.Age ascending
118 select a).ToList();
119
120 Assert.AreEqual(2, animals.Count);
121 Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
122 Assert.AreEqual(19, animals[0].Age);
123 Assert.IsInstanceOfType(typeof(Animal), animals[1]);
124 Assert.AreEqual(20, animals[1].Age);
125 }
126
127 [Test]
128 public void Should_fetch_with_inherited_class_collection()
129 {
130 var animalCollection = DB.GetCollection<Animal>();
131 animalCollection.Save(new Animal() { Age = 20 });
132 animalCollection.Save(new Tiger() { Age = 19 });
133
134 var catCollection = DB.GetCollection<Cat>();
135
136 var cats = catCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
137
138 Assert.AreEqual(1, cats.Count);
139 Assert.IsInstanceOfType(typeof(Tiger), cats[0]);
140 Assert.AreEqual(19, cats[0].Age);
141 }
142
143 [Test]
144 public void Should_fetch_with_inherited_class_collection_through_linq()
145 {
146 var animalCollection = DB.GetCollection<Animal>();
147 animalCollection.Save(new Animal() { Age = 20 });
148 animalCollection.Save(new Tiger() { Age = 19 });
149
150 var catCollection = DB.GetCollection<Cat>();
151
152 var animals = (from a in catCollection.Linq()
153 orderby a.Age ascending
154 select a).ToList();
155
156 Assert.AreEqual(1, animals.Count);
157 Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
158 Assert.AreEqual(19, animals[0].Age);
159 }
160
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
232 [Test]
233 public void Should_get_correct_count_with_base_class_collection()
234 {
235 var animalCollection = DB.GetCollection<Animal>();
236 animalCollection.Save(new Animal() { Age = 20 });
237 animalCollection.Save(new Tiger() { Age = 19 });
238
239 Assert.AreEqual(2, animalCollection.Count());
240 }
241
242 [Test]
243 public void Should_get_correct_count_with_base_class_collection_using_linq()
244 {
245 var animalCollection = DB.GetCollection<Animal>();
246 animalCollection.Save(new Animal() { Age = 20 });
247 animalCollection.Save(new Tiger() { Age = 19 });
248
249 Assert.AreEqual(2, animalCollection.Linq().Count());
250 }
251
252 [Test]
253 public void Should_get_correct_count_with_inherited_class_collection()
254 {
255 var animalCollection = DB.GetCollection<Animal>();
256 animalCollection.Save(new Animal() { Age = 20 });
257 animalCollection.Save(new Tiger() { Age = 19 });
258
259 var catCollection = DB.GetCollection<Cat>();
260
261 Assert.AreEqual(1, catCollection.Count());
262 }
263
264 [Test]
265 public void Should_get_correct_count_with_inherited_class_collection_using_linq()
266 {
267 var animalCollection = DB.GetCollection<Animal>();
268 animalCollection.Save(new Animal() { Age = 20 });
269 animalCollection.Save(new Tiger() { Age = 19 });
270
271 var catCollection = DB.GetCollection<Cat>();
272
273 Assert.AreEqual(1, catCollection.Linq().Count());
274 }
275 }
276}