在C#/ nHibernate项目中,我使用SQLite对我的代码进行单元测试,大致使用此处描述的方法:http://ayende.com/blog/3983/nhibernate-unit-testing。
但是,我发现构建和配置内存数据库通常需要大约150毫秒。我有很多单元测试,所以这很快就会增加。
我想构建和配置数据库一次,将其存储在静态变量中,并在每次单元测试需要数据库时复制它。
如何备份内存数据库?
我首先尝试创建一个命名的内存数据库。根据{{3}},这是可能的。我曾经有过:
private const string ConnectionString = "Data Source=:memory:;Version=3;";
我尝试过的连接字符串是:
private const string ConnectionString = "FullUri=file:memorydb.db?mode=memory&cache=shared";
private const string ConnectionString2 = "FullUri=file:memorydb2.db?mode=memory&cache=shared";
所以现在我只需要了解如何快速将内容从一个复制到另一个? 我差不多了:我可以创建两个内存数据库,并调用“BackupDatabase”来复制数据库。
然而,单元测试的行为类似于“实例”数据库没有表,甚至“原型”数据库也没有。
private static ISessionFactory _prototypeSessionFactory;
private const string InstanceConnectionString = "FullUri=file:memorydb.db?mode=memory&cache=shared";
private const string PrototypeConnectionString = "FullUri=file:memorydb2.db?mode=memory&cache=shared";
private SQLiteConnection _instanceConnection;
private ISessionFactory _instanceSessionFactory;
public DatabaseScope(Assembly assembly)
{
var prototyeConfiguration = SQLiteConfiguration.Standard.ConnectionString(PrototypeConnectionString);
var cfg = Fluently
.Configure()
.Database(prototyeConfiguration)
.Mappings(m => m.HbmMappings.AddFromAssembly(assembly));
cfg.ExposeConfiguration(BuildSchema);
_prototypeSessionFactory = cfg.BuildSessionFactory();
var instanceConfiguration = SQLiteConfiguration.Standard.ConnectionString(InstanceConnectionString);
_instanceSessionFactory = Fluently
.Configure()
.Database(instanceConfiguration)
.BuildSessionFactory();
CopyDatabase();
}
private void CopyDatabase()
{
var cnnIn = new SQLiteConnection(PrototypeConnectionString);
var cnnOut = new SQLiteConnection(InstanceConnectionString);
cnnIn.Open();
cnnOut.Open();
cnnIn.BackupDatabase(cnnOut, "main", "main", -1, null, -1);
cnnIn.Close();
cnnOut.Close();
}
答案 0 :(得分:3)
我最终得到了这个有效的代码。 我的单元测试时间从十多分钟到两分钟不等。 (为便于阅读,代码略有简化)
using System;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Reflection;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Mapping;
using NHibernate.Tool.hbm2ddl;
namespace TestHelper.DbHelper.SqLite
{
public class DatabaseScope : IDisposable
{
private static Assembly _prototypeAssembly;
private const string PrototypeConnectionString = "FullUri=file:prototype.db?mode=memory&cache=shared";
private static ISessionFactory _prototypeSessionFactory;
private static SQLiteConnection _prototypeConnection;
private const string InstanceConnectionString = "FullUri=file:instance.db?mode=memory&cache=shared";
private ISessionFactory _instanceSessionFactory;
private SQLiteConnection _instanceConnection;
public DatabaseScope(Assembly assembly)
{
InitDatabasePrototype(assembly);
InitDatabaseInstance();
}
private void InitDatabasePrototype(Assembly assembly)
{
if (_prototypeAssembly == assembly) return;
if (_prototypeConnection != null)
{
_prototypeConnection.Close();
_prototypeConnection.Dispose();
_prototypeSessionFactory.Dispose();
}
_prototypeAssembly = assembly;
_prototypeConnection = new SQLiteConnection(PrototypeConnectionString);
_prototypeConnection.Open();
_prototypeSessionFactory = Fluently
.Configure()
.Database(SQLiteConfiguration.Standard.ConnectionString(PrototypeConnectionString))
.Mappings(m => m.HbmMappings.AddFromAssembly(assembly))
.ExposeConfiguration(cfg => new SchemaExport(cfg).Execute(false, true, false, _prototypeConnection, null))
.BuildSessionFactory();
}
private void InitDatabaseInstance()
{
_instanceSessionFactory = Fluently
.Configure()
.Database(SQLiteConfiguration.Standard.ConnectionString(InstanceConnectionString))
.Mappings(m => m.HbmMappings.AddFromAssembly(_prototypeAssembly))
.BuildSessionFactory();
_instanceConnection = new SQLiteConnection(InstanceConnectionString);
_instanceConnection.Open();
_prototypeConnection.BackupDatabase(_instanceConnection, "main", "main", -1, null, -1);
}
public ISession OpenSession()
{
return _instanceSessionFactory.OpenSession(_instanceConnection);
}
public void Dispose()
{
_instanceConnection.Close();
_instanceConnection.Dispose();
_instanceSessionFactory.Dispose();
}
}
}
答案 1 :(得分:1)
我在内存数据库中使用SQLite观察到的是,只要关闭连接,数据库中的所有内容都将消失。所以要做你想做的事,
为备份数据库创建会话工厂,打开会话和构建架构不要关闭此会话,直到完成整个测试套件
为目标数据库创建会话工厂,打开会话并使用此会话对象的连接以及从步骤1创建的会话连接到复制数据
使用在步骤2中创建的会话进行测试,并在测试完成后将其关闭
另一个解决方案是使用单个会话执行多个测试(单个测试夹具中的所有测试)然后您不需要为每个测试创建新的会话工厂,但每个testfixture