Given that most real world applications have fairly complicated relationships between entities, is there much value in testing individual class mappings? It seems that to be truly valuable, NHibernate tests should revolve around retrieving, persisting and deleting entire object graphs, starting at the aggregate root level (i.e. Customer-->Order-->OrderDetails). But if I go down that road, it seems I would have to test CRUD operations at every conceivable level in the object tree to validate that the "whole" works as expected; leading to an explosion of tests:
So, unless I'm missing something, which I very likely am, my choices are:
If at all relevant, I'm using the NHibernate.Mapping.ByCode
ConventionModelMapper to generate mappings using conventions.
Yes, when testing that my mappings are working as expected, I test at the class level and test each of that individual classes relationships.
For instance, if I have a Customer object which has a list of Order objects, I would write integration tests for my Customer object to ensure that I can do all of the CRUD operations on that object. I would then write tests for all of the relationships that the Customer object has such as having a list of Orders, Addresses, etc. Those tests would cover things such as cascading inserts/updates/deletes and the ability to eagerly fetch those child collections in a query.
So although I'm testing at at the class level, because I'm testing all of the relationships for each class, I am technically testing the entire object graph as long as I continue this testing behavior in each of my tests for each of my mapped classes.
Update ( Adding brief example ):
public class Customer
{
public int CustomerId { get; set; }
public string CompanyName { get; set; }
public IList<Address> Addresses { get; set; }
public IList<Order> Orders { get; set; }
}
public class Address
{
public int AddressId { get; set; }
}
public class Order
{
public int OrderId { get; set; }
public string Status { get; set; }
public IList<OrderDetail> OrderDetails { get; set; }
}
public class OrderDetail
{
public int OrderDetailId { get; set; }
public string City { get; set; }
}
[TestFixture]
public class CustomerMappingTests
{
private ISession session;
[SetUp]
public void SetUp()
{
session = UnitOfWork.Current.GetSession();
}
[TearDown]
public void TearDown()
{
session.Dispose();
}
[Test]
public void CanGetCustomer()
{
// Arrange
const int customerId = 1;
// Act
var customer = session.Query<Customer>()
.Where( x => x.CustomerId == customerId )
.FirstOrDefault();
// Assert
Assert.NotNull( customer );
Assert.That( customer.CustomerId == customerId );
}
[Test]
public void CanGetCustomerAddresses()
{
// Arrange
const int customerId = 1;
// Act
var customer = session.Query<Customer>()
.Where( x => x.CustomerId == customerId )
.Fetch( x => x.Addresses )
.FirstOrDefault();
// Assert
Assert.NotNull( customer.Addresses.Count > 0 );
}
[Test]
public void CanGetCustomerOrders()
{
// Arrange
const int customerId = 1;
// Act
var customer = session.Query<Customer>()
.Where( x => x.CustomerId == customerId )
.Fetch( x => x.Orders )
.FirstOrDefault();
// Assert
Assert.NotNull( customer.Orders.Count > 0 );
}
[Test]
public void CanSaveCustomer()
{
// Arrange
const string companyName = "SnapShot Corporation";
var customer = new Customer { CompanyName = companyName };
// Act
session.Save( customer );
session.Flush(); // Update the database right away
session.Clear(); // Clear cache
var customer2 = session.Query<Customer>()
.Where( x => x.CompanyName == companyName )
.FirstOrDefault();
// Assert
Assert.NotNull( customer2 );
Assert.That( customer2.CompanyName == companyName );
}
}