PageRenderTime 63ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/nikunjdhawan/mongodb-csharp
C# | 276 lines | 218 code | 58 blank | 0 comment | 0 complexity | cb801b3218211454065b6969fd05cedd MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NUnit.Framework;
  6. namespace MongoDB.IntegrationTests.Inheritance
  7. {
  8. [TestFixture]
  9. public class TestInheritanceWithConcreteBaseClass : MongoTestBase
  10. {
  11. class Animal
  12. {
  13. public Oid Id { get; set; }
  14. public int Age { get; set; }
  15. public string Name { get; set; }
  16. }
  17. class Bear : Animal
  18. { }
  19. abstract class Cat : Animal
  20. { }
  21. class Tiger : Cat
  22. { }
  23. class Lion : Cat
  24. { }
  25. public override string TestCollections
  26. {
  27. get { return "Animal"; }
  28. }
  29. [SetUp]
  30. public void TestSetup()
  31. {
  32. CleanDB();
  33. }
  34. protected override Configuration.MongoConfigurationBuilder GetConfiguration()
  35. {
  36. var builder = base.GetConfiguration();
  37. builder.Mapping(mapping =>
  38. {
  39. mapping.DefaultProfile(profile =>
  40. {
  41. profile.SubClassesAre(x => x.IsSubclassOf(typeof(Animal)));
  42. });
  43. mapping.Map<Bear>();
  44. mapping.Map<Cat>();
  45. mapping.Map<Tiger>();
  46. mapping.Map<Lion>();
  47. });
  48. return builder;
  49. }
  50. [Test]
  51. public void Should_persist_discriminator_using_base_class_collection()
  52. {
  53. var animalCollection = DB.GetCollection<Animal>();
  54. animalCollection.Save(new Animal() { Age = 20 });
  55. animalCollection.Save(new Tiger() { Age = 19 });
  56. var docCollection = DB.GetCollection<Document>("Animal");
  57. var docs = docCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
  58. Assert.AreEqual(new[] { "Cat", "Tiger" }, (List<string>)docs[0]["_t"]);
  59. Assert.IsNull(docs[1]["_t"]);
  60. }
  61. [Test]
  62. public void Should_persist_discriminator_using_inherited_class_collection()
  63. {
  64. var animalCollection = DB.GetCollection<Cat>();
  65. animalCollection.Save(new Lion() { Age = 20 });
  66. animalCollection.Save(new Tiger() { Age = 19 });
  67. var docCollection = DB.GetCollection<Document>("Animal");
  68. var docs = docCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
  69. Assert.AreEqual(new[] { "Cat", "Tiger" }, (List<string>)docs[0]["_t"]);
  70. Assert.AreEqual(new[] { "Cat", "Lion" }, (List<string>)docs[1]["_t"]);
  71. }
  72. [Test]
  73. public void Should_fetch_with_base_class_collection()
  74. {
  75. var animalCollection = DB.GetCollection<Animal>();
  76. animalCollection.Save(new Animal() { Age = 20 });
  77. animalCollection.Save(new Tiger() { Age = 19 });
  78. var animals = animalCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
  79. Assert.AreEqual(2, animals.Count);
  80. Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
  81. Assert.AreEqual(19, animals[0].Age);
  82. Assert.IsInstanceOfType(typeof(Animal), animals[1]);
  83. Assert.AreEqual(20, animals[1].Age);
  84. }
  85. [Test]
  86. public void Should_fetch_with_base_class_collection_through_linq()
  87. {
  88. var animalCollection = DB.GetCollection<Animal>();
  89. animalCollection.Save(new Animal() { Age = 20 });
  90. animalCollection.Save(new Tiger() { Age = 19 });
  91. var animals = (from a in animalCollection.Linq()
  92. orderby a.Age ascending
  93. select a).ToList();
  94. Assert.AreEqual(2, animals.Count);
  95. Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
  96. Assert.AreEqual(19, animals[0].Age);
  97. Assert.IsInstanceOfType(typeof(Animal), animals[1]);
  98. Assert.AreEqual(20, animals[1].Age);
  99. }
  100. [Test]
  101. public void Should_fetch_with_inherited_class_collection()
  102. {
  103. var animalCollection = DB.GetCollection<Animal>();
  104. animalCollection.Save(new Animal() { Age = 20 });
  105. animalCollection.Save(new Tiger() { Age = 19 });
  106. var catCollection = DB.GetCollection<Cat>();
  107. var cats = catCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
  108. Assert.AreEqual(1, cats.Count);
  109. Assert.IsInstanceOfType(typeof(Tiger), cats[0]);
  110. Assert.AreEqual(19, cats[0].Age);
  111. }
  112. [Test]
  113. public void Should_fetch_with_inherited_class_collection_through_linq()
  114. {
  115. var animalCollection = DB.GetCollection<Animal>();
  116. animalCollection.Save(new Animal() { Age = 20 });
  117. animalCollection.Save(new Tiger() { Age = 19 });
  118. var catCollection = DB.GetCollection<Cat>();
  119. var animals = (from a in catCollection.Linq()
  120. orderby a.Age ascending
  121. select a).ToList();
  122. Assert.AreEqual(1, animals.Count);
  123. Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
  124. Assert.AreEqual(19, animals[0].Age);
  125. }
  126. [Test]
  127. public void Should_support_projections_with_base_class_collection()
  128. {
  129. var animalCollection = DB.GetCollection<Animal>();
  130. animalCollection.Save(new Animal() { Age = 20, Name = "Jim" });
  131. animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
  132. var animals = animalCollection.FindAll().Fields(new { Age = true }).Sort("Age", IndexOrder.Ascending).Documents.ToList();
  133. Assert.AreEqual(2, animals.Count);
  134. Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
  135. Assert.AreEqual(19, animals[0].Age);
  136. Assert.IsNull(animals[0].Name);
  137. Assert.IsInstanceOfType(typeof(Animal), animals[1]);
  138. Assert.AreEqual(20, animals[1].Age);
  139. Assert.IsNull(animals[1].Name);
  140. }
  141. [Test]
  142. public void Should_support_projections_with_base_class_collections_with_linq()
  143. {
  144. var animalCollection = DB.GetCollection<Animal>();
  145. animalCollection.Save(new Animal() { Age = 20, Name = "Jim" });
  146. animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
  147. var animals = (from a in animalCollection.Linq()
  148. orderby a.Age ascending
  149. select new { a.Name, a.Age }).ToList();
  150. Assert.AreEqual(2, animals.Count);
  151. Assert.AreEqual(19, animals[0].Age);
  152. Assert.AreEqual("Bob", animals[0].Name);
  153. Assert.AreEqual(20, animals[1].Age);
  154. Assert.AreEqual("Jim", animals[1].Name);
  155. }
  156. [Test]
  157. public void Should_support_projections_with_inherited_class_collection()
  158. {
  159. var animalCollection = DB.GetCollection<Animal>();
  160. animalCollection.Save(new Animal() { Age = 20, Name = "Jim" });
  161. animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
  162. var catCollection = DB.GetCollection<Cat>();
  163. var cats = catCollection.FindAll().Fields(new { Age = true }).Sort("Age", IndexOrder.Ascending).Documents.ToList();
  164. Assert.AreEqual(1, cats.Count);
  165. Assert.IsInstanceOfType(typeof(Tiger), cats[0]);
  166. Assert.AreEqual(19, cats[0].Age);
  167. Assert.IsNull(cats[0].Name);
  168. }
  169. [Test]
  170. public void Should_support_projections_with_inherited_class_collections_with_linq()
  171. {
  172. var animalCollection = DB.GetCollection<Animal>();
  173. animalCollection.Save(new Animal() { Age = 20, Name = "Jim" });
  174. animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
  175. var catCollection = DB.GetCollection<Cat>();
  176. var cats = (from a in catCollection.Linq()
  177. orderby a.Age ascending
  178. select new { a.Name, a.Age }).ToList();
  179. Assert.AreEqual(1, cats.Count);
  180. Assert.AreEqual(19, cats[0].Age);
  181. Assert.AreEqual("Bob", cats[0].Name);
  182. }
  183. [Test]
  184. public void Should_get_correct_count_with_base_class_collection()
  185. {
  186. var animalCollection = DB.GetCollection<Animal>();
  187. animalCollection.Save(new Animal() { Age = 20 });
  188. animalCollection.Save(new Tiger() { Age = 19 });
  189. Assert.AreEqual(2, animalCollection.Count());
  190. }
  191. [Test]
  192. public void Should_get_correct_count_with_base_class_collection_using_linq()
  193. {
  194. var animalCollection = DB.GetCollection<Animal>();
  195. animalCollection.Save(new Animal() { Age = 20 });
  196. animalCollection.Save(new Tiger() { Age = 19 });
  197. Assert.AreEqual(2, animalCollection.Linq().Count());
  198. }
  199. [Test]
  200. public void Should_get_correct_count_with_inherited_class_collection()
  201. {
  202. var animalCollection = DB.GetCollection<Animal>();
  203. animalCollection.Save(new Animal() { Age = 20 });
  204. animalCollection.Save(new Tiger() { Age = 19 });
  205. var catCollection = DB.GetCollection<Cat>();
  206. Assert.AreEqual(1, catCollection.Count());
  207. }
  208. [Test]
  209. public void Should_get_correct_count_with_inherited_class_collection_using_linq()
  210. {
  211. var animalCollection = DB.GetCollection<Animal>();
  212. animalCollection.Save(new Animal() { Age = 20 });
  213. animalCollection.Save(new Tiger() { Age = 19 });
  214. var catCollection = DB.GetCollection<Cat>();
  215. Assert.AreEqual(1, catCollection.Linq().Count());
  216. }
  217. }
  218. }