feat(infra): MedioRepository + SeccionRepository + integration tests — ADM-001 B5
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
using Dapper;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Respawn;
|
||||
using SIGCM2.Domain.Entities;
|
||||
using SIGCM2.Infrastructure.Persistence;
|
||||
|
||||
namespace SIGCM2.Application.Tests.Secciones;
|
||||
|
||||
/// <summary>
|
||||
/// Integration tests for SeccionRepository against SIGCM2_Test.
|
||||
/// TDD: RED written before implementation, GREEN after SeccionRepository was created.
|
||||
/// Temporal: after UpdateAsync, dbo.Seccion_History MUST have ≥1 row for that Id.
|
||||
/// </summary>
|
||||
[Collection("Database")]
|
||||
public class SeccionRepositoryTests : 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 SeccionRepository _repository = null!;
|
||||
private MedioRepository _medioRepository = null!;
|
||||
private int _medioId;
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
_connection = new SqlConnection(ConnectionString);
|
||||
await _connection.OpenAsync();
|
||||
|
||||
_respawner = await Respawner.CreateAsync(_connection, new RespawnerOptions
|
||||
{
|
||||
DbAdapter = DbAdapter.SqlServer,
|
||||
TablesToIgnore =
|
||||
[
|
||||
new Respawn.Graph.Table("dbo", "Rol"),
|
||||
new Respawn.Graph.Table("dbo", "Permiso"),
|
||||
new Respawn.Graph.Table("dbo", "RolPermiso"),
|
||||
// *_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"),
|
||||
]
|
||||
});
|
||||
|
||||
await _respawner.ResetAsync(_connection);
|
||||
await SeedRolCanonicalAsync();
|
||||
|
||||
var factory = new SqlConnectionFactory(ConnectionString);
|
||||
_repository = new SeccionRepository(factory);
|
||||
_medioRepository = new MedioRepository(factory);
|
||||
|
||||
// Seed a canonical Medio for FK-valid Seccion tests.
|
||||
_medioId = await _medioRepository.AddAsync(Medio.ForCreation("TESTMEDIO", "Medio de Prueba", TipoMedio.Diario, null));
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
await _connection.CloseAsync();
|
||||
await _connection.DisposeAsync();
|
||||
}
|
||||
|
||||
// ── AddAsync + GetByIdAsync roundtrip ─────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task AddAsync_ThenGetById_ReturnsAllColumns()
|
||||
{
|
||||
var seccion = Seccion.ForCreation(_medioId, "SEC01", "Sección Uno", "clasificados");
|
||||
|
||||
var id = await _repository.AddAsync(seccion);
|
||||
var result = await _repository.GetByIdAsync(id);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(id, result!.Id);
|
||||
Assert.Equal(_medioId, result.MedioId);
|
||||
Assert.Equal("SEC01", result.Codigo);
|
||||
Assert.Equal("Sección Uno", result.Nombre);
|
||||
Assert.Equal("clasificados", result.Tipo);
|
||||
Assert.True(result.Activo);
|
||||
Assert.True(result.FechaCreacion > DateTime.MinValue);
|
||||
Assert.Null(result.FechaModificacion);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_NonExistent_ReturnsNull()
|
||||
{
|
||||
var result = await _repository.GetByIdAsync(999999);
|
||||
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
// ── FK violation ──────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task AddAsync_WithInvalidMedioId_ThrowsSqlException()
|
||||
{
|
||||
var seccion = Seccion.ForCreation(99999, "FKERR01", "FK Error", "notables");
|
||||
|
||||
await Assert.ThrowsAsync<Microsoft.Data.SqlClient.SqlException>(
|
||||
() => _repository.AddAsync(seccion));
|
||||
}
|
||||
|
||||
// ── ExistsByCodigoInMedioAsync ─────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task ExistsByCodigoInMedioAsync_AfterAdd_ReturnsTrue()
|
||||
{
|
||||
await _repository.AddAsync(Seccion.ForCreation(_medioId, "EXIST01", "Existe", "clasificados"));
|
||||
|
||||
var exists = await _repository.ExistsByCodigoInMedioAsync(_medioId, "EXIST01");
|
||||
|
||||
Assert.True(exists);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistsByCodigoInMedioAsync_NotAdded_ReturnsFalse()
|
||||
{
|
||||
var exists = await _repository.ExistsByCodigoInMedioAsync(_medioId, "NOEXISTE_XYZ");
|
||||
|
||||
Assert.False(exists);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistsByCodigoInMedioAsync_SameCodigoDifferentMedio_ReturnsFalse()
|
||||
{
|
||||
// Seccion with same Codigo exists for _medioId, but NOT for a different medioId.
|
||||
await _repository.AddAsync(Seccion.ForCreation(_medioId, "SHARED01", "Shared Codigo", "suplementos"));
|
||||
|
||||
var otherMedioId = await _medioRepository.AddAsync(Medio.ForCreation("OTRO01", "Otro Medio", TipoMedio.Radio, null));
|
||||
var exists = await _repository.ExistsByCodigoInMedioAsync(otherMedioId, "SHARED01");
|
||||
|
||||
Assert.False(exists);
|
||||
}
|
||||
|
||||
// ── UpdateAsync + Temporal ────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_ThenQuery_ReflectsNewValues()
|
||||
{
|
||||
var id = await _repository.AddAsync(Seccion.ForCreation(_medioId, "UPD01", "Original", "clasificados"));
|
||||
var original = await _repository.GetByIdAsync(id);
|
||||
|
||||
var updated = original!.WithUpdatedProfile("Actualizado", "notables");
|
||||
await _repository.UpdateAsync(updated);
|
||||
|
||||
var result = await _repository.GetByIdAsync(id);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("Actualizado", result!.Nombre);
|
||||
Assert.Equal("notables", result.Tipo);
|
||||
Assert.NotNull(result.FechaModificacion);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_ProducesHistoryRow()
|
||||
{
|
||||
// Temporal: SQL Server automatically writes the previous row version to Seccion_History on UPDATE.
|
||||
var id = await _repository.AddAsync(Seccion.ForCreation(_medioId, "HIST01", "Historial", "clasificados"));
|
||||
var original = await _repository.GetByIdAsync(id);
|
||||
|
||||
var updated = original!.WithUpdatedProfile("Historial v2", "suplementos");
|
||||
await _repository.UpdateAsync(updated);
|
||||
|
||||
var historyCount = await _connection.ExecuteScalarAsync<int>(
|
||||
"SELECT COUNT(*) FROM dbo.Seccion_History WHERE Id = @Id", new { Id = id });
|
||||
|
||||
Assert.True(historyCount >= 1, $"Expected ≥1 history row for Seccion Id={id}, got {historyCount}");
|
||||
}
|
||||
|
||||
// ── GetPagedAsync ─────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task GetPagedAsync_FilterByMedioId_ReturnsOnlySecciones_OfThatMedio()
|
||||
{
|
||||
var otherMedioId = await _medioRepository.AddAsync(Medio.ForCreation("OTHER02", "Otro Medio 2", TipoMedio.Radio, null));
|
||||
|
||||
await _repository.AddAsync(Seccion.ForCreation(_medioId, "M1S01", "M1 Sec 1", "clasificados"));
|
||||
await _repository.AddAsync(Seccion.ForCreation(otherMedioId, "M2S01", "M2 Sec 1", "notables"));
|
||||
|
||||
var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 50, MedioId: _medioId, Tipo: null, Activo: null, Search: null));
|
||||
|
||||
Assert.All(result.Items, s => Assert.Equal(_medioId, s.MedioId));
|
||||
Assert.Contains(result.Items, s => s.Codigo == "M1S01");
|
||||
Assert.DoesNotContain(result.Items, s => s.Codigo == "M2S01");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPagedAsync_FilterByTipo_ReturnsOnlyMatchingTipo()
|
||||
{
|
||||
await _repository.AddAsync(Seccion.ForCreation(_medioId, "CL01", "Clasificados", "clasificados"));
|
||||
await _repository.AddAsync(Seccion.ForCreation(_medioId, "NT01", "Notables", "notables"));
|
||||
|
||||
var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 50, MedioId: null, Tipo: "clasificados", Activo: null, Search: null));
|
||||
|
||||
Assert.All(result.Items, s => Assert.Equal("clasificados", s.Tipo));
|
||||
Assert.Contains(result.Items, s => s.Codigo == "CL01");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPagedAsync_FilterByActivo_ReturnsOnlyActive()
|
||||
{
|
||||
await _repository.AddAsync(Seccion.ForCreation(_medioId, "ACTV01", "Activa", "clasificados"));
|
||||
var inactId = await _repository.AddAsync(Seccion.ForCreation(_medioId, "INACT01", "Inactiva", "clasificados"));
|
||||
|
||||
var inact = await _repository.GetByIdAsync(inactId);
|
||||
await _repository.UpdateAsync(inact!.WithActivo(false));
|
||||
|
||||
var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 50, MedioId: _medioId, Tipo: null, Activo: true, Search: null));
|
||||
|
||||
var codigos = result.Items.Select(s => s.Codigo).ToHashSet();
|
||||
Assert.Contains("ACTV01", codigos);
|
||||
Assert.DoesNotContain("INACT01", codigos);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPagedAsync_TotalCount_ReflectsAllMatchingRows()
|
||||
{
|
||||
await _repository.AddAsync(Seccion.ForCreation(_medioId, "P01", "Page 1", "suplementos"));
|
||||
await _repository.AddAsync(Seccion.ForCreation(_medioId, "P02", "Page 2", "suplementos"));
|
||||
await _repository.AddAsync(Seccion.ForCreation(_medioId, "P03", "Page 3", "suplementos"));
|
||||
|
||||
var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 2, MedioId: _medioId, Tipo: "suplementos", Activo: null, Search: null));
|
||||
|
||||
Assert.Equal(3, result.Total);
|
||||
Assert.Equal(2, result.Items.Count);
|
||||
}
|
||||
|
||||
// ── helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user