Nenad
Nenad

Reputation:

Using Sqlite InMemory DB for unittesting MSSQL-DB

I am trying to implement this solution: NHibernate-20-SQLite-and-In-Memory-Databases

The only problem is that we have hbms like this:

<class name="aTable" table="[dbo].[aTable]" mutable="true" lazy="false">

with [dbo] in the table name, because we are working with mssql, and this does not work with Sqlite.

I found this posting on the rhino-tools-dev group where they talk about just removing the schema from the mapping, but on NH2 there doesn't seem to be a classMapping.Schema.

There is a classMapping.Table.Schema, but it seems to be read-only. For example, this doesn't work:

 foreach (PersistentClass cp in configuration.ClassMappings)            {
  // Does not work - throws a 
  //System.IndexOutOfRangeException: Index was outside the bounds of the array.
        cp.Table.Schema = "";
    }

Upvotes: 7

Views: 4784

Answers (3)

Nenad
Nenad

Reputation:

After looking through the source of NH and some experimenting i think i found a simple workaround -

    foreach (PersistentClass cp in configuration.ClassMappings)
    {
        // Input : [dbo].[Tablename] Output : Tablename
        cp.Table.Name = Regex.Replace(cp.Table.Name, @"^\[.*\]\.\[", "");
        cp.Table.Name = Regex.Replace(cp.Table.Name, @"\]$", "");
        // just to be sure
        cp.Table.Schema = null;                
    }

note that i can set Table.Schema to null while an empty string threw an exception ...

thanks for the answers !

Upvotes: 0

Andriy Volkov
Andriy Volkov

Reputation: 18923

We had too many problems with SQLite which eventually pushed us to switch to SQL Express. Problems I remember:

  1. SQLite, when used in-memory, discards the database when Session is closed
  2. SQLite does not support bunch of SQL constructs such basic ones as ISNULL, but also more advanced like common table expressions and others added in SQL 2005 and 2008. This becomes important when you start writing complex named queries.
  3. SQLite's datetime has bigger range of possible values than SQL Server's
  4. The API NHibernate uses for SQLite behaves differently than ADO.NET for MS SQL Server when used in scope of transaction. One example is the hbm-to-ddl tool whose Execute method does not work inside transaction with SQL Server but works fine with SQLite.

To summarize, SQLite-based unit-testing is very far from being conclusively representative of the issues you'll encounter when using MS SQL Server in PROD and therefore undermines the credibility of unit-testing overall.

Upvotes: 9

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

We are using Sqlite to run unit tests with NH 2.0.1. Actually, I didn't run into this problem. I just didn't specify dbo, I think it is default on SqlServer.

By the way, there is a default_schema parameter in the configuration file. This is actually the database name, but you can try putting the dbo there, only for the SqlServer configuration of course.

Upvotes: 1

Related Questions