using Marathon.Infrastructure.Persistence;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace Marathon.Infrastructure.Tests.Persistence;
///
/// Shared in-memory SQLite fixture for persistence tests.
/// Uses a named in-memory database with a shared cache connection so the schema
/// created in EnsureCreated() is visible across the lifetime of each test.
///
public sealed class InMemoryDbFixture : IDisposable
{
private readonly SqliteConnection _keepAliveConnection;
public MarathonDbContext DbContext { get; }
public InMemoryDbFixture()
{
// Keep a single connection open so the in-memory DB is not dropped between
// DbContext operations. Cache=Shared ensures the same DB is reused.
_keepAliveConnection = new SqliteConnection("Data Source=marathon_tests;Mode=Memory;Cache=Shared");
_keepAliveConnection.Open();
var options = new DbContextOptionsBuilder()
.UseSqlite("Data Source=marathon_tests;Mode=Memory;Cache=Shared")
.Options;
DbContext = new MarathonDbContext(options);
DbContext.Database.EnsureCreated();
}
public void Dispose()
{
DbContext.Dispose();
_keepAliveConnection.Dispose();
}
}