测试
This commit is contained in:
97
HardwarePerformance.Tests/Services/CategoryServiceTests.cs
Normal file
97
HardwarePerformance.Tests/Services/CategoryServiceTests.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using AutoMapper;
|
||||
using HardwarePerformance.Models;
|
||||
using HardwarePerformance.Models.DTOs;
|
||||
using HardwarePerformance.Services;
|
||||
using Moq;
|
||||
|
||||
namespace HardwarePerformance.Tests.Services
|
||||
{
|
||||
public class CategoryServiceTests
|
||||
{
|
||||
private readonly Mock<ICategoryRepository> _mockCategoryRepository;
|
||||
private readonly Mock<IMapper> _mockMapper;
|
||||
private readonly CategoryService _categoryService;
|
||||
|
||||
public CategoryServiceTests()
|
||||
{
|
||||
_mockCategoryRepository = new Mock<ICategoryRepository>();
|
||||
_mockMapper = new Mock<IMapper>();
|
||||
_categoryService = new CategoryService(_mockCategoryRepository.Object, _mockMapper.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllCategoriesAsync_ReturnsAllCategories()
|
||||
{
|
||||
// Arrange
|
||||
var categories = new List<Category>
|
||||
{
|
||||
new Category { Id = 1, Name = "手机CPU", Description = "手机处理器" },
|
||||
new Category { Id = 2, Name = "电脑CPU", Description = "电脑处理器" }
|
||||
};
|
||||
|
||||
var categoryDtos = new List<CategoryDto>
|
||||
{
|
||||
new CategoryDto { Id = 1, Name = "手机CPU", Description = "手机处理器" },
|
||||
new CategoryDto { Id = 2, Name = "电脑CPU", Description = "电脑处理器" }
|
||||
};
|
||||
|
||||
_mockCategoryRepository.Setup(repo => repo.GetAllAsync())
|
||||
.ReturnsAsync(categories);
|
||||
|
||||
_mockMapper.Setup(m => m.Map<IEnumerable<CategoryDto>>(categories))
|
||||
.Returns(categoryDtos);
|
||||
|
||||
// Act
|
||||
var result = await _categoryService.GetAllCategoriesAsync();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Count());
|
||||
_mockCategoryRepository.Verify(repo => repo.GetAllAsync(), Times.Once);
|
||||
_mockMapper.Verify(m => m.Map<IEnumerable<CategoryDto>>(categories), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCategoryByIdAsync_ExistingCategory_ReturnsCategoryDto()
|
||||
{
|
||||
// Arrange
|
||||
var categoryId = 1;
|
||||
var category = new Category { Id = categoryId, Name = "手机CPU", Description = "手机处理器" };
|
||||
var categoryDto = new CategoryDto { Id = categoryId, Name = "手机CPU", Description = "手机处理器" };
|
||||
|
||||
_mockCategoryRepository.Setup(repo => repo.GetByIdAsync(categoryId))
|
||||
.ReturnsAsync(category);
|
||||
|
||||
_mockMapper.Setup(m => m.Map<CategoryDto>(category))
|
||||
.Returns(categoryDto);
|
||||
|
||||
// Act
|
||||
var result = await _categoryService.GetCategoryByIdAsync(categoryId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(categoryId, result.Id);
|
||||
Assert.Equal("手机CPU", result.Name);
|
||||
_mockCategoryRepository.Verify(repo => repo.GetByIdAsync(categoryId), Times.Once);
|
||||
_mockMapper.Verify(m => m.Map<CategoryDto>(category), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCategoryByIdAsync_NonExistingCategory_ReturnsNull()
|
||||
{
|
||||
// Arrange
|
||||
var categoryId = 999;
|
||||
|
||||
_mockCategoryRepository.Setup(repo => repo.GetByIdAsync(categoryId))
|
||||
.ReturnsAsync((Category?)null);
|
||||
|
||||
// Act
|
||||
var result = await _categoryService.GetCategoryByIdAsync(categoryId);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
_mockCategoryRepository.Verify(repo => repo.GetByIdAsync(categoryId), Times.Once);
|
||||
_mockMapper.Verify(m => m.Map<CategoryDto>(It.IsAny<Category>()), Times.Never);
|
||||
}
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
184
HardwarePerformance.Tests/Services/ProductServiceTests.cs
Normal file
184
HardwarePerformance.Tests/Services/ProductServiceTests.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user