Files
zszq-trs/UnitTestProject/Modules/UnderlyingModule/UnderlyingHelperTest.cs
2026-04-29 10:51:26 +08:00

98 lines
4.0 KiB
C#

using YLErp.Modules.UnderlyingModule;
namespace YLErp.UnitTestProject.Modules.UnderlyingModule
{
[TestClass]
public class UnderlyingHelperTest : YLUnitTestBase
{
[TestMethod]
public void GetApplicableMarginTerm_RuleTable_AllCases()
{
// <=5年,应返回<5y
var code1 = "UNLIMITED_UNDER5Y";
var maturity1 = new DateTime(2030, 1, 1);
var underlying1 = CreateTestUnderlying(code1, null, maturity1);
var valueDate1 = maturity1.AddYears(-1); // 剩余1年
var result1 = UnderlyingHelper.GetApplicableMarginTerm(code1, valueDate1);
Assert.AreEqual(ConsMarginTerm.UnderFiveYear, result1, "<=5年 应为<5y");
// (5,10]年,应返回5y-10y
var code2 = "UNLIMITED_5Y_10Y";
var maturity2 = new DateTime(2030, 1, 1);
var underlying2 = CreateTestUnderlying(code2, null, maturity2);
var valueDate2 = maturity2.AddYears(-7); // 剩余7年
var result2 = UnderlyingHelper.GetApplicableMarginTerm(code2, valueDate2);
Assert.AreEqual(ConsMarginTerm.FiveToTenYear, result2, "(5,10]年 应为5y-10y");
// (10,30]年,应返回10y-30y
var code3 = "UNLIMITED_10Y_30Y";
var maturity3 = new DateTime(2050, 1, 1);
var underlying3 = CreateTestUnderlying(code3, null, maturity3);
var valueDate3 = maturity3.AddYears(-20); // 剩余20年
var result3 = UnderlyingHelper.GetApplicableMarginTerm(code3, valueDate3);
Assert.AreEqual(ConsMarginTerm.TenToThirtyYear, result3, "(10,30]年 应为10y-30y");
// >30年,应返回>30y
var code4 = "OVER_30Y";
var maturity4 = new DateTime(2060, 1, 1);
var underlying4 = CreateTestUnderlying(code4, null, maturity4);
var valueDate4 = maturity4.AddYears(-31); // 剩余31年
var result4 = UnderlyingHelper.GetApplicableMarginTerm(code4, valueDate4);
Assert.AreEqual(ConsMarginTerm.OverThirtyYear, result4, ">30年 应为>30y");
// 无效的到期日,应为<5y(兜底默认)
var code5 = "INVALID_TERM";
DateTime? maturity5 = null;
var underlying5 = CreateTestUnderlying(code5, null, maturity5);
var valueDate5 = DateTime.Now.Date;
var result5 = UnderlyingHelper.GetApplicableMarginTerm(code5, valueDate5);
Assert.AreEqual(ConsMarginTerm.UnderFiveYear, result5, "无效的到期日 应为<5y");
}
#region 辅助方法
/// <summary>
/// 创建测试用的标的对象
/// </summary>
private underlying_manager CreateTestUnderlying(string code, DateTime? openDate, DateTime? maturityDate)
{
DbContext.Database.ExecuteSqlRaw($"DELETE FROM underlying_manager WHERE UnderlyingCode = '{code}'");
var underlying = new underlying_manager
{
UnderlyingCode = code,
OpenDate = openDate,
MaturityDate = maturityDate,
UnderlyingName = $"测试标的{code}",
MarketCode = "TEST",
MarketName = "测试市场",
OptDate = DateTime.Now,
OptId = UserId,
OptName = UserName
};
// 添加到数据库
DbContext.underlying_manager.Add(underlying);
DbContext.SaveChanges();
// 添加到清理列表
AddClearSQL($"DELETE FROM underlying_manager WHERE UnderlyingCode = '{code}';");
return underlying;
}
/// <summary>
/// 添加清理SQL
/// </summary>
private void AddClearSQL(string sql)
{
// _clearSQL 字段在 YLUnitTestBase 中定义
// 这里假设 _clearSQL 是 protected 或 internal
if (!string.IsNullOrWhiteSpace(sql))
{
_clearSQL.AppendLine(sql);
}
}
#endregion
}
}