Tests: Add in-memory database for testing

This commit is contained in:
2024-01-05 17:06:58 +01:00
parent 3f6a94e773
commit 2556033a07
9 changed files with 72 additions and 1 deletions

27
Tests/DatabaseSetup.cs Normal file
View File

@ -0,0 +1,27 @@
using Elwig.Helpers;
using Microsoft.Data.Sqlite;
using System.Reflection;
namespace Tests {
[SetUpFixture]
public class DatabaseSetup {
private SqliteConnection? Connection;
[OneTimeSetUp]
public async Task SetupDatabase() {
AppDbContext.ConnectionStringOverride = $"Data Source=ElwigTestDB; Mode=Memory; Foreign Keys=True; Cache=Default";
Connection = await AppDbContext.ConnectAsync();
await AppDbContext.ExecuteEmbeddedScript(Connection, Assembly.GetExecutingAssembly(), "Tests.Resources.Create.sql");
}
[OneTimeTearDown]
public async Task TeardownDatabase() {
AppDbContext.ConnectionStringOverride = null;
if (Connection == null) return;
await Connection.DisposeAsync();
Connection = null;
// The in-memory database will be dropped if all connections to it are closed
}
}
}