Files
zszq-trs/UnitTestProject/Modules/SwapModule/SwapIncomeScenarioTest.cs

98 lines
5.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using YLErp.DBModels;
using YLErp.DBModels.Enums;
namespace YLErp.Modules.SwapModule
{
/// <summary>
/// 互换结息(SwapIncome)测试
/// ============================================================================
/// 借鉴 testable 分支命名,基于当前分支 TestableSwapDealService 共享 stub。
/// SwapIncome 是写客户资金流水(ClientCashInCashOut)的核心入口之一。
/// ============================================================================
[TestClass]
public class SwapIncomeScenarioTest
{
// ================================================================
// 场景1SwapIncome 正常结息 —— 资金流水金额正确
// ================================================================
/// <summary>
/// SwapIncome 正常结息:SwapRealizedPnL=1000 → 客户资金流水金额=-1000。
/// 后端 SwapDealService SwapIncome 直接用前端传入的 SwapRealizedPnL 记账。
/// </summary>
[TestMethod]
public void SI_001_SwapIncome_正常结息_资金流水金额正确()
{
var td = SwapDealTestFactory.CreateTrade();
td.ExerciseDate = new DateTime(2026, 12, 31); // 未到期,不走"已到期"分支
var service = new TestableSwapDealService(td);
var unwindData = SwapDealTestFactory.CreateUnwindData(swapRealizedPnL: 1000m);
service.SwapIncome(unwindData);
Assert.AreEqual(1, service.ClientCashCalls.Count, "应生成1条资金流水(互换)");
Assert.AreEqual(-1000.0, service.ClientCashCalls[0].amount, 0.001, "资金流水金额 = -SwapRealizedPnL");
Assert.AreEqual(ClientCashInCashOut.系统操作_互换, service.ClientCashCalls[0].action, "操作类型=系统操作_互换");
Assert.AreEqual(1, service.SaveSwapDealCalls.Count, "应调用 SaveSwapDeal 1次");
Assert.AreEqual((int)SwapEventTypeEnum.互换, service.SaveSwapDealCalls[0].eventType, "事件类型=互换(3)");
Console.WriteLine($"SI_001: 资金流水={service.ClientCashCalls[0].amount}, 事件类型=互换 ✅");
}
// ================================================================
// 场景2SwapIncome 含预付金返息 —— 两条资金流水
// ================================================================
/// <summary>
/// SwapIncome 含预付金返息:SwapRealizedPnL=1000, SwapMarginRebatePnl=200
/// → 生成2条资金流水(互换 + 预付金返息),金额分别为 -1000、-200。
/// </summary>
[TestMethod]
public void SI_002_SwapIncome_含预付金返息_两条资金流水()
{
var td = SwapDealTestFactory.CreateTrade();
td.ExerciseDate = new DateTime(2026, 12, 31);
var service = new TestableSwapDealService(td);
var unwindData = SwapDealTestFactory.CreateUnwindData(swapRealizedPnL: 1000m, swapMarginRebatePnl: 200m);
service.SwapIncome(unwindData);
Assert.AreEqual(2, service.ClientCashCalls.Count, "应生成2条资金流水(互换+预付金返息)");
Assert.AreEqual(-1000.0, service.ClientCashCalls[0].amount, 0.001, "第1条=互换金额");
Assert.AreEqual(ClientCashInCashOut.系统操作_互换, service.ClientCashCalls[0].action);
Assert.AreEqual(-200.0, service.ClientCashCalls[1].amount, 0.001, "第2条=预付金返息");
Assert.AreEqual(ClientCashInCashOut.系统操作_预付金返息, service.ClientCashCalls[1].action);
Console.WriteLine($"SI_002: 互换={service.ClientCashCalls[0].amount}, 预付金返息={service.ClientCashCalls[1].amount} ✅");
}
[TestMethod]
public void SI_003_SwapIncome_含预付金腿_不返还预付金本金()
{
var td = SwapDealTestFactory.CreateTrade();
td.ExerciseDate = new DateTime(2026, 12, 31);
var service = new TestableSwapDealService(td);
var unwindData = SwapDealTestFactory.CreateUnwindData(swapRealizedPnL: 0m);
unwindData.FlowEvents.Add(new swap_flow_event
{
UnderlyingCode = "UT-FLOAT",
MarkClosePnl = 100m,
PayDirection = 1
});
unwindData.FlowEvents.Add(new swap_flow_event
{
InterestMode = (int)InterestModeEnum.初始预付金,
InterestDirection = 1,
InterestPrincipal = 10000m,
InterestClosePnL = 2m
});
service.SwapIncome(unwindData);
Assert.AreEqual(0m, unwindData.SwapMarginAmount, "手动互换不应返还预付金本金");
Assert.IsFalse(service.ClientCashCalls.Any(x => x.action == ClientCashInCashOut.系统操作_应付预付金),
"手动互换不应生成应付预付金流水");
Assert.IsTrue(service.ClientCashCalls.Any(x => x.action == ClientCashInCashOut.系统操作_预付金返息),
"手动互换仍应结算预付金返息");
}
}
}