Hibernate version: 1.2
One unit test phenomenon I continually revisit:
On TestFixtureSetup, I use SchemaExport (a very nice feature) to give me a fresh database.
On Test Setup, I use a test data "factory" to populate the database with some meaningful data. The testdatafactory is some very common code that varies by domain.
On TearDown, I clean all of the data from all the tables... something like this
Code:
using (SqlCommand cmd = new SqlCommand(null, session.Connection as SqlConnection))
{
cmd.CommandText =
@"EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL';
EXEC sp_MSForEachTable 'delete from ?';
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL';";
cmd.ExecuteNonQuery();
}
On TestFixutreTearDown, I use schemaexport to drop the schema
I am interested in automating my test data factory code, to remove some of the more mundane work involved with building a graph of dummy domain objects.
I was wondering if this syntax was attractive to you:
Code:
ICriteria criteria = session.CreateCriteria(typeof(Cat)).Add(Expression.Eq("Name","Sam")).Add(Expression.Eq("Owner",ownerObj));
Cat cat = criteria.FetchOrCreate<Cat>();
If the cat exists, return it, otherwise, construct it and add it to the session.