Files
Heimdall/tests/Heimdall.Infrastructure.Tests/PostgreSqlContainerFixture.cs
T
Arnaud Dartois 53e0d4c199 fix: resolve CI test failures after InMemory->Testcontainers migration
Three issues fixed:
- Camera.AssignAgent(Guid.Empty) violated FK constraint; changed to
  nullable Guid? and pass null on camera release
- Test fixtures now check DATABASE_URL env var first, falling back to
  Testcontainers locally (CI has no Docker socket)
- Added database truncation before each test via IAsyncLifetime to
  prevent shared state between test methods (InMemory was fresh per test,
  real PostgreSQL persists data)
2026-07-21 17:46:32 +00:00

57 lines
1.7 KiB
C#

using Testcontainers.PostgreSql;
namespace Heimdall.Infrastructure.Tests;
/// <summary>
/// xUnit collection fixture that manages a PostgreSQL Testcontainer.
/// One container per test collection — shared across all test classes.
/// In CI mode (DATABASE_URL env var), uses the CI-provided PostgreSQL service
/// instead of starting a Testcontainer.
/// </summary>
public class PostgreSqlContainerFixture : IAsyncLifetime
{
private readonly PostgreSqlContainer? _container;
private readonly string _connectionString;
public string ConnectionString => _connectionString;
public PostgreSqlContainerFixture()
{
var envUrl = Environment.GetEnvironmentVariable("DATABASE_URL");
if (!string.IsNullOrEmpty(envUrl))
{
// CI mode — use the CI-provided PostgreSQL service
_connectionString = envUrl;
_container = null;
}
else
{
// Local dev — start a Testcontainer
_container = new PostgreSqlBuilder("postgres:17-alpine")
.WithDatabase("heimdall_test")
.WithUsername("heimdall")
.WithPassword("heimdall_test_pwd")
.WithCleanUp(true)
.Build();
_connectionString = _container.GetConnectionString();
}
}
public async Task InitializeAsync()
{
if (_container is not null)
await _container.StartAsync();
}
public async Task DisposeAsync()
{
if (_container is not null)
await _container.DisposeAsync();
}
}
[CollectionDefinition("PostgresInfra")]
public class PostgresInfraFixture : ICollectionFixture<PostgreSqlContainerFixture>
{
}