Files
Heimdall/tests/Heimdall.IntegrationTests/Controllers/PostgresTestBase.cs
T
arnaud.dartois 636154572b
Heimdall Gitea / Detect CI scope (push) Successful in 6s
Heimdall Gitea / Build and tests (push) Successful in 19m19s
Heimdall Gitea / Publish versioned release (push) Skipped
Heimdall Gitea / Build and scan server image (push) Successful in 6m13s
Heimdall Gitea / Build and scan agent image (push) Successful in 14m44s
Heimdall Gitea / Publish Docker Hub latest tags (push) Successful in 1m48s
Heimdall Gitea / Build and scan NVIDIA Agent image (push) Successful in 19m9s
Heimdall Gitea / CI policy (push) Successful in 10s
Complete remaining Heimdall features and acceptance
Merge pull request #138

Co-authored-by: Arnaud Dartois <admin@tordais.cc>
2026-11-06 11:14:49 +01:00

69 lines
2.5 KiB
C#

using Heimdall.Infrastructure.Data;
using Heimdall.Infrastructure.Data.Interceptors;
using Heimdall.IntegrationTests.Factories;
using Heimdall.TestCommon;
using Microsoft.EntityFrameworkCore;
namespace Heimdall.IntegrationTests.Controllers;
/// <summary>
/// Base class for integration tests that need a real PostgreSQL database
/// via Testcontainers. All test classes in the "Postgres" collection share
/// the same PostgreSQL database.
/// Resets the database before each test to guarantee isolation.
/// If Docker/PostgreSQL is unavailable, all tests skip gracefully instead of failing.
/// </summary>
[Collection("Postgres")]
public abstract class PostgresTestBase : IAsyncLifetime
{
private readonly PostgreSqlContainerFixture _postgres;
private readonly Lazy<HeimdallWebApplicationFactory> _factoryLazy;
private readonly string _connectionString;
protected PostgresTestBase(PostgreSqlContainerFixture postgres)
{
_postgres = postgres;
_connectionString = postgres.IsAvailable ? postgres.ConnectionString : string.Empty;
_factoryLazy = new Lazy<HeimdallWebApplicationFactory>(
() => new HeimdallWebApplicationFactory(postgres.ConnectionString));
}
/// <summary>HTTP client factory connected to the real PostgreSQL Database.</summary>
protected HeimdallWebApplicationFactory Factory => _factoryLazy.Value;
public virtual async Task InitializeAsync()
{
if (!_postgres.IsAvailable)
return;
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
}
}
}