Skip to content

Commit 90d80df

Browse files
committed
Create docs for in-memory testing utilizing FM
1 parent 478123c commit 90d80df

File tree

2 files changed

+106
-28
lines changed

2 files changed

+106
-28
lines changed

articles/in-memory-testing.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
uid: in-memory-testing
3+
title: In-Memory Testing
4+
---
5+
6+
# In-Memory Testing
7+
8+
When performing integration testing, it's sometimes helpful to be able to use an in-memory database (such as Sqlite) to act as the database while tests are running. As a result, this article describes how you can accomplish this with FluentMigrator.
9+
10+
_Credit to Craig on this [StackOverflow answer](https://stackoverflow.com/a/66926940) that in turn created an issue for us to add this to the documentation._
11+
12+
_Note: Sqlite only keeps the in-memory database in memory for the time the process is running. In addition, all connection strings that utilize the same name will access the same database._
13+
14+
Create InMemory database, setup FluentMigrator and execute migrations during test setup:
15+
16+
```csharp
17+
public static IDisposable CreateInMemoryDatabase(Assembly AssemblyWithMigrations, IServiceCollection services = null)
18+
{
19+
if (services == null)
20+
services = new ServiceCollection();
21+
22+
var connectionString = GetSharedConnectionString();
23+
var connection = GetPersistantConnection(connectionString);
24+
MigrateDb(services, connectionString, AssemblyWithMigrations);
25+
26+
services.AddSingleton<IConnectionFactory>(new InMemoryConnectionFactory(connectionString));
27+
28+
return services.BuildServiceProvider()
29+
.GetRequiredService<IDisposableUnderlyingQueryingTool>();
30+
}
31+
32+
private static string GetSharedConnectionString()
33+
{
34+
var dbName = Guid.NewGuid().ToString();
35+
return $"Data Source={dbName};Mode=Memory;Cache=Shared";
36+
}
37+
38+
private static void MigrateDb(IServiceCollection services, string connectionString, Assembly assemblyWithMigrations)
39+
{
40+
var sp = services.AddFluentMigratorCore()
41+
.ConfigureRunner(fluentMigratorBuilder => fluentMigratorBuilder
42+
.AddSQLite()
43+
.WithGlobalConnectionString(connectionString)
44+
.ScanIn(assemblyWithMigrations).For.Migrations()
45+
)
46+
.BuildServiceProvider();
47+
48+
var runner = sp.GetRequiredService<IMigrationRunner>();
49+
runner.MigrateUp();
50+
}
51+
52+
private static IDbConnection GetPersistantConnection(string connectionString)
53+
{
54+
var connection = new SqliteConnection(connectionString);
55+
connection.Open();
56+
57+
return connection;
58+
}
59+
```
60+
61+
This serves as an example test utilizing the static methods above:
62+
63+
```csharp
64+
[TestFixture]
65+
public Test : IDisposable {
66+
private readonly IDisposable _holdingConnection;
67+
68+
[Test]
69+
public Test() {
70+
_holdingConnection = CreateInMemoryDatabase(typeof(MyFirstMigration).Assembly);
71+
}
72+
73+
public void Dispose() {
74+
_holdingConnection.Dispose();
75+
}
76+
}
77+
```

articles/intro.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,56 @@
22

33
The project is split into multiple packages:
44

5-
Package | Description
6-
--------|-------------
7-
FluentMigrator | The base assembly needed to create migrations
8-
FluentMigrator.Runner | The runner classes required for in-process execution of migrations
9-
FluentMigrator.Console | The .NET 4.0/4.5/.NET Core 2.0 executable for out-of-process execution of migrations
10-
FluentMigrator.MSBuild | The .NET 4.0/4.5/.NET Standard 2.0 task for MSBuild
11-
FluentMigrator.DotNet.Cli | The .NET Core 2.1 executable that integrates into the .NET Core CLI tooling (`dotnet` command)
5+
| Package | Description |
6+
| ------------------------- | ---------------------------------------------------------------------------------------------- |
7+
| FluentMigrator | The base assembly needed to create migrations |
8+
| FluentMigrator.Runner | The runner classes required for in-process execution of migrations |
9+
| FluentMigrator.Console | The .NET 4.0/4.5/.NET Core 2.0 executable for out-of-process execution of migrations |
10+
| FluentMigrator.MSBuild | The .NET 4.0/4.5/.NET Standard 2.0 task for MSBuild |
11+
| FluentMigrator.DotNet.Cli | The .NET Core 2.1 executable that integrates into the .NET Core CLI tooling (`dotnet` command) |
1212

1313
## Getting Started
1414

15-
* Check out the tour of FluentMigrator in our [Quickstart](xref:quickstart.md)
16-
* We also started a [FAQ](xref:faq)
15+
- Check out the tour of FluentMigrator in our [Quickstart](xref:quickstart.md)
16+
- We also started a [FAQ](xref:faq)
1717

1818
# Usage details
1919

20-
* How to create a [migration](xref:migration-example.md)
21-
* Learn about the [fluent interface](fluent-interface.md)
22-
* [Profiles](profiles.md) can be used to seed test data
23-
* And then choose one of the [migration runners](migration-runners.md) to run your migrations
20+
- How to create a [migration](xref:migration-example.md)
21+
- Learn about the [fluent interface](fluent-interface.md)
22+
- [Profiles](profiles.md) can be used to seed test data
23+
- And then choose one of the [migration runners](migration-runners.md) to run your migrations
2424

2525
# More Features
2626

27-
* [Database functions as default value](xref:db-functions)
28-
* [Microsoft SQL Server specific extensions](xref:sql-server-extensions.md)
29-
* [Using raw SQL](xref:raw-sql)
30-
* [Auto-reversing migrations](xref:migration-auto-reversing)
31-
* [Transaction modes for the migration runner](xref:migration-transaction-behavior)
27+
- [Database functions as default value](xref:db-functions)
28+
- [Microsoft SQL Server specific extensions](xref:sql-server-extensions.md)
29+
- [Using raw SQL](xref:raw-sql)
30+
- [Auto-reversing migrations](xref:migration-auto-reversing)
31+
- [Transaction modes for the migration runner](xref:migration-transaction-behavior)
32+
- [In-Memory Testing](xref:in-memory-testing)
3233

3334
# Advanced Features and Techniques of FluentMigrator
3435

35-
* [Dealing with multiple database types](multi-db-support.md)
36-
* [Filter migrations run based on Tags](xref:migration-filter-tags)
37-
* [Enforcing migration version numbering rules](xref:migration-attribute-custom)
38-
* [Create custom metadata for the VersionInfo table](xref:version-table-metadata)
36+
- [Dealing with multiple database types](multi-db-support.md)
37+
- [Filter migrations run based on Tags](xref:migration-filter-tags)
38+
- [Enforcing migration version numbering rules](xref:migration-attribute-custom)
39+
- [Create custom metadata for the VersionInfo table](xref:version-table-metadata)
3940

4041
# Current Release
4142

42-
* [Release Notes](https://github.com/fluentmigrator/fluentmigrator/releases)
43+
- [Release Notes](https://github.com/fluentmigrator/fluentmigrator/releases)
4344

4445
# Upgrade guides
4546

46-
* [3.1 to 3.2](xref:upgrade-guide-3.1-to-3.2)
47-
* [3.0 to 3.1](xref:upgrade-guide-3.0-to-3.1)
48-
* [2.x to 3.0](xref:upgrade-guide-2.0-to-3.0)
47+
- [3.1 to 3.2](xref:upgrade-guide-3.1-to-3.2)
48+
- [3.0 to 3.1](xref:upgrade-guide-3.0-to-3.1)
49+
- [2.x to 3.0](xref:upgrade-guide-2.0-to-3.0)
4950

5051
# Runners
5152

52-
* [Console](xref:runner-console)
53-
* [dotnet-fm](xref:dotnet-fm)
53+
- [Console](xref:runner-console)
54+
- [dotnet-fm](xref:dotnet-fm)
5455

5556
# Supported databases
5657

0 commit comments

Comments
 (0)