SqliteMemoryDatabaseProvider 7.2.4

dotnet add package SqliteMemoryDatabaseProvider --version 7.2.4                
NuGet\Install-Package SqliteMemoryDatabaseProvider -Version 7.2.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SqliteMemoryDatabaseProvider" Version="7.2.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SqliteMemoryDatabaseProvider --version 7.2.4                
#r "nuget: SqliteMemoryDatabaseProvider, 7.2.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SqliteMemoryDatabaseProvider as a Cake Addin
#addin nuget:?package=SqliteMemoryDatabaseProvider&version=7.2.4

// Install SqliteMemoryDatabaseProvider as a Cake Tool
#tool nuget:?package=SqliteMemoryDatabaseProvider&version=7.2.4                

SqliteMemoryDatabaseProvider

This provider to aid with creating in-memory databases for unit tests.

Create Database

You can set up a new SqliteMemoryDatabaseProvider and call CreateDatabase. This will set up the database in-memory to be used in your test. SqliteMemoryDatabaseProvider needs to be instantiated so it can open up a new database connection using in-memory SQLite.

An example of this in a test:

var mock = new AutoMocker();
using var provider = new SqliteMemoryDatabaseProvider();
var mockDatabase = provider.CreateDatabase<TestEntities>();
mockDatabase.TestModels.Add(testRecord);
await mockDatabase.SaveChangesAsync();
mock.Use<ITestEntities>(mockDatabase);
var testClass = mock.CreateInstance<TestClass>();

Optionally, you can provide actions to execute after the database is created. Commonly, this can be used to populate data into your new database. These changes are saved automatically.

An example of this in a test:

var mock = new AutoMocker();
using var provider = new SqliteMemoryDatabaseProvider();
var mockDatabase = provider.CreateDatabase<TestEntities>(x =>
    x.TestModels.Add(testRecord);
);
mock.Use<ITestEntities>(mockDatabase);
var testClass = mock.CreateInstance<TestClass>();

SqliteMemoryDatabaseProvider can also be used at a class level for your tests:

private SqliteMemoryDatabaseProvider sqliteMemoryDatabaseProvider;

[SetUp]
public void SetUp()
{
    sqliteMemoryDatabaseProvider = new SqliteMemoryDatabaseProvider();
}

[TearDown]
public void TearDown()
{
    sqliteMemoryDatabaseProvider.Dispose();
}

This can help simplify your tests like so:

var mock = new AutoMocker();
var mockDatabase = sqliteMemoryDatabaseProvider.CreateDatabase<TestEntities>(x =>
    x.TestModels.Add(testRecord);
);
mock.Use<ITestEntities>(mockDatabase);
var testClass = mock.CreateInstance<TestClass>();

Additional Parameters

If your entity needs additional parameters, an overload is available to provide them:

var mock = new AutoMocker();
var mockedDependency = mock.GetMock<ITestDependencies>();
using var provider = new SqliteMemoryDatabaseProvider();
var mockDatabase = provider.CreateDatabase<TestEntities>(mockedDependency.Object);
await mockDatabase.SaveChangesAsync();
var testClass = mock.CreateInstance<TestClass>();

You can optionally combine providing actions and additional constructor parameters.

var mock = new AutoMocker();
var mockedDependency = mock.GetMock<ITestDependencies>();
using var provider = new SqliteMemoryDatabaseProvider();
var mockDatabase = provider.CreateDatabase<TestEntities>(x => {
    x.TestModels.Add(testRecord);
}, mockedDependency.Object);
await mockDatabase.SaveChangesAsync();
var testClass = mock.CreateInstance<TestClass>();

SQLite Type Converters

By default, SQLite doesn't support certain types, such as DateTimeOffset, Decimal, and TimeSpan. This package automatically sets up converters by these when setting up the SQLite database. You can choose to not utilize these converters by using an overload:

using var provider = new SqliteMemoryDatabaseProvider();
var mockDatabase = provider.CreateDatabase<TestEntities>(false, x =>
    x.TestModels.Add(testRecord);
);

Closing In-Memory Database Connections

Although tests are short lived, it's still best practice to close database connections upon test completion.

SqliteMemoryDatabaseProvider will automatically close its database connections when it's being disposed. The simplest ways to do this are to either set up your provider at the class level or scope the provider in your test to dispose when done by using the using keyword:

Scope Provider

using var provider = new SqliteMemoryDatabaseProvider();

When the test completes, the database connection will automatically be closed and disposed.

Class Level

You can also set up the SqliteMemoryDatabaseProvider at the class level and call its Dispose method during test teardown:

private SqliteMemoryDatabaseProvider sqliteMemoryDatabaseProvider;

[SetUp]
public void SetUp()
{
    sqliteMemoryDatabaseProvider = new SqliteMemoryDatabaseProvider();
}

[TearDown]
public void TearDown()
{
    sqliteMemoryDatabaseProvider.Dispose();
}

Contribute

If you encounter an issue or want to contribute to this package, please visit this package's GitHub page.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SqliteMemoryDatabaseProvider:

Package Downloads
SqliteMemoryDatabaseProvider.AutoMocker

For creating in-memory SQLite databases to be used within unit tests using AutoMocker.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.2.4 509 6/17/2024
7.2.3 376 2/15/2024
7.2.2 131 2/15/2024
7.2.1 314 11/15/2023
7.2.0 142 11/15/2023
7.1.0 370 7/30/2023
7.0.0 456 1/26/2023
6.0.0 369 1/25/2023
1.0.3 368 1/9/2023
1.0.2 381 1/5/2023
1.0.1 386 1/3/2023
1.0.0 385 1/3/2023