gpt4 book ai didi

c# - 复制内存中的 SQLite 数据库,使单元测试更快

转载 作者:IT王子 更新时间:2023-10-29 06:26:12 26 4
gpt4 key购买 nike

在 C#/nHibernate-projects 中,我使用 SQLite 对我的代码进行单元测试,大致使用此处描述的方法:http://ayende.com/blog/3983/nhibernate-unit-testing .

但是,我发现构建和配置内存数据库通常需要大约 150 毫秒。我有很多单元测试,所以这很快就会加起来。

我想一次构建和配置数据库,将其存储在静态变量中,并在每次单元测试需要数据库时复制它。

如何备份内存数据库?

我首先尝试创建一个命名的内存数据库。根据https://www.sqlite.org/inmemorydb.html这个有可能。我曾经有:

    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”来复制数据库。

然而,单元测试的行为就像“实例”数据库没有表,即使是“原型(prototype)”数据库也是如此。

        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();
}

最佳答案

我最终得到了这段工作代码。我的单元测试时间从十多分钟缩短到不到两分钟。(为了便于阅读,代码略有简化)

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();
}
}
}

关于c# - 复制内存中的 SQLite 数据库,使单元测试更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31073478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com