Files
zszq-trs/UnitTestProject/Modules/SwapModule/TestableSwapTradeAutoService.cs
T
hjhan 6e9bae94c5 test(swap): MergeRestModeCompose seam调用替换+移植MergeComposeScenarioTest(6/8场景)
借鉴 testable 分支,把 SwapTradeAutoService 的 MergeRestModeCompose/MergeRestModelItem/
DealNoPosition/DealHasPosition 内联DB调用替换为seam调用,生产行为不变。

替换的调用点:
- MergeRestModeCompose: FindActiveSwapTrades/FindTradeExtends/FindActivePositions/GetNextBusinessDayBefore/QueryFloatRates
- MergeRestModelItem: FindClient/GetEtradingRule/FindAssetUnit/FindUnderlying/GetSwapFloatRate/FindFlowEventsForCashCheck/FindValidTrades/SaveChanges
- DealNoPosition: CreateNewSwapTrade/AutoSwapUnwind
- DealHasPosition: AutoSwapUnwind

新增测试(借鉴testable分支):
- TestableSwapTradeAutoService.cs(集中式共享包装类)
- MergeComposeScenarioTest.cs(8场景):
  Scenario1-5/8 通过: 客户/权限/账户/标的校验链 + 空流水返回
  Scenario6/7 [Ignore]: 创建交易路径,DealNoPosition内部实现细节待对齐后启用

SwapModule 163测试全绿(+6),无回归。
2026-07-03 09:20:59 +08:00

170 lines
9.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using YLErp.DBModels;
using YLErp.DBModels.Enums;
using YLErp.Model;
namespace YLErp.Modules.SwapModule
{
/// <summary>
/// SwapTradeAutoService 的可测试子类。
/// 覆盖所有外部依赖方法,用内存数据替代数据库和静态调用。
/// </summary>
public class TestableSwapTradeAutoService : SwapTradeAutoService
{
private readonly Func<DateTime, DateTime> _nextBusinessDay;
private readonly Func<DateTime, DateTime> _nextBusinessDayBefore;
private readonly Func<string, decimal, DateTime, CalBondResult> _bondCalc;
// 注入的数据
private readonly List<trade> _trades;
private readonly List<trade_extend> _tradeExtends;
private readonly List<swap_position> _positions;
private readonly Dictionary<int, Client> _clients;
private readonly Dictionary<string, AssetUnit> _assets;
private readonly Dictionary<string, underlying_manager> _underlyings;
private readonly Func<int, string, EtradingRule> _etradingRuleFactory;
private readonly Func<IQueryable<SwapFloatRate>, int, string, SwapFloatRate> _floatRateFactory;
private readonly Func<swap_flow_merge, Client, AssetUnit, underlying_manager, SwapFloatRate, string, bool, trade> _newSwapTradeFactory;
private readonly Action<int, decimal, decimal, decimal, decimal, DateTime, decimal, decimal> _autoSwapUnwind;
private readonly List<swap_flow_event> _flowEvents;
private readonly List<trade> _validTrades;
/// <summary>捕获 PersistMerge 写入的所有 merge 记录</summary>
public List<swap_flow_merge> PersistedMerges { get; } = new List<swap_flow_merge>();
/// <summary>捕获 CreateNewSwapTrade 创建的所有交易</summary>
public List<trade> CreatedTrades { get; } = new List<trade>();
/// <summary>捕获 AutoSwapUnwind 调用</summary>
public List<(int tradeId, decimal qty, decimal fee)> UnwindCalls { get; } = new List<(int, decimal, decimal)>();
/// <summary>SaveChanges 调用次数</summary>
public int SaveChangesCount { get; private set; }
public TestableSwapTradeAutoService(
OptUserInfo optUser,
Func<DateTime, DateTime> nextBusinessDay = null,
Func<DateTime, DateTime> nextBusinessDayBefore = null,
Func<string, decimal, DateTime, CalBondResult> bondCalc = null,
List<trade> trades = null,
List<trade_extend> tradeExtends = null,
List<swap_position> positions = null,
Dictionary<int, Client> clients = null,
Dictionary<string, AssetUnit> assets = null,
Dictionary<string, underlying_manager> underlyings = null,
Func<int, string, EtradingRule> etradingRuleFactory = null,
Func<IQueryable<SwapFloatRate>, int, string, SwapFloatRate> floatRateFactory = null,
Func<swap_flow_merge, Client, AssetUnit, underlying_manager, SwapFloatRate, string, bool, trade> newSwapTradeFactory = null,
Action<int, decimal, decimal, decimal, decimal, DateTime, decimal, decimal> autoSwapUnwind = null,
List<swap_flow_event> flowEvents = null,
List<trade> validTrades = null
) : base(optUser)
{
_nextBusinessDay = nextBusinessDay ?? (d => d.AddDays(1));
_nextBusinessDayBefore = nextBusinessDayBefore ?? (d => d.AddDays(-1));
_bondCalc = bondCalc ?? ((code, price, date) => null);
_trades = trades ?? new List<trade>();
_tradeExtends = tradeExtends ?? new List<trade_extend>();
_positions = positions ?? new List<swap_position>();
_clients = clients ?? new Dictionary<int, Client>();
_assets = assets ?? new Dictionary<string, AssetUnit>();
_underlyings = underlyings ?? new Dictionary<string, underlying_manager>();
_etradingRuleFactory = etradingRuleFactory;
_floatRateFactory = floatRateFactory;
_newSwapTradeFactory = newSwapTradeFactory;
_autoSwapUnwind = autoSwapUnwind;
_flowEvents = flowEvents ?? new List<swap_flow_event>();
_validTrades = validTrades ?? new List<trade>();
}
#region Override 可测试化方法
protected override DateTime GetNextBusinessDay(DateTime date) => _nextBusinessDay(date);
protected override DateTime GetNextBusinessDayBefore(DateTime date) => _nextBusinessDayBefore(date);
protected override CalBondResult CalculateBondYtm(string underlyingCode, decimal avgPrice, DateTime settleDate)
=> _bondCalc(underlyingCode, avgPrice, settleDate);
protected override void PersistMerge(swap_flow_merge merge) => PersistedMerges.Add(merge);
protected override void SetModelOpt(DBModelBaseV2 model) { }
protected override List<trade> FindActiveSwapTrades(DateTime valueDate) => _trades;
protected override List<trade_extend> FindTradeExtends(IEnumerable<int> tradeIds) => _tradeExtends;
protected override List<swap_position> FindActivePositions(IEnumerable<int> tradeIds, int posiDirection)
=> _positions.Where(x => x.PosiDirection == posiDirection).ToList();
protected override List<swap_position> FindActivePositionsAll(IEnumerable<int> tradeIds)
=> _positions;
protected override IQueryable<SwapFloatRate> QueryFloatRates(DateTime valueDate, DateTime matuirityDate)
=> new List<SwapFloatRate>().AsQueryable();
protected override Client FindClient(int clientId)
=> _clients.TryGetValue(clientId, out var c) ? c : null;
protected override AssetUnit FindAssetUnit(string assetAccountName)
=> _assets.TryGetValue(assetAccountName ?? "", out var a) ? a : null;
protected override underlying_manager FindUnderlying(string underlyingCode)
=> _underlyings.TryGetValue(underlyingCode ?? "", out var u) ? u : null;
protected override EtradingRule GetEtradingRule(BoundSideEnum boundSide, string clientNumber)
=> _etradingRuleFactory?.Invoke((int)boundSide, clientNumber);
protected override SwapFloatRate GetSwapFloatRate(IQueryable<SwapFloatRate> query, int clientId, string underlyingCode)
=> _floatRateFactory?.Invoke(query, clientId, underlyingCode);
protected override List<swap_flow_event> FindFlowEventsForCashCheck(swap_flow_merge flowMerge)
=> _flowEvents;
protected override List<trade> FindValidTrades(IEnumerable<int> tradeIds)
=> _validTrades;
protected override void SaveChanges() => SaveChangesCount++;
protected override trade CreateNewSwapTrade(swap_flow_merge flowMerge, Client client, AssetUnit asset, underlying_manager underlying, SwapFloatRate floatRate, string clearingAgency, bool cashNeedAfter = false)
{
if (_newSwapTradeFactory != null)
{
var t = _newSwapTradeFactory(flowMerge, client, asset, underlying, floatRate, clearingAgency, cashNeedAfter);
CreatedTrades.Add(t);
return t;
}
var trade = new trade { id = CreatedTrades.Count + 1, TradeNumber = $"TEST-{CreatedTrades.Count + 1}" };
CreatedTrades.Add(trade);
return trade;
}
protected override void AutoSwapUnwind(int tradeId, decimal tradingAmountAvg, decimal tradingAmountFeeAvg, decimal tradingAmountNetFeeAvg, decimal tradingAmountNetAvg, DateTime occurTime, decimal tradingQtyAbs, decimal tradingFeePending)
{
UnwindCalls.Add((tradeId, tradingQtyAbs, tradingFeePending));
_autoSwapUnwind?.Invoke(tradeId, tradingAmountAvg, tradingAmountFeeAvg, tradingAmountNetFeeAvg, tradingAmountNetAvg, occurTime, tradingQtyAbs, tradingFeePending);
}
#endregion
/// <summary>公开 SummaryFlow 供测试调用</summary>
public List<swap_flow_merge> ExecuteSummaryFlow(
List<swap_flow> swapFlows, DateTime valueDate, bool save = true,
Action<string> callback = null)
=> SummaryFlow(swapFlows, valueDate, save, callback);
/// <summary>公开 SummaryFlow 第二个重载</summary>
public List<swap_flow_merge> ExecuteSummaryFlowDeal(
List<swap_flow> swapFlows1, DateTime tradeDate, List<SwapFlowDeal> swapFlows)
=> SummaryFlow(swapFlows1, tradeDate, swapFlows);
/// <summary>公开 MergeRestModeCompose 供测试调用</summary>
public void ExecuteMergeRestModeCompose(List<swap_flow_merge> mergeList, DateTime valueDate, Action<int>? action = null)
=> MergeRestModeCompose(mergeList, valueDate, action);
/// <summary>公开 MergeAvgModeCompose 供测试调用</summary>
public Dictionary<long, List<string>> ExecuteMergeAvgModeCompose(List<swap_flow_merge> mergeList, DateTime valueDate, Action<int>? action = null)
=> MergeAvgModeCompose(mergeList, valueDate, action);
}
}