refactor(margin): 抽出独立 Margin 限界上下文,移除 PrepayLeg 错误继承
- 新建 YLErp.Modules.SwapModule.Margin:MarginBalance / MarginLeg / IMarginResolver(Cash/Credit/Guarantee) - MarginLeg 不继承 IInterestLegStrategy,与利息腿完全解耦(保证金是客户存款/担保,非互换腿) - 删除 PrepayLeg,工厂不再注册 mode 5/6 预付金 - 预付金用例迁入 MarginLegTest,旧 InterestLegStrategyTest 改为断言预付金已非利息腿
This commit is contained in:
@@ -8,6 +8,7 @@ namespace UnitTestProject.Modules.SwapModule.InterestLegs
|
||||
/// <summary>
|
||||
/// 利息腿策略单测。验证每个策略的 CalcNotional 与现有 CalcNotionalByMode switch 完全一致。
|
||||
/// 这组测试是后续"迁移调用点"的安全网——迁移前后行为必须不变。
|
||||
/// 注:原预付金(PrepayLeg)用例已迁出,见 MarginLegTest(保证金独立限界上下文)。
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class InterestLegStrategyTest
|
||||
@@ -99,37 +100,6 @@ namespace UnitTestProject.Modules.SwapModule.InterestLegs
|
||||
|
||||
#endregion
|
||||
|
||||
#region 预付金(mode 5/6):按Fix缩放,posiPrincipal=Fix全额
|
||||
|
||||
[TestMethod]
|
||||
public void 预付金_初始_部分平仓_本金按Fix缩放()
|
||||
{
|
||||
var leg = new PrepayLeg(InterestModeEnum.初始预付金);
|
||||
var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 0.5m);
|
||||
|
||||
Assert.AreEqual(1_000_000m, r.ClosePrincipal, "平仓本金=Fix×50%");
|
||||
Assert.AreEqual(Fix, r.PosiPrincipal, "持仓本金=Fix全额");
|
||||
Assert.AreEqual(0.5m, r.ClosePercent);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void 预付金_追加_全平_本金等于Fix()
|
||||
{
|
||||
var leg = new PrepayLeg(InterestModeEnum.追加预付金);
|
||||
var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 1m);
|
||||
|
||||
Assert.AreEqual(Fix, r.ClosePrincipal, "全平本金=Fix");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void 预付金_构造非法mode应抛异常()
|
||||
{
|
||||
Assert.ThrowsException<ArgumentException>(() =>
|
||||
new PrepayLeg(InterestModeEnum.固定值));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 守卫:策略对应各自mode
|
||||
|
||||
[TestMethod]
|
||||
@@ -138,8 +108,6 @@ namespace UnitTestProject.Modules.SwapModule.InterestLegs
|
||||
Assert.AreEqual(InterestModeEnum.固定值, new FixedNotionalLeg().Mode);
|
||||
Assert.AreEqual(InterestModeEnum.合约名义本金规模, new ContractNotionalLeg().Mode);
|
||||
Assert.AreEqual(InterestModeEnum.标的期初全价, new UnderlyingFullPriceLeg().Mode);
|
||||
Assert.AreEqual(InterestModeEnum.初始预付金, new PrepayLeg(InterestModeEnum.初始预付金).Mode);
|
||||
Assert.AreEqual(InterestModeEnum.追加预付金, new PrepayLeg(InterestModeEnum.追加预付金).Mode);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -152,8 +120,6 @@ namespace UnitTestProject.Modules.SwapModule.InterestLegs
|
||||
Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.固定值), typeof(FixedNotionalLeg));
|
||||
Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.合约名义本金规模), typeof(ContractNotionalLeg));
|
||||
Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.标的期初全价), typeof(UnderlyingFullPriceLeg));
|
||||
Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.初始预付金), typeof(PrepayLeg));
|
||||
Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.追加预付金), typeof(PrepayLeg));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -163,6 +129,11 @@ namespace UnitTestProject.Modules.SwapModule.InterestLegs
|
||||
InterestLegStrategyFactory.Get(InterestModeEnum.持仓名义本金));
|
||||
Assert.ThrowsException<ArgumentException>(() =>
|
||||
InterestLegStrategyFactory.Get(InterestModeEnum.持仓市值));
|
||||
// 预付金 5/6 已不再是利息腿,工厂不再注册,按未注册处理
|
||||
Assert.ThrowsException<ArgumentException>(() =>
|
||||
InterestLegStrategyFactory.Get(InterestModeEnum.初始预付金));
|
||||
Assert.ThrowsException<ArgumentException>(() =>
|
||||
InterestLegStrategyFactory.Get(InterestModeEnum.追加预付金));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using YLErp.Modules.SwapModule.Margin;
|
||||
|
||||
namespace UnitTestProject.Modules.SwapModule.Margin
|
||||
{
|
||||
/// <summary>
|
||||
/// 保证金利息计提(MarginLeg)单测。验证计息余额计算,以及三种形态解析器可互换产余额。
|
||||
/// 替换原 InterestLegStrategyTest 里的预付金(PrepayLeg)用例——预付金已迁出利息腿限界上下文。
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class MarginLegTest
|
||||
{
|
||||
private const decimal Balance = 2_000_000m;
|
||||
|
||||
[TestMethod]
|
||||
public void 计息余额_默认不缩放_等于保证金全额()
|
||||
{
|
||||
var leg = new MarginLeg();
|
||||
var r = leg.CalcBalance(Balance);
|
||||
Assert.AreEqual(Balance, r.Balance, "默认不缩放,余额=保证金全额");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void 计息余额_部分释放_按适用比例缩放()
|
||||
{
|
||||
var leg = new MarginLeg();
|
||||
var r = leg.CalcBalance(Balance, 0.5m);
|
||||
Assert.AreEqual(1_000_000m, r.Balance, "部分释放:余额×适用比例");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void 计息余额_零适用比例_为零()
|
||||
{
|
||||
var leg = new MarginLeg();
|
||||
var r = leg.CalcBalance(Balance, 0m);
|
||||
Assert.AreEqual(0m, r.Balance);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void 三种形态解析器_均可互换产出余额()
|
||||
{
|
||||
IMarginResolver cash = new CashMargin();
|
||||
IMarginResolver credit = new CreditMargin();
|
||||
IMarginResolver guarantee = new GuaranteeMargin();
|
||||
|
||||
Assert.AreEqual(MarginForm.Cash, cash.Form);
|
||||
Assert.AreEqual(MarginForm.Credit, credit.Form);
|
||||
Assert.AreEqual(MarginForm.Guarantee, guarantee.Form);
|
||||
|
||||
Assert.AreEqual(1_000_000m, cash.Resolve(Balance, 0.5m).Balance);
|
||||
Assert.AreEqual(1_000_000m, credit.Resolve(Balance, 0.5m).Balance);
|
||||
Assert.AreEqual(1_000_000m, guarantee.Resolve(Balance, 0.5m).Balance);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user