This commit is contained in:
SunCheng
2026-02-15 10:10:28 +08:00
parent e51a3edd50
commit a88556c784
92 changed files with 6751 additions and 776 deletions

View File

@@ -316,4 +316,105 @@ public class BudgetApplicationTest : BaseApplicationTest
}
#endregion
#region GetCategoryStatsAsync Tests
[Fact]
public async Task GetCategoryStatsAsync_Should_Include_Trend_And_Description_In_Month_Stats()
{
// Arrange
var referenceDate = new DateTime(2026, 2, 14);
var category = BudgetCategory.Expense;
var serviceResponse = new BudgetCategoryStats
{
Month = new BudgetStatsDto
{
Limit = 3000,
Current = 1200,
Rate = 40,
Trend = new List<decimal?> { 100, 200, 300, 400, 500, null, null },
Description = "<table><tr><th>日期</th><th>金额</th></tr></table>"
},
Year = new BudgetStatsDto
{
Limit = 36000,
Current = 5000,
Rate = 13.89m,
Trend = new List<decimal?> { 1000, 2000, 3000, null },
Description = "<table><tr><th>月份</th><th>金额</th></tr></table>"
}
};
_budgetService.GetCategoryStatsAsync(category, referenceDate).Returns(serviceResponse);
// Act
var result = await _application.GetCategoryStatsAsync(category, referenceDate);
// Assert
result.Should().NotBeNull();
// 验证 Month 数据
result.Month.Limit.Should().Be(3000);
result.Month.Current.Should().Be(1200);
result.Month.Remaining.Should().Be(1800);
result.Month.UsagePercentage.Should().Be(40);
result.Month.Trend.Should().NotBeNull();
result.Month.Trend.Should().HaveCount(7);
result.Month.Trend[0].Should().Be(100);
result.Month.Trend[5].Should().BeNull();
result.Month.Description.Should().NotBeEmpty();
result.Month.Description.Should().Contain("<table>");
}
[Fact]
public async Task GetCategoryStatsAsync_Should_Include_Trend_And_Description_In_Year_Stats()
{
// Arrange
var referenceDate = new DateTime(2026, 2, 14);
var category = BudgetCategory.Income;
var serviceResponse = new BudgetCategoryStats
{
Month = new BudgetStatsDto
{
Limit = 5000,
Current = 3000,
Rate = 60,
Trend = new List<decimal?> { 500, 1000, 1500, 2000, 2500, 3000 },
Description = "<p>月度收入明细</p>"
},
Year = new BudgetStatsDto
{
Limit = 60000,
Current = 10000,
Rate = 16.67m,
Trend = new List<decimal?> { 5000, 10000, null, null, null, null, null, null, null, null, null, null },
Description = "<p>年度收入明细</p>"
}
};
_budgetService.GetCategoryStatsAsync(category, referenceDate).Returns(serviceResponse);
// Act
var result = await _application.GetCategoryStatsAsync(category, referenceDate);
// Assert
result.Should().NotBeNull();
// 验证 Year 数据
result.Year.Limit.Should().Be(60000);
result.Year.Current.Should().Be(10000);
result.Year.Remaining.Should().Be(50000);
result.Year.UsagePercentage.Should().Be(16.67m);
result.Year.Trend.Should().NotBeNull();
result.Year.Trend.Should().HaveCount(12);
result.Year.Trend[0].Should().Be(5000);
result.Year.Trend[1].Should().Be(10000);
result.Year.Trend[2].Should().BeNull();
result.Year.Description.Should().NotBeEmpty();
result.Year.Description.Should().Contain("年度收入明细");
}
#endregion
}

View File

@@ -441,4 +441,82 @@ public class TransactionCategoryApplicationTest : BaseApplicationTest
}
#endregion
#region DeleteIconAsync Tests
[Fact]
public async Task DeleteIconAsync_存在的分类_应成功删除图标()
{
// Arrange
var category = new TransactionCategory
{
Id = 1,
Name = "测试",
Type = TransactionType.Expense,
Icon = """["<svg>icon1</svg>","<svg>icon2</svg>"]"""
};
_categoryRepository.GetByIdAsync(1).Returns(category);
_categoryRepository.UpdateAsync(Arg.Any<TransactionCategory>()).Returns(true);
// Act
await _application.DeleteIconAsync(1);
// Assert
await _categoryRepository.Received(1).UpdateAsync(Arg.Is<TransactionCategory>(
c => c.Id == 1 && c.Icon == null
));
}
[Fact]
public async Task DeleteIconAsync_分类不存在_应抛出NotFoundException()
{
// Arrange
_categoryRepository.GetByIdAsync(999).Returns((TransactionCategory?)null);
// Act & Assert
await Assert.ThrowsAsync<NotFoundException>(() => _application.DeleteIconAsync(999));
}
[Fact]
public async Task DeleteIconAsync_更新失败_应抛出BusinessException()
{
// Arrange
var category = new TransactionCategory
{
Id = 1,
Name = "测试",
Type = TransactionType.Expense,
Icon = """["<svg>icon1</svg>"]"""
};
_categoryRepository.GetByIdAsync(1).Returns(category);
_categoryRepository.UpdateAsync(Arg.Any<TransactionCategory>()).Returns(false);
// Act & Assert
await Assert.ThrowsAsync<BusinessException>(() => _application.DeleteIconAsync(1));
}
[Fact]
public async Task DeleteIconAsync_无图标的分类_应成功()
{
// Arrange
var category = new TransactionCategory
{
Id = 1,
Name = "测试",
Type = TransactionType.Expense,
Icon = null
};
_categoryRepository.GetByIdAsync(1).Returns(category);
_categoryRepository.UpdateAsync(Arg.Any<TransactionCategory>()).Returns(true);
// Act
await _application.DeleteIconAsync(1);
// Assert
await _categoryRepository.Received(1).UpdateAsync(Arg.Is<TransactionCategory>(
c => c.Id == 1 && c.Icon == null
));
}
#endregion
}