views:

1173

answers:

2

I'm mapping a ProductCategory tree using Fluent NHibernate and everything was going fine until I tried to walk the tree that is returned from the database to ensure it's saving and retreiving appropriately.

Here's how I'm testing:

  1. Instantiate 4 categories: Beverages, Beer, Light Beer, and Dark Beer
  2. Add Beer to Beverages, then Light Beer and Dark Beer to Beer.
  3. Save Beverages (cascade is set to AllDeleteOrphan)
  4. Flush the session, which persists the entire tree
  5. Evict each of the ProductCategories from the session
  6. Load Beverages from the database
  7. Check that the loaded object (fromDB) is EqualTo but not SameAs Beverages.
  8. Check that fromDB has only one child ProductCategory
  9. Check that the only child in fromDB is EqualTo but not SameAs Beer

The test fails because the child is the SameAs Beer. Which means that it's not actually loading the object from the database, because it's still in the NHibernate session somewhere.

Any insight will be much appreciated.

Edit: In response to Sean's comments below. I am using an in memory SQLite database, so as soon as the session/connections are closed the database is blown away.

A: 

From a testing perspective, you are better off closing the initial session that was used to create the objects and creating a new session to retrieve the objects. This will assure that the database is being hit again to instantiate the objects (assuming that the 2nd level cache is not enabled).

Sean Carpenter
Thanks for the answer, I responded in an edit to the question.
RKitson
A: 

Just figured it out, turns out it was a copy&paste bug. Heh, PEBKAC.

I added 4 assertions that verify that the objects are not in the session:

Assert.That(Session.Contains(_beveragesCategory), Is.False); 
Assert.That(Session.Contains(_beerCategory), Is.False);
Assert.That(Session.Contains(_darkBeerCategory), Is.False);
Assert.That(Session.Contains(_lightBeerCategory), Is.False);

When all of those passed (the first time I ran them) I took a closer look at the code that was asserting that the objects were differenta nd found that the assertions were wrong.

Was:

Assert.That(_beverageCategory.ChildCategories[0], Is.Not.SameAs(_beerCategory));

Should have been:

Assert.That(fromDB.ChildCategories[0], Is.Not.SameAs(_beerCategory));
RKitson