Sun 5 Apr 2009
SQLite, Rhino.Commons, NHibernate and NUnit testing
Posted by admin under SQLite, Rhino.Commons, NHibernate
There are many examples of writing unit test for NHibernate on the internet. Most of them show how to do this using session factory, session and MS SQL Server. I wanted to do this using Rhino.Commons and SQLite. Here is the code.
using System; using System.Collections.Generic; using System.Data.SQLite; using System.IO; using System.Reflection; using Castle.Windsor; using NHibernate; using NHibernate.Cfg; using NHibernate.Criterion; using NHibernate.Tool.hbm2ddl; using NUnit.Framework; using Rhino.Commons; using Rhino.Mocks; namespace CrawBuck.Commons.Tests.Repository { [TestFixture] public class RhinoCommonsUnitOfWork { protected Configuration configuration; [SetUp] public void SetUp() { System.Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; WindsorContainer container = new WindsorContainer(Path.GetFullPath(@”RepositoryWindsor.cfg.xml”)); configuration = new Configuration(); configuration.Configure(); Rhino.Commons.IoC.Initialize(container); } [Test] public void CanStartUnitOfWorkTest() { Customer customer = new Customer(“Alan”, “Buck”); Guid guid = customer.Id; using(IUnitOfWork uow = UnitOfWork.Start()) { uow.BeginTransaction(); new SchemaExport(configuration).Execute(false, true, false, true, UnitOfWork.CurrentSession.Connection, null); Repository<Customer>.Save(customer); uow.TransactionalFlush(); Customer loadCustomer = Repository<Customer>.Load(guid); Assert.AreEqual(loadCustomer.FirstName, “Alan”); Assert.AreEqual(loadCustomer.LastName, “Buck”); DetachedCriteria criteria = DetachedCriteria.For(typeof(Customer)).Add(Restrictions.Eq(“Id”, guid)); Customer repositoryCustomer = Repository<Customer>.FindOne(criteria); Assert.AreEqual(repositoryCustomer.FirstName, “Alan”); Assert.AreEqual(repositoryCustomer.LastName, “Buck”); } } } public class Customer { private Guid id = Guid.NewGuid(); private string firstName; private string lastName; public Customer() { } public Customer(string firstName, string lastName) { this.firstName = firstName; this.lastName = lastName; } public virtual Guid Id { get { return id; } set { id = value; } } public virtual string FirstName { get { return firstName; } set { firstName = value; } } public virtual string LastName { get { return lastName; } set { lastName = value; } } } }
I’ve included the using statements because I find that without them you’re never sure what DLLs you need. This was not easy for me to figure out but once I got it to work it was very simple. In the SetUp I create a Windsor container, set up my NHibernate.Cfg.Configuration variable and initialize the Rhino.Commons.IoC with the WindsorContainer. This is most of the configuration that I need. In the CanStartUnitOfWorkTest I create a Customer object and then use it inside of Rhino.Commons.UnitOfWork. The last piece of configuration that I do is to use SchemaExport to create an in memory database with SQLite with the UnitOfWork context. As the code shows I save the Customer object using Rhino.Commons.Repository<Customer>.Save. I retrieve it back out of the database using Rhino.Commons.Repository<Customer>.Load and Rhino.Commons.Repository<Customer>.FindOne.
I use the Windsor.cfg.xml file to configure the container.
<?xml version=“1.0″ encoding=“utf-8″ ?> <configuration> <components> <component id=“nhibernate.repository” service =“Rhino.Commons.IRepository`1, Rhino.Commons.NHibernate” type=“Rhino.Commons.NHRepository`1, Rhino.Commons.NHibernate”/> <component id=“nhibernate.unit-of-work.factory” service =“Rhino.Commons.IUnitOfWorkFactory, Rhino.Commons.NHibernate” type=“Rhino.Commons.NHibernateUnitOfWorkFactory, Rhino.Commons.NHibernate”/> </components> </configuration>
I use the hibernate.cfg.xml file to configure NHibernate.
<?xml version=“1.0″ encoding=“utf-8″ ?> <hibernate-configuration xmlns=“urn:nhibernate-configuration-2.2″ > <session-factory> <!– properties –> <property name=“connection.provider”>NHibernate.Connection.DriverConnectionProvider</property> <property name=“connection.driver_class”>NHibernate.Driver.SQLite20Driver</property> <property name=“connection.connection_string”>Data Source=:memory:;Version=3;New=True;</property> <property name=“show_sql”>true</property> <property name=“dialect”>NHibernate.Dialect.SQLiteDialect</property> <property name=“proxyfactory.factory_class”>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> <property name=“connection.release_mode”>on_close</property> <mapping assembly=“CrawBuck.Commons.Tests” /> </session-factory> </hibernate-configuration>
That is all there is to do this.
One Response to “ SQLite, Rhino.Commons, NHibernate and NUnit testing ”
Comments:
Leave a Reply
You must be logged in to post a comment.
Trackbacks & Pingbacks:
-
Pingback from Different flavors of Rhino.Commons, NHibernate and Windsor container »
April 27th, 2009 at 12:41 am[…] last blog entry showed how to use Rhino.Commons with an in memory SQLite database. The example also used Windsor […]