P0 - 安全修复: - 移除硬编码 API Key,启动时校验必填环境变量 P1 - 高优先级: - Entity 拆分:Position.cs, Transaction.cs 独立文件 - Controller Facade 封装:IPortfolioFacade 减少依赖注入 P2 - 中优先级: - Repository 抽象:IPortfolioRepository, IMarketDataRepository - MarketDataService 拆分:组合模式整合 Tencent/Tiingo/OKX P3 - 低优先级: - DTO 命名规范:统一 PascalCase - 单元测试框架:xUnit + Moq + FluentAssertions
92 lines
2.2 KiB
C#
92 lines
2.2 KiB
C#
using AssetManager.Data;
|
|
using AssetManager.Data.Repositories;
|
|
using FluentAssertions;
|
|
using Moq;
|
|
using SqlSugar;
|
|
using Xunit;
|
|
|
|
namespace AssetManager.Tests.Repositories;
|
|
|
|
/// <summary>
|
|
/// PortfolioRepository 单元测试
|
|
/// </summary>
|
|
public class PortfolioRepositoryTests
|
|
{
|
|
private readonly Mock<ISqlSugarClient> _dbMock;
|
|
private readonly Mock<ILogger<PortfolioRepository>> _loggerMock;
|
|
|
|
public PortfolioRepositoryTests()
|
|
{
|
|
_dbMock = new Mock<ISqlSugarClient>();
|
|
_loggerMock = new Mock<ILogger<PortfolioRepository>>();
|
|
}
|
|
|
|
#region GetByIdAsync Tests
|
|
|
|
[Fact(Skip = "需要 SqlSugar 完整 mock 设置")]
|
|
public async Task GetByIdAsync_ExistingPortfolio_ShouldReturnPortfolio()
|
|
{
|
|
// Arrange
|
|
var portfolioId = "port-001";
|
|
var userId = "user-001";
|
|
|
|
// Act
|
|
// var result = await _repo.GetByIdAsync(portfolioId, userId);
|
|
|
|
// Assert
|
|
// result.Should().NotBeNull();
|
|
// result.Id.Should().Be(portfolioId);
|
|
}
|
|
|
|
[Fact(Skip = "需要 SqlSugar 完整 mock 设置")]
|
|
public async Task GetByIdAsync_WrongUser_ShouldReturnNull()
|
|
{
|
|
// Arrange
|
|
var portfolioId = "port-001";
|
|
var wrongUserId = "user-002";
|
|
|
|
// Act
|
|
// var result = await _repo.GetByIdAsync(portfolioId, wrongUserId);
|
|
|
|
// Assert
|
|
// result.Should().BeNull();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region DeleteAsync Tests
|
|
|
|
[Fact(Skip = "需要 SqlSugar 完整 mock 设置")]
|
|
public async Task DeleteAsync_ShouldDeleteRelatedData()
|
|
{
|
|
// Arrange
|
|
var portfolioId = "port-001";
|
|
var userId = "user-001";
|
|
|
|
// Act
|
|
// var result = await _repo.DeleteAsync(portfolioId, userId);
|
|
|
|
// Assert
|
|
// 验证删除了 Position, Transaction, NavHistory
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetTransactionsAsync Tests
|
|
|
|
[Fact(Skip = "需要 SqlSugar 完整 mock 设置")]
|
|
public async Task GetTransactionsAsync_ShouldReturnOrderedByDate()
|
|
{
|
|
// Arrange
|
|
var portfolioId = "port-001";
|
|
|
|
// Act
|
|
// var result = await _repo.GetTransactionsAsync(portfolioId, 10, 0);
|
|
|
|
// Assert
|
|
// result.Should().BeInDescendingOrder(t => t.TransactionTime);
|
|
}
|
|
|
|
#endregion
|
|
}
|