Files
git.stella-ops.org/src/BinaryIndex/__Tests/StellaOps.BinaryIndex.Persistence.Tests/BinaryIndexIntegrationFixture.cs

78 lines
2.3 KiB
C#

// -----------------------------------------------------------------------------
// 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;
/// <summary>
/// PostgreSQL integration test fixture for BinaryIndex module.
/// Spins up a real PostgreSQL container and runs schema migrations.
/// </summary>
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();
}
/// <summary>
/// Creates a new database context with the test tenant configured.
/// </summary>
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);
}
/// <summary>
/// Gets the default test tenant ID.
/// </summary>
public string GetTestTenantId() => TestTenantId;
}
/// <summary>
/// Simple tenant context implementation for testing.
/// </summary>
internal sealed class TestTenantContext : ITenantContext
{
public TestTenantContext(string tenantId)
{
TenantId = tenantId;
}
public string TenantId { get; }
}