Manage In memory nhibernate test with sqlite and database schema
When you work with nhibernate you usually write a lot of tests that interact with your database, mainly to test your mapping but also when you do not want to shield the session behind a IRepository. To avoid Slow Test you should use some In Memory database like Sqlite, but it can be problematic when you use features of your real database (like Sql Server) that are not supported by the Sqlite engine.
One of the most frustrating one is the lack of schema support. When you have hundreds of entities, it is of fundamental importance that you use schema, avoiding to pollute the dbo with all the table, but this makes difficult to use Sqlite for testing purpose. Suppose you map an entity to the table * myschema.RawMetabaseData *, when you create the schema with schemaexport you will obtain this error
|
|
SQlite does not know the concept of schema, so it believes that myschema is a database name and not a schema. There are some solution to this problem, but the most simple one is to change the mapping directly in memory.
|
|
After you have called the AddAssembly() method, but before you create the SessionFactory with BuildSessionFactory(), mappings are compiled in memory, so you can browse the object model created from the xml mapping files, looking for table that contains dot character, and change the dot into an underscore, so the table can be used by Sqlite. This code is executed only for tests that uses SqlLite so my standard test that use Sql Server are not affected by the modification, but I can still use Sqlite to do fast in memory test even with tables in custom schema.
alk.
Tags: NHibernate Sqlite Unit Testing