using AutoMapper; using HardwarePerformance.Models; using HardwarePerformance.Models.DTOs; using HardwarePerformance.Services; using Moq; namespace HardwarePerformance.Tests.Services { public class ProductServiceTests { private readonly Mock _mockProductRepository; private readonly Mock _mockMapper; private readonly ProductService _productService; public ProductServiceTests() { _mockProductRepository = new Mock(); _mockMapper = new Mock(); _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 { new Product { Id = 1, Name = "Intel Core i9", CategoryId = categoryId }, new Product { Id = 2, Name = "AMD Ryzen 9", CategoryId = categoryId } }; var productDtos = new List { new ProductListDto { Id = 1, Name = "Intel Core i9" }, new ProductListDto { Id = 2, Name = "AMD Ryzen 9" } }; var pagedResult = new PagedResultDto { 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>(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>(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(), Specifications = new List() }; var productDto = new ProductDto { Id = productId, Name = "Intel Core i9", CategoryId = 1, PerformanceScores = new List(), Specifications = new List() }; _mockProductRepository.Setup(repo => repo.GetByIdAsync(productId)) .ReturnsAsync(product); _mockMapper.Setup(m => m.Map(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(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(It.IsAny()), 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 { 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 { new ProductListDto { Id = 1, Name = "Intel Core i9" }, new ProductListDto { Id = 2, Name = "Intel Core i7" } }; var pagedResult = new PagedResultDto { 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>(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>(products), Times.Once); } } }