184 lines
7.0 KiB
C#
184 lines
7.0 KiB
C#
using AutoMapper;
|
|
using HardwarePerformance.Models;
|
|
using HardwarePerformance.Models.DTOs;
|
|
using HardwarePerformance.Services;
|
|
using Moq;
|
|
|
|
namespace HardwarePerformance.Tests.Services
|
|
{
|
|
public class ProductServiceTests
|
|
{
|
|
private readonly Mock<IProductRepository> _mockProductRepository;
|
|
private readonly Mock<IMapper> _mockMapper;
|
|
private readonly ProductService _productService;
|
|
|
|
public ProductServiceTests()
|
|
{
|
|
_mockProductRepository = new Mock<IProductRepository>();
|
|
_mockMapper = new Mock<IMapper>();
|
|
_productService = new ProductService(_mockProductRepository.Object, _mockMapper.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetProductsByCategoryAsync_ReturnsPagedProducts()
|
|
{
|
|
// Arrange
|
|
var categoryId = 1;
|
|
var page = 1;
|
|
var pageSize = 10;
|
|
var sortBy = "Name";
|
|
var sortOrder = "asc";
|
|
|
|
var products = new List<Product>
|
|
{
|
|
new Product { Id = 1, Name = "Intel Core i9", CategoryId = categoryId },
|
|
new Product { Id = 2, Name = "AMD Ryzen 9", CategoryId = categoryId }
|
|
};
|
|
|
|
var productDtos = new List<ProductListDto>
|
|
{
|
|
new ProductListDto { Id = 1, Name = "Intel Core i9" },
|
|
new ProductListDto { Id = 2, Name = "AMD Ryzen 9" }
|
|
};
|
|
|
|
var pagedResult = new PagedResultDto<ProductListDto>
|
|
{
|
|
Items = productDtos,
|
|
TotalCount = 2,
|
|
Page = page,
|
|
PageSize = pageSize
|
|
};
|
|
|
|
_mockProductRepository.Setup(repo => repo.GetByCategoryAsync(categoryId, page, pageSize, sortBy, sortOrder))
|
|
.ReturnsAsync(products);
|
|
|
|
_mockProductRepository.Setup(repo => repo.CountAsync(categoryId))
|
|
.ReturnsAsync(2);
|
|
|
|
_mockMapper.Setup(m => m.Map<IEnumerable<ProductListDto>>(products))
|
|
.Returns(productDtos);
|
|
|
|
// Act
|
|
var result = await _productService.GetProductsByCategoryAsync(categoryId, page, pageSize, sortBy, sortOrder);
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Equal(2, result.TotalCount);
|
|
Assert.Equal(2, result.Items.Count());
|
|
_mockProductRepository.Verify(repo => repo.GetByCategoryAsync(categoryId, page, pageSize, sortBy, sortOrder), Times.Once);
|
|
_mockProductRepository.Verify(repo => repo.CountAsync(categoryId), Times.Once);
|
|
_mockMapper.Verify(m => m.Map<IEnumerable<ProductListDto>>(products), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetProductByIdAsync_ExistingProduct_ReturnsProductDto()
|
|
{
|
|
// Arrange
|
|
var productId = 1;
|
|
var product = new Product
|
|
{
|
|
Id = productId,
|
|
Name = "Intel Core i9",
|
|
CategoryId = 1,
|
|
PerformanceScores = new List<PerformanceScore>(),
|
|
Specifications = new List<Specification>()
|
|
};
|
|
|
|
var productDto = new ProductDto
|
|
{
|
|
Id = productId,
|
|
Name = "Intel Core i9",
|
|
CategoryId = 1,
|
|
PerformanceScores = new List<PerformanceScore>(),
|
|
Specifications = new List<Specification>()
|
|
};
|
|
|
|
_mockProductRepository.Setup(repo => repo.GetByIdAsync(productId))
|
|
.ReturnsAsync(product);
|
|
|
|
_mockMapper.Setup(m => m.Map<ProductDto>(product))
|
|
.Returns(productDto);
|
|
|
|
// Act
|
|
var result = await _productService.GetProductByIdAsync(productId);
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Equal(productId, result.Id);
|
|
Assert.Equal("Intel Core i9", result.Name);
|
|
_mockProductRepository.Verify(repo => repo.GetByIdAsync(productId), Times.Once);
|
|
_mockMapper.Verify(m => m.Map<ProductDto>(product), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetProductByIdAsync_NonExistingProduct_ReturnsNull()
|
|
{
|
|
// Arrange
|
|
var productId = 999;
|
|
|
|
_mockProductRepository.Setup(repo => repo.GetByIdAsync(productId))
|
|
.ReturnsAsync((Product?)null);
|
|
|
|
// Act
|
|
var result = await _productService.GetProductByIdAsync(productId);
|
|
|
|
// Assert
|
|
Assert.Null(result);
|
|
_mockProductRepository.Verify(repo => repo.GetByIdAsync(productId), Times.Once);
|
|
_mockMapper.Verify(m => m.Map<ProductDto>(It.IsAny<Product>()), Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SearchProductsAsync_ReturnsPagedSearchResults()
|
|
{
|
|
// Arrange
|
|
var query = "Intel";
|
|
var categoryId = 1;
|
|
var manufacturer = "Intel";
|
|
var minScore = 1000;
|
|
var maxScore = 5000;
|
|
var page = 1;
|
|
var pageSize = 10;
|
|
|
|
var products = new List<Product>
|
|
{
|
|
new Product { Id = 1, Name = "Intel Core i9", Manufacturer = "Intel", CategoryId = categoryId, PerformanceScore = 4000 },
|
|
new Product { Id = 2, Name = "Intel Core i7", Manufacturer = "Intel", CategoryId = categoryId, PerformanceScore = 3000 }
|
|
};
|
|
|
|
var productDtos = new List<ProductListDto>
|
|
{
|
|
new ProductListDto { Id = 1, Name = "Intel Core i9" },
|
|
new ProductListDto { Id = 2, Name = "Intel Core i7" }
|
|
};
|
|
|
|
var pagedResult = new PagedResultDto<ProductListDto>
|
|
{
|
|
Items = productDtos,
|
|
TotalCount = 2,
|
|
Page = page,
|
|
PageSize = pageSize
|
|
};
|
|
|
|
_mockProductRepository.Setup(repo => repo.SearchAsync(query, categoryId, manufacturer, minScore, maxScore, page, pageSize))
|
|
.ReturnsAsync(products);
|
|
|
|
_mockProductRepository.Setup(repo => repo.CountSearchResultsAsync(query, categoryId, manufacturer, minScore, maxScore))
|
|
.ReturnsAsync(2);
|
|
|
|
_mockMapper.Setup(m => m.Map<IEnumerable<ProductListDto>>(products))
|
|
.Returns(productDtos);
|
|
|
|
// Act
|
|
var result = await _productService.SearchProductsAsync(query, categoryId, manufacturer, minScore, maxScore, page, pageSize);
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Equal(2, result.TotalCount);
|
|
Assert.Equal(2, result.Items.Count());
|
|
_mockProductRepository.Verify(repo => repo.SearchAsync(query, categoryId, manufacturer, minScore, maxScore, page, pageSize), Times.Once);
|
|
_mockProductRepository.Verify(repo => repo.CountSearchResultsAsync(query, categoryId, manufacturer, minScore, maxScore), Times.Once);
|
|
_mockMapper.Verify(m => m.Map<IEnumerable<ProductListDto>>(products), Times.Once);
|
|
}
|
|
}
|
|
} |