如何使用进程内迁移运行器和内存 SQLite 数据库测试流畅的迁移

How to test fluent migrations with an in-process migration runner and a in memory SQLite database

我刚刚开始在我当前的项目中使用 FluentMigration。我编写了我的第一个迁移,但我在为其编写单元测试时遇到了一些问题。

下面是一些示例代码:

private ServiceProvider CreateServiceProvider()
{
    return new ServiceCollection()
        .AddLogging(lb => lb.AddFluentMigratorConsole())
        .AddFluentMigratorCore()
        .ConfigureRunner(
            builder => builder
                .AddSQLite()
                    .WithGlobalConnectionString("Data Source=:memory:;Version=3;New=True;")
                    .WithMigrationsIn(typeof(MigrationOne).Assembly))
            .BuildServiceProvider();
}

private void PerformMigrateUp(IServiceScope scope)
{
    var runner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>();

    runner.MigrateUp(1);
}

[Test]
public void ShouldHaveTablesAfterMigrateUp()
{
    var provider = this.CreateServiceProvider();

    using (var scope = provider.CreateScope())
    {
        this.PerformMigrateUp(scope);

        // here I'd like to test if tables have been created in the database by the migration
    }
}

我不知道如何(或是否可能)访问当前数据库连接以便执行查询。任何的意见都将会有帮助。谢谢

好的,我找到了解决办法。我必须使用跑步者处理器的 Process 方法来执行我自己的 sql 查询。

看起来像这样:

private ServiceProvider CreateServiceProvider()
{
    return new ServiceCollection()
        .AddLogging(lb => lb.AddFluentMigratorConsole())
        .AddFluentMigratorCore()
        .ConfigureRunner(
            builder => builder
                .AddSQLite()
                .WithGlobalConnectionString(@"Data Source=:memory:;Version=3;New=True;")
                .WithMigrationsIn(typeof(MigrationDate20181026113000Zero).Assembly))
        .BuildServiceProvider();
}

[Test]
public void ShouldHaveNewVersionAfterMigrateUp()
{
    var serviceProvider = this.CreateServiceProvider();
    var scope = serviceProvider.CreateScope();
    var runner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>();

    runner.MigrateUp(1);

    string sqlStatement = "SELECT Description FROM VersionInfo";

    DataSet dataSet = runner.Processor.Read(sqlStatement, string.Empty);

    Assert.That(dataSet, Is.Not.Null);
    Assert.That(dataSet.Tables[0].Rows[0].ItemArray[0], Is.EqualTo("Migration1"));
}

这是一个老问题,但很重要。我觉得很奇怪,我找不到任何关于这个的文档。

无论如何,这是我的解决方案,我发现它更好一些,因为您不需要依赖跑步者。由于您不需要为构造函数参数打开大量选项。

首先确保你安装了Microsoft.Data.Sqlite否则你会得到一个奇怪的错误。

只要连接存在,内存数据库中的 SQLite 就存在 - 乍一看每个连接一个数据库。 实际上,根据我的实验,只要至少有一个连接始终处于打开状态,就有一种方法可以在连接之间共享数据库。您只需为其命名即可。 https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/connection-strings#sharable-in-memory

因此,首先我创建了一个连接,该连接将保持打开状态直到测试完成。它将使用 Guid.NewGuid() 命名,以便后续连接将按预期工作。

var dbName = Guid.NewGuid().ToString();
var connectionString = $"Data Source={dbName};Mode=Memory;Cache=Shared";
var connection = new SqliteConnection(connectionString);
connection.Open();

之后 运行 迁移的症结与之前回答的相同,但连接字符串使用命名数据库:

var sp = services.AddFluentMigratorCore()
    .ConfigureRunner(fluentMigratorBuilder => fluentMigratorBuilder
        .AddSQLite()
        .WithGlobalConnectionString(connectionString)
        .ScanIn(AssemblyWithMigrations).For.Migrations()
    )
    .BuildServiceProvider();

var runner = sp.GetRequiredService<IMigrationRunner>();
runner.MigrateUp();

这里是一个class我用来在任何需要连接数据库的地方注入一个连接工厂正常执行:

internal class PostgresConnectionFactory : IConnectionFactory
{
    private readonly string connectionString;

    public PostgresConnectionFactory(string connectionString)
    {
        this.connectionString = connectionString;
    }

    public DbConnection Create()
    {
        return new NpgsqlConnection(connectionString);
    }
}

我刚刚将此 (所有冰雹依赖倒置) 替换为:

internal class InMemoryConnectionFactory : IConnectionFactory
{
    private readonly string connectionstring;

    public InMemoryConnectionFactory(string connectionstring)
    {
        this.connectionstring = connectionstring;
    }

    public DbConnection Create()
    {
        return new SqliteConnection(connectionstring);
    }
}

其中连接字符串与我在上面定义的名称相同。

现在您可以在任何需要连接到同一内存数据库的地方简单地使用该连接工厂,并且由于我们现在可以连接多次,因此打开了集成测试的可能性。

以下是我的大部分实现:

public static IDisposable CreateInMemoryDatabase(Assembly AssemblyWithMigrations, IServiceCollection services = null)
{
    if (services == null)
        services = new ServiceCollection();

    var connectionString = GetSharedConnectionString();
    var connection = GetPersistantConnection(connectionString);
    MigrateDb(services, connectionString, AssemblyWithMigrations);

    services.AddSingleton<IConnectionFactory>(new InMemoryConnectionFactory(connectionString));

    return services.BuildServiceProvider()
        .GetRequiredService<IDisposableUnderlyingQueryingTool>();
}

private static string GetSharedConnectionString()
{
    var dbName = Guid.NewGuid().ToString();
    return $"Data Source={dbName};Mode=Memory;Cache=Shared";
}

private static void MigrateDb(IServiceCollection services, string connectionString, Assembly assemblyWithMigrations)
{
    var sp = services.AddFluentMigratorCore()
        .ConfigureRunner(fluentMigratorBuilder => fluentMigratorBuilder
            .AddSQLite()
            .WithGlobalConnectionString(connectionString)
            .ScanIn(assemblyWithMigrations).For.Migrations()
        )
        .BuildServiceProvider();

    var runner = sp.GetRequiredService<IMigrationRunner>();
    runner.MigrateUp();
}

private static IDbConnection GetPersistantConnection(string connectionString)
{
    var connection = new SqliteConnection(connectionString);
    connection.Open();

    return connection;
}

那么这里是一个示例测试:

public Test : IDisposable {
    private readonly IDisposable _holdingConnection;
    
    public Test() {
        _holdingConnection = CreateInMemoryDatabase(typeof(MyFirstMigration).Assembly);
    }
    
    public void Dispose() {
        _holdingConnection.Dispose();
    }
}

您可能会注意到静态工厂 returns 一个自定义接口。它只是一个扩展我注入到存储库的普通工具的接口,而且还实现了 IDisposable。

集成测试的未测试奖励,您将通过 WebApplicationFactory 或 TestServer 等创建服务集合:

public void AddInMemoryPostgres(Assembly AssemblyWithMigrations)
{
    var lifetime = services.BuildServiceProvider().GetService<IHostApplicationLifetime>();

    var holdingConnection= InMemoryDatabaseFactory.CreateInMemoryDapperTools(AssemblyWithMigrations, services);

    lifetime.ApplicationStopping.Register(() => {
        holdingConnection.Dispose();
    });
}