Skip to content
Browse files

adding tests for inheritance

  • Loading branch information...
1 parent 8f7cde7 commit 1b120235389d3dd92edd37a39a1fc6a4cfa65d85 @craiggwilson craiggwilson committed
View
246 source/MongoDB.Tests/IntegrationTests/Inheritance/TestInheritanceWithAbstractBaseClass.cs
@@ -0,0 +1,246 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+
+namespace MongoDB.IntegrationTests.Inheritance
+{
+ [TestFixture]
+ public class TestInheritanceWithAbstractBaseClass : MongoTestBase
+ {
+ abstract class Animal
+ {
+ public Oid Id { get; set; }
+
+ public int Age { get; set; }
+
+ public string Name { get; set; }
+ }
+
+ class Bear : Animal
+ { }
+
+ abstract class Cat : Animal
+ { }
+
+ class Tiger : Cat
+ { }
+
+ class Lion : Cat
+ { }
+
+ public override string TestCollections
+ {
+ get { return "Animal"; }
+ }
+
+ [SetUp]
+ public void TestSetup()
+ {
+ CleanDB();
+ }
+
+ protected override Configuration.MongoConfigurationBuilder GetConfiguration()
+ {
+ var builder = base.GetConfiguration();
+ builder.Mapping(mapping =>
+ {
+ mapping.DefaultProfile(profile =>
+ {
+ profile.SubClassesAre(x => x.IsSubclassOf(typeof(Animal)));
+ });
+
+ mapping.Map<Bear>();
+ mapping.Map<Cat>();
+ mapping.Map<Tiger>();
+ mapping.Map<Lion>();
+ });
+
+ return builder;
+ }
+
+ [Test]
+ public void Should_persist_discriminator_using_base_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
+ animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
+
+ var docCollection = DB.GetCollection<Document>("Animal");
+
+ var docs = docCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
+
+ Assert.AreEqual(new[] { "Cat", "Tiger" }, (List<string>)docs[0]["_t"]);
+ Assert.AreEqual("Bear", (string)docs[1]["_t"]);
+ }
+
+ [Test]
+ public void Should_persist_discriminator_using_concrete_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Cat>();
+ animalCollection.Save(new Lion() { Age = 20, Name = "Jim" });
+ animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
+
+ var docCollection = DB.GetCollection<Document>("Animal");
+
+ var docs = docCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
+
+ Assert.AreEqual(new[] { "Cat", "Tiger" }, (List<string>)docs[0]["_t"]);
+ Assert.AreEqual(new[] { "Cat", "Lion" }, (List<string>)docs[1]["_t"]);
+ }
+
+ [Test]
+ public void Should_fetch_with_base_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
+ animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
+
+ var animals = animalCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
+
+ Assert.AreEqual(2, animals.Count);
+ Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
+ Assert.AreEqual(19, animals[0].Age);
+ Assert.AreEqual("Bob", animals[0].Name);
+ Assert.IsInstanceOfType(typeof(Bear), animals[1]);
+ Assert.AreEqual(20, animals[1].Age);
+ Assert.AreEqual("Jim", animals[1].Name);
+ }
+
+ [Test]
+ public void Should_fetch_with_base_class_collection_through_linq()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
+ animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
+
+ var animals = (from a in animalCollection.Linq()
+ orderby a.Age ascending
+ select a).ToList();
+
+ Assert.AreEqual(2, animals.Count);
+ Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
+ Assert.AreEqual(19, animals[0].Age);
+ Assert.AreEqual("Bob", animals[0].Name);
+ Assert.IsInstanceOfType(typeof(Bear), animals[1]);
+ Assert.AreEqual(20, animals[1].Age);
+ Assert.AreEqual("Jim", animals[1].Name);
+ }
+
+ [Test]
+ public void Should_support_projections_with_base_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
+ animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
+
+ var animals = animalCollection.FindAll().Fields(new { Age = true }).Sort("Age", IndexOrder.Ascending).Documents.ToList();
+
+ Assert.AreEqual(2, animals.Count);
+ Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
+ Assert.AreEqual(19, animals[0].Age);
+ Assert.IsNull(animals[0].Name);
+ Assert.IsInstanceOfType(typeof(Bear), animals[1]);
+ Assert.AreEqual(20, animals[1].Age);
+ Assert.IsNull(animals[1].Name);
+ }
+
+ [Test]
+ public void Should_support_projections_with_base_class_collections_with_linq()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
+ animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
+
+ var animals = (from a in animalCollection.Linq()
+ orderby a.Age ascending
+ select new { a.Name, a.Age }).ToList();
+
+ Assert.AreEqual(2, animals.Count);
+ Assert.AreEqual(19, animals[0].Age);
+ Assert.AreEqual("Bob", animals[0].Name);
+ Assert.IsNull(animals[0].Name);
+ Assert.AreEqual(20, animals[1].Age);
+ Assert.AreEqual("Jim", animals[1].Name);
+ }
+
+ [Test]
+ public void Should_fetch_with_concrete_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
+ animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
+
+ var catCollection = DB.GetCollection<Cat>();
+
+ var cats = catCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
+
+ Assert.AreEqual(1, cats.Count);
+ Assert.IsInstanceOfType(typeof(Tiger), cats[0]);
+ Assert.AreEqual(19, cats[0].Age);
+ }
+
+ [Test]
+ public void Should_fetch_with_concrete_class_collection_through_linq()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
+ animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
+
+ var catCollection = DB.GetCollection<Cat>();
+
+ var animals = (from a in catCollection.Linq()
+ orderby a.Age ascending
+ select a).ToList();
+
+ Assert.AreEqual(1, animals.Count);
+ Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
+ Assert.AreEqual(19, animals[0].Age);
+ }
+
+ [Test]
+ public void Should_get_correct_count_with_base_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
+ animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
+
+ Assert.AreEqual(2, animalCollection.Count());
+ }
+
+ [Test]
+ public void Should_get_correct_count_with_base_class_collection_using_linq()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
+ animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
+
+ Assert.AreEqual(2, animalCollection.Linq().Count());
+ }
+
+ [Test]
+ public void Should_get_correct_count_with_concrete_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20 });
+ animalCollection.Save(new Tiger() { Age = 19 });
+
+ var catCollection = DB.GetCollection<Cat>();
+
+ Assert.AreEqual(1, catCollection.Count());
+ }
+
+ [Test]
+ public void Should_get_correct_count_with_concrete_class_collection_using_linq()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20, Name = "Jim" });
+ animalCollection.Save(new Tiger() { Age = 19, Name = "Bob" });
+
+ var catCollection = DB.GetCollection<Cat>();
+
+ Assert.AreEqual(1, catCollection.Linq().Count());
+ }
+ }
+}
View
197 source/MongoDB.Tests/IntegrationTests/Inheritance/TestInheritanceWithConcreteBaseClass.cs
@@ -0,0 +1,197 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+
+namespace MongoDB.IntegrationTests.Inheritance
+{
+ [TestFixture]
+ public class TestInheritanceWithConcreteBaseClass : MongoTestBase
+ {
+ abstract class Animal
+ {
+ public Oid Id { get; set; }
+
+ public int Age { get; set; }
+ }
+
+ class Bear : Animal
+ { }
+
+ abstract class Cat : Animal
+ { }
+
+ class Tiger : Cat
+ { }
+
+ class Lion : Cat
+ { }
+
+ public override string TestCollections
+ {
+ get { return "Animal"; }
+ }
+
+ protected override Configuration.MongoConfigurationBuilder GetConfiguration()
+ {
+ var builder = base.GetConfiguration();
+ builder.Mapping(mapping =>
+ {
+ mapping.DefaultProfile(profile =>
+ {
+ profile.SubClassesAre(x => x.IsSubclassOf(typeof(Animal)));
+ });
+
+ mapping.Map<Bear>();
+ mapping.Map<Cat>();
+ mapping.Map<Tiger>();
+ mapping.Map<Lion>();
+ });
+
+ return builder;
+ }
+
+ [Test]
+ public void Should_persist_discriminator_using_base_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20 });
+ animalCollection.Save(new Tiger() { Age = 19 });
+
+ var docCollection = DB.GetCollection<Document>("Animal");
+
+ var docs = docCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
+
+ Assert.AreEqual(new[] { "Cat", "Tiger" }, (List<string>)docs[0]["_t"]);
+ Assert.AreEqual("Bear", (string)docs[1]["_t"]);
+ }
+
+ [Test]
+ public void Should_persist_discriminator_using_concrete_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Cat>();
+ animalCollection.Save(new Lion() { Age = 20 });
+ animalCollection.Save(new Tiger() { Age = 19 });
+
+ var docCollection = DB.GetCollection<Document>("Animal");
+
+ var docs = docCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
+
+ Assert.AreEqual(new[] { "Cat", "Tiger" }, (List<string>)docs[0]["_t"]);
+ Assert.AreEqual(new[] { "Cat", "Lion" }, (List<string>)docs[1]["_t"]);
+ }
+
+ [Test]
+ public void Should_fetch_with_base_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20 });
+ animalCollection.Save(new Tiger() { Age = 19 });
+
+ var animals = animalCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
+
+ Assert.AreEqual(2, animals.Count);
+ Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
+ Assert.AreEqual(19, animals[0].Age);
+ Assert.IsInstanceOfType(typeof(Bear), animals[1]);
+ Assert.AreEqual(20, animals[1].Age);
+ }
+
+ [Test]
+ public void Should_fetch_with_base_class_collection_through_linq()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20 });
+ animalCollection.Save(new Tiger() { Age = 19 });
+
+ var animals = (from a in animalCollection.Linq()
+ orderby a.Age ascending
+ select a).ToList();
+
+ Assert.AreEqual(2, animals.Count);
+ Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
+ Assert.AreEqual(19, animals[0].Age);
+ Assert.IsInstanceOfType(typeof(Bear), animals[1]);
+ Assert.AreEqual(20, animals[1].Age);
+ }
+
+ [Test]
+ public void Should_fetch_with_concrete_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20 });
+ animalCollection.Save(new Tiger() { Age = 19 });
+
+ var catCollection = DB.GetCollection<Cat>();
+
+ var cats = catCollection.FindAll().Sort("Age", IndexOrder.Ascending).Documents.ToList();
+
+ Assert.AreEqual(1, cats.Count);
+ Assert.IsInstanceOfType(typeof(Tiger), cats[0]);
+ Assert.AreEqual(19, cats[0].Age);
+ }
+
+ [Test]
+ public void Should_fetch_with_concrete_class_collection_through_linq()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20 });
+ animalCollection.Save(new Tiger() { Age = 19 });
+
+ var catCollection = DB.GetCollection<Cat>();
+
+ var animals = (from a in catCollection.Linq()
+ orderby a.Age ascending
+ select a).ToList();
+
+ Assert.AreEqual(1, animals.Count);
+ Assert.IsInstanceOfType(typeof(Tiger), animals[0]);
+ Assert.AreEqual(19, animals[0].Age);
+ }
+
+ [Test]
+ public void Should_get_correct_count_with_base_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20 });
+ animalCollection.Save(new Tiger() { Age = 19 });
+
+ Assert.AreEqual(2, animalCollection.Count());
+ }
+
+ [Test]
+ public void Should_get_correct_count_with_base_class_collection_using_linq()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20 });
+ animalCollection.Save(new Tiger() { Age = 19 });
+
+ Assert.AreEqual(2, animalCollection.Linq().Count());
+ }
+
+ [Test]
+ public void Should_get_correct_count_with_concrete_class_collection()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20 });
+ animalCollection.Save(new Tiger() { Age = 19 });
+
+ var catCollection = DB.GetCollection<Cat>();
+
+ Assert.AreEqual(1, catCollection.Count());
+ }
+
+ [Test]
+ public void Should_get_correct_count_with_concrete_class_collection_using_linq()
+ {
+ var animalCollection = DB.GetCollection<Animal>();
+ animalCollection.Save(new Bear() { Age = 20 });
+ animalCollection.Save(new Tiger() { Age = 19 });
+
+ var catCollection = DB.GetCollection<Cat>();
+
+ Assert.AreEqual(1, catCollection.Linq().Count());
+ }
+ }
+}
View
3 source/MongoDB.Tests/IntegrationTests/TestDatabase.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
+using System.Configuration;
namespace MongoDB.IntegrationTests
{
@@ -126,7 +127,7 @@ public class TestDatabase : MongoTestBase
[Test]
public void TestCanCreateFromConnectionString(){
- var builder = new MongoConnectionStringBuilder(ConnectionString) {Database = "tests"};
+ var builder = new MongoConnectionStringBuilder(ConfigurationManager.AppSettings["tests"]) {Database = "tests"};
/*
using(var database = new MongoDatabase(builder.ToString()))
{
View
2 source/MongoDB.Tests/MongoDB.Tests.csproj
@@ -89,6 +89,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="IntegrationTests\Inheritance\TestInheritanceWithConcreteBaseClass.cs" />
+ <Compile Include="IntegrationTests\Inheritance\TestInheritanceWithAbstractBaseClass.cs" />
<Compile Include="IntegrationTests\SecondServer\TestPooledConnectionFactory.cs" />
<Compile Include="IntegrationTests\Linq\LinqDomain.cs" />
<Compile Include="IntegrationTests\Linq\MapReduceTests.cs" />
View
19 source/MongoDB.Tests/MongoTestBase.cs
@@ -1,6 +1,7 @@
using System;
using System.Configuration;
using NUnit.Framework;
+using MongoDB.Configuration;
namespace MongoDB
{
@@ -15,12 +16,6 @@ public abstract class MongoTestBase
}
/// <summary>
- /// Gets or sets the connection string.
- /// </summary>
- /// <value>The connection string.</value>
- public string ConnectionString { get; private set; }
-
- /// <summary>
/// Comma separated list of collections to clean at startup.
/// </summary>
public abstract string TestCollections{get;}
@@ -40,10 +35,7 @@ public abstract class MongoTestBase
/// </summary>
[TestFixtureSetUp]
public virtual void Init(){
- ConnectionString = ConfigurationManager.AppSettings["tests"];
- if(String.IsNullOrEmpty(ConnectionString))
- throw new ArgumentNullException("Connection string not found.");
- this.Mongo = new Mongo(ConnectionString);
+ this.Mongo = new Mongo(GetConfiguration().BuildConfiguration());
this.Mongo.Connect();
CleanDB();
OnInit();
@@ -62,5 +54,12 @@ public abstract class MongoTestBase
//Console.WriteLine("Dropping " + col);
}
}
+
+ protected virtual MongoConfigurationBuilder GetConfiguration()
+ {
+ var builder = new MongoConfigurationBuilder();
+ builder.ReadConnectionStringFromAppSettings("tests");
+ return builder;
+ }
}
}

0 comments on commit 1b12023

Please sign in to comment.
Something went wrong with that request. Please try again.