Permalink
Browse files

Merge branch 'master' of git://github.com/Amorano/NoRM

  • Loading branch information...
schotime committed Apr 5, 2010
2 parents 01d6569 + ef444e6 commit 4d3a274751565187ffa2e79ac717eb50f812c1bd
View
@@ -0,0 +1,26 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NoRM", "NoRM\NoRM.csproj", "{297A83DB-DACE-4264-B28E-769CF165D4E4}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NoRM.Tests", "NoRM.Tests\NoRM.Tests.csproj", "{505E617D-1F40-40AF-BDF0-730EFBC5BA95}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {297A83DB-DACE-4264-B28E-769CF165D4E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {297A83DB-DACE-4264-B28E-769CF165D4E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {297A83DB-DACE-4264-B28E-769CF165D4E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {297A83DB-DACE-4264-B28E-769CF165D4E4}.Release|Any CPU.Build.0 = Release|Any CPU
+ {505E617D-1F40-40AF-BDF0-730EFBC5BA95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {505E617D-1F40-40AF-BDF0-730EFBC5BA95}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {505E617D-1F40-40AF-BDF0-730EFBC5BA95}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {505E617D-1F40-40AF-BDF0-730EFBC5BA95}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
@@ -9,7 +9,7 @@
namespace Norm.Tests
{
public class BSONSerializerTest
- {
+ {
[Fact]
public void DoesntSerializeIgnoredProperties()
{
@@ -200,11 +200,39 @@ public void SerializationOfGuidIsNotLossy()
[Fact]
public void SerializationOfInheritenceIsNotLossy()
{
- var obj1 = new ChildGeneralDTO {Pi = 3.14, IsOver9000 = true};
- var hydratedObj1 = BsonDeserializer.Deserialize<ChildGeneralDTO>(BsonSerializer.Serialize(obj1));
- Assert.Equal(obj1.Pi, hydratedObj1.Pi);
- Assert.Equal(obj1.IsOver9000, hydratedObj1.IsOver9000);
+ var obj1 = new SubClassedObject {Title = "Subclassed", ABool = true};
+ var hydratedObj1 = BsonDeserializer.Deserialize<SubClassedObject>(BsonSerializer.Serialize(obj1));
+ Assert.Equal(obj1.Title, hydratedObj1.Title);
+ Assert.Equal(obj1.ABool, hydratedObj1.ABool);
}
+
+ [Fact]
+ public void SerializationOfInheritenceIsNotLossy_EvenWhenWeAskForTheBaseType()
+ {
+ var obj1 = new SubClassedObject { Title = "The Title", ABool = true };
+ var hydratedObj1 = (SubClassedObject)BsonDeserializer.Deserialize<SuperClassObject>(BsonSerializer.Serialize(obj1));
+ Assert.Equal(obj1.Title, hydratedObj1.Title);
+ Assert.Equal(obj1.ABool, hydratedObj1.ABool);
+ }
+
+ [Fact]
+ public void SerializationOfInheritenceIsNotLossy_EvenWhenDiscriminatorIsOnAnInterface()
+ {
+ var obj1 = new InterfaceDiscriminatedClass();
+ var hydratedObj1 = BsonDeserializer.Deserialize<InterfaceDiscriminatedClass>(BsonSerializer.Serialize(obj1));
+
+ Assert.Equal(obj1.Id, hydratedObj1.Id);
+ }
+
+ [Fact]
+ public void SerializationOfInheritenceIsNotLossy_EvenWhenDiscriminatorIsOnAnInterfaceAndWeTryToDeserializeUsingTheInterface()
+ {
+ var obj1 = new InterfaceDiscriminatedClass();
+ var hydratedObj1 = (InterfaceDiscriminatedClass)BsonDeserializer.Deserialize<IDiscriminated>(BsonSerializer.Serialize(obj1));
+
+ Assert.Equal(obj1.Id, hydratedObj1.Id);
+ }
+
[Fact]
public void SerializationOfRegexIsNotLossy()
{
@@ -398,5 +426,10 @@ public void WillDeserializeObjectWithPrivateConstructor()
Assert.Equal(start.Name, end.Name);
}
+ [Fact]
+ public void WillSerializeObjectWithDiscriminator()
+ {
+
+ }
}
}
@@ -1,39 +1,26 @@
-using System.Linq;
+using System.Collections.Generic;
+using System.Linq;
using Norm.BSON.DbTypes;
-using Norm.Configuration;
using Xunit;
-using Norm.Collections;
-
namespace Norm.Tests
{
- public class DBRefTests
+ public class DbRefTests
{
- public DBRefTests()
- {
- using (var session = new Session())
- {
- session.Drop<Product>();
- session.Drop<ProductReference>();
- }
- }
-
[Fact]
- public void DBRefMapsToOtherDocumentsByOid()
+ public void DbRefMapsToOtherDocumentsByOid()
{
- string databaseName = "NormTests";
+ const string databaseName = "NormTests";
var id = ObjectId.NewObjectId();
using (var session = new Session())
{
+ session.Drop<Product>();
+ session.Drop<ProductReference>();
+
session.Add(new Product { _id = id, Name = "RefProduct" });
- var productReference = new DBReference<Product>
- {
- Collection = MongoConfiguration.GetCollectionName(typeof(Product)),
- DatabaseName = databaseName,
- ID = id,
- };
+ var productReference = new DbReference<Product>(id);
session.Add(new ProductReference
{
@@ -49,5 +36,39 @@ public void DBRefMapsToOtherDocumentsByOid()
Assert.Equal(id.Value, product._id.Value);
}
+
+ [Fact]
+ public void DbRefMapsToOtherDocumentsByCustomId()
+ {
+ const string databaseName = "NormTests";
+ const string userId = "Tim Berners-Lee";
+ const string roleName = "Administrator";
+
+ using (var session = new Session())
+ {
+ session.Drop<User>();
+ session.Drop<Role>();
+
+ session.Add(new User
+ {
+ Id = userId,
+ EmailAddress = "user@domain.com"
+ });
+ session.Add(new Role
+ {
+ Id = roleName,
+ Users = new List<DbReference<User, string>>
+ {
+ new DbReference<User, string>(userId)
+ }
+ });
+ }
+
+ var server = Mongo.Create("mongodb://localhost/" + databaseName);
+ var role = server.GetCollection<Role>().Find().First();
+ var user = role.Users[0].Fetch(() => server);
+
+ Assert.Equal(userId, user.Id);
+ }
}
-}
+}
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using Norm.Linq;
using Xunit;
using Norm.Configuration;
@@ -642,8 +643,80 @@ public void CanQueryWithinEmbeddedArray()
session.Add(post1);
session.Add(post2);
var found = session.Posts.Where(p => p.Comments.Any(a => a.Text == "commentA")).SingleOrDefault();
+
Assert.Equal("Second", found.Title);
+ }
+ }
+
+ [Fact]
+ public void CanQueryAndReturnSubClassedObjects()
+ {
+ using (var session = new Session())
+ {
+ session.Drop<SuperClassObject>();
+
+ session.Add<SuperClassObject>(new SubClassedObject { Title = "Find This", ABool = true });
+ session.Add<SuperClassObject>(new SubClassedObject { Title = "Don't Find This", ABool = false });
+
+ var query = new MongoQuery<SuperClassObject>(session.Provider);
+
+ var dtos = query.Where(dto => dto.Title == "Find This").ToList();
+
+ Assert.Equal(1, dtos.Count);
+ Assert.Equal("Find This", dtos[0].Title);
+ }
+ }
+
+ [Fact]
+ public void CanQueryAndReturnSubClassedObjects_EvenWhenAddedBySubClass()
+ {
+ using (var session = new Session())
+ {
+ session.Drop<SuperClassObject>();
+
+ session.Add(new SubClassedObject());
+
+ var query = new MongoQuery<SuperClassObject>(session.Provider);
+
+ var dtos = query.ToList();
+
+ Assert.Equal(1, dtos.Count);
+ }
+ }
+
+ [Fact]
+ public void CanQueryAndReturnSubClassedObjects_EvenWhenQueriedBySubClass()
+ {
+ using (var session = new Session())
+ {
+ session.Drop<SuperClassObject>();
+
+ session.Add(new SubClassedObject());
+
+ var query = new MongoQuery<SubClassedObject>(session.Provider);
+
+ var dtos = query.ToList();
+
+ Assert.Equal(1, dtos.Count);
+ }
+ }
+
+ [Fact]
+ public void CanQueryAndReturnSubClassedObjects_EvenWhenQueriedByInterface()
+ {
+ using (var session = new Session())
+ {
+ session.Drop<IDiscriminated>();
+
+ var obj = new InterfaceDiscriminatedClass();
+ session.Add(obj);
+
+ var query = new MongoQuery<IDiscriminated>(session.Provider);
+
+ var dtos = query.ToList();
+ Assert.Equal(1, dtos.Count);
+ Assert.Equal(obj.Id, dtos.Single().Id);
}
}
}
@@ -208,7 +208,32 @@ public void Are_Queries_Fully_Linqified()
}
}
+ [Fact]
+ public void Can_correctly_determine_collection_name()
+ {
+ var collectionName = MongoConfiguration.GetCollectionName(typeof(SuperClassObject));
+
+ Assert.Equal("SuperClassObject", collectionName);
+ }
+
+ [Fact]
+ public void Can_correctly_determine_collection_name_from_discriminated_sub_class()
+ {
+ var collectionName = MongoConfiguration.GetCollectionName(typeof(SubClassedObject));
+
+ Assert.Equal("SuperClassObject", collectionName);
+ }
+
+ [Fact]
+ public void Can_correctly_determine_collection_name_when_discriminator_is_on_an_interface()
+ {
+ var collectionName = MongoConfiguration.GetCollectionName(typeof(InterfaceDiscriminatedClass));
+
+ Assert.Equal("IDiscriminated", collectionName);
+ }
+
#region Test classes
+
internal class Shoppers : MongoQuery<Shopper>, IDisposable
{
private readonly MongoQueryProvider _provider;
@@ -310,6 +335,7 @@ public User2()
public string FirstName { get; set; }
public string LastName { get; set; }
}
+
public class ShopperMap : MongoConfigurationMap
{
public ShopperMap()
@@ -381,6 +407,7 @@ public CustomMap()
this.For<Product>(cfg => cfg.ForProperty(p => p.Name).UseAlias("productname"));
}
}
+
#endregion
}
}
@@ -102,6 +102,7 @@
<Compile Include="MongoOptimizationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestClasses.cs" />
+ <Compile Include="TypeHelperTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NoRM\NoRM.csproj">
Oops, something went wrong.

0 comments on commit 4d3a274

Please sign in to comment.