2026-04-18 19:25:35 -03:00
|
|
|
using FluentAssertions;
|
|
|
|
|
using Microsoft.Extensions.Time.Testing;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using SIGCM2.Application.Abstractions.Persistence;
|
|
|
|
|
using SIGCM2.Application.Audit;
|
|
|
|
|
using SIGCM2.Application.Rubros.Deactivate;
|
|
|
|
|
using SIGCM2.Domain.Entities;
|
|
|
|
|
using SIGCM2.Domain.Exceptions;
|
|
|
|
|
|
|
|
|
|
namespace SIGCM2.Application.Tests.Rubros.Deactivate;
|
|
|
|
|
|
|
|
|
|
public class DeactivateRubroCommandHandlerTests
|
|
|
|
|
{
|
|
|
|
|
private readonly IRubroRepository _repo = Substitute.For<IRubroRepository>();
|
|
|
|
|
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
2026-04-19 17:08:30 -03:00
|
|
|
private readonly IProductQueryRepository _productQuery = Substitute.For<IProductQueryRepository>();
|
2026-04-18 19:25:35 -03:00
|
|
|
private readonly FakeTimeProvider _timeProvider = new(new DateTimeOffset(2026, 4, 18, 12, 0, 0, TimeSpan.Zero));
|
|
|
|
|
private readonly DeactivateRubroCommandHandler _handler;
|
|
|
|
|
|
|
|
|
|
private static Rubro LeafRubro(int id = 10) => new(id, null, "Autos", 0, activo: true,
|
|
|
|
|
tarifarioBaseId: null, fechaCreacion: DateTime.UtcNow, fechaModificacion: null);
|
|
|
|
|
|
|
|
|
|
public DeactivateRubroCommandHandlerTests()
|
|
|
|
|
{
|
|
|
|
|
_repo.CountActiveChildrenAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(0);
|
2026-04-19 17:08:30 -03:00
|
|
|
_productQuery.CountActiveByRubroAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(0);
|
|
|
|
|
_handler = new DeactivateRubroCommandHandler(_repo, _audit, _productQuery, _timeProvider);
|
2026-04-18 19:25:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Happy path: leaf soft-delete ─────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Handle_LeafRubro_SoftDeletes()
|
|
|
|
|
{
|
|
|
|
|
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(LeafRubro());
|
|
|
|
|
|
|
|
|
|
var result = await _handler.Handle(new DeactivateRubroCommand(Id: 10));
|
|
|
|
|
|
|
|
|
|
result.Id.Should().Be(10);
|
|
|
|
|
result.Activo.Should().BeFalse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Handle_LeafRubro_CallsUpdateAsync()
|
|
|
|
|
{
|
|
|
|
|
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(LeafRubro());
|
|
|
|
|
|
|
|
|
|
await _handler.Handle(new DeactivateRubroCommand(Id: 10));
|
|
|
|
|
|
|
|
|
|
await _repo.Received(1).UpdateAsync(
|
|
|
|
|
Arg.Is<Rubro>(r => r.Id == 10 && !r.Activo),
|
|
|
|
|
Arg.Any<CancellationToken>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Handle_LeafRubro_CallsAuditLog()
|
|
|
|
|
{
|
|
|
|
|
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(LeafRubro());
|
|
|
|
|
|
|
|
|
|
await _handler.Handle(new DeactivateRubroCommand(Id: 10));
|
|
|
|
|
|
|
|
|
|
await _audit.Received(1).LogAsync(
|
|
|
|
|
action: "rubro.deleted",
|
|
|
|
|
targetType: "Rubro",
|
|
|
|
|
targetId: "10",
|
|
|
|
|
metadata: Arg.Any<object?>(),
|
|
|
|
|
ct: Arg.Any<CancellationToken>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Has active children → RubroTieneHijosActivosException ───────────────
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Handle_HasActiveChildren_ThrowsRubroTieneHijosActivosException()
|
|
|
|
|
{
|
|
|
|
|
_repo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(LeafRubro(5));
|
|
|
|
|
_repo.CountActiveChildrenAsync(5, Arg.Any<CancellationToken>()).Returns(3);
|
|
|
|
|
|
|
|
|
|
var act = () => _handler.Handle(new DeactivateRubroCommand(Id: 5));
|
|
|
|
|
|
|
|
|
|
await act.Should().ThrowAsync<RubroTieneHijosActivosException>()
|
|
|
|
|
.Where(ex => ex.Id == 5 && ex.Count == 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Handle_HasActiveChildren_DoesNotCallAuditLog()
|
|
|
|
|
{
|
|
|
|
|
_repo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(LeafRubro(5));
|
|
|
|
|
_repo.CountActiveChildrenAsync(5, Arg.Any<CancellationToken>()).Returns(1);
|
|
|
|
|
|
|
|
|
|
try { await _handler.Handle(new DeactivateRubroCommand(Id: 5)); } catch { }
|
|
|
|
|
|
|
|
|
|
await _audit.DidNotReceive().LogAsync(
|
|
|
|
|
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
|
|
|
|
|
Arg.Any<object?>(), Arg.Any<CancellationToken>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Not found → RubroNotFoundException ──────────────────────────────────
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Handle_NotFound_ThrowsRubroNotFoundException()
|
|
|
|
|
{
|
|
|
|
|
_repo.GetByIdAsync(99, Arg.Any<CancellationToken>()).Returns((Rubro?)null);
|
|
|
|
|
|
|
|
|
|
var act = () => _handler.Handle(new DeactivateRubroCommand(Id: 99));
|
|
|
|
|
|
|
|
|
|
await act.Should().ThrowAsync<RubroNotFoundException>();
|
|
|
|
|
}
|
2026-04-19 17:08:30 -03:00
|
|
|
|
|
|
|
|
// ── Active Products guard (issue #41) ────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Deactivate_WithActiveProducts_ThrowsRubroConProductosActivosException()
|
|
|
|
|
{
|
|
|
|
|
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(LeafRubro(10));
|
|
|
|
|
_productQuery.CountActiveByRubroAsync(10, Arg.Any<CancellationToken>()).Returns(3);
|
|
|
|
|
|
|
|
|
|
var act = () => _handler.Handle(new DeactivateRubroCommand(Id: 10));
|
|
|
|
|
|
|
|
|
|
await act.Should().ThrowAsync<RubroConProductosActivosException>()
|
|
|
|
|
.Where(ex => ex.RubroId == 10 && ex.ProductosActivosCount == 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Deactivate_WithZeroActiveProducts_Succeeds()
|
|
|
|
|
{
|
|
|
|
|
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(LeafRubro(10));
|
|
|
|
|
_productQuery.CountActiveByRubroAsync(10, Arg.Any<CancellationToken>()).Returns(0);
|
|
|
|
|
|
|
|
|
|
var result = await _handler.Handle(new DeactivateRubroCommand(Id: 10));
|
|
|
|
|
|
|
|
|
|
result.Activo.Should().BeFalse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Deactivate_WithActiveChildrenAndProducts_ThrowsChildrenFirst()
|
|
|
|
|
{
|
|
|
|
|
// Children guard fires BEFORE products guard — order of checks must be stable.
|
|
|
|
|
_repo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(LeafRubro(5));
|
|
|
|
|
_repo.CountActiveChildrenAsync(5, Arg.Any<CancellationToken>()).Returns(2);
|
|
|
|
|
_productQuery.CountActiveByRubroAsync(5, Arg.Any<CancellationToken>()).Returns(3);
|
|
|
|
|
|
|
|
|
|
var act = () => _handler.Handle(new DeactivateRubroCommand(Id: 5));
|
|
|
|
|
|
|
|
|
|
// Children exception must win — products guard is never reached
|
|
|
|
|
await act.Should().ThrowAsync<RubroTieneHijosActivosException>();
|
|
|
|
|
}
|
2026-04-18 19:25:35 -03:00
|
|
|
}
|