2026-11-02 09:02:22 +08:00
|
|
|
|
"""QMT策略类型扩展:保留原报文、签名覆盖及真实发送出口的策略归属。"""
|
|
|
|
|
|
import unit_test_guard
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
import copy
|
|
|
|
|
|
import os
|
|
|
|
|
|
import sys
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
from contextlib import ExitStack
|
|
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
from config.settings import settings
|
|
|
|
|
|
from app.core import ws_codec as wsc, strategy_book as books
|
|
|
|
|
|
from app.repo import pms_repo, qmt_repo
|
|
|
|
|
|
from app.services import dispatcher, execution_conflicts
|
|
|
|
|
|
from app.ws.runner import WsRunner
|
|
|
|
|
|
|
|
|
|
|
|
BASE = dict(instruction_id='P_D01', ts_code='600000.SH', side='buy', qty=200,
|
|
|
|
|
|
limit_price=12.35, valid_until=9999999999999, intent='OPEN', note='原始备注不变')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StrategyPayloadTest(unittest.TestCase):
|
|
|
|
|
|
def test_only_adds_strategy_type(self):
|
|
|
|
|
|
self.assertEqual(wsc.place_order_payload(**BASE), BASE)
|
|
|
|
|
|
for kind in ('subjective_quant', 'micro_rotation'):
|
|
|
|
|
|
self.assertEqual(wsc.place_order_payload(**BASE, strategy_type=kind),
|
|
|
|
|
|
dict(BASE, strategy_type=kind))
|
|
|
|
|
|
|
|
|
|
|
|
def test_unknown_type_does_not_silently_mislabel(self):
|
|
|
|
|
|
for kind in ('original', 'micro30', '', 'unknown'):
|
|
|
|
|
|
with self.assertRaises(wsc.CodecError):
|
|
|
|
|
|
wsc.place_order_payload(**BASE, strategy_type=kind)
|
|
|
|
|
|
|
|
|
|
|
|
def test_strategy_type_is_signed(self):
|
|
|
|
|
|
seed = '01'*32 # 独立测试向量,不读取业务签名环境。
|
|
|
|
|
|
body = wsc.place_order_payload(**BASE, strategy_type='micro_rotation')
|
|
|
|
|
|
env = wsc.build('place_order', body, seed_hex=seed, msg_id='m-test')
|
|
|
|
|
|
self.assertEqual(wsc.parse(wsc.dumps(env), peer_pubkey_b64=wsc.public_key_b64(seed))['payload'], body)
|
|
|
|
|
|
env['payload']['strategy_type'] = 'subjective_quant'
|
|
|
|
|
|
with self.assertRaises(wsc.CodecError) as ctx:
|
|
|
|
|
|
wsc.parse(wsc.dumps(env), peer_pubkey_b64=wsc.public_key_b64(seed))
|
|
|
|
|
|
self.assertEqual(ctx.exception.code, 'SIG_INVALID')
|
|
|
|
|
|
|
|
|
|
|
|
def test_legacy_instance_stays_original_without_parent_read(self):
|
|
|
|
|
|
with patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', False), \
|
|
|
|
|
|
patch.object(pms_repo, 'get_instruction') as read:
|
|
|
|
|
|
self.assertEqual(dispatcher.order_strategy_type('P'), 'subjective_quant')
|
|
|
|
|
|
read.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
def test_cancel_payload_is_unchanged(self):
|
|
|
|
|
|
self.assertEqual(wsc.cancel_order_payload(cancel_id='C', instruction_id='P_D01'),
|
|
|
|
|
|
{'cancel_id': 'C', 'instruction_id': 'P_D01'})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StrategyOutboxTest(unittest.TestCase):
|
|
|
|
|
|
def drain(self, parent, *, side='buy', lookup_error=None):
|
|
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
|
|
runner = WsRunner()
|
|
|
|
|
|
row = dict(BASE, side=side, parent_id='P', status='SENDING', cancel_state='NONE')
|
|
|
|
|
|
scopes = []
|
|
|
|
|
|
def read(_):
|
|
|
|
|
|
scopes.append(books.CURRENT.get())
|
|
|
|
|
|
if lookup_error:
|
|
|
|
|
|
raise lookup_error
|
|
|
|
|
|
return copy.deepcopy(parent)
|
|
|
|
|
|
try:
|
|
|
|
|
|
with ExitStack() as stack:
|
2026-11-05 09:26:15 +08:00
|
|
|
|
from contextlib import nullcontext
|
|
|
|
|
|
from app.services import execution_orders
|
|
|
|
|
|
from app.repo import execution_repo
|
|
|
|
|
|
stack.enter_context(patch.object(execution_orders, 'account_guard', side_effect=nullcontext))
|
|
|
|
|
|
stack.enter_context(patch.object(execution_repo, 'pending_corrections', return_value=0))
|
2026-11-02 09:02:22 +08:00
|
|
|
|
for obj, name, kw in [
|
|
|
|
|
|
(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', {'new': True}),
|
|
|
|
|
|
(qmt_repo, 'next_queued', {'return_value': [row]}),
|
|
|
|
|
|
(qmt_repo, 'get_order', {'return_value': row}),
|
2026-11-19 16:28:37 +08:00
|
|
|
|
(qmt_repo, 'list_orders', {'return_value': []}),
|
2026-11-02 09:02:22 +08:00
|
|
|
|
(qmt_repo, 'claim_order', {'return_value': True}),
|
|
|
|
|
|
(qmt_repo, 'mark_send_attempt', {'return_value': True}),
|
|
|
|
|
|
(qmt_repo, 'mark_sent', {}),
|
|
|
|
|
|
(pms_repo, 'get_instruction', {'side_effect': read}),
|
|
|
|
|
|
(execution_conflicts, 'queued_buy_blocker', {'return_value': None}),
|
|
|
|
|
|
]:
|
|
|
|
|
|
stack.enter_context(patch.object(obj, name, **kw))
|
|
|
|
|
|
send = stack.enter_context(patch.object(runner, '_send', new_callable=AsyncMock))
|
|
|
|
|
|
retry = stack.enter_context(patch.object(qmt_repo, 'requeue_order'))
|
|
|
|
|
|
loop.run_until_complete(runner._drain_outbox('unused'))
|
|
|
|
|
|
return send, retry, scopes
|
|
|
|
|
|
finally:
|
|
|
|
|
|
loop.close()
|
|
|
|
|
|
asyncio.set_event_loop(None)
|
|
|
|
|
|
|
|
|
|
|
|
def test_both_books_and_sides_use_parent_not_current_scope_or_profile(self):
|
|
|
|
|
|
for book, expected in [('original', 'subjective_quant'), ('micro30', 'micro_rotation')]:
|
|
|
|
|
|
for side in ('buy', 'sell'):
|
|
|
|
|
|
# 原策略减持筹资也可能采用rotation执行方式,仍归主观量化。
|
|
|
|
|
|
parent = {'book_id': book, 'progress': {'execution_profile': 'rotation'}}
|
|
|
|
|
|
with books.scope('micro30' if book == 'original' else 'original'):
|
|
|
|
|
|
send, retry, scopes = self.drain(parent, side=side)
|
|
|
|
|
|
send.assert_awaited_once()
|
|
|
|
|
|
self.assertEqual(send.call_args.args[:2],
|
|
|
|
|
|
('place_order', dict(BASE, side=side, strategy_type=expected)))
|
|
|
|
|
|
self.assertEqual(send.call_args.kwargs, {'corr_id': BASE['instruction_id']})
|
2026-11-05 09:26:15 +08:00
|
|
|
|
self.assertTrue(scopes)
|
|
|
|
|
|
self.assertEqual(set(scopes), {'all'}) # 编码与发送前复核均须按持久父指令归属。
|
2026-11-02 09:02:22 +08:00
|
|
|
|
retry.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
def test_missing_or_unknown_parent_keeps_queue_without_send(self):
|
|
|
|
|
|
for parent in (None, {}, {'book_id': 'other'}):
|
|
|
|
|
|
send, retry, _ = self.drain(parent, side='sell')
|
|
|
|
|
|
send.assert_not_called()
|
|
|
|
|
|
retry.assert_called_once()
|
|
|
|
|
|
self.assertFalse(retry.call_args.args[-1])
|
|
|
|
|
|
|
|
|
|
|
|
def test_parent_lookup_failure_keeps_queue_without_send(self):
|
|
|
|
|
|
send, retry, _ = self.drain(None, lookup_error=RuntimeError('temporary read failure'))
|
|
|
|
|
|
send.assert_not_called()
|
|
|
|
|
|
retry.assert_called_once()
|
|
|
|
|
|
self.assertFalse(retry.call_args.args[-1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
unittest.main()
|