using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using NovelSoftware.Infrastructure.Persistence;
namespace NovelSoftware.Tests;
///
/// A throwaway SQLite database held in memory. Using real SQLite rather than the
/// in-memory provider means the tests exercise the same relational behaviour the app
/// ships with — cascade deletes, foreign keys and all.
///
public sealed class TestDatabase : IDisposable
{
private readonly SqliteConnection _connection;
public TestDatabase()
{
_connection = new SqliteConnection("Data Source=:memory:");
_connection.Open();
Context = CreateContext();
Context.Database.EnsureCreated();
}
public NovelDbContext Context { get; }
/// A second context over the same database, for asserting on persisted state.
public NovelDbContext CreateContext() =>
new(new DbContextOptionsBuilder().UseSqlite(_connection).Options);
public void Dispose()
{
Context.Dispose();
_connection.Dispose();
}
}