// ----------------------------------------------------------------------------- // BinaryIndexIntegrationFixture.cs // Sprint: SPRINT_20251226_011_BINIDX // Task: BINCAT-18 - Integration tests with Testcontainers PostgreSQL // ----------------------------------------------------------------------------- using System.Reflection; using Dapper; using Npgsql; using StellaOps.BinaryIndex.Core.Services; using StellaOps.Infrastructure.Postgres.Testing; namespace StellaOps.BinaryIndex.Persistence.Tests; /// /// PostgreSQL integration test fixture for BinaryIndex module. /// Spins up a real PostgreSQL container and runs schema migrations. /// public sealed class BinaryIndexIntegrationFixture : PostgresIntegrationFixture { private const string TestTenantId = "00000000-0000-0000-0000-000000000001"; private NpgsqlDataSource? _dataSource; protected override Assembly? GetMigrationAssembly() => typeof(BinaryIndexDbContext).Assembly; protected override string GetModuleName() => "BinaryIndex"; protected override string? GetResourcePrefix() => "StellaOps.BinaryIndex.Persistence.Migrations"; public override async Task InitializeAsync() { await base.InitializeAsync(); _dataSource = NpgsqlDataSource.Create(ConnectionString); } public override async Task DisposeAsync() { if (_dataSource != null) { await _dataSource.DisposeAsync(); } await base.DisposeAsync(); } /// /// Creates a new database context with the test tenant configured. /// public BinaryIndexDbContext CreateDbContext(string? tenantId = null) { if (_dataSource == null) { throw new InvalidOperationException("Fixture not initialized. Call InitializeAsync first."); } var tenant = new TestTenantContext(tenantId ?? TestTenantId); return new BinaryIndexDbContext(_dataSource, tenant); } /// /// Gets the default test tenant ID. /// public string GetTestTenantId() => TestTenantId; } /// /// Simple tenant context implementation for testing. /// internal sealed class TestTenantContext : ITenantContext { public TestTenantContext(string tenantId) { TenantId = tenantId; } public string TenantId { get; } }