This commit is contained in:
2026-09-11 14:38:24 +08:00
parent 2888126a3a
commit af790bca28
4 changed files with 186 additions and 87 deletions

View File

@@ -81,7 +81,7 @@ class PerformanceRegressionTests(unittest.TestCase):
signals = [SignalItem(code=c) for c in ('new-b', 'held', 'new-a', 'new-b')]
with patch.object(boot, 'trading_time', return_value=True), \
patch.object(boot, 'market_allow_open', return_value=True), \
patch.object(boot, '_cache_portfolio'), patch('builtins.print'):
patch.object(boot, 'cache_portfolio'), patch('builtins.print'):
boot.RunOnce(run, signals)
run.client.full_tick.assert_called_once_with(['held', 'new-b', 'new-a'])
self.assertEqual(run.executor.submit.call_args_list[1].args,

View File

@@ -7,8 +7,8 @@ from contextlib import ExitStack, redirect_stdout
from types import SimpleNamespace
from unittest.mock import Mock, patch
from libs import collector
from sdk import Assets, PositionItem
from libs import collector, snapshot
from sdk import Assets, DealItem, PositionItem
from strategy.trend import boot
@@ -19,25 +19,28 @@ class TrendCollectorTests(unittest.TestCase):
cls.app = importlib.import_module('main')
def setUp(self):
old_snapshot = boot._collector_snapshot
self.addCleanup(setattr, boot, '_collector_snapshot', old_snapshot)
boot._collector_snapshot = None
old_snapshot = snapshot._collector_snapshot
self.addCleanup(setattr, snapshot, '_collector_snapshot', old_snapshot)
snapshot._collector_snapshot = None
def test_submission_reads_latest_cache_and_skips_empty(self):
with patch.object(collector, 'collector_push') as push:
collector.submit_trend_data()
push.assert_not_called()
boot._cache_portfolio('account', Assets(available=100), [])
snapshot.cache_portfolio('account', Assets(available=100), [], [])
assets = Assets(available=200)
positions = [PositionItem(stock_code='600000.SH', volume=100)]
boot._cache_portfolio('account', assets, positions)
deals = [DealItem(stock_code='600000.SH', volume=100)]
snapshot.cache_portfolio('account', assets, positions, deals)
collector.submit_trend_data()
push.assert_called_once_with('account', assets, positions)
push.assert_called_once_with('account', assets, positions, deals)
uploaded = push.call_args.args
uploaded[1].available = 0
uploaded[2].clear()
self.assertEqual(boot.get_collector_snapshot()[1].available, 200)
self.assertEqual(len(boot.get_collector_snapshot()[2]), 1)
uploaded[3][0].volume = 0
self.assertEqual(snapshot.get_collector_snapshot()[3][0].volume, 100)
self.assertEqual(snapshot.get_collector_snapshot()[1].available, 200)
self.assertEqual(len(snapshot.get_collector_snapshot()[2]), 1)
def test_run_once_caches_portfolio_without_submitting_data(self):
completed = Future()
@@ -48,16 +51,31 @@ class TrendCollectorTests(unittest.TestCase):
)
assets = Assets(available=100, total=1000)
run.client.portfolio.return_value = SimpleNamespace(assets=assets, positions={}, orders=[])
deals = [DealItem(stock_code='600000.SH', volume=100)]
run.client.deals.return_value = deals
run.client.full_tick.return_value = {}
run.executor.submit.return_value = completed
with patch.object(boot, 'trading_time', return_value=True), \
patch.object(boot, 'market_allow_open', return_value=True), \
patch.object(collector, 'collector_push') as push, redirect_stdout(io.StringIO()):
boot.RunOnce(run, [])
self.assertEqual(boot.get_collector_snapshot(), ('account', assets, []))
self.assertEqual(snapshot.get_collector_snapshot(), ('account', assets, [], deals))
run.client.deals.assert_called_once_with()
push.assert_not_called()
run.executor.submit.assert_called_once_with(boot.manage_positions, run, {}, [], True, 100)
def test_submission_serializes_deals(self):
snapshot.cache_portfolio(
'account', Assets(available=100), [],
[DealItem(stock_code='600000.SH', volume=100)],
)
with patch.object(collector.httpx, 'post') as post:
collector.submit_trend_data()
payload = post.call_args.kwargs['json']
self.assertEqual(payload['account_id'], 'account')
self.assertEqual(payload['deals'][0]['stock_code'], '600000.SH')
self.assertEqual(payload['deals'][0]['volume'], 100)
def test_main_registers_five_minute_collector_job(self):
for strategy in ('trend', 'zt'):
with self.subTest(strategy=strategy), ExitStack() as stack: