feat(infra): MedioRepository + SeccionRepository + integration tests — ADM-001 B5
This commit is contained in:
262
tests/SIGCM2.Application.Tests/Medios/MedioRepositoryTests.cs
Normal file
262
tests/SIGCM2.Application.Tests/Medios/MedioRepositoryTests.cs
Normal file
@@ -0,0 +1,262 @@
|
||||
using Dapper;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Respawn;
|
||||
using SIGCM2.Domain.Entities;
|
||||
using SIGCM2.Infrastructure.Persistence;
|
||||
|
||||
namespace SIGCM2.Application.Tests.Medios;
|
||||
|
||||
/// <summary>
|
||||
/// Integration tests for MedioRepository against SIGCM2_Test.
|
||||
/// TDD: RED written before implementation, GREEN after MedioRepository was created.
|
||||
/// Temporal: after UpdateAsync, dbo.Medio_History MUST have ≥1 row for that Id.
|
||||
/// </summary>
|
||||
[Collection("Database")]
|
||||
public class MedioRepositoryTests : 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 MedioRepository _repository = null!;
|
||||
|
||||
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 MedioRepository(factory);
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
await _connection.CloseAsync();
|
||||
await _connection.DisposeAsync();
|
||||
}
|
||||
|
||||
// ── AddAsync + GetByIdAsync roundtrip ─────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task AddAsync_ThenGetById_ReturnsAllColumns()
|
||||
{
|
||||
var medio = Medio.ForCreation("DIARIO01", "Diario Uno", TipoMedio.Diario, null);
|
||||
|
||||
var id = await _repository.AddAsync(medio);
|
||||
var result = await _repository.GetByIdAsync(id);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(id, result!.Id);
|
||||
Assert.Equal("DIARIO01", result.Codigo);
|
||||
Assert.Equal("Diario Uno", result.Nombre);
|
||||
Assert.Equal(TipoMedio.Diario, result.Tipo);
|
||||
Assert.Null(result.PlataformaEmpresaId);
|
||||
Assert.True(result.Activo);
|
||||
Assert.True(result.FechaCreacion > DateTime.MinValue);
|
||||
Assert.Null(result.FechaModificacion);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddAsync_WithPlataformaEmpresaId_PersistsValue()
|
||||
{
|
||||
var medio = Medio.ForCreation("RADIO99", "Radio Test", TipoMedio.Radio, 42);
|
||||
|
||||
var id = await _repository.AddAsync(medio);
|
||||
var result = await _repository.GetByIdAsync(id);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(42, result!.PlataformaEmpresaId);
|
||||
Assert.Equal(TipoMedio.Radio, result.Tipo);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_NonExistent_ReturnsNull()
|
||||
{
|
||||
var result = await _repository.GetByIdAsync(999999);
|
||||
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
// ── ExistsByCodigoAsync ───────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task ExistsByCodigoAsync_AfterAdd_ReturnsTrue()
|
||||
{
|
||||
var medio = Medio.ForCreation("EXIST01", "Existe", TipoMedio.Web, null);
|
||||
await _repository.AddAsync(medio);
|
||||
|
||||
var exists = await _repository.ExistsByCodigoAsync("EXIST01");
|
||||
|
||||
Assert.True(exists);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistsByCodigoAsync_NotAdded_ReturnsFalse()
|
||||
{
|
||||
var exists = await _repository.ExistsByCodigoAsync("NOEXISTE_XYZ");
|
||||
|
||||
Assert.False(exists);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistsByCodigoAsync_IsCaseSensitive_MatchesAsStored()
|
||||
{
|
||||
var medio = Medio.ForCreation("UPPER01", "Upper Medio", TipoMedio.Diario, null);
|
||||
await _repository.AddAsync(medio);
|
||||
|
||||
// Stored as 'UPPER01'; searching lowercase should not match (SQL_Latin1 collation is CI
|
||||
// on most default installs, but the contract is: match as stored).
|
||||
var exactMatch = await _repository.ExistsByCodigoAsync("UPPER01");
|
||||
Assert.True(exactMatch);
|
||||
}
|
||||
|
||||
// ── UpdateAsync + Temporal ────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_ThenQuery_ReflectsNewValues()
|
||||
{
|
||||
var id = await _repository.AddAsync(Medio.ForCreation("UPD01", "Original", TipoMedio.Diario, null));
|
||||
var original = await _repository.GetByIdAsync(id);
|
||||
|
||||
var updated = original!.WithUpdatedProfile("Actualizado", TipoMedio.Radio, 7);
|
||||
await _repository.UpdateAsync(updated);
|
||||
|
||||
var result = await _repository.GetByIdAsync(id);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("Actualizado", result!.Nombre);
|
||||
Assert.Equal(TipoMedio.Radio, result.Tipo);
|
||||
Assert.Equal(7, result.PlataformaEmpresaId);
|
||||
Assert.NotNull(result.FechaModificacion);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_ProducesHistoryRow()
|
||||
{
|
||||
// Temporal: SQL Server automatically writes the previous row version to Medio_History on UPDATE.
|
||||
var id = await _repository.AddAsync(Medio.ForCreation("HIST01", "Historial", TipoMedio.Diario, null));
|
||||
var original = await _repository.GetByIdAsync(id);
|
||||
|
||||
var updated = original!.WithUpdatedProfile("Historial v2", TipoMedio.Web, null);
|
||||
await _repository.UpdateAsync(updated);
|
||||
|
||||
var historyCount = await _connection.ExecuteScalarAsync<int>(
|
||||
"SELECT COUNT(*) FROM dbo.Medio_History WHERE Id = @Id", new { Id = id });
|
||||
|
||||
Assert.True(historyCount >= 1, $"Expected ≥1 history row for Medio Id={id}, got {historyCount}");
|
||||
}
|
||||
|
||||
// ── GetPagedAsync ─────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task GetPagedAsync_FilterByActivo_ReturnsOnlyActiveWhenTrue()
|
||||
{
|
||||
var idActivo = await _repository.AddAsync(Medio.ForCreation("ACTV01", "Activo", TipoMedio.Diario, null));
|
||||
var idInact = await _repository.AddAsync(Medio.ForCreation("INACT01", "Inactivo", TipoMedio.Diario, null));
|
||||
|
||||
// Deactivate second medio
|
||||
var inact = await _repository.GetByIdAsync(idInact);
|
||||
await _repository.UpdateAsync(inact!.WithActivo(false));
|
||||
|
||||
var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 50, Activo: true, Tipo: null, Search: null));
|
||||
|
||||
var codigos = result.Items.Select(m => m.Codigo).ToHashSet();
|
||||
Assert.Contains("ACTV01", codigos);
|
||||
Assert.DoesNotContain("INACT01", codigos);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPagedAsync_FilterByTipo_ReturnsOnlyMatchingTipo()
|
||||
{
|
||||
await _repository.AddAsync(Medio.ForCreation("RADIO01", "Radio Uno", TipoMedio.Radio, null));
|
||||
await _repository.AddAsync(Medio.ForCreation("WEB01", "Web Uno", TipoMedio.Web, null));
|
||||
|
||||
var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 50, Activo: null, Tipo: TipoMedio.Radio, Search: null));
|
||||
|
||||
Assert.All(result.Items, m => Assert.Equal(TipoMedio.Radio, m.Tipo));
|
||||
Assert.Contains(result.Items, m => m.Codigo == "RADIO01");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPagedAsync_SearchByNombre_ReturnsMatches()
|
||||
{
|
||||
await _repository.AddAsync(Medio.ForCreation("SRCH01", "Buscable Nombre", TipoMedio.Diario, null));
|
||||
await _repository.AddAsync(Medio.ForCreation("SRCH02", "Otro Medio", TipoMedio.Diario, null));
|
||||
|
||||
var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 50, Activo: null, Tipo: null, Search: "Buscable"));
|
||||
|
||||
Assert.Single(result.Items, m => m.Codigo == "SRCH01");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPagedAsync_PaginationClamping_PageSizeClampedTo1Min()
|
||||
{
|
||||
await _repository.AddAsync(Medio.ForCreation("PAG01", "Paginacion", TipoMedio.Diario, null));
|
||||
|
||||
// pageSize=0 should clamp to 1
|
||||
var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 0, Activo: null, Tipo: null, Search: null));
|
||||
|
||||
Assert.Equal(1, result.PageSize);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPagedAsync_TotalCount_ReflectsAllMatchingRows()
|
||||
{
|
||||
await _repository.AddAsync(Medio.ForCreation("CNT01", "Contador 1", TipoMedio.Diario, null));
|
||||
await _repository.AddAsync(Medio.ForCreation("CNT02", "Contador 2", TipoMedio.Diario, null));
|
||||
await _repository.AddAsync(Medio.ForCreation("CNT03", "Contador 3", TipoMedio.Diario, null));
|
||||
|
||||
var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 2, Activo: null, Tipo: null, Search: "Contador"));
|
||||
|
||||
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