136 lines
6.1 KiB
C#
136 lines
6.1 KiB
C#
using YLErp.DBModels;
|
||
using YLErp.DBModels.Enums;
|
||
using YLErp.Model;
|
||
|
||
namespace YLErp.Modules.SwapModule
|
||
{
|
||
/// <summary>
|
||
/// SwapEodPositionService 的可测试化基类(纯内存,不连库)。
|
||
/// ============================================================================
|
||
/// 收敛各 ScenarioTest 中 Stub 子类的重复 override:
|
||
/// - PersistEodSwapPosition:收集到列表,不写库
|
||
/// - SaveAllChanges:no-op
|
||
/// - GetCurrencyRate:返回 1.0(本币)
|
||
/// - 统一构造函数(注入 OptUserInfo,标记 UnitTest 来源)
|
||
///
|
||
/// 暴露 PersistedPositions / SaveChangesCount / ClientCashCalls 等输出捕获属性,
|
||
/// 供断言使用。各测试子类按需再 override 业务 seam(FindTrade/GetUnderlyingPrice 等)。
|
||
///
|
||
/// 设计原则:
|
||
/// - 只收敛 8/8 Stub 都重复的高频 override,不预设业务数据注入方式
|
||
/// (ComposePage 用属性字典、SwapPositionCompose 用构造函数 List,差异留给子类)
|
||
/// - 不提供 Execute* 包装器(签名各异且大多只出现 1-2 次,留在各子类避免基类膨胀)
|
||
/// ============================================================================
|
||
/// </summary>
|
||
public class TestableSwapEodPositionService : SwapEodPositionService
|
||
{
|
||
/// <summary>捕获所有持久化的 eod 持仓(按调用顺序)</summary>
|
||
public List<eod_swap_position> PersistedPositions { get; } = new();
|
||
|
||
/// <summary>
|
||
/// 捕获所有持久化的互换流水事件(按调用顺序)。
|
||
/// 生产 PersistFlowEvent 写 DbContext.swap_flow_event;GetConsumedInterest 也读同一张表。
|
||
/// 测试不连库,这里把真实收盘产生的 swap_flow_event 收集起来,
|
||
/// 使 GetConsumedInterest 能像生产一样从"真实累积的流水"里算已结利息,
|
||
/// 而无需硬编码、无需连库。这是消除"return 0m"伪绿的关键。
|
||
/// </summary>
|
||
public List<swap_flow_event> FlowEvents { get; } = new();
|
||
|
||
/// <summary>SaveAllChanges 调用次数</summary>
|
||
public int SaveChangesCount { get; private set; }
|
||
|
||
/// <summary>AddClientCash 调用记录(金额, 操作)</summary>
|
||
public List<(double amount, string action)> ClientCashCalls { get; } = new();
|
||
|
||
/// <summary>SwapPositionCompose 使用的公司行为内存数据;默认空,避免测试访问数据库。</summary>
|
||
public List<ex_dividend_info> ExDividendInfos { get; } = new();
|
||
|
||
/// <summary>捕获公司行为生命周期事件,避免事件测试访问真实 swap_event 表。</summary>
|
||
public List<swap_event> CorporateActionEvents { get; } = new();
|
||
|
||
/// <summary>自增 id 模拟器(新增 eod 时分配 id)</summary>
|
||
private int _nextId = 1;
|
||
|
||
protected TestableSwapEodPositionService(string testName)
|
||
: base(new OptUserInfo(0, testName ?? nameof(TestableSwapEodPositionService), OptUserFrom.UnitTest))
|
||
{
|
||
}
|
||
|
||
// ===== 高频 seam override(8/8 Stub 都重复,收敛到基类)=====
|
||
|
||
protected override void PersistEodSwapPosition(eod_swap_position position)
|
||
{
|
||
if (position.id == 0) position.id = _nextId++;
|
||
PersistedPositions.Add(position);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 捕获真实收盘产生的 swap_flow_event(生产写 DbContext.swap_flow_event)。
|
||
/// 与 PersistEodSwapPosition 同理,这里只收集不写库,供 GetConsumedInterest 真实计算。
|
||
/// </summary>
|
||
protected override void PersistFlowEvent(swap_flow_event flowEvent)
|
||
{
|
||
if (flowEvent.id == 0) flowEvent.id = _nextId++;
|
||
FlowEvents.Add(flowEvent);
|
||
}
|
||
|
||
protected override void SaveAllChanges()
|
||
{
|
||
SaveChangesCount++;
|
||
}
|
||
|
||
protected override double GetCurrencyRate(string quoteCurrency, string settlementCurrency, DateTime valueDate, bool seekPreday, CurrencyRateType currencyRateType)
|
||
{
|
||
return 1.0; // 本币,汇率=1
|
||
}
|
||
|
||
protected override List<ex_dividend_info> FindCorporateActionInfos(DateTime settleDate)
|
||
{
|
||
return ExDividendInfos
|
||
.Where(x => x.ValidStatus
|
||
&& (x.ExDividendDate?.Date == settleDate.Date
|
||
|| x.EffectiveDate?.Date == settleDate.Date))
|
||
.ToList();
|
||
}
|
||
|
||
protected override List<swap_event> FindCorporateActionEvents(int swapTradeId)
|
||
{
|
||
return CorporateActionEvents
|
||
.Where(x => x.SwapTradeId == swapTradeId && !x.Invalid
|
||
&& x.EventType == (int)SwapEventTypeEnum.公司行为)
|
||
.ToList();
|
||
}
|
||
|
||
protected override decimal GetFundCorporateActionClosePrice(
|
||
ex_dividend_info dividendInfo,
|
||
decimal fallbackPrice)
|
||
=> fallbackPrice;
|
||
|
||
protected override int AddClientCash(trade td, double amount, string action, DateTime valueDate)
|
||
{
|
||
ClientCashCalls.Add((amount, action));
|
||
return _nextId++;
|
||
}
|
||
|
||
/// <summary>
|
||
/// AddClientCashInCashOut 生产实现会查 DataCacheProvider.GetClientDataSource().GetData(ClientId),
|
||
/// 纯内存测试无客户缓存会抛"客户信息未找到"。与 AddClientCash 同构 no-op,
|
||
/// 仅捕获调用记录,供断言使用。
|
||
/// </summary>
|
||
public override int AddClientCashInCashOut(OtcTradeBase td, double amount, string action, DateTime valueDate)
|
||
{
|
||
ClientCashCalls.Add((amount, action));
|
||
return _nextId++;
|
||
}
|
||
|
||
/// <summary>
|
||
/// R4 自动互换返息按标签分流:纯内存测试不连库(查 swap_position 标签),
|
||
/// stub 为全额现金返息(与既有断言语义一致)。
|
||
/// </summary>
|
||
protected override decimal GetAutoSwapCashRebate(trade td, List<swap_flow_event> flowEvents, decimal totalRebate)
|
||
{
|
||
return totalRebate;
|
||
}
|
||
}
|
||
}
|