// ----------------------------------------------------------------------------- // 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; /// /// PostgreSQL integration test fixture for HLC scheduler tests. /// Runs migrations from embedded resources and provides test isolation. /// public sealed class HlcSchedulerPostgresFixture : PostgresIntegrationFixture, ICollectionFixture { 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(); 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); } } /// /// Collection definition for HLC scheduler PostgreSQL integration tests. /// Tests in this collection share a single PostgreSQL container instance. /// [CollectionDefinition(Name)] public sealed class HlcSchedulerPostgresCollection : ICollectionFixture { public const string Name = "HlcSchedulerPostgres"; }