86 lines
2.4 KiB
C#
86 lines
2.4 KiB
C#
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);
|
|
}
|
|
}
|