Files
git.stella-ops.org/src/Scheduler/__Tests/StellaOps.Scheduler.Queue.Tests/HlcSchedulerPostgresFixture.cs
2026-01-06 19:07:48 +02:00

73 lines
2.8 KiB
C#

// -----------------------------------------------------------------------------
// HlcSchedulerPostgresFixture.cs
// Sprint: SPRINT_20260105_002_002_SCHEDULER_hlc_queue_chain
// Task: SQC-017 - Integration tests for HLC scheduler services
// -----------------------------------------------------------------------------
using System.Reflection;
using Npgsql;
using StellaOps.Infrastructure.Postgres.Testing;
using StellaOps.Scheduler.Persistence.Postgres;
using Xunit;
namespace StellaOps.Scheduler.Queue.Tests;
/// <summary>
/// PostgreSQL integration test fixture for HLC scheduler tests.
/// Runs migrations from embedded resources and provides test isolation.
/// </summary>
public sealed class HlcSchedulerPostgresFixture : PostgresIntegrationFixture, ICollectionFixture<HlcSchedulerPostgresFixture>
{
protected override Assembly? GetMigrationAssembly()
=> typeof(SchedulerDataSource).Assembly;
protected override string GetModuleName() => "Scheduler";
public new async Task TruncateAllTablesAsync(CancellationToken cancellationToken = default)
{
// Base fixture truncates the randomly-generated test schema
await Fixture.TruncateAllTablesAsync(cancellationToken).ConfigureAwait(false);
// Scheduler migrations create the canonical `scheduler.*` schema explicitly
await using var connection = new NpgsqlConnection(ConnectionString);
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
const string listTablesSql = """
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'scheduler'
AND table_type = 'BASE TABLE';
""";
var tables = new List<string>();
await using (var command = new NpgsqlCommand(listTablesSql, connection))
await using (var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false))
{
while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
{
tables.Add(reader.GetString(0));
}
}
if (tables.Count == 0)
{
return;
}
var qualified = tables.Select(static t => $"scheduler.\"{t}\"");
var truncateSql = $"TRUNCATE TABLE {string.Join(", ", qualified)} RESTART IDENTITY CASCADE;";
await using var truncateCommand = new NpgsqlCommand(truncateSql, connection);
await truncateCommand.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Collection definition for HLC scheduler PostgreSQL integration tests.
/// Tests in this collection share a single PostgreSQL container instance.
/// </summary>
[CollectionDefinition(Name)]
public sealed class HlcSchedulerPostgresCollection : ICollectionFixture<HlcSchedulerPostgresFixture>
{
public const string Name = "HlcSchedulerPostgres";
}