Add initial implementation of product management API

This commit is contained in:
2026-02-21 23:51:01 +01:00
commit 8e1b023303
31 changed files with 969 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
using EndavaTask.Contracts;
using EndavaTask.Dtos;
using EndavaTask.Mappings;
using EndavaTask.Models;
using Xunit;
namespace EndavaTask.Tests;
public class EntityDtoMappingsTests
{
[Fact]
public void Product_ToDto_And_ToEntity_Roundtrip_Works()
{
var entity = new Product
{
Id = Guid.NewGuid(),
Name = "Keyboard",
Price = 90m,
CategoryId = Guid.NewGuid()
};
var dto = entity.ToDto();
var mappedBack = dto.ToEntity();
Assert.Equal(entity.Id, dto.Id);
Assert.Equal(entity.Name, dto.Name);
Assert.Equal(entity.Price, dto.Price);
Assert.Equal(entity.CategoryId, dto.CategoryId);
Assert.Equal(entity.Id, mappedBack.Id);
Assert.Equal(entity.Name, mappedBack.Name);
Assert.Equal(entity.Price, mappedBack.Price);
Assert.Equal(entity.CategoryId, mappedBack.CategoryId);
}
[Fact]
public void Category_ToDto_And_ToEntity_Roundtrip_Works()
{
var entity = new Category
{
Id = Guid.NewGuid(),
Name = "Home",
Description = "Home essentials"
};
var dto = entity.ToDto();
var mappedBack = dto.ToEntity();
Assert.Equal(entity.Id, dto.Id);
Assert.Equal(entity.Name, dto.Name);
Assert.Equal(entity.Description, dto.Description);
Assert.Equal(entity.Id, mappedBack.Id);
Assert.Equal(entity.Name, mappedBack.Name);
Assert.Equal(entity.Description, mappedBack.Description);
}
[Fact]
public void PagedProducts_ToDto_MapsMetadataAndItems()
{
var paged = new PagedResult<Product>
{
Items =
[
new Product { Id = Guid.NewGuid(), Name = "Laptop", Price = 1200m, CategoryId = Guid.NewGuid() },
new Product { Id = Guid.NewGuid(), Name = "Monitor", Price = 300m, CategoryId = Guid.NewGuid() }
],
PageNumber = 2,
PageSize = 2,
TotalCount = 5,
TotalPages = 3
};
var dtoPaged = paged.ToDto();
Assert.Equal(2, dtoPaged.Items.Count);
Assert.Equal(2, dtoPaged.PageNumber);
Assert.Equal(2, dtoPaged.PageSize);
Assert.Equal(5, dtoPaged.TotalCount);
Assert.Equal(3, dtoPaged.TotalPages);
var first = Assert.IsType<ProductDto>(dtoPaged.Items.First());
Assert.Equal("Laptop", first.Name);
}
}