初始化
Some checks failed
Some checks failed
This commit is contained in:
@@ -0,0 +1,561 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using HardwarePerformance.Application.Interfaces;
|
||||
using HardwarePerformance.Core.Entities;
|
||||
using HardwarePerformance.Infrastructure.Data;
|
||||
using HardwarePerformance.Infrastructure.Repositories;
|
||||
|
||||
namespace HardwarePerformance.Infrastructure.Tests.Repositories
|
||||
{
|
||||
/// <summary>
|
||||
/// ProductRepository单元测试
|
||||
/// </summary>
|
||||
public class ProductRepositoryTests : TestBase
|
||||
{
|
||||
private readonly IProductRepository _productRepository;
|
||||
|
||||
public ProductRepositoryTests()
|
||||
{
|
||||
_productRepository = new ProductRepository(_context);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_WithValidId_ReturnsProduct()
|
||||
{
|
||||
// Arrange
|
||||
var productId = 1;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetByIdAsync(productId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(productId, result.Id);
|
||||
Assert.Equal("Snapdragon 8 Gen 3", result.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync_WithInvalidId_ReturnsNull()
|
||||
{
|
||||
// Arrange
|
||||
var productId = 999;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetByIdAsync(productId);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync_ReturnsAllProducts()
|
||||
{
|
||||
// Act
|
||||
var result = await _productRepository.GetAllAsync();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(6, result.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FindAsync_WithValidPredicate_ReturnsFilteredProducts()
|
||||
{
|
||||
// Arrange
|
||||
Expression<Func<Product, bool>> predicate = p => p.CategoryId == 1;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.FindAsync(predicate);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Count());
|
||||
Assert.All(result, p => Assert.Equal(1, p.CategoryId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistsAsync_WithExistingProduct_ReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
Expression<Func<Product, bool>> predicate = p => p.Name == "Snapdragon 8 Gen 3";
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.ExistsAsync(predicate);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistsAsync_WithNonExistingProduct_ReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
Expression<Func<Product, bool>> predicate = p => p.Name == "不存在的产品";
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.ExistsAsync(predicate);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddAsync_WithValidProduct_ReturnsAddedProduct()
|
||||
{
|
||||
// Arrange
|
||||
var newProduct = new Product
|
||||
{
|
||||
Name = "测试产品",
|
||||
Model = "Test-001",
|
||||
Manufacturer = "测试厂商",
|
||||
CategoryId = 1,
|
||||
ReleaseDate = DateTime.Now,
|
||||
ImageUrl = "test.jpg",
|
||||
Description = "测试产品描述",
|
||||
CurrentRank = 0
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.AddAsync(newProduct);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.True(result.Id > 0);
|
||||
Assert.Equal("测试产品", result.Name);
|
||||
|
||||
// 验证是否已添加到数据库
|
||||
var savedProduct = await _productRepository.GetByIdAsync(result.Id);
|
||||
Assert.NotNull(savedProduct);
|
||||
Assert.Equal("测试产品", savedProduct.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WithValidProduct_ReturnsUpdatedProduct()
|
||||
{
|
||||
// Arrange
|
||||
var product = await _productRepository.GetByIdAsync(1);
|
||||
product.Description = "更新后的描述";
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.UpdateAsync(product);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("更新后的描述", result.Description);
|
||||
|
||||
// 验证是否已更新到数据库
|
||||
var savedProduct = await _productRepository.GetByIdAsync(1);
|
||||
Assert.Equal("更新后的描述", savedProduct.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteAsync_WithValidProduct_ReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var product = await _productRepository.GetByIdAsync(1);
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.DeleteAsync(product);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
|
||||
// 验证是否已从数据库删除
|
||||
var deletedProduct = await _productRepository.GetByIdAsync(1);
|
||||
Assert.Null(deletedProduct);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteByIdAsync_WithValidId_ReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var productId = 1;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.DeleteByIdAsync(productId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
|
||||
// 验证是否已从数据库删除
|
||||
var deletedProduct = await _productRepository.GetByIdAsync(productId);
|
||||
Assert.Null(deletedProduct);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteByIdAsync_WithInvalidId_ReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var productId = 999;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.DeleteByIdAsync(productId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CountAsync_ReturnsCorrectCount()
|
||||
{
|
||||
// Act
|
||||
var result = await _productRepository.CountAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(6, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CountAsync_WithPredicate_ReturnsCorrectCount()
|
||||
{
|
||||
// Arrange
|
||||
Expression<Func<Product, bool>> predicate = p => p.CategoryId == 1;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.CountAsync(predicate);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByCategoryAsync_WithValidCategory_ReturnsPagedProducts()
|
||||
{
|
||||
// Arrange
|
||||
var categoryId = 1;
|
||||
var page = 1;
|
||||
var pageSize = 10;
|
||||
var sortBy = "Name";
|
||||
var ascending = true;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetByCategoryAsync(categoryId, page, pageSize, sortBy, ascending);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Items.Count());
|
||||
Assert.Equal(2, result.TotalCount);
|
||||
Assert.Equal(1, result.Page);
|
||||
Assert.Equal(10, result.PageSize);
|
||||
Assert.Equal(1, result.TotalPages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByCategoryAsync_WithInvalidCategory_ReturnsEmptyResult()
|
||||
{
|
||||
// Arrange
|
||||
var categoryId = 999;
|
||||
var page = 1;
|
||||
var pageSize = 10;
|
||||
var sortBy = "Name";
|
||||
var ascending = true;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetByCategoryAsync(categoryId, page, pageSize, sortBy, ascending);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Empty(result.Items);
|
||||
Assert.Equal(0, result.TotalCount);
|
||||
Assert.Equal(1, result.Page);
|
||||
Assert.Equal(10, result.PageSize);
|
||||
Assert.Equal(0, result.TotalPages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByCategoryAsync_WithPagination_ReturnsCorrectPage()
|
||||
{
|
||||
// Arrange
|
||||
var categoryId = 1;
|
||||
var page = 1;
|
||||
var pageSize = 1;
|
||||
var sortBy = "Name";
|
||||
var ascending = true;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetByCategoryAsync(categoryId, page, pageSize, sortBy, ascending);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.Items);
|
||||
Assert.Equal(2, result.TotalCount);
|
||||
Assert.Equal(1, result.Page);
|
||||
Assert.Equal(1, result.PageSize);
|
||||
Assert.Equal(2, result.TotalPages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByCategoryAsync_WithSortByScore_ReturnsSortedProducts()
|
||||
{
|
||||
// Arrange
|
||||
var categoryId = 1;
|
||||
var page = 1;
|
||||
var pageSize = 10;
|
||||
var sortBy = "PerformanceScore";
|
||||
var ascending = false;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetByCategoryAsync(categoryId, page, pageSize, sortBy, ascending);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Items.Count());
|
||||
|
||||
// 验证按性能分数降序排列
|
||||
var products = result.Items.ToList();
|
||||
Assert.True(products[0].PerformanceScore >= products[1].PerformanceScore);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SearchAsync_WithValidQuery_ReturnsPagedResults()
|
||||
{
|
||||
// Arrange
|
||||
var query = "Snapdragon";
|
||||
var page = 1;
|
||||
var pageSize = 10;
|
||||
var sortBy = "Name";
|
||||
var ascending = true;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.SearchAsync(query, page, pageSize, sortBy, ascending);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.Items);
|
||||
Assert.Equal(1, result.TotalCount);
|
||||
Assert.Equal(1, result.Page);
|
||||
Assert.Equal(10, result.PageSize);
|
||||
Assert.Equal(1, result.TotalPages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SearchAsync_WithInvalidQuery_ReturnsEmptyResult()
|
||||
{
|
||||
// Arrange
|
||||
var query = "不存在的产品";
|
||||
var page = 1;
|
||||
var pageSize = 10;
|
||||
var sortBy = "Name";
|
||||
var ascending = true;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.SearchAsync(query, page, pageSize, sortBy, ascending);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Empty(result.Items);
|
||||
Assert.Equal(0, result.TotalCount);
|
||||
Assert.Equal(1, result.Page);
|
||||
Assert.Equal(10, result.PageSize);
|
||||
Assert.Equal(0, result.TotalPages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SearchAsync_WithCategoryFilter_ReturnsFilteredResults()
|
||||
{
|
||||
// Arrange
|
||||
var query = "Gen";
|
||||
var categoryId = 1;
|
||||
var page = 1;
|
||||
var pageSize = 10;
|
||||
var sortBy = "Name";
|
||||
var ascending = true;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.SearchAsync(query, page, pageSize, sortBy, ascending, categoryId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.Items);
|
||||
Assert.Equal(1, result.TotalCount);
|
||||
Assert.Equal(1, result.Items.First().CategoryId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SearchAsync_WithManufacturerFilter_ReturnsFilteredResults()
|
||||
{
|
||||
// Arrange
|
||||
var query = "Gen";
|
||||
var manufacturer = "Qualcomm";
|
||||
var page = 1;
|
||||
var pageSize = 10;
|
||||
var sortBy = "Name";
|
||||
var ascending = true;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.SearchAsync(query, page, pageSize, sortBy, ascending, null, manufacturer);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.Items);
|
||||
Assert.Equal("Qualcomm", result.Items.First().Manufacturer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FilterAsync_WithScoreRange_ReturnsFilteredResults()
|
||||
{
|
||||
// Arrange
|
||||
var minScore = 2000;
|
||||
var maxScore = 3000;
|
||||
var page = 1;
|
||||
var pageSize = 10;
|
||||
var sortBy = "Name";
|
||||
var ascending = true;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.FilterAsync(minScore, maxScore, page, pageSize, sortBy, ascending);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Items.Count());
|
||||
Assert.Equal(2, result.TotalCount);
|
||||
Assert.All(result.Items, p => Assert.True(p.PerformanceScore >= minScore && p.PerformanceScore <= maxScore));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FilterAsync_WithYearFilter_ReturnsFilteredResults()
|
||||
{
|
||||
// Arrange
|
||||
var year = 2023;
|
||||
var page = 1;
|
||||
var pageSize = 10;
|
||||
var sortBy = "Name";
|
||||
var ascending = true;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.FilterAsync(null, null, page, pageSize, sortBy, ascending, year);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Items.Count());
|
||||
Assert.Equal(2, result.TotalCount);
|
||||
Assert.All(result.Items, p => Assert.Equal(year, p.ReleaseDate.Year));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FilterAsync_WithManufacturerFilter_ReturnsFilteredResults()
|
||||
{
|
||||
// Arrange
|
||||
var manufacturer = "Qualcomm";
|
||||
var page = 1;
|
||||
var pageSize = 10;
|
||||
var sortBy = "Name";
|
||||
var ascending = true;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.FilterAsync(null, null, page, pageSize, sortBy, ascending, null, manufacturer);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result.Items);
|
||||
Assert.Equal(1, result.TotalCount);
|
||||
Assert.Equal(manufacturer, result.Items.First().Manufacturer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FilterAsync_WithCategoryFilter_ReturnsFilteredResults()
|
||||
{
|
||||
// Arrange
|
||||
var categoryId = 1;
|
||||
var page = 1;
|
||||
var pageSize = 10;
|
||||
var sortBy = "Name";
|
||||
var ascending = true;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.FilterAsync(null, null, page, pageSize, sortBy, ascending, null, null, categoryId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Items.Count());
|
||||
Assert.Equal(2, result.TotalCount);
|
||||
Assert.All(result.Items, p => Assert.Equal(categoryId, p.CategoryId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetTopNByCategoryAsync_WithValidParameters_ReturnsTopProducts()
|
||||
{
|
||||
// Arrange
|
||||
var categoryId = 1;
|
||||
var n = 1;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetTopNByCategoryAsync(categoryId, n);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result);
|
||||
Assert.Equal(categoryId, result.First().CategoryId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetTopNByCategoryAsync_WithInvalidCategory_ReturnsEmptyResult()
|
||||
{
|
||||
// Arrange
|
||||
var categoryId = 999;
|
||||
var n = 5;
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetTopNByCategoryAsync(categoryId, n);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByModelAsync_WithExistingModel_ReturnsProduct()
|
||||
{
|
||||
// Arrange
|
||||
var model = "SM-G998B";
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetByModelAsync(model);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(model, result.Model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByModelAsync_WithNonExistingModel_ReturnsNull()
|
||||
{
|
||||
// Arrange
|
||||
var model = "不存在的型号";
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetByModelAsync(model);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByManufacturerAsync_WithExistingManufacturer_ReturnsProducts()
|
||||
{
|
||||
// Arrange
|
||||
var manufacturer = "Qualcomm";
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetByManufacturerAsync(manufacturer);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result);
|
||||
Assert.Equal(manufacturer, result.First().Manufacturer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByManufacturerAsync_WithNonExistingManufacturer_ReturnsEmptyResult()
|
||||
{
|
||||
// Arrange
|
||||
var manufacturer = "不存在的厂商";
|
||||
|
||||
// Act
|
||||
var result = await _productRepository.GetByManufacturerAsync(manufacturer);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Empty(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user