- 新建 YLErp.Modules.SwapModule.Margin:MarginBalance / MarginLeg / IMarginResolver(Cash/Credit/Guarantee) - MarginLeg 不继承 IInterestLegStrategy,与利息腿完全解耦(保证金是客户存款/担保,非互换腿) - 删除 PrepayLeg,工厂不再注册 mode 5/6 预付金 - 预付金用例迁入 MarginLegTest,旧 InterestLegStrategyTest 改为断言预付金已非利息腿
150 lines
6.0 KiB
C#
150 lines
6.0 KiB
C#
using System;
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||
using YLErp.DBModels.Enums;
|
||
using YLErp.Modules.SwapModule.InterestLegs;
|
||
|
||
namespace UnitTestProject.Modules.SwapModule.InterestLegs
|
||
{
|
||
/// <summary>
|
||
/// 利息腿策略单测。验证每个策略的 CalcNotional 与现有 CalcNotionalByMode switch 完全一致。
|
||
/// 这组测试是后续"迁移调用点"的安全网——迁移前后行为必须不变。
|
||
/// 注:原预付金(PrepayLeg)用例已迁出,见 MarginLegTest(保证金独立限界上下文)。
|
||
/// </summary>
|
||
[TestClass]
|
||
public class InterestLegStrategyTest
|
||
{
|
||
private const decimal Fix = 2_000_000m; // 合约固定本金
|
||
private const decimal Notional = 100_000_000m; // 剩余名义本金 1 亿
|
||
private const decimal LongNotional = 60_000_000m;
|
||
private const decimal ShortNotional = 40_000_000m;
|
||
|
||
#region 固定值(mode 1):恒=Fix,不随比例变
|
||
|
||
[TestMethod]
|
||
public void 固定值_部分平仓_计息基数恒等于Fix()
|
||
{
|
||
var leg = new FixedNotionalLeg();
|
||
var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 0.5m);
|
||
|
||
Assert.AreEqual(Fix, r.ClosePrincipal, "平仓本金恒=Fix");
|
||
Assert.AreEqual(Fix, r.PosiPrincipal, "持仓本金恒=Fix");
|
||
Assert.AreEqual(1m, r.ClosePercent, "有效比例恒=1(固定值不随比例缩放)");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void 固定值_全平_计息基数仍等于Fix()
|
||
{
|
||
var leg = new FixedNotionalLeg();
|
||
var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 1m);
|
||
|
||
Assert.AreEqual(Fix, r.ClosePrincipal, "全平本金仍=Fix");
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 合约名义本金规模(mode 2):按比例线性缩放
|
||
|
||
[TestMethod]
|
||
public void 合约名义本金_部分平仓_本金按比例缩放()
|
||
{
|
||
var leg = new ContractNotionalLeg();
|
||
var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 0.5m);
|
||
|
||
Assert.AreEqual(50_000_000m, r.ClosePrincipal, "平仓本金=Notional×50%");
|
||
Assert.AreEqual(Notional, r.PosiPrincipal, "持仓本金=Notional全额");
|
||
Assert.AreEqual(0.5m, r.ClosePercent, "有效比例=入参");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void 合约名义本金_全平_本金等于全额()
|
||
{
|
||
var leg = new ContractNotionalLeg();
|
||
var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 1m);
|
||
|
||
Assert.AreEqual(Notional, r.ClosePrincipal, "全平本金=Notional");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void 合约名义本金_零平仓_本金为零()
|
||
{
|
||
var leg = new ContractNotionalLeg();
|
||
var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 0m);
|
||
|
||
Assert.AreEqual(0m, r.ClosePrincipal, "零平仓本金=0");
|
||
Assert.AreEqual(Notional, r.PosiPrincipal, "持仓本金仍=Notional");
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 标的期初全价(mode 9):主路径公式与mode2相同
|
||
|
||
[TestMethod]
|
||
public void 标的期初全价_部分平仓_主路径公式同mode2()
|
||
{
|
||
var leg = new UnderlyingFullPriceLeg();
|
||
var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 0.5m);
|
||
|
||
Assert.AreEqual(50_000_000m, r.ClosePrincipal, "平仓本金=Notional×50%(与mode2主路径一致)");
|
||
Assert.AreEqual(Notional, r.PosiPrincipal);
|
||
Assert.AreEqual(0.5m, r.ClosePercent);
|
||
}
|
||
|
||
[TestMethod]
|
||
public void 标的期初全价_全平_本金等于全额()
|
||
{
|
||
var leg = new UnderlyingFullPriceLeg();
|
||
var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 1m);
|
||
|
||
Assert.AreEqual(Notional, r.ClosePrincipal);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 守卫:策略对应各自mode
|
||
|
||
[TestMethod]
|
||
public void 各策略对应正确枚举值()
|
||
{
|
||
Assert.AreEqual(InterestModeEnum.固定值, new FixedNotionalLeg().Mode);
|
||
Assert.AreEqual(InterestModeEnum.合约名义本金规模, new ContractNotionalLeg().Mode);
|
||
Assert.AreEqual(InterestModeEnum.标的期初全价, new UnderlyingFullPriceLeg().Mode);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 工厂:按mode分发
|
||
|
||
[TestMethod]
|
||
public void 工厂_返回各活跃mode的策略()
|
||
{
|
||
Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.固定值), typeof(FixedNotionalLeg));
|
||
Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.合约名义本金规模), typeof(ContractNotionalLeg));
|
||
Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.标的期初全价), typeof(UnderlyingFullPriceLeg));
|
||
}
|
||
|
||
[TestMethod]
|
||
public void 工厂_未注册mode抛异常()
|
||
{
|
||
Assert.ThrowsException<ArgumentException>(() =>
|
||
InterestLegStrategyFactory.Get(InterestModeEnum.持仓名义本金));
|
||
Assert.ThrowsException<ArgumentException>(() =>
|
||
InterestLegStrategyFactory.Get(InterestModeEnum.持仓市值));
|
||
// 预付金 5/6 已不再是利息腿,工厂不再注册,按未注册处理
|
||
Assert.ThrowsException<ArgumentException>(() =>
|
||
InterestLegStrategyFactory.Get(InterestModeEnum.初始预付金));
|
||
Assert.ThrowsException<ArgumentException>(() =>
|
||
InterestLegStrategyFactory.Get(InterestModeEnum.追加预付金));
|
||
}
|
||
|
||
[TestMethod]
|
||
public void 工厂_int重载和枚举重载等价()
|
||
{
|
||
var byEnum = InterestLegStrategyFactory.Get(InterestModeEnum.固定值);
|
||
var byInt = InterestLegStrategyFactory.Get((int)InterestModeEnum.固定值);
|
||
Assert.AreEqual(byEnum.Mode, byInt.Mode);
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|