Merge pull request 'fix: issue #29 — integration tests flakiness (DB split + SqlTestFixture consolidado)' (#34) from fix/issue-29-flakiness into main

This commit was merged in pull request #34.
This commit is contained in:
2026-04-19 10:41:27 +00:00
41 changed files with 228 additions and 514 deletions

View File

@@ -13,8 +13,7 @@ namespace SIGCM2.Application.Tests.Infrastructure.Audit;
[Collection("Database")]
public sealed class AuditEventRepositoryTests : IAsyncLifetime
{
private const string ConnectionString =
"Server=TECNICA3;Database=SIGCM2_Test;User Id=desarrollo;Password=desarrollo2026;TrustServerCertificate=True;";
private const string ConnectionString = TestConnectionStrings.AppTestDb;
private SqlConnection _connection = null!;
private AuditEventRepository _repo = null!;
@@ -130,7 +129,10 @@ public sealed class AuditEventRepositoryTests : IAsyncLifetime
[Fact]
public async Task QueryAsync_Limit_EmitsCursor_WhenMoreRowsAvailable()
{
var t0 = DateTime.UtcNow.AddMinutes(-10);
// Determinístico: DATETIME2(3) + cursor roundtrip via "O" format puede perder ticks
// sub-ms de `DateTime.UtcNow` (observado ~37% flake rate, cursor vuelve como parentesis
// de la página anterior). Timestamp fijo con sub-ms = 0 elimina la ambigüedad.
var t0 = new DateTime(2026, 4, 19, 12, 0, 0, DateTimeKind.Utc);
await Seed(5, t0);
var page1 = await _repo.QueryAsync(new AuditEventFilter(null, null, null, null, null, null, Limit: 2));

View File

@@ -16,8 +16,7 @@ namespace SIGCM2.Application.Tests.Infrastructure.Audit;
[Collection("Database")]
public sealed class AuditJobsTests : IAsyncLifetime
{
private const string ConnectionString =
"Server=TECNICA3;Database=SIGCM2_Test;User Id=desarrollo;Password=desarrollo2026;TrustServerCertificate=True;";
private const string ConnectionString = TestConnectionStrings.AppTestDb;
private SqlConnection _connection = null!;
private SqlConnectionFactory _factory = null!;

View File

@@ -11,8 +11,7 @@ namespace SIGCM2.Application.Tests.Infrastructure.Audit;
[Collection("Database")]
public sealed class SecurityEventRepositoryTests : IAsyncLifetime
{
private const string ConnectionString =
"Server=TECNICA3;Database=SIGCM2_Test;User Id=desarrollo;Password=desarrollo2026;TrustServerCertificate=True;";
private const string ConnectionString = TestConnectionStrings.AppTestDb;
private SqlConnection _connection = null!;
private SecurityEventRepository _repo = null!;

View File

@@ -18,8 +18,7 @@ namespace SIGCM2.Application.Tests.Infrastructure;
[Collection("Database")]
public class IngresosBrutosRepositoryTests : IAsyncLifetime
{
private const string ConnectionString =
"Server=TECNICA3;Database=SIGCM2_Test;User Id=desarrollo;Password=desarrollo2026;TrustServerCertificate=True;";
private const string ConnectionString = TestConnectionStrings.AppTestDb;
private SqlConnection _connection = null!;
private IIngresosBrutosRepository _repo = null!;

View File

@@ -1,103 +1,44 @@
using Dapper;
using Microsoft.Data.SqlClient;
using Respawn;
using SIGCM2.Domain.Entities;
using SIGCM2.Infrastructure.Persistence;
using SIGCM2.TestSupport;
namespace SIGCM2.Application.Tests.Infrastructure;
/// <summary>
/// Integration tests for RefreshTokenRepository against SIGCM2_Test.
/// Uses Respawn to reset the DB between test classes; the repository opens its own
/// Integration tests for RefreshTokenRepository against SIGCM2_Test_App.
/// Uses shared SqlTestFixture via xUnit collection fixture; the repository opens its own
/// connections so transaction-scoped isolation would block on FK locks.
/// </summary>
[Collection("Database")]
public class RefreshTokenRepositoryTests : IAsyncLifetime
{
private const string ConnectionString =
"Server=TECNICA3;Database=SIGCM2_Test;User Id=desarrollo;Password=desarrollo2026;TrustServerCertificate=True;";
private SqlConnection _connection = null!;
private Respawner _respawner = null!;
private readonly SqlTestFixture _db;
private RefreshTokenRepository _repository = null!;
private int _testUserId;
public RefreshTokenRepositoryTests(SqlTestFixture db)
{
_db = db;
}
public async Task InitializeAsync()
{
_connection = new SqlConnection(ConnectionString);
await _connection.OpenAsync();
_respawner = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.SqlServer,
// Rol is a lookup table seeded by migration V003 — never wipe or Usuario FK breaks.
TablesToIgnore =
[
new Respawn.Graph.Table("dbo", "Rol"),
new Respawn.Graph.Table("dbo", "Permiso"),
new Respawn.Graph.Table("dbo", "RolPermiso"),
// UDT-010: *_History tables are system-versioned — engine rejects direct DELETE.
new Respawn.Graph.Table("dbo", "Usuario_History"),
new Respawn.Graph.Table("dbo", "Rol_History"),
new Respawn.Graph.Table("dbo", "Permiso_History"),
new Respawn.Graph.Table("dbo", "RolPermiso_History"),
// ADM-001 (V011): Medio + Seccion are temporal — history tables cannot be directly deleted.
new Respawn.Graph.Table("dbo", "Medio_History"),
new Respawn.Graph.Table("dbo", "Seccion_History"),
// ADM-008 (V013): PuntoDeVenta is temporal; SecuenciaComprobante is NOT temporal (AD8 revisitado).
new Respawn.Graph.Table("dbo", "PuntoDeVenta_History"),
// ADM-009 (V014): TipoDeIva + IngresosBrutos son temporales.
new Respawn.Graph.Table("dbo", "TipoDeIva_History"),
new Respawn.Graph.Table("dbo", "IngresosBrutos_History"),
new Respawn.Graph.Table("dbo", "TipoDeIva"),
new Respawn.Graph.Table("dbo", "IngresosBrutos"),
]
});
await _respawner.ResetAsync(_connection);
await SeedRolCanonicalAsync();
await _db.ResetAndSeedAsync();
await SeedTestUserAsync();
_testUserId = await _connection.QuerySingleAsync<int>(
_testUserId = await _db.Connection.QuerySingleAsync<int>(
"SELECT Id FROM dbo.Usuario WHERE Username = 'test_rt_user'");
var factory = new SqlConnectionFactory(ConnectionString);
var factory = new SqlConnectionFactory(TestConnectionStrings.AppTestDb);
_repository = new RefreshTokenRepository(factory);
}
public async Task DisposeAsync()
{
await _respawner.ResetAsync(_connection);
await _connection.CloseAsync();
await _connection.DisposeAsync();
}
private async Task SeedRolCanonicalAsync()
{
const string sql = """
SET QUOTED_IDENTIFIER ON;
MERGE dbo.Rol AS t
USING (VALUES
('admin', N'Administrador', N'Supervisor total'),
('cajero', N'Cajero', N'Mostrador contado'),
('operador_ctacte', N'Operador Cta Cte', N'Cuenta corriente'),
('picadora', N'Picadora/Correctora', N'Edición de textos'),
('jefe_publicidad', N'Jefe de Publicidad', N'Supervisión de pauta'),
('productor', N'Productor', N'Carga restringida'),
('diagramacion', N'Diagramación/Taller', N'Solo lectura pauta'),
('reportes', N'Reportes', N'Solo lectura reportes')
) AS s (Codigo, Nombre, Descripcion)
ON t.Codigo = s.Codigo
WHEN NOT MATCHED BY TARGET THEN
INSERT (Codigo, Nombre, Descripcion, Activo)
VALUES (s.Codigo, s.Nombre, s.Descripcion, 1);
""";
await _connection.ExecuteAsync(sql);
}
public Task DisposeAsync() => Task.CompletedTask;
private async Task SeedTestUserAsync()
{
await _connection.ExecuteAsync("""
await _db.Connection.ExecuteAsync("""
SET QUOTED_IDENTIFIER ON;
IF NOT EXISTS (SELECT 1 FROM dbo.Usuario WHERE Username = 'test_rt_user')
INSERT INTO dbo.Usuario (Username, PasswordHash, Nombre, Apellido, Rol, PermisosJson, Activo)

View File

@@ -19,8 +19,7 @@ namespace SIGCM2.Application.Tests.Infrastructure;
[Collection("Database")]
public class TipoDeIvaRepositoryTests : IAsyncLifetime
{
private const string ConnectionString =
"Server=TECNICA3;Database=SIGCM2_Test;User Id=desarrollo;Password=desarrollo2026;TrustServerCertificate=True;";
private const string ConnectionString = TestConnectionStrings.AppTestDb;
private SqlConnection _connection = null!;
private ITipoDeIvaRepository _repo = null!;