using AutoMapper; using HardwarePerformance.Models; using HardwarePerformance.Models.DTOs; using HardwarePerformance.Services; using Moq; namespace HardwarePerformance.Tests.Services { public class ComparisonServiceTests { private readonly Mock _mockProductRepository; private readonly Mock _mockMapper; private readonly ComparisonService _comparisonService; public ComparisonServiceTests() { _mockProductRepository = new Mock(); _mockMapper = new Mock(); _comparisonService = new ComparisonService(_mockProductRepository.Object, _mockMapper.Object); } [Fact] public async Task CompareProductsAsync_ValidProductIds_ReturnsComparisonData() { // Arrange var productIds = new List { 1, 2 }; var products = new List { new Product { Id = 1, Name = "Intel Core i9", Manufacturer = "Intel", CategoryId = 1, CurrentRank = 1, ReleaseDate = DateTime.Now.AddMonths(-6), Price = 500, PerformanceScore = 4000, PerformanceScores = new List { new PerformanceScore { Id = 1, ProductId = 1, TestType = "Single-Core", Score = 1500 }, new PerformanceScore { Id = 2, ProductId = 1, TestType = "Multi-Core", Score = 8000 } }, Specifications = new List { new Specification { Id = 1, ProductId = 1, Name = "Cores", Value = "8" }, new Specification { Id = 2, ProductId = 1, Name = "Threads", Value = "16" } } }, new Product { Id = 2, Name = "AMD Ryzen 9", Manufacturer = "AMD", CategoryId = 1, CurrentRank = 2, ReleaseDate = DateTime.Now.AddMonths(-4), Price = 450, PerformanceScore = 3800, PerformanceScores = new List { new PerformanceScore { Id = 3, ProductId = 2, TestType = "Single-Core", Score = 1400 }, new PerformanceScore { Id = 4, ProductId = 2, TestType = "Multi-Core", Score = 7500 } }, Specifications = new List { new Specification { Id = 3, ProductId = 2, Name = "Cores", Value = "8" }, new Specification { Id = 4, ProductId = 2, Name = "Threads", Value = "16" } } } }; var productDtos = new List { new ProductDto { Id = 1, Name = "Intel Core i9", Manufacturer = "Intel", CategoryId = 1, CurrentRank = 1, ReleaseDate = DateTime.Now.AddMonths(-6), Price = 500, PerformanceScore = 4000, PerformanceScores = new List { new PerformanceScore { Id = 1, ProductId = 1, TestType = "Single-Core", Score = 1500 }, new PerformanceScore { Id = 2, ProductId = 1, TestType = "Multi-Core", Score = 8000 } }, Specifications = new List { new Specification { Id = 1, ProductId = 1, Name = "Cores", Value = "8" }, new Specification { Id = 2, ProductId = 1, Name = "Threads", Value = "16" } } }, new ProductDto { Id = 2, Name = "AMD Ryzen 9", Manufacturer = "AMD", CategoryId = 1, CurrentRank = 2, ReleaseDate = DateTime.Now.AddMonths(-4), Price = 450, PerformanceScore = 3800, PerformanceScores = new List { new PerformanceScore { Id = 3, ProductId = 2, TestType = "Single-Core", Score = 1400 }, new PerformanceScore { Id = 4, ProductId = 2, TestType = "Multi-Core", Score = 7500 } }, Specifications = new List { new Specification { Id = 3, ProductId = 2, Name = "Cores", Value = "8" }, new Specification { Id = 4, ProductId = 2, Name = "Threads", Value = "16" } } } }; _mockProductRepository.Setup(repo => repo.GetByIdsAsync(productIds)) .ReturnsAsync(products); _mockMapper.Setup(m => m.Map>(products)) .Returns(productDtos); // Act var result = await _comparisonService.CompareProductsAsync(productIds); // Assert Assert.NotNull(result); Assert.Equal(2, result.Products.Count); Assert.NotNull(result.ComparisonMatrix); _mockProductRepository.Verify(repo => repo.GetByIdsAsync(productIds), Times.Once); _mockMapper.Verify(m => m.Map>(products), Times.Once); } [Fact] public async Task CompareProductsAsync_EmptyProductIds_ThrowsArgumentException() { // Arrange var productIds = new List(); // Act & Assert await Assert.ThrowsAsync(async () => await _comparisonService.CompareProductsAsync(productIds)); } [Fact] public async Task CompareProductsAsync_TooManyProductIds_ThrowsArgumentException() { // Arrange var productIds = new List { 1, 2, 3, 4, 5 }; // Act & Assert await Assert.ThrowsAsync(async () => await _comparisonService.CompareProductsAsync(productIds)); } [Fact] public async Task CompareProductsAsync_NonExistingProductIds_ThrowsKeyNotFoundException() { // Arrange var productIds = new List { 1, 2 }; _mockProductRepository.Setup(repo => repo.GetByIdsAsync(productIds)) .ReturnsAsync(new List()); // Act & Assert await Assert.ThrowsAsync(async () => await _comparisonService.CompareProductsAsync(productIds)); } } }