using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Musify.Application.Contracts;
using Musify.Domain.Entities;
namespace Musify.Application.Tests.TestSupport;
///
/// An backed by a private, in-memory SQLite database.
///
///
///
/// exposes raw properties, so handlers
/// compose real LINQ queries (Where, Select, Include, ...) against it.
/// Substituting with a mock cannot express that — there is no
/// provider behind a mock. SQLite's in-memory mode gives us a real
/// relational engine instead: fast, no external dependency, and — unlike the EF Core InMemory
/// provider — it actually supports transactions, which two handlers rely on.
///
///
/// The model mirrors the parts of Musify.Infrastructure.Persistence.Database that affect
/// query shape (owned types, table-per-type inheritance, unique indexes). MassTransit outbox/saga
/// state and Postgres-specific configuration are left out — the Application layer never touches
/// them.
///
///
public sealed class TestDatabase : DbContext, IDatabase
{
private readonly SqliteConnection connection;
private TestDatabase(SqliteConnection connection, DbContextOptions options)
: base(options)
{
this.connection = connection;
}
///
/// Creates a fresh, empty database with its own private connection. Call
/// (via ) to populate it, then
/// when the test is done.
///
public static async Task CreateAsync()
{
var connection = new SqliteConnection("Filename=:memory:");
await connection.OpenAsync();
var options = new DbContextOptionsBuilder()
.UseSqlite(connection)
.Options;
var database = new TestDatabase(connection, options);
await database.Database.EnsureCreatedAsync();
return database;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity