-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Azure Pipelines
committed
Mar 26, 2024
1 parent
a9b75b0
commit add7de9
Showing
5 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
<!DOCTYPE html> | ||
<!--[if IE]><![endif]--> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | ||
<title>In-Memory Testing | FluentMigrator documentation </title> | ||
<meta name="viewport" content="width=device-width"> | ||
<meta name="title" content="In-Memory Testing | FluentMigrator documentation "> | ||
|
||
|
||
<link rel="shortcut icon" href="../images/favicon.ico"> | ||
<link rel="stylesheet" href="../styles/docfx.vendor.min.css"> | ||
<link rel="stylesheet" href="../styles/docfx.css"> | ||
<link rel="stylesheet" href="../styles/main.css"> | ||
<meta property="docfx:navrel" content="../toc.html"> | ||
<meta property="docfx:tocrel" content="toc.html"> | ||
|
||
<meta property="docfx:rel" content="../"> | ||
|
||
</head> | ||
<body data-spy="scroll" data-target="#affix" data-offset="120"> | ||
<div id="wrapper"> | ||
<header> | ||
|
||
<nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation"> | ||
<div class="container"> | ||
<div class="navbar-header"> | ||
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar"> | ||
<span class="sr-only">Toggle navigation</span> | ||
<span class="icon-bar"></span> | ||
<span class="icon-bar"></span> | ||
<span class="icon-bar"></span> | ||
</button> | ||
|
||
<a class="navbar-brand" href="../index.html"> | ||
<img id="logo" class="svg" src="../images/logo.svg" alt=""> | ||
</a> | ||
</div> | ||
<div class="collapse navbar-collapse" id="navbar"> | ||
<form class="navbar-form navbar-right" role="search" id="search"> | ||
<div class="form-group"> | ||
<input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off"> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</nav> | ||
|
||
<div class="subnav navbar navbar-default"> | ||
<div class="container hide-when-search" id="breadcrumb"> | ||
<ul class="breadcrumb"> | ||
<li></li> | ||
</ul> | ||
</div> | ||
</div> | ||
</header> | ||
<div class="container body-content"> | ||
|
||
<div id="search-results"> | ||
<div class="search-list">Search Results for <span></span></div> | ||
<div class="sr-items"> | ||
<p><i class="glyphicon glyphicon-refresh index-loading"></i></p> | ||
</div> | ||
<ul id="pagination" data-first="First" data-prev="Previous" data-next="Next" data-last="Last"></ul> | ||
</div> | ||
</div> | ||
<div role="main" class="container body-content hide-when-search"> | ||
|
||
<div class="sidenav hide-when-search"> | ||
<a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a> | ||
<div class="sidetoggle collapse" id="sidetoggle"> | ||
<div id="sidetoc"></div> | ||
</div> | ||
</div> | ||
<div class="article row grid-right"> | ||
<div class="col-md-10"> | ||
<article class="content wrap" id="_content" data-uid="in-memory-testing"> | ||
<h1 id="in-memory-testing">In-Memory Testing</h1> | ||
|
||
<p>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.</p> | ||
<p><em>Credit to Craig on this <a href="https://stackoverflow.com/a/66926940">StackOverflow answer</a> that in turn created an issue for us to add this to the documentation.</em></p> | ||
<p><em>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.</em></p> | ||
<p>Create InMemory database, setup FluentMigrator and execute migrations during test setup:</p> | ||
<pre><code class="lang-csharp">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; | ||
} | ||
</code></pre> | ||
<p>This serves as an example test utilizing the static methods above:</p> | ||
<pre><code class="lang-csharp">[TestFixture] | ||
public Test : IDisposable { | ||
private readonly IDisposable _holdingConnection; | ||
|
||
[Test] | ||
public Test() { | ||
_holdingConnection = CreateInMemoryDatabase(typeof(MyFirstMigration).Assembly); | ||
} | ||
|
||
public void Dispose() { | ||
_holdingConnection.Dispose(); | ||
} | ||
} | ||
</code></pre> | ||
|
||
</article> | ||
</div> | ||
|
||
<div class="hidden-sm col-md-2" role="complementary"> | ||
<div class="sideaffix"> | ||
<div class="contribution"> | ||
<ul class="nav"> | ||
<li> | ||
<a href="https://github.com/fluentmigrator/documentation/blob/master/articles/in-memory-testing.md/#L1" class="contribution-link">Edit this page</a> | ||
</li> | ||
</ul> | ||
</div> | ||
<nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix"> | ||
<h5>In this article</h5> | ||
<div></div> | ||
</nav> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<footer> | ||
<div class="grad-bottom"></div> | ||
<div class="footer"> | ||
<div class="container"> | ||
<span class="pull-right"> | ||
<a href="#top">Back to top</a> | ||
</span> | ||
<table border='0'><tr><td><span>Copyright © 2018 Fluent Migrator Project<br>Generated by <strong>DocFX</strong></span></td><td><a rel='license' href='http://creativecommons.org/licenses/by-sa/4.0/'><img alt='Creative Commons License' style='border-width:0' src='https://i.creativecommons.org/l/by-sa/4.0/88x31.png'></a><br><span xmlns:dct='http://purl.org/dc/terms/' property='dct:title'>FluentMigrator Documentation</span> by <a xmlns:cc='http://creativecommons.org/ns#' href='https://fluentmigrator.github.io' property='cc:attributionName' rel='cc:attributionURL'>FluentMigrator Project</a> is licensed under a <a rel='license' href='http://creativecommons.org/licenses/by-sa/4.0/'>Creative Commons Attribution-ShareAlike 4.0 International License</a>.</td></tr></table> | ||
|
||
</div> | ||
</div> | ||
</footer> | ||
</div> | ||
|
||
<script type="text/javascript" src="../styles/docfx.vendor.min.js"></script> | ||
<script type="text/javascript" src="../styles/docfx.js"></script> | ||
<script type="text/javascript" src="../styles/main.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters