using YLErp.Modules.UnderlyingModule;
namespace YLErp.UnitTestProject.Modules.UnderlyingModule
{
[TestClass]
public class UnderlyingHelperTest : YLUnitTestBase
{
[TestMethod]
public void GetApplicableMarginTerm_RuleTable_AllCases()
{
// <=2年,应返回2年
var code1 = "UNLIMITED_2Y";
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.TwoYear, result1, "<=2年 应为2年");
// (2,5]年,应返回5年
var code2 = "UNLIMITED_5Y";
var maturity2 = new DateTime(2030, 1, 1);
var underlying2 = CreateTestUnderlying(code2, null, maturity2);
var valueDate2 = maturity2.AddYears(-3); // 剩余3年
var result2 = UnderlyingHelper.GetApplicableMarginTerm(code2, valueDate2);
Assert.AreEqual(ConsMarginTerm.FiveYear, result2, "(2,5]年 应为5年");
// (5,10]年,应返回10年
var code3 = "UNLIMITED_10Y";
var maturity3 = new DateTime(2030, 8, 2);
var underlying3 = CreateTestUnderlying(code3, null, maturity3);
var valueDate3 = maturity3.AddYears(-7); // 剩余13年
var valueDate3_2 = DateTime.Now.Date; // 剩余10年
var result3 = UnderlyingHelper.GetApplicableMarginTerm(code3, valueDate3_2);
Assert.AreEqual(ConsMarginTerm.TenYear, result3, "(5,10]年 应为10年");
// (10,30]年,应返回30年
var code4 = "UNLIMITED_30Y";
var maturity4 = new DateTime(2050, 1, 1);
var underlying4 = CreateTestUnderlying(code4, null, maturity4);
var valueDate4 = maturity4.AddYears(-28); // 剩余28年
var result4 = UnderlyingHelper.GetApplicableMarginTerm(code4, valueDate4);
Assert.AreEqual(ConsMarginTerm.ThirtyYear, result4, "(10,30]年 应为30年");
// >30年,应返回30年以上
var code5 = "OVER_30Y";
var maturity5 = new DateTime(2060, 1, 1);
var underlying5 = CreateTestUnderlying(code5, null, maturity5);
var valueDate5 = maturity5.AddYears(-31); // 剩余31年
var result5 = UnderlyingHelper.GetApplicableMarginTerm(code5, valueDate5);
Assert.AreEqual(ConsMarginTerm.OverThirtyYear, result5, ">30年 应为30年以上");
// 无效的到期日,应为默认
var code6 = "INVALID_TERM";
DateTime? maturity6 = null;
var underlying6 = CreateTestUnderlying(code6, null, maturity6);
var valueDate6 = DateTime.Now.Date;
var result6 = UnderlyingHelper.GetApplicableMarginTerm(code6, valueDate6);
Assert.AreEqual(ConsMarginTerm.Default, result6, "无效的到期日 应为默认");
}
#region 辅助方法
///
/// 创建测试用的标的对象
///
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;
}
///
/// 添加清理SQL
///
private void AddClearSQL(string sql)
{
// _clearSQL 字段在 YLUnitTestBase 中定义
// 这里假设 _clearSQL 是 protected 或 internal
if (!string.IsNullOrWhiteSpace(sql))
{
_clearSQL.AppendLine(sql);
}
}
#endregion
}
}