Hello all.
I had a stupid bug last week. Cost me 8 hours or so to find and fix.
Quick overview:
We use FluentNHibernate to map our classes. I added a couple of new
class, but one of them wasn't being persisted. Turns out I didn't
make the mapping class "public". I spent a few hours putting together
this unit test that will find stupid stuff like that.
Note: "Entity" is our base database object. It defines a single
property, "Id" that is an integer. If you have database entities in
multiple assemblies, each will have to be combined.
We use StructureMap for IoC (among other things). the call to
ObjectFactory.GetInstance<ISessionFactory>() retrieves the NHibernate
session factory. Replace that with your own code.
[Test()]
public void ClassesAreMapped()
{
var entityAssemblyList = typeof (Entity).Assembly.GetTypes();
var entityList = entityAssemblyList.Where(
x => !x.IsAbstract
&& x.IsPublic
&& x.IsVisible
&& x.IsSubclassOf(typeof (Entity))
&& (x.GetCustomAttributes(typeof(BaseClass),
false).Length == 0)
);
Assert.That(entityList, Is.Not.Null);
Assert.That(entityList, Is.Not.Empty);
var metadata =
ObjectFactory.GetInstance<ISessionFactory>().GetAllClassMetadata();
foreach (var item in entityList)
{
Assert.That(metadata.ContainsKey(item.FullName), item.FullName
+ " isn't in the list!");
}
foreach (var item in metadata.Values)
{
Assert.That(entityList.Any(x => x.FullName ==
item.EntityName), "Entity is not exported: " + item.EntityName);
}
}
Also needed is this Attribute class definition:
/// <summary>
/// For entities that are not supposed to be persisted but inherit
from Entity
/// (their descendent classes persist), add this attribute. For unit
tests.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false,
Inherited = false)]
public class BaseClass : Attribute
{ }
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.