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)
This commit is contained in:
1 parent
58a3ce5b1b
commit
53e0d4c199
7 files changed
+127
-25
No files matched your search
@@ -1,4 +1,7 @@
|
||||
using Heimdall.Infrastructure.Data;
|
||||
using Heimdall.Infrastructure.Data.Interceptors;
|
||||
using Heimdall.IntegrationTests.Factories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Heimdall.IntegrationTests.Controllers;
|
||||
|
||||
@@ -6,26 +9,53 @@ namespace Heimdall.IntegrationTests.Controllers;
|
||||
/// Base class for integration tests that need a real PostgreSQL database
|
||||
/// via Testcontainers. All test classes in the "Postgres" collection share
|
||||
/// the same PostgreSQL container.
|
||||
/// Resets the database before each test to guarantee isolation.
|
||||
/// </summary>
|
||||
[Collection("Postgres")]
|
||||
public abstract class PostgresTestBase : IAsyncLifetime
|
||||
{
|
||||
private readonly Lazy<HeimdallWebApplicationFactory> _factoryLazy;
|
||||
private readonly string _connectionString;
|
||||
|
||||
protected PostgresTestBase(PostgreSqlContainerFixture postgres)
|
||||
{
|
||||
_connectionString = postgres.ConnectionString;
|
||||
_factoryLazy = new Lazy<HeimdallWebApplicationFactory>(
|
||||
() => new HeimdallWebApplicationFactory(postgres.ConnectionString));
|
||||
}
|
||||
|
||||
/// <summary>HTTP client factory connected to the real PostgreSQL Testcontainer.</summary>
|
||||
/// <summary>HTTP client factory connected to the real PostgreSQL Database.</summary>
|
||||
protected HeimdallWebApplicationFactory Factory => _factoryLazy.Value;
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
await ResetDatabaseAsync();
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
if (_factoryLazy.IsValueCreated)
|
||||
await _factoryLazy.Value.DisposeAsync();
|
||||
}
|
||||
|
||||
private async Task ResetDatabaseAsync()
|
||||
{
|
||||
var opts = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseNpgsql(_connectionString)
|
||||
.Options;
|
||||
|
||||
await using var db = new AppDbContext(opts, new AuditInterceptor());
|
||||
var tables = await db.Database.SqlQuery<string>(
|
||||
$"""SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE' AND table_name != '__EFMigrationsHistory'""")
|
||||
.ToListAsync();
|
||||
|
||||
if (tables.Count > 0)
|
||||
{
|
||||
// Table names from information_schema (not user input) — safe to embed directly
|
||||
var joined = string.Join(", ", tables.Select(t => "\"" + t + "\""));
|
||||
#pragma warning disable EF1002, EF1003
|
||||
await db.Database.ExecuteSqlRawAsync("TRUNCATE TABLE " + joined + " CASCADE");
|
||||
#pragma warning restore EF1002, EF1003
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user