Files
Heimdall/tests/Heimdall.IntegrationTests/Factories/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

63 lines
2.0 KiB
C#

using Testcontainers.PostgreSql;
namespace Heimdall.IntegrationTests.Factories;
/// <summary>
/// xUnit collection fixture that manages a PostgreSQL Testcontainer
/// for integration tests. One container per test collection (shared
/// across all test classes in the same collection).
/// 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;
/// <summary>Connection string for the running PostgreSQL instance.</summary>
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();
}
}
/// <summary>
/// Defines the "Postgres" test collection. All test classes in this
/// collection share the same PostgreSQL container via the fixture.
/// </summary>
[CollectionDefinition("Postgres")]
public class PostgresCollectionFixture : ICollectionFixture<PostgreSqlContainerFixture>
{
}