feat(api): pagination on GET product prices (closes #47)

- GET /api/v1/products/{id}/prices now returns PagedResult<ProductPriceDto>
  with OFFSET/FETCH + COUNT via Dapper (two queries on same connection)
- Query params: ?page (default 1) and ?pageSize (default 20, max 100)
- Clamping: Math.Max(1, page) + Math.Clamp(pageSize, 1, 100) in handler
- Auth upgraded from [Authorize] to [RequirePermission("catalogo:productos:gestionar")]
- IProductPriceRepository.GetByProductIdAsync signature updated to paginated form
- AddProductPriceCommandHandler adapted to read back via page=1, pageSize=2
- TDD cycle: RED (tests updated to PagedResult shape) -> GREEN (implementation) -> REFACTOR
- Tests: 1418 total (1106 Application + 312 Api), 0 failures

closes #47
This commit is contained in:
2026-04-19 19:47:18 -03:00
parent da063ad677
commit 0dce3ee4ac
11 changed files with 425 additions and 98 deletions

View File

@@ -4,6 +4,7 @@ using NSubstitute;
using NSubstitute.ExceptionExtensions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Common;
using SIGCM2.Application.Products.Prices;
using SIGCM2.Application.Products.Prices.AddPrice;
using SIGCM2.Domain.Entities;
@@ -50,11 +51,10 @@ public class AddProductPriceCommandHandlerTests
_pricesRepo.AddAsync(1, Arg.Any<decimal>(), Arg.Any<DateOnly>(), Arg.Any<CancellationToken>())
.Returns((10L, (long?)null));
_pricesRepo.GetByProductIdAsync(1, Arg.Any<CancellationToken>())
.Returns(new List<ProductPrice>
{
MakePrice(10, 1, 150m, Today)
}.AsReadOnly() as IReadOnlyList<ProductPrice>);
_pricesRepo.GetByProductIdAsync(1, Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(new PagedResult<ProductPrice>(
new List<ProductPrice> { MakePrice(10, 1, 150m, Today) },
1, 2, 1));
_handler = new AddProductPriceCommandHandler(_pricesRepo, _productsRepo, _audit, _time);
}
@@ -110,12 +110,14 @@ public class AddProductPriceCommandHandlerTests
_pricesRepo.AddAsync(1, Arg.Any<decimal>(), Arg.Any<DateOnly>(), Arg.Any<CancellationToken>())
.Returns((20L, (long?)5L)); // newId=20, closedId=5
_pricesRepo.GetByProductIdAsync(1, Arg.Any<CancellationToken>())
.Returns(new List<ProductPrice>
{
MakePrice(20, 1, 200m, Tomorrow),
MakePrice(5, 1, 150m, Today, pvt: Tomorrow.AddDays(-1))
}.AsReadOnly() as IReadOnlyList<ProductPrice>);
_pricesRepo.GetByProductIdAsync(1, Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(new PagedResult<ProductPrice>(
new List<ProductPrice>
{
MakePrice(20, 1, 200m, Tomorrow),
MakePrice(5, 1, 150m, Today, pvt: Tomorrow.AddDays(-1))
},
1, 2, 2));
var result = await _handler.Handle(ValidCmd() with { Price = 200m, PriceValidFrom = Tomorrow });

View File

@@ -1,6 +1,7 @@
using FluentAssertions;
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Common;
using SIGCM2.Application.Products.Prices;
using SIGCM2.Application.Products.Prices.GetHistory;
using SIGCM2.Domain.Entities;
@@ -9,8 +10,9 @@ using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.Products.Prices;
/// <summary>
/// PRD-003 — GetProductPricesQueryHandler tests.
/// Covers: §REQ-4.1 (historial descending), §REQ-4.3 (lista vacía), §REQ-3.3 (producto no existe → 404).
/// PRD-003 (paginated) — GetProductPricesQueryHandler tests.
/// Covers: §P.1 (defaults), §P.3 (empty), §P.4 (pageSize clamp), §P.5 (page clamp),
/// §REQ-4.1 (historial descending), §REQ-4.3 (lista vacía), §REQ-3.3 (producto no existe → 404).
/// </summary>
public class GetProductPricesQueryHandlerTests
{
@@ -31,24 +33,30 @@ public class GetProductPricesQueryHandlerTests
new(id, ProductId: 1, Price: 100m * id, pvf, pvt,
FechaCreacion: new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc));
private static PagedResult<ProductPrice> MakePagedResult(
IReadOnlyList<ProductPrice> items, int page = 1, int pageSize = 20, int? total = null) =>
new(items, page, pageSize, total ?? items.Count);
public GetProductPricesQueryHandlerTests()
{
_productsRepo.GetByIdAsync(1, Arg.Any<CancellationToken>())
.Returns(ActiveProduct());
// default: lista con 2 precios, el repo ya los devuelve descending (responsabilidad del repo)
_pricesRepo.GetByProductIdAsync(1, Arg.Any<CancellationToken>())
.Returns(new List<ProductPrice>
{
MakePrice(3, Date3), // activo (pvt=null)
MakePrice(2, Date2, Date3.AddDays(-1)), // cerrado
MakePrice(1, Date1, Date2.AddDays(-1)) // cerrado más antiguo
}.AsReadOnly() as IReadOnlyList<ProductPrice>);
// default: 3 precios devueltos descending por el repo
var defaultItems = new List<ProductPrice>
{
MakePrice(3, Date3),
MakePrice(2, Date2, Date3.AddDays(-1)),
MakePrice(1, Date1, Date2.AddDays(-1))
};
_pricesRepo
.GetByProductIdAsync(1, Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(MakePagedResult(defaultItems));
_handler = new GetProductPricesQueryHandler(_pricesRepo, _productsRepo);
}
// ── Orden descending ────────────────────────────────────────────────────────
// ── Orden descending y mapping ──────────────────────────────────────────────
[Fact]
public async Task Handle_ReturnsAllPrices_InDescendingOrder()
@@ -56,10 +64,10 @@ public class GetProductPricesQueryHandlerTests
// §REQ-4.1 — historial completo ordenado descending por PriceValidFrom
var result = await _handler.Handle(new GetProductPricesQuery(ProductId: 1));
result.Should().HaveCount(3);
result[0].PriceValidFrom.Should().Be(Date3); // más reciente primero
result[1].PriceValidFrom.Should().Be(Date2);
result[2].PriceValidFrom.Should().Be(Date1);
result.Items.Should().HaveCount(3);
result.Items[0].PriceValidFrom.Should().Be(Date3);
result.Items[1].PriceValidFrom.Should().Be(Date2);
result.Items[2].PriceValidFrom.Should().Be(Date1);
}
[Fact]
@@ -67,22 +75,76 @@ public class GetProductPricesQueryHandlerTests
{
var result = await _handler.Handle(new GetProductPricesQuery(ProductId: 1));
result[0].IsActive.Should().BeTrue(); // pvt=null → activo
result[1].IsActive.Should().BeFalse(); // pvt IS NOT NULL → cerrado
result.Items[0].IsActive.Should().BeTrue(); // pvt=null → activo
result.Items[1].IsActive.Should().BeFalse(); // pvt IS NOT NULL → cerrado
}
[Fact]
public async Task Handle_ReturnsPagedResultShape_WithCorrectMeta()
{
// §P.1 — defaults page=1, pageSize=20 forwarded to repo and reflected in result
var result = await _handler.Handle(new GetProductPricesQuery(ProductId: 1));
result.Page.Should().Be(1);
result.PageSize.Should().Be(20);
}
// ── Lista vacía (nuevo producto sin precios) ─────────────────────────────────
[Fact]
public async Task Handle_EmptyHistory_ReturnsEmptyList()
public async Task Handle_EmptyHistory_ReturnsEmptyPagedResult()
{
// §REQ-4.3 — nuevo producto aún no tiene precios → lista vacía (no 404)
_pricesRepo.GetByProductIdAsync(1, Arg.Any<CancellationToken>())
.Returns(new List<ProductPrice>().AsReadOnly() as IReadOnlyList<ProductPrice>);
// §REQ-4.3 / §P.6 — nuevo producto aún no tiene precios → items vacíos (no 404)
_pricesRepo
.GetByProductIdAsync(1, Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(new PagedResult<ProductPrice>(new List<ProductPrice>(), 1, 20, 0));
var result = await _handler.Handle(new GetProductPricesQuery(ProductId: 1));
result.Should().BeEmpty();
result.Items.Should().BeEmpty();
result.Total.Should().Be(0);
}
// ── Clamping defensivo ──────────────────────────────────────────────────────
[Fact]
public async Task Handle_PageZero_ClampsToOne()
{
// §P.5 — page=0 → Math.Max(1,0) = 1
await _handler.Handle(new GetProductPricesQuery(ProductId: 1, Page: 0));
await _pricesRepo.Received(1)
.GetByProductIdAsync(1, page: 1, pageSize: 20, Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_PageNegative_ClampsToOne()
{
// §P.5 — page=-5 → Math.Max(1,-5) = 1
await _handler.Handle(new GetProductPricesQuery(ProductId: 1, Page: -5));
await _pricesRepo.Received(1)
.GetByProductIdAsync(1, page: 1, pageSize: 20, Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_PageSizeOver100_ClampsTo100()
{
// §P.4 — pageSize=500 → Math.Clamp(500,1,100) = 100
await _handler.Handle(new GetProductPricesQuery(ProductId: 1, PageSize: 500));
await _pricesRepo.Received(1)
.GetByProductIdAsync(1, page: 1, pageSize: 100, Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_PageSizeZero_ClampsToOne()
{
// pageSize=0 → Math.Clamp(0,1,100) = 1
await _handler.Handle(new GetProductPricesQuery(ProductId: 1, PageSize: 0));
await _pricesRepo.Received(1)
.GetByProductIdAsync(1, page: 1, pageSize: 1, Arg.Any<CancellationToken>());
}
// ── Producto inexistente ────────────────────────────────────────────────────
@@ -108,6 +170,6 @@ public class GetProductPricesQueryHandlerTests
await act.Should().ThrowAsync<ProductNotFoundException>();
await _pricesRepo.DidNotReceive()
.GetByProductIdAsync(99, Arg.Any<CancellationToken>());
.GetByProductIdAsync(99, Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>());
}
}