Last month I wrote a post explaining how to test your data access layer by using Fluent NHibernate to generate an in-memory SQLite database from an applications domain model and fluent mappings. You can then run unit tests against the SQLite database rather than mocking objects.
Over the last couple of weeks I have become less fond of Fluent NHibernate (long story! Curse & Swear!) and wanted to recreate the same functionality using NHibernates standard mappings and features. So without further adieu…..
Firstly you will need to build up the schema for the SQLite database. This is created automatically based upon the applications domain model and mapping files.
public abstract class InMemoryDatabase : IDisposable
{
private static Configuration _configuration;
protected static ISessionFactory _sessionFactory;
protected static ISession _session;
protected InMemoryDatabase()
{
_sessionFactory = CreateSessionFactory();
_session = _sessionFactory.OpenSession();
BuildSchema(_session);
}
private static ISessionFactory CreateSessionFactory()
{
//NHibernateProfiler.Initialize();
Dictionary<string, string> properties = new Dictionary<string, string>
{
{"connection.driver_class","NHibernate.Driver.SQLite20Driver"},
{"dialect", "NHibernate.Dialect.SQLiteDialect"},
{"connection.provider","NHibernate.Connection.DriverConnectionProvider"},
{"query.substitutions", "true=1;false=0"},
//{"show_sql", "true"},
{"connection.connection_string","Data Source=MyTestDb.db;Version=3;New=True;"},
{"proxyfactory.factory_class","NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"},
{"cache.provider_class", "NHibernate.Cache.HashtableCacheProvider"},
{"cache.use_second_level_cache", "true"},
{"cache.use_query_cache", "true"}
};
_configuration = new Configuration()
.SetProperties(properties)
.AddAssembly(typeof(Person).Assembly);
return _configuration.BuildSessionFactory();
}
private static void BuildSchema(ISession session)
{
SchemaExport export = new SchemaExport(_configuration);
export.Execute(true, true, false, session.Connection, null);
}
public void Dispose()
{
_sessionFactory.Dispose();
}
}
You will then need to create a class that derives from InMemoryDatabase, this is where the static data is setup. When calling Session.Flush() the data is persisted to the SQLite database.
public abstract class InMemoryData : InMemoryDatabase
{
protected InMemoryData()
{
Account accountOne = new Account(1, "AC001");
Account accountTwo = new Account(2, "AC002");
_session.Save(accountOne);
_session.Save(accountTwo);
_session.Flush();
_session.Save(new Person(1, "Dan", "Watson", accountOne));
_session.Save(new Person(1, "James", "May", accountTwo));
_session.Save(new Person(1, "Andrew", "Plum", accountOne));
_session.Flush();
}
}
As long as your unit test classes then derive from InMemoryData you will have access to the ISession object that is associated to the SQLite database and will be able to execute unit tests against the static data.
I usually create a base class that all my data access tests derive from. I setup my data access repository’s to accept an ISession object via a constructor argument (this is also really useful for dependency injection which is outside the scope of this post) and in this instance pass in the ISession object associated with the SQLite database.
[TestFixture]
public abstract class TestBase : InMemoryData
{
protected IPersonData PersonData { get; set; }
protected IAccountData AccountData { get; set; }
[SetUp]
public void Init()
{
PersonData = new PersonData(_session);
AccountData = new AccountData(_session);
}
[TestFixtureTearDown]
public void Die()
{
Dispose();
}
}
Here is an example of a unit test class. The class is derived from TestBase which then allows my data tests to make assertions on the data access methods but with those methods executing against the SQLite database and static data.
public class PersonDataTest : TestBase
{
[Test]
public void Select_All_People_Should_Return_3()
{
Assert.AreEqual(3, PersonData.SelectAllPeople().Count());
}
}
As always here’s the code for you to download.