feat: paginación en GET /api/v1/products/{id}/prices (closes #47) #51

Merged
dmolinari merged 3 commits from feature/prd-003-prices-pagination into main 2026-04-19 23:08:32 +00:00
11 changed files with 425 additions and 98 deletions
Showing only changes of commit 0dce3ee4ac - Show all commits

View File

@@ -1,8 +1,8 @@
using FluentValidation; using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using SIGCM2.Api.Authorization; using SIGCM2.Api.Authorization;
using SIGCM2.Application.Abstractions; using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Common;
using SIGCM2.Application.Products.Prices; using SIGCM2.Application.Products.Prices;
using SIGCM2.Application.Products.Prices.AddPrice; using SIGCM2.Application.Products.Prices.AddPrice;
using SIGCM2.Application.Products.Prices.GetHistory; using SIGCM2.Application.Products.Prices.GetHistory;
@@ -11,7 +11,7 @@ namespace SIGCM2.Api.Controllers;
/// <summary> /// <summary>
/// PRD-003: ProductPrices historic pricing management. /// PRD-003: ProductPrices historic pricing management.
/// Read endpoint at GET /api/v1/products/{id}/prices — requires authentication (any role). /// Read endpoint at GET /api/v1/products/{id}/prices — requires 'catalogo:productos:gestionar'.
/// Write endpoint at POST /api/v1/admin/products/{id}/prices — requires 'catalogo:productos:gestionar'. /// Write endpoint at POST /api/v1/admin/products/{id}/prices — requires 'catalogo:productos:gestionar'.
/// </summary> /// </summary>
[ApiController] [ApiController]
@@ -31,19 +31,28 @@ public sealed class ProductPricesController : ControllerBase
// ── READ endpoint ────────────────────────────────────────────────────────── // ── READ endpoint ──────────────────────────────────────────────────────────
/// <summary> /// <summary>
/// Returns the full price history for a Product, ordered descending by PriceValidFrom. /// Returns a paginated page of price history for a Product, ordered descending by PriceValidFrom.
/// Returns 200 with empty array if the product has no prices yet. /// Defaults: page=1, pageSize=20. Clamping: page ≥ 1, pageSize ∈ [1, 100].
/// Returns 200 with empty items if the product has no prices yet or page is beyond total.
/// Returns 404 if the product does not exist. /// Returns 404 if the product does not exist.
/// Returns 401 if not authenticated, 403 if missing 'catalogo:productos:gestionar' permission.
/// </summary> /// </summary>
[HttpGet("api/v1/products/{id:int}/prices")] [HttpGet("api/v1/products/{id:int}/prices")]
[Authorize] [RequirePermission("catalogo:productos:gestionar")]
[ProducesResponseType(typeof(IReadOnlyList<ProductPriceDto>), StatusCodes.Status200OK)] [ProducesResponseType(typeof(PagedResult<ProductPriceDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetProductPrices([FromRoute] int id) public async Task<IActionResult> GetProductPrices(
[FromRoute] int id,
[FromQuery] int? page,
[FromQuery] int? pageSize)
{ {
var query = new GetProductPricesQuery(id); var query = new GetProductPricesQuery(
var result = await _dispatcher.Send<GetProductPricesQuery, IReadOnlyList<ProductPriceDto>>(query); ProductId: id,
Page: page ?? 1,
PageSize: pageSize ?? 20);
var result = await _dispatcher.Send<GetProductPricesQuery, PagedResult<ProductPriceDto>>(query);
return Ok(result); return Ok(result);
} }

View File

@@ -1,3 +1,4 @@
using SIGCM2.Application.Common;
using SIGCM2.Domain.Entities; using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Abstractions.Persistence; namespace SIGCM2.Application.Abstractions.Persistence;
@@ -21,11 +22,14 @@ public interface IProductPriceRepository
CancellationToken ct = default); CancellationToken ct = default);
/// <summary> /// <summary>
/// Returns all price rows for the product, ordered descending by PriceValidFrom (active first). /// Returns a paginated page of price rows for the product, ordered descending by PriceValidFrom.
/// Returns empty list when the product has no price history. /// Caller is responsible for clamping page (≥ 1) and pageSize (1100) before calling.
/// Returns PagedResult with empty Items when the product has no price history or page is beyond total.
/// </summary> /// </summary>
Task<IReadOnlyList<ProductPrice>> GetByProductIdAsync( Task<PagedResult<ProductPrice>> GetByProductIdAsync(
int productId, int productId,
int page,
int pageSize,
CancellationToken ct = default); CancellationToken ct = default);
/// <summary> /// <summary>

View File

@@ -188,7 +188,7 @@ public static class DependencyInjection
// ProductPrices (PRD-003) // ProductPrices (PRD-003)
services.AddScoped<ICommandHandler<AddProductPriceCommand, AddProductPriceResponse>, AddProductPriceCommandHandler>(); services.AddScoped<ICommandHandler<AddProductPriceCommand, AddProductPriceResponse>, AddProductPriceCommandHandler>();
services.AddScoped<ICommandHandler<GetProductPricesQuery, IReadOnlyList<ProductPriceDto>>, GetProductPricesQueryHandler>(); services.AddScoped<ICommandHandler<GetProductPricesQuery, PagedResult<ProductPriceDto>>, GetProductPricesQueryHandler>();
services.AddScoped<IProductPricingService, ProductPricingService>(); services.AddScoped<IProductPricingService, ProductPricingService>();
// ProductTypes (PRD-001) // ProductTypes (PRD-001)

View File

@@ -75,7 +75,10 @@ public sealed class AddProductPriceCommandHandler
} // TX disposed (committed) here — BEFORE the post-commit read below. } // TX disposed (committed) here — BEFORE the post-commit read below.
// 3. Compongo la respuesta post-commit con lectura de historial actualizado. // 3. Compongo la respuesta post-commit con lectura de historial actualizado.
var prices = await _pricesRepo.GetByProductIdAsync(command.ProductId); // La primera página (pageSize=2) es suficiente: solo necesitamos el nuevo y el cerrado,
// que son siempre los más recientes (ORDER BY PriceValidFrom DESC).
var pricesPage = await _pricesRepo.GetByProductIdAsync(command.ProductId, page: 1, pageSize: 2);
var prices = pricesPage.Items;
var created = prices.Single(p => p.Id == newId); var created = prices.Single(p => p.Id == newId);
var closed = closedId.HasValue var closed = closedId.HasValue
? prices.SingleOrDefault(p => p.Id == closedId.Value) ? prices.SingleOrDefault(p => p.Id == closedId.Value)

View File

@@ -1,8 +1,12 @@
namespace SIGCM2.Application.Products.Prices.GetHistory; namespace SIGCM2.Application.Products.Prices.GetHistory;
/// <summary> /// <summary>
/// PRD-003 — Query para obtener el historial de precios de un Product. /// PRD-003 (paginated) — Query para obtener el historial de precios de un Product.
/// Devuelve lista ordenada descending por PriceValidFrom (activo primero). /// Devuelve PagedResult ordenado descending por PriceValidFrom (activo primero).
/// Lanza ProductNotFoundException si el producto no existe. /// Lanza ProductNotFoundException si el producto no existe.
/// Page y PageSize son clampeados por el handler: page ≥ 1, pageSize ∈ [1, 100].
/// </summary> /// </summary>
public sealed record GetProductPricesQuery(int ProductId); public sealed record GetProductPricesQuery(
int ProductId,
int Page = 1,
int PageSize = 20);

View File

@@ -1,18 +1,19 @@
using SIGCM2.Application.Abstractions; using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence; using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Common;
using SIGCM2.Application.Products.Prices; using SIGCM2.Application.Products.Prices;
using SIGCM2.Domain.Exceptions; using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Products.Prices.GetHistory; namespace SIGCM2.Application.Products.Prices.GetHistory;
/// <summary> /// <summary>
/// PRD-003 — Handler de GetProductPricesQuery. /// PRD-003 (paginated) — Handler de GetProductPricesQuery.
/// Verifica que el producto exista (404 si no), luego retorna historial de precios /// Verifica que el producto exista (404 si no), aplica clamping defensivo de
/// ordenado descending por PriceValidFrom (responsabilidad del repo — SQL ORDER BY). /// page/pageSize y retorna PagedResult ordenado descending por PriceValidFrom.
/// Lista vacía es válida (nuevo producto sin precios registrados aún). /// Lista vacía es válida (nuevo producto sin precios o página más allá del total).
/// </summary> /// </summary>
public sealed class GetProductPricesQueryHandler public sealed class GetProductPricesQueryHandler
: ICommandHandler<GetProductPricesQuery, IReadOnlyList<ProductPriceDto>> : ICommandHandler<GetProductPricesQuery, PagedResult<ProductPriceDto>>
{ {
private readonly IProductPriceRepository _pricesRepo; private readonly IProductPriceRepository _pricesRepo;
private readonly IProductRepository _productsRepo; private readonly IProductRepository _productsRepo;
@@ -25,18 +26,24 @@ public sealed class GetProductPricesQueryHandler
_productsRepo = productsRepo; _productsRepo = productsRepo;
} }
public async Task<IReadOnlyList<ProductPriceDto>> Handle(GetProductPricesQuery query) public async Task<PagedResult<ProductPriceDto>> Handle(GetProductPricesQuery query)
{ {
// Verifica existencia del producto (lanza 404 si no existe). // Verifica existencia del producto (lanza 404 si no existe).
_ = await _productsRepo.GetByIdAsync(query.ProductId) _ = await _productsRepo.GetByIdAsync(query.ProductId)
?? throw new ProductNotFoundException(query.ProductId); ?? throw new ProductNotFoundException(query.ProductId);
var prices = await _pricesRepo.GetByProductIdAsync(query.ProductId); // Clamping defensivo — igual al patrón de ListProductsQueryHandler.
var page = Math.Max(1, query.Page);
var pageSize = Math.Clamp(query.PageSize, 1, 100);
return prices var paged = await _pricesRepo.GetByProductIdAsync(query.ProductId, page, pageSize);
var dtoItems = paged.Items
.Select(p => new ProductPriceDto( .Select(p => new ProductPriceDto(
p.Id, p.ProductId, p.Price, p.Id, p.ProductId, p.Price,
p.PriceValidFrom, p.PriceValidTo, p.IsActive)) p.PriceValidFrom, p.PriceValidTo, p.IsActive))
.ToList(); .ToList();
return new PagedResult<ProductPriceDto>(dtoItems, paged.Page, paged.PageSize, paged.Total);
} }
} }

View File

@@ -2,6 +2,7 @@ using System.Data;
using Dapper; using Dapper;
using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClient;
using SIGCM2.Application.Abstractions.Persistence; using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Common;
using SIGCM2.Domain.Entities; using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions; using SIGCM2.Domain.Exceptions;
@@ -70,25 +71,42 @@ public sealed class ProductPriceRepository : IProductPriceRepository
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<IReadOnlyList<ProductPrice>> GetByProductIdAsync( public async Task<PagedResult<ProductPrice>> GetByProductIdAsync(
int productId, int productId,
int page,
int pageSize,
CancellationToken ct = default) CancellationToken ct = default)
{ {
// Uses IX_ProductPrices_Lookup (ProductId, PriceValidFrom DESC). // Uses IX_ProductPrices_Lookup (ProductId, PriceValidFrom DESC).
const string sql = """ // Two separate queries on the same open connection: COUNT first, then paginated DATA.
const string countSql = """
SELECT COUNT(1) FROM dbo.ProductPrices WHERE ProductId = @ProductId
""";
const string dataSql = """
SELECT Id, ProductId, Price, PriceValidFrom, PriceValidTo, FechaCreacion SELECT Id, ProductId, Price, PriceValidFrom, PriceValidTo, FechaCreacion
FROM dbo.ProductPrices FROM dbo.ProductPrices
WHERE ProductId = @ProductId WHERE ProductId = @ProductId
ORDER BY PriceValidFrom DESC, Id DESC ORDER BY PriceValidFrom DESC, Id DESC
OFFSET @Offset ROWS FETCH NEXT @PageSize ROWS ONLY
"""; """;
var offset = (page - 1) * pageSize;
await using var connection = _factory.CreateConnection(); await using var connection = _factory.CreateConnection();
await connection.OpenAsync(ct); await connection.OpenAsync(ct);
var rows = await connection.QueryAsync<ProductPriceRow>( var total = await connection.ExecuteScalarAsync<int>(
new CommandDefinition(sql, new { ProductId = productId }, cancellationToken: ct)); new CommandDefinition(countSql, new { ProductId = productId }, cancellationToken: ct));
return rows.Select(MapRow).ToList(); var rows = await connection.QueryAsync<ProductPriceRow>(
new CommandDefinition(dataSql,
new { ProductId = productId, Offset = offset, PageSize = pageSize },
cancellationToken: ct));
var items = rows.Select(MapRow).ToList();
return new PagedResult<ProductPrice>(items, page, pageSize, total);
} }
/// <inheritdoc/> /// <inheritdoc/>

View File

@@ -7,6 +7,8 @@ using FluentAssertions;
using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using SIGCM2.Application.Abstractions.Security; using SIGCM2.Application.Abstractions.Security;
using SIGCM2.Application.Common;
using SIGCM2.Application.Products.Prices;
using SIGCM2.Domain.Entities; using SIGCM2.Domain.Entities;
using SIGCM2.TestSupport; using SIGCM2.TestSupport;
using Xunit; using Xunit;
@@ -140,6 +142,7 @@ public sealed class ProductPricesControllerTests : IAsyncLifetime
// ── GET /api/v1/products/{id}/prices ───────────────────────────────────── // ── GET /api/v1/products/{id}/prices ─────────────────────────────────────
/// <summary>§P.8 — No token → 401.</summary>
[Fact] [Fact]
public async Task GetPrices_WithoutAuth_Returns401() public async Task GetPrices_WithoutAuth_Returns401()
{ {
@@ -148,8 +151,20 @@ public sealed class ProductPricesControllerTests : IAsyncLifetime
resp.StatusCode.Should().Be(HttpStatusCode.Unauthorized); resp.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
} }
/// <summary>§P.8 — Token with no 'catalogo:productos:gestionar' → 403.</summary>
[Fact] [Fact]
public async Task GetPrices_EmptyHistory_Returns200WithEmptyArray() public async Task GetPrices_WithoutPermission_Returns403()
{
var productId = await SeedProductAsync();
var token = GetCajeroToken();
using var req = BuildRequest(HttpMethod.Get, $"/api/v1/products/{productId}/prices", token: token);
var resp = await _client.SendAsync(req);
resp.StatusCode.Should().Be(HttpStatusCode.Forbidden);
}
/// <summary>§P.6 — Producto sin histórico → 200 con items=[], total=0, page=1, pageSize=20.</summary>
[Fact]
public async Task GetPrices_EmptyHistory_Returns200WithPagedResultEmpty()
{ {
var productId = await SeedProductAsync(); var productId = await SeedProductAsync();
var token = GetAdminToken(); var token = GetAdminToken();
@@ -158,42 +173,138 @@ public sealed class ProductPricesControllerTests : IAsyncLifetime
var resp = await _client.SendAsync(req); var resp = await _client.SendAsync(req);
resp.StatusCode.Should().Be(HttpStatusCode.OK); resp.StatusCode.Should().Be(HttpStatusCode.OK);
var json = await resp.Content.ReadFromJsonAsync<JsonElement>(); var paged = await resp.Content.ReadFromJsonAsync<PagedResult<ProductPriceDto>>();
json.ValueKind.Should().Be(JsonValueKind.Array); paged.Should().NotBeNull();
json.GetArrayLength().Should().Be(0); paged!.Items.Should().BeEmpty();
paged.Page.Should().Be(1);
paged.PageSize.Should().Be(20);
paged.Total.Should().Be(0);
} }
/// <summary>§P.1 — 10 precios, sin query params → defaults: page=1, pageSize=20, total=10, items=10.</summary>
[Fact] [Fact]
public async Task GetPrices_WithHistory_Returns200OrderedDescending() public async Task GetPrices_TenPrices_NoParams_ReturnsDefaultsPagedResult()
{ {
var productId = await SeedProductAsync(); var productId = await SeedProductAsync();
// Seed 10 prices — all but the last have explicit PVT to respect UX_ProductPrices_Active
// Seed 3 prices: 2 closed + 1 active (in ascending order to verify API returns DESC) for (var i = 1; i <= 10; i++)
await SeedPriceDirectAsync(productId, 50m, new DateOnly(2026, 1, 1), new DateOnly(2026, 1, 31)); {
await SeedPriceDirectAsync(productId, 75m, new DateOnly(2026, 2, 1), new DateOnly(2026, 2, 28)); var pvt = i < 10 ? (DateOnly?)new DateOnly(2026, 1, i) : null;
await SeedPriceDirectAsync(productId, 100m, new DateOnly(2026, 3, 1), null); await SeedPriceDirectAsync(productId, i * 10m, new DateOnly(2026, 1, i), pvt);
}
var token = GetAdminToken(); var token = GetAdminToken();
using var req = BuildRequest(HttpMethod.Get, $"/api/v1/products/{productId}/prices", token: token); using var req = BuildRequest(HttpMethod.Get, $"/api/v1/products/{productId}/prices", token: token);
var resp = await _client.SendAsync(req); var resp = await _client.SendAsync(req);
resp.StatusCode.Should().Be(HttpStatusCode.OK); resp.StatusCode.Should().Be(HttpStatusCode.OK);
var items = await resp.Content.ReadFromJsonAsync<JsonElement>(); var paged = await resp.Content.ReadFromJsonAsync<PagedResult<ProductPriceDto>>();
items.GetArrayLength().Should().Be(3); paged.Should().NotBeNull();
paged!.Page.Should().Be(1);
// First item = most recent (active, March) paged.PageSize.Should().Be(20);
var first = items[0]; paged.Total.Should().Be(10);
first.GetProperty("priceValidFrom").GetString().Should().Be("2026-03-01"); paged.Items.Should().HaveCount(10);
first.GetProperty("isActive").GetBoolean().Should().BeTrue(); // First item must be most recent (Jan 10)
first.GetProperty("priceValidTo").ValueKind.Should().Be(JsonValueKind.Null); paged.Items[0].PriceValidFrom.Should().Be(new DateOnly(2026, 1, 10));
// Last item = oldest (January)
var last = items[2];
last.GetProperty("priceValidFrom").GetString().Should().Be("2026-01-01");
last.GetProperty("isActive").GetBoolean().Should().BeFalse();
last.GetProperty("priceValidTo").GetString().Should().Be("2026-01-31");
} }
/// <summary>§P.2 — 30 precios, page=2, pageSize=10 → items 11-20 ordenados DESC.</summary>
[Fact]
public async Task GetPrices_ThirtyPrices_Page2PageSize10_ReturnsCorrectPage()
{
var productId = await SeedProductAsync();
// Seed 30 prices — all but the last have explicit PVT to respect UX_ProductPrices_Active
for (var i = 1; i <= 30; i++)
{
var pvt = i < 30 ? (DateOnly?)new DateOnly(2026, 1, i) : null;
await SeedPriceDirectAsync(productId, i * 5m, new DateOnly(2026, 1, i), pvt);
}
var token = GetAdminToken();
using var req = BuildRequest(
HttpMethod.Get, $"/api/v1/products/{productId}/prices?page=2&pageSize=10", token: token);
var resp = await _client.SendAsync(req);
resp.StatusCode.Should().Be(HttpStatusCode.OK);
var paged = await resp.Content.ReadFromJsonAsync<PagedResult<ProductPriceDto>>();
paged.Should().NotBeNull();
paged!.Page.Should().Be(2);
paged.PageSize.Should().Be(10);
paged.Total.Should().Be(30);
paged.Items.Should().HaveCount(10);
// Ordered DESC by PVF: rank 11-20 from newest = Jan 20 down to Jan 11
paged.Items[0].PriceValidFrom.Should().Be(new DateOnly(2026, 1, 20));
paged.Items[9].PriceValidFrom.Should().Be(new DateOnly(2026, 1, 11));
}
/// <summary>§P.3 — 30 precios, page=10, pageSize=10 → items=[], total=30.</summary>
[Fact]
public async Task GetPrices_ThirtyPrices_PageBeyondTotal_ReturnsEmptyItems()
{
var productId = await SeedProductAsync();
for (var i = 1; i <= 30; i++)
{
var pvt = i < 30 ? (DateOnly?)new DateOnly(2026, 1, i) : null;
await SeedPriceDirectAsync(productId, i * 5m, new DateOnly(2026, 1, i), pvt);
}
var token = GetAdminToken();
using var req = BuildRequest(
HttpMethod.Get, $"/api/v1/products/{productId}/prices?page=10&pageSize=10", token: token);
var resp = await _client.SendAsync(req);
resp.StatusCode.Should().Be(HttpStatusCode.OK);
var paged = await resp.Content.ReadFromJsonAsync<PagedResult<ProductPriceDto>>();
paged.Should().NotBeNull();
paged!.Page.Should().Be(10);
paged.PageSize.Should().Be(10);
paged.Total.Should().Be(30);
paged.Items.Should().BeEmpty();
}
/// <summary>§P.4 — pageSize=500 → clamp to 100 en la respuesta.</summary>
[Fact]
public async Task GetPrices_PageSizeOver100_ClampsTo100()
{
var productId = await SeedProductAsync();
// Seed 5 prices — all but the last have explicit PVT to respect UX_ProductPrices_Active
for (var i = 1; i <= 5; i++)
{
var pvt = i < 5 ? (DateOnly?)new DateOnly(2026, 1, i) : null;
await SeedPriceDirectAsync(productId, i * 10m, new DateOnly(2026, 1, i), pvt);
}
var token = GetAdminToken();
using var req = BuildRequest(
HttpMethod.Get, $"/api/v1/products/{productId}/prices?pageSize=500", token: token);
var resp = await _client.SendAsync(req);
resp.StatusCode.Should().Be(HttpStatusCode.OK);
var paged = await resp.Content.ReadFromJsonAsync<PagedResult<ProductPriceDto>>();
paged.Should().NotBeNull();
paged!.PageSize.Should().Be(100, "pageSize must be clamped to max 100");
paged.Items.Should().HaveCount(5);
}
/// <summary>§P.5 — page=0 → clamp to 1.</summary>
[Fact]
public async Task GetPrices_PageZero_ClampsToOne()
{
var productId = await SeedProductAsync();
await SeedPriceDirectAsync(productId, 100m, new DateOnly(2026, 1, 1), null); // single active row
var token = GetAdminToken();
using var req = BuildRequest(
HttpMethod.Get, $"/api/v1/products/{productId}/prices?page=0", token: token);
var resp = await _client.SendAsync(req);
resp.StatusCode.Should().Be(HttpStatusCode.OK);
var paged = await resp.Content.ReadFromJsonAsync<PagedResult<ProductPriceDto>>();
paged.Should().NotBeNull();
paged!.Page.Should().Be(1, "page=0 must be clamped to 1");
}
/// <summary>§P.7 — Producto inexistente → 404.</summary>
[Fact] [Fact]
public async Task GetPrices_ProductNotFound_Returns404() public async Task GetPrices_ProductNotFound_Returns404()
{ {
@@ -206,6 +317,41 @@ public sealed class ProductPricesControllerTests : IAsyncLifetime
body.GetProperty("error").GetString().Should().Be("product_not_found"); body.GetProperty("error").GetString().Should().Be("product_not_found");
} }
/// <summary>
/// §P.1 compat — 3 prices, no params → PagedResult with items ordered DESC.
/// Replaces the old GetPrices_WithHistory_Returns200OrderedDescending.
/// </summary>
[Fact]
public async Task GetPrices_WithHistory_Returns200OrderedDescendingPaged()
{
var productId = await SeedProductAsync();
// Seed 3 prices: 2 closed + 1 active (ascending order to verify API returns DESC)
await SeedPriceDirectAsync(productId, 50m, new DateOnly(2026, 1, 1), new DateOnly(2026, 1, 31));
await SeedPriceDirectAsync(productId, 75m, new DateOnly(2026, 2, 1), new DateOnly(2026, 2, 28));
await SeedPriceDirectAsync(productId, 100m, new DateOnly(2026, 3, 1), null);
var token = GetAdminToken();
using var req = BuildRequest(HttpMethod.Get, $"/api/v1/products/{productId}/prices", token: token);
var resp = await _client.SendAsync(req);
resp.StatusCode.Should().Be(HttpStatusCode.OK);
var paged = await resp.Content.ReadFromJsonAsync<PagedResult<ProductPriceDto>>();
paged.Should().NotBeNull();
paged!.Total.Should().Be(3);
paged.Items.Should().HaveCount(3);
// First item = most recent (active, March)
paged.Items[0].PriceValidFrom.Should().Be(new DateOnly(2026, 3, 1));
paged.Items[0].IsActive.Should().BeTrue();
paged.Items[0].PriceValidTo.Should().BeNull();
// Last item = oldest (January)
paged.Items[2].PriceValidFrom.Should().Be(new DateOnly(2026, 1, 1));
paged.Items[2].IsActive.Should().BeFalse();
paged.Items[2].PriceValidTo.Should().Be(new DateOnly(2026, 1, 31));
}
// ── POST /api/v1/admin/products/{id}/prices ─────────────────────────────── // ── POST /api/v1/admin/products/{id}/prices ───────────────────────────────
[Fact] [Fact]

View File

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

View File

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

View File

@@ -1,6 +1,7 @@
using Dapper; using Dapper;
using FluentAssertions; using FluentAssertions;
using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClient;
using SIGCM2.Application.Common;
using SIGCM2.Domain.Exceptions; using SIGCM2.Domain.Exceptions;
using SIGCM2.Infrastructure.Persistence; using SIGCM2.Infrastructure.Persistence;
using SIGCM2.TestSupport; using SIGCM2.TestSupport;
@@ -337,7 +338,7 @@ public class ProductPriceRepositoryIntegrationTests : IAsyncLifetime
// Batch 4 — Via ProductPriceRepository (Dapper wrapper) // Batch 4 — Via ProductPriceRepository (Dapper wrapper)
// ───────────────────────────────────────────────────────────────────────── // ─────────────────────────────────────────────────────────────────────────
// §REQ-4.1 — GetByProductIdAsync orders descending by PriceValidFrom // §REQ-4.1 / §P.1 — GetByProductIdAsync (paginated) orders descending by PriceValidFrom
[Fact] [Fact]
public async Task GetByProductIdAsync_MultipleRows_OrdersDescendingByPriceValidFrom() public async Task GetByProductIdAsync_MultipleRows_OrdersDescendingByPriceValidFrom()
{ {
@@ -350,22 +351,93 @@ public class ProductPriceRepositoryIntegrationTests : IAsyncLifetime
await ExecAddPriceSpAsync(seedConn, _defaultProductId, 300.00m, new DateOnly(2026, 5, 1)); await ExecAddPriceSpAsync(seedConn, _defaultProductId, 300.00m, new DateOnly(2026, 5, 1));
var repo = BuildRepository(); var repo = BuildRepository();
var result = await repo.GetByProductIdAsync(_defaultProductId); var result = await repo.GetByProductIdAsync(_defaultProductId, page: 1, pageSize: 20);
result.Should().HaveCount(3); result.Total.Should().Be(3);
result[0].PriceValidFrom.Should().Be(new DateOnly(2026, 5, 1), "most recent first"); result.Items.Should().HaveCount(3);
result[1].PriceValidFrom.Should().Be(new DateOnly(2026, 3, 1)); result.Items[0].PriceValidFrom.Should().Be(new DateOnly(2026, 5, 1), "most recent first");
result[2].PriceValidFrom.Should().Be(new DateOnly(2026, 1, 1)); result.Items[1].PriceValidFrom.Should().Be(new DateOnly(2026, 3, 1));
result.Items[2].PriceValidFrom.Should().Be(new DateOnly(2026, 1, 1));
} }
// §REQ-4.3 — GetByProductIdAsync returns empty list when no history // §REQ-4.3 / §P.6 — GetByProductIdAsync returns empty PagedResult when no history
[Fact] [Fact]
public async Task GetByProductIdAsync_NoHistory_ReturnsEmptyList() public async Task GetByProductIdAsync_NoHistory_ReturnsEmptyPagedResult()
{ {
var repo = BuildRepository(); var repo = BuildRepository();
var result = await repo.GetByProductIdAsync(_defaultProductId); var result = await repo.GetByProductIdAsync(_defaultProductId, page: 1, pageSize: 20);
result.Should().BeEmpty("product exists but has no price history yet"); result.Items.Should().BeEmpty("product exists but has no price history yet");
result.Total.Should().Be(0);
result.Page.Should().Be(1);
result.PageSize.Should().Be(20);
}
// §P.2 — OFFSET/FETCH: page 2 with pageSize=2 returns correct items
[Fact]
public async Task GetByProductIdAsync_Page2_ReturnsCorrectOffset()
{
await using var seedConn = new SqlConnection(TestConnectionStrings.AppTestDb);
await seedConn.OpenAsync();
// Seed 5 prices: PVF Jan-1 through Jan-5 (DESC order: Jan5, Jan4, Jan3, Jan2, Jan1)
await ExecAddPriceSpAsync(seedConn, _defaultProductId, 100.00m, new DateOnly(2026, 1, 1));
await ExecAddPriceSpAsync(seedConn, _defaultProductId, 200.00m, new DateOnly(2026, 1, 2));
await ExecAddPriceSpAsync(seedConn, _defaultProductId, 300.00m, new DateOnly(2026, 1, 3));
await ExecAddPriceSpAsync(seedConn, _defaultProductId, 400.00m, new DateOnly(2026, 1, 4));
await ExecAddPriceSpAsync(seedConn, _defaultProductId, 500.00m, new DateOnly(2026, 1, 5));
var repo = BuildRepository();
// page=2, pageSize=2 → skip 2 → items at rank 3 and 4 (Jan3, Jan2)
var result = await repo.GetByProductIdAsync(_defaultProductId, page: 2, pageSize: 2);
result.Total.Should().Be(5);
result.Page.Should().Be(2);
result.PageSize.Should().Be(2);
result.Items.Should().HaveCount(2);
result.Items[0].PriceValidFrom.Should().Be(new DateOnly(2026, 1, 3), "rank 3 in DESC order");
result.Items[1].PriceValidFrom.Should().Be(new DateOnly(2026, 1, 2), "rank 4 in DESC order");
}
// §P.3 — OFFSET/FETCH: page beyond total → empty items, correct total
[Fact]
public async Task GetByProductIdAsync_PageBeyondTotal_ReturnsEmptyItemsWithCorrectTotal()
{
await using var seedConn = new SqlConnection(TestConnectionStrings.AppTestDb);
await seedConn.OpenAsync();
await ExecAddPriceSpAsync(seedConn, _defaultProductId, 100.00m, new DateOnly(2026, 1, 1));
await ExecAddPriceSpAsync(seedConn, _defaultProductId, 200.00m, new DateOnly(2026, 2, 1));
await ExecAddPriceSpAsync(seedConn, _defaultProductId, 300.00m, new DateOnly(2026, 3, 1));
var repo = BuildRepository();
var result = await repo.GetByProductIdAsync(_defaultProductId, page: 100, pageSize: 10);
result.Total.Should().Be(3, "COUNT always reflects actual total regardless of page");
result.Items.Should().BeEmpty("offset far beyond available data");
result.Page.Should().Be(100);
}
// §P.2 no-overlap — two different pages have no overlapping items
[Fact]
public async Task GetByProductIdAsync_TwoPages_HaveNoOverlap()
{
await using var seedConn = new SqlConnection(TestConnectionStrings.AppTestDb);
await seedConn.OpenAsync();
for (var i = 1; i <= 6; i++)
await ExecAddPriceSpAsync(seedConn, _defaultProductId, i * 100m, new DateOnly(2026, 1, i));
var repo = BuildRepository();
var page1 = await repo.GetByProductIdAsync(_defaultProductId, page: 1, pageSize: 3);
var page2 = await repo.GetByProductIdAsync(_defaultProductId, page: 2, pageSize: 3);
page1.Items.Select(p => p.PriceValidFrom)
.Intersect(page2.Items.Select(p => p.PriceValidFrom))
.Should().BeEmpty("pages must not overlap");
page1.Total.Should().Be(6);
page2.Total.Should().Be(6);
} }
// §REQ-4.4 — GetActiveAsync: exact boundary PriceValidFrom = query date → returns row // §REQ-4.4 — GetActiveAsync: exact boundary PriceValidFrom = query date → returns row