feat(application): repository abstraction + DTOs + validators + handlers CRUD PuntosDeVenta con auditoría + retry deadlock

This commit is contained in:
2026-04-17 12:28:11 -03:00
parent 43877bd4a1
commit 50f6f2b67a
36 changed files with 1296 additions and 0 deletions

View File

@@ -0,0 +1,122 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.PuntosDeVenta.Create;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.PuntosDeVenta.Create;
public class CreatePuntoDeVentaCommandHandlerTests
{
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly CreatePuntoDeVentaCommandHandler _handler;
private static Medio MakeMedio(int id = 5, bool activo = true)
=> new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
private static CreatePuntoDeVentaCommand ValidCommand() => new(
MedioId: 5,
NumeroAFIP: 1,
Nombre: "PdV Central",
Descripcion: null);
public CreatePuntoDeVentaCommandHandlerTests()
{
_handler = new CreatePuntoDeVentaCommandHandler(_repo, _medioRepo, _audit);
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5));
_repo.ExistsByNumeroAFIPInMedioAsync(Arg.Any<int>(), Arg.Any<short>(), Arg.Any<int?>(), Arg.Any<CancellationToken>()).Returns(false);
_repo.AddAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>()).Returns(10);
}
// ── medio not found → throws ─────────────────────────────────────────────
[Fact]
public async Task Handle_MedioNotFound_ThrowsMedioNotFoundException()
{
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns((Medio?)null);
await Assert.ThrowsAsync<MedioNotFoundException>(
() => _handler.Handle(ValidCommand()));
}
// ── medio inactivo → throws ──────────────────────────────────────────────
[Fact]
public async Task Handle_MedioInactivo_ThrowsMedioInactivoException()
{
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
await Assert.ThrowsAsync<MedioInactivoException>(
() => _handler.Handle(ValidCommand()));
}
// ── NumeroAFIP duplicado → throws ────────────────────────────────────────
[Fact]
public async Task Handle_NumeroAFIPDuplicado_ThrowsNumeroAFIPDuplicadoException()
{
_repo.ExistsByNumeroAFIPInMedioAsync(5, 1, null, Arg.Any<CancellationToken>()).Returns(true);
await Assert.ThrowsAsync<NumeroAFIPDuplicadoException>(
() => _handler.Handle(ValidCommand()));
}
[Fact]
public async Task Handle_NumeroAFIPDuplicado_DoesNotCallAddAsync()
{
_repo.ExistsByNumeroAFIPInMedioAsync(Arg.Any<int>(), Arg.Any<short>(), Arg.Any<int?>(), Arg.Any<CancellationToken>()).Returns(true);
try { await _handler.Handle(ValidCommand()); } catch (NumeroAFIPDuplicadoException) { }
await _repo.DidNotReceive().AddAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>());
}
// ── happy path ───────────────────────────────────────────────────────────
[Fact]
public async Task Handle_HappyPath_ReturnsDtoWithIdFromRepository()
{
var result = await _handler.Handle(ValidCommand());
Assert.Equal(10, result.Id);
}
[Fact]
public async Task Handle_HappyPath_DtoContainsCorrectFields()
{
var result = await _handler.Handle(ValidCommand());
Assert.Equal(5, result.MedioId);
Assert.Equal(1, result.NumeroAFIP);
Assert.Equal("PdV Central", result.Nombre);
Assert.True(result.Activo);
}
[Fact]
public async Task Handle_HappyPath_CallsAuditWithCreateAction()
{
await _handler.Handle(ValidCommand());
await _audit.Received(1).LogAsync(
action: "punto_de_venta.create",
targetType: "PuntoDeVenta",
targetId: "10",
metadata: Arg.Any<object?>(),
ct: Arg.Any<CancellationToken>());
}
// ── audit fail-closed ────────────────────────────────────────────────────
[Fact]
public async Task Handle_AuditLoggerThrows_ExceptionBubblesUpAndAddNotCommitted()
{
_audit.LogAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<object?>(), Arg.Any<CancellationToken>())
.Returns(Task.FromException(new InvalidOperationException("audit fail")));
await Assert.ThrowsAsync<InvalidOperationException>(
() => _handler.Handle(ValidCommand()));
}
}

View File

@@ -0,0 +1,86 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.PuntosDeVenta.Deactivate;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.PuntosDeVenta.Deactivate;
public class DeactivatePuntoDeVentaCommandHandlerTests
{
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly DeactivatePuntoDeVentaCommandHandler _handler;
private static PuntoDeVenta MakePdv(int id = 10, int medioId = 5, bool activo = true)
=> new(id, medioId, 1, "PdV Test", null, activo, DateTime.UtcNow, null);
private static Medio MakeMedio(int id = 5, bool activo = true)
=> new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
public DeactivatePuntoDeVentaCommandHandlerTests()
{
_handler = new DeactivatePuntoDeVentaCommandHandler(_repo, _medioRepo, _audit);
_medioRepo.GetByIdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(MakeMedio(5, true));
}
[Fact]
public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException()
{
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((PuntoDeVenta?)null);
await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
() => _handler.Handle(new DeactivatePuntoDeVentaCommand(999)));
}
[Fact]
public async Task Handle_AlreadyInactive_IsIdempotentAndDoesNotCallUpdateAsync()
{
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));
await _repo.DidNotReceive().UpdateAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_AlreadyInactive_DoesNotWriteAuditEvent()
{
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));
await _audit.DidNotReceive().LogAsync(
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
Arg.Any<object?>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_ActivePdv_CallsUpdateAsyncWithInactiveEntity()
{
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: true));
await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));
await _repo.Received(1).UpdateAsync(
Arg.Is<PuntoDeVenta>(p => !p.Activo),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_ActivePdv_WritesAuditWithDeactivateAction()
{
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: true));
await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));
await _audit.Received(1).LogAsync(
action: "punto_de_venta.deactivate",
targetType: "PuntoDeVenta",
targetId: "10",
metadata: Arg.Any<object?>(),
ct: Arg.Any<CancellationToken>());
}
}

View File

@@ -0,0 +1,46 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.PuntosDeVenta.GetById;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.PuntosDeVenta.GetById;
public class GetPuntoDeVentaByIdQueryHandlerTests
{
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
private readonly GetPuntoDeVentaByIdQueryHandler _handler;
public GetPuntoDeVentaByIdQueryHandlerTests()
{
_handler = new GetPuntoDeVentaByIdQueryHandler(_repo);
}
private static PuntoDeVenta MakePdv(int id = 5) =>
new(id, 2, 3, "PdV " + id, "Desc", true, DateTime.UtcNow, null);
[Fact]
public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException()
{
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((PuntoDeVenta?)null);
await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
() => _handler.Handle(new GetPuntoDeVentaByIdQuery(999)));
}
[Fact]
public async Task Handle_HappyPath_ReturnsDtoWithCorrectFields()
{
var pdv = MakePdv(5);
_repo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(pdv);
var result = await _handler.Handle(new GetPuntoDeVentaByIdQuery(5));
Assert.Equal(5, result.Id);
Assert.Equal(2, result.MedioId);
Assert.Equal(3, result.NumeroAFIP);
Assert.Equal("PdV 5", result.Nombre);
Assert.Equal("Desc", result.Descripcion);
Assert.True(result.Activo);
}
}

View File

@@ -0,0 +1,76 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Common;
using SIGCM2.Application.PuntosDeVenta.List;
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Tests.PuntosDeVenta.List;
public class ListPuntosDeVentaQueryHandlerTests
{
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
private readonly ListPuntosDeVentaQueryHandler _handler;
public ListPuntosDeVentaQueryHandlerTests()
{
_handler = new ListPuntosDeVentaQueryHandler(_repo);
}
private static PuntoDeVenta MakePdv(int id) =>
new(id, 5, (short)id, "PdV " + id, null, true, DateTime.UtcNow, null);
[Fact]
public async Task Handle_ReturnsPagedDtoItems()
{
var items = new List<PuntoDeVenta> { MakePdv(1), MakePdv(2) };
var pagedResult = new PagedResult<PuntoDeVenta>(items, 1, 20, 2);
_repo.GetPagedAsync(Arg.Any<PuntosDeVentaQuery>(), Arg.Any<CancellationToken>())
.Returns(pagedResult);
var result = await _handler.Handle(new ListPuntosDeVentaQuery(1, 20, null, null));
Assert.Equal(2, result.Total);
Assert.Equal(2, result.Items.Count);
Assert.Equal(1, result.Items[0].NumeroAFIP);
}
[Fact]
public async Task Handle_ClampsPageSizeToMax100()
{
_repo.GetPagedAsync(Arg.Any<PuntosDeVentaQuery>(), Arg.Any<CancellationToken>())
.Returns(new PagedResult<PuntoDeVenta>([], 1, 100, 0));
await _handler.Handle(new ListPuntosDeVentaQuery(1, 500, null, null));
await _repo.Received(1).GetPagedAsync(
Arg.Is<PuntosDeVentaQuery>(q => q.PageSize == 100),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_ClampsPageToMin1()
{
_repo.GetPagedAsync(Arg.Any<PuntosDeVentaQuery>(), Arg.Any<CancellationToken>())
.Returns(new PagedResult<PuntoDeVenta>([], 1, 20, 0));
await _handler.Handle(new ListPuntosDeVentaQuery(0, 20, null, null));
await _repo.Received(1).GetPagedAsync(
Arg.Is<PuntosDeVentaQuery>(q => q.Page == 1),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_FiltersByMedioId()
{
_repo.GetPagedAsync(Arg.Any<PuntosDeVentaQuery>(), Arg.Any<CancellationToken>())
.Returns(new PagedResult<PuntoDeVenta>([], 1, 20, 0));
await _handler.Handle(new ListPuntosDeVentaQuery(1, 20, MedioId: 5, null));
await _repo.Received(1).GetPagedAsync(
Arg.Is<PuntosDeVentaQuery>(q => q.MedioId == 5),
Arg.Any<CancellationToken>());
}
}

View File

@@ -0,0 +1,52 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.PuntosDeVenta.ProximoNumero;
using SIGCM2.Domain.Enums;
namespace SIGCM2.Application.Tests.PuntosDeVenta.ProximoNumero;
public class GetProximoNumeroQueryHandlerTests
{
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
private readonly GetProximoNumeroQueryHandler _handler;
public GetProximoNumeroQueryHandlerTests()
{
_handler = new GetProximoNumeroQueryHandler(_repo);
}
[Fact]
public async Task Handle_ExistingSequence_ReturnsUltimoNumeroMasUno()
{
_repo.GetUltimoNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
.Returns(7);
var result = await _handler.Handle(new GetProximoNumeroQuery(10, TipoComprobante.FacturaA));
Assert.Equal(TipoComprobante.FacturaA, result.TipoComprobante);
Assert.Equal(8, result.ProximoNumero);
}
[Fact]
public async Task Handle_NoExistingSequence_ReturnsOne()
{
_repo.GetUltimoNumeroAsync(10, TipoComprobante.FacturaB, Arg.Any<CancellationToken>())
.Returns((int?)null);
var result = await _handler.Handle(new GetProximoNumeroQuery(10, TipoComprobante.FacturaB));
Assert.Equal(1, result.ProximoNumero);
}
[Fact]
public async Task Handle_DoesNotCallReservarNumero()
{
_repo.GetUltimoNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
.Returns(5);
await _handler.Handle(new GetProximoNumeroQuery(10, TipoComprobante.FacturaA));
await _repo.DidNotReceive().ReservarNumeroAsync(
Arg.Any<int>(), Arg.Any<TipoComprobante>(), Arg.Any<CancellationToken>());
}
}

View File

@@ -0,0 +1,98 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.PuntosDeVenta.Deactivate;
using SIGCM2.Application.PuntosDeVenta.Reactivate;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.PuntosDeVenta.Reactivate;
public class ReactivatePuntoDeVentaCommandHandlerTests
{
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly ReactivatePuntoDeVentaCommandHandler _handler;
private static PuntoDeVenta MakePdv(int id = 10, int medioId = 5, bool activo = false)
=> new(id, medioId, 1, "PdV Test", null, activo, DateTime.UtcNow, null);
private static Medio MakeMedio(int id = 5, bool activo = true)
=> new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
public ReactivatePuntoDeVentaCommandHandlerTests()
{
_handler = new ReactivatePuntoDeVentaCommandHandler(_repo, _medioRepo, _audit);
_medioRepo.GetByIdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(MakeMedio(5, true));
}
[Fact]
public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException()
{
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((PuntoDeVenta?)null);
await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
() => _handler.Handle(new ReactivatePuntoDeVentaCommand(999)));
}
[Fact]
public async Task Handle_MedioInactivo_ThrowsMedioInactivoException()
{
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
await Assert.ThrowsAsync<MedioInactivoException>(
() => _handler.Handle(new ReactivatePuntoDeVentaCommand(10)));
}
[Fact]
public async Task Handle_AlreadyActive_IsIdempotentAndDoesNotCallUpdateAsync()
{
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: true));
await _handler.Handle(new ReactivatePuntoDeVentaCommand(10));
await _repo.DidNotReceive().UpdateAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_InactivePdv_CallsUpdateAsyncWithActiveEntity()
{
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
await _handler.Handle(new ReactivatePuntoDeVentaCommand(10));
await _repo.Received(1).UpdateAsync(
Arg.Is<PuntoDeVenta>(p => p.Activo),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_InactivePdv_WritesAuditWithReactivateAction()
{
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
await _handler.Handle(new ReactivatePuntoDeVentaCommand(10));
await _audit.Received(1).LogAsync(
action: "punto_de_venta.reactivate",
targetType: "PuntoDeVenta",
targetId: "10",
metadata: Arg.Any<object?>(),
ct: Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_MedioInactivo_NoAuditLogged()
{
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
try { await _handler.Handle(new ReactivatePuntoDeVentaCommand(10)); } catch (MedioInactivoException) { }
await _audit.DidNotReceive().LogAsync(
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
Arg.Any<object?>(), Arg.Any<CancellationToken>());
}
}

View File

@@ -0,0 +1,126 @@
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.PuntosDeVenta.Reservar;
using SIGCM2.Domain.Enums;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.PuntosDeVenta.Reservar;
public class ReservarNumeroCommandHandlerTests
{
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
private readonly ReservarNumeroCommandHandler _handler;
private static readonly ReservarNumeroCommand ValidCommand =
new(PuntoDeVentaId: 10, TipoComprobante: TipoComprobante.FacturaA);
public ReservarNumeroCommandHandlerTests()
{
// Use delay = 0 for fast tests
_handler = new ReservarNumeroCommandHandler(_repo, deadlockBackoffMs: [0, 0, 0]);
}
// ── happy path ───────────────────────────────────────────────────────────
[Fact]
public async Task Handle_HappyPath_ReturnsNumeroReservado()
{
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
.Returns(7);
var result = await _handler.Handle(ValidCommand);
Assert.Equal(TipoComprobante.FacturaA, result.TipoComprobante);
Assert.Equal(7, result.NumeroReservado);
}
// ── retry deadlock ────────────────────────────────────────────────────────
[Fact]
public async Task Handle_DeadlockTwiceThenSucceeds_ReturnsResult()
{
var deadlock = new DeadlockTransientException();
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
.Returns(
_ => Task.FromException<int>(deadlock),
_ => Task.FromException<int>(deadlock),
_ => Task.FromResult(3));
var result = await _handler.Handle(ValidCommand);
Assert.Equal(3, result.NumeroReservado);
}
[Fact]
public async Task Handle_DeadlockThreeTimes_BubblesUpDeadlockException()
{
var deadlock = new DeadlockTransientException();
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
.Returns(
_ => Task.FromException<int>(deadlock),
_ => Task.FromException<int>(deadlock),
_ => Task.FromException<int>(deadlock));
await Assert.ThrowsAsync<DeadlockTransientException>(
() => _handler.Handle(ValidCommand));
}
[Fact]
public async Task Handle_DeadlockExhaustsBackoff_TriedFourTimesTotal()
{
// backoff = [0,0,0] → 3 retries → 4 total attempts (1 initial + 3 retries)
var deadlock = new DeadlockTransientException();
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
.Returns(_ => Task.FromException<int>(deadlock));
try { await _handler.Handle(ValidCommand); } catch (DeadlockTransientException) { }
await _repo.Received(4).ReservarNumeroAsync(
Arg.Any<int>(), Arg.Any<TipoComprobante>(), Arg.Any<CancellationToken>());
}
// ── domain exceptions bubble up without retry ─────────────────────────────
[Fact]
public async Task Handle_PuntoDeVentaInactivo_BubblesUpImmediately()
{
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
.Throws(new PuntoDeVentaInactivoException(10));
await Assert.ThrowsAsync<PuntoDeVentaInactivoException>(
() => _handler.Handle(ValidCommand));
await _repo.Received(1).ReservarNumeroAsync(
Arg.Any<int>(), Arg.Any<TipoComprobante>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_MedioInactivo_BubblesUpImmediately()
{
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
.Throws(new MedioInactivoException(5));
await Assert.ThrowsAsync<MedioInactivoException>(
() => _handler.Handle(ValidCommand));
await _repo.Received(1).ReservarNumeroAsync(
Arg.Any<int>(), Arg.Any<TipoComprobante>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_PdvNotFound_BubblesUpImmediately()
{
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
.Throws(new PuntoDeVentaNotFoundException(10));
await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
() => _handler.Handle(ValidCommand));
await _repo.Received(1).ReservarNumeroAsync(
Arg.Any<int>(), Arg.Any<TipoComprobante>(), Arg.Any<CancellationToken>());
}
}

View File

@@ -0,0 +1,103 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.PuntosDeVenta.Update;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.PuntosDeVenta.Update;
public class UpdatePuntoDeVentaCommandHandlerTests
{
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly UpdatePuntoDeVentaCommandHandler _handler;
private static PuntoDeVenta MakePdv(int id = 10, int medioId = 5, bool activo = true)
=> new(id, medioId, 1, "Original", null, activo, DateTime.UtcNow, null);
private static Medio MakeMedio(int id = 5, bool activo = true)
=> new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
private static UpdatePuntoDeVentaCommand ValidCommand(int id = 10) =>
new(Id: id, Nombre: "Nuevo Nombre", NumeroAFIP: 3, Descripcion: null);
public UpdatePuntoDeVentaCommandHandlerTests()
{
_handler = new UpdatePuntoDeVentaCommandHandler(_repo, _medioRepo, _audit);
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10));
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5));
_repo.ExistsByNumeroAFIPInMedioAsync(Arg.Any<int>(), Arg.Any<short>(), Arg.Any<int?>(), Arg.Any<CancellationToken>()).Returns(false);
}
[Fact]
public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException()
{
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((PuntoDeVenta?)null);
await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
() => _handler.Handle(new UpdatePuntoDeVentaCommand(999, "X", 1, null)));
}
[Fact]
public async Task Handle_MedioInactivo_ThrowsMedioInactivoException()
{
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
await Assert.ThrowsAsync<MedioInactivoException>(
() => _handler.Handle(ValidCommand()));
}
[Fact]
public async Task Handle_NumeroAFIPDuplicado_ThrowsNumeroAFIPDuplicadoException()
{
_repo.ExistsByNumeroAFIPInMedioAsync(5, 3, 10, Arg.Any<CancellationToken>()).Returns(true);
await Assert.ThrowsAsync<NumeroAFIPDuplicadoException>(
() => _handler.Handle(ValidCommand()));
}
[Fact]
public async Task Handle_HappyPath_CallsUpdateAsyncOnce()
{
await _handler.Handle(ValidCommand());
await _repo.Received(1).UpdateAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_HappyPath_ReturnsDtoWithUpdatedFields()
{
var result = await _handler.Handle(ValidCommand());
Assert.Equal(10, result.Id);
Assert.Equal("Nuevo Nombre", result.Nombre);
Assert.Equal(3, result.NumeroAFIP);
}
[Fact]
public async Task Handle_HappyPath_CallsAuditWithUpdateAction()
{
await _handler.Handle(ValidCommand());
await _audit.Received(1).LogAsync(
action: "punto_de_venta.update",
targetType: "PuntoDeVenta",
targetId: "10",
metadata: Arg.Any<object?>(),
ct: Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_MedioInactivo_NoAuditLogged()
{
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
try { await _handler.Handle(ValidCommand()); } catch (MedioInactivoException) { }
await _audit.DidNotReceive().LogAsync(
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
Arg.Any<object?>(), Arg.Any<CancellationToken>());
}
}