Tests: Add in-memory database for testing
This commit is contained in:
27
Tests/DatabaseSetup.cs
Normal file
27
Tests/DatabaseSetup.cs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user