测试
This commit is contained in:
173
HardwarePerformance.Tests/Services/ComparisonServiceTests.cs
Normal file
173
HardwarePerformance.Tests/Services/ComparisonServiceTests.cs
Normal file
@@ -0,0 +1,173 @@
|
||||
using AutoMapper;
|
||||
using HardwarePerformance.Models;
|
||||
using HardwarePerformance.Models.DTOs;
|
||||
using HardwarePerformance.Services;
|
||||
using Moq;
|
||||
|
||||
namespace HardwarePerformance.Tests.Services
|
||||
{
|
||||
public class ComparisonServiceTests
|
||||
{
|
||||
private readonly Mock<IProductRepository> _mockProductRepository;
|
||||
private readonly Mock<IMapper> _mockMapper;
|
||||
private readonly ComparisonService _comparisonService;
|
||||
|
||||
public ComparisonServiceTests()
|
||||
{
|
||||
_mockProductRepository = new Mock<IProductRepository>();
|
||||
_mockMapper = new Mock<IMapper>();
|
||||
_comparisonService = new ComparisonService(_mockProductRepository.Object, _mockMapper.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CompareProductsAsync_ValidProductIds_ReturnsComparisonData()
|
||||
{
|
||||
// Arrange
|
||||
var productIds = new List<int> { 1, 2 };
|
||||
|
||||
var products = new List<Product>
|
||||
{
|
||||
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<PerformanceScore>
|
||||
{
|
||||
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<Specification>
|
||||
{
|
||||
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<PerformanceScore>
|
||||
{
|
||||
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<Specification>
|
||||
{
|
||||
new Specification { Id = 3, ProductId = 2, Name = "Cores", Value = "8" },
|
||||
new Specification { Id = 4, ProductId = 2, Name = "Threads", Value = "16" }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var productDtos = new List<ProductDto>
|
||||
{
|
||||
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<PerformanceScore>
|
||||
{
|
||||
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<Specification>
|
||||
{
|
||||
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<PerformanceScore>
|
||||
{
|
||||
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<Specification>
|
||||
{
|
||||
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<List<ProductDto>>(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<List<ProductDto>>(products), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CompareProductsAsync_EmptyProductIds_ThrowsArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
var productIds = new List<int>();
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<ArgumentException>(async () =>
|
||||
await _comparisonService.CompareProductsAsync(productIds));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CompareProductsAsync_TooManyProductIds_ThrowsArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
var productIds = new List<int> { 1, 2, 3, 4, 5 };
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<ArgumentException>(async () =>
|
||||
await _comparisonService.CompareProductsAsync(productIds));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CompareProductsAsync_NonExistingProductIds_ThrowsKeyNotFoundException()
|
||||
{
|
||||
// Arrange
|
||||
var productIds = new List<int> { 1, 2 };
|
||||
|
||||
_mockProductRepository.Setup(repo => repo.GetByIdsAsync(productIds))
|
||||
.ReturnsAsync(new List<Product>());
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<KeyNotFoundException>(async () =>
|
||||
await _comparisonService.CompareProductsAsync(productIds));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user