"""策略成交更正版本:真实 SQLite CAS、旧运行快照与提议交错,禁止联网。""" import unit_test_guard # noqa: F401 import copy import datetime as dt import json import sys import tempfile import unittest from pathlib import Path from unittest.mock import patch sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from sqlalchemy import create_engine, event, text from app.core import strategy_book as books from app.db import session as db from app.repo import pms_repo from app.services import strategy_runner as runner, rotation_service from config.settings import settings CODE = '600001.SH' DECISION = {'side': 'buy', 'action': 'ADD', 'qty': 100, 'leg': 'grid_buy:1', 'reason': '测试档位', 'grid_buy_idx': 1, 'grid_buy_price': 10.0} class StrategyCorrectionTests(unittest.TestCase): def setUp(self): self.now = dt.datetime(2026, 11, 5, 10) owner = self class Clock(dt.datetime): @classmethod def now(cls, tz=None): return owner.now self.use(runner, 'datetime', Clock) self.tmp = tempfile.TemporaryDirectory(); self.addCleanup(self.tmp.cleanup) self.engine = create_engine('sqlite:///'+self.tmp.name+'/strategy.sqlite') self.addCleanup(self.engine.dispose) self.statements = [] @event.listens_for(self.engine, 'before_cursor_execute', retval=True) def dialect(conn, cursor, sql, params, context, many): db.assert_single_table(sql) self.statements.append(sql) sql = sql.replace(' FOR UPDATE', '') if 'ON DUPLICATE KEY UPDATE' in sql: self.assertIn('pms_rotation_document', sql) sql = sql.split('ON DUPLICATE KEY UPDATE')[0]+'ON CONFLICT DO NOTHING' return sql, params for obj, name, value in [ (db, 'get_engine', lambda *args: self.engine), (db, '_check_cooldown', lambda *args: None), (settings, 'INFRA_STRATEGY_BOOK_SCHEMA', True), (rotation_service, 'original_buy_blocker', lambda: ''), (runner, '_today', lambda: 20261105), (runner, '_buypause_codes', lambda: set()), (runner, '_macro_gate_active', lambda: False), (runner.param_store, 'get_bool', lambda key, default=False: key == 'PMS_STRATEGY_ENABLED'), (runner.param_store, 'get_float', lambda key, default=0: default), (runner.portfolio, 'positions_view', lambda: {'positions': [{'ts_code': CODE, 'total_qty': 100, 'avail_qty': 100}], 'totals': {'scale': 10000}}), (runner.market, 'day_snapshot', lambda code: {'price': 10}), ]: self.use(obj, name, value) schema = { 'pms_strategy': 'id INTEGER PRIMARY KEY,book_id TEXT,strategy_id TEXT UNIQUE,ts_code TEXT,type TEXT,status TEXT,autonomy TEXT,state_json TEXT,params_json TEXT,note TEXT,archived_at TEXT,created_at TEXT,updated_at TEXT', 'pms_instruction': 'id INTEGER PRIMARY KEY,book_id TEXT,instruction_id TEXT UNIQUE,origin_type TEXT,origin_id TEXT,ts_code TEXT,action TEXT,side TEXT,qty INTEGER,limit_price REAL,window_tdays INTEGER,status TEXT,exec_qty INTEGER,exec_avg_price REAL,progress_json TEXT,archived_at TEXT,created_at TEXT,updated_at TEXT', 'pms_proposal': 'id INTEGER PRIMARY KEY,book_id TEXT,proposal_id TEXT UNIQUE,ts_code TEXT,action TEXT,qty INTEGER,hard_numbers_json TEXT,judge_verdict TEXT,judge_reason TEXT,status TEXT,expire_at TEXT,created_at TEXT,archived_at TEXT', 'pms_rotation_document': 'doc_id TEXT PRIMARY KEY,kind TEXT,payload_json TEXT,revision INTEGER,updated_at TEXT', } with self.engine.begin() as conn: for table, cols in schema.items(): conn.execute(text('CREATE TABLE '+table+'('+cols+')')) pms_repo.insert_strategy(strategy_id='GRID', ts_code=CODE, stype='GRID', state={'invested': 1000, 'filled_levels': {'0': {'price': 10, 'qty': 100}}}) self.original = pms_repo.get_strategy('GRID') def use(self, obj, name, value): p = patch.object(obj, name, value); p.start(); self.addCleanup(p.stop) def count(self, table): with self.engine.connect() as conn: return conn.execute(text('SELECT COUNT(*) FROM '+table)).scalar_one() def correction(self, cid='C1', paused=False): old = pms_repo.get_strategy('GRID')['state'] new = copy.deepcopy(old) new.update(invested=1100, trade_correction={'correction_id': cid}) if paused: new['correction_hold'] = '数量更正后等待核对' pms_repo.update_strategy('GRID', state=new, status='PAUSED' if paused else 'ACTIVE', correction_expected=(old.get('trade_correction') or {}).get('correction_id')) return new def test_legacy_state_and_json_null_epoch_update(self): self.assertEqual(pms_repo.update_strategy('GRID', state={'pending': None}), 1) self.assertEqual(pms_repo.update_strategy('GRID', state={'trade_correction': {'correction_id': None}}), 1) self.assertEqual(pms_repo.update_strategy('GRID', state={'invested': 2}), 1) def test_original_snapshot_cannot_overwrite_new_state_or_paused_status(self): new = self.correction(paused=True) with self.assertRaisesRegex(RuntimeError, '更正版本'): pms_repo.update_strategy('GRID', state=self.original['state'], status='ACTIVE', note='旧任务') fresh = pms_repo.get_strategy('GRID') self.assertEqual(fresh['state'], new) self.assertEqual(fresh['status'], 'PAUSED') self.assertIsNone(fresh['note']) def test_chain_compare_and_set_requires_previous_id(self): self.correction('C1') self.correction('C2') with self.assertRaises(RuntimeError): pms_repo.update_strategy('GRID', state={'trade_correction': {'correction_id': 'C3'}}, correction_expected='C1') self.assertEqual(runner._correction_id(pms_repo.get_strategy('GRID')), 'C2') def test_json_state_cannot_bypass_cas(self): self.correction() with self.assertRaises(RuntimeError): pms_repo.update_strategy('GRID', state_json='{}') self.assertEqual(pms_repo.update_strategy('GRID', state_json=json.dumps({'trade_correction': {'correction_id': 'C1'}, 'pending': None})), 1) def test_missing_strategy_state_write_fails_but_ordinary_status_keeps_zero(self): self.assertEqual(pms_repo.update_strategy('missing', status='PAUSED'), 0) with self.assertRaises(RuntimeError): pms_repo.update_strategy('missing', state={}) def test_version_keyword_is_not_a_column_and_requires_state(self): with self.assertRaises(ValueError): pms_repo.update_strategy('GRID', status='PAUSED', correction_expected=None) self.correction() self.assertFalse(any('SET correction_expected' in sql for sql in self.statements)) def test_cross_book_cannot_touch_other_strategy(self): with books.scope('micro30'): with self.assertRaises(RuntimeError): pms_repo.update_strategy('GRID', state={}) self.assertEqual(pms_repo.get_strategy('GRID')['state'], self.original['state']) def test_stale_snapshot_rejected_for_instruction_and_proposal(self): self.correction() for emit in (runner._emit_instruction, runner._emit_proposal): with self.assertRaisesRegex(RuntimeError, '更正版本'): emit(self.original, DECISION) self.assertEqual(self.count('pms_instruction'), 0) self.assertEqual(self.count('pms_proposal'), 0) def test_paused_hold_and_archived_strategy_do_not_emit(self): for fields in ({'status': 'PAUSED'}, {'state': {'correction_hold': '等待核对'}}): old = pms_repo.get_strategy('GRID') pms_repo.update_strategy('GRID', **fields) with self.assertRaises(RuntimeError): runner._emit_instruction(old, DECISION) pms_repo.update_strategy('GRID', status='ACTIVE', state={}) with self.engine.begin() as conn: conn.execute(text("UPDATE pms_strategy SET archived_at='2026-11-05'")) with self.assertRaises(RuntimeError): runner._emit_proposal(self.original, DECISION) self.assertEqual(self.count('pms_instruction'), 0) self.assertEqual(self.count('pms_proposal'), 0) def test_new_instruction_and_proposal_record_exact_epoch_under_lock(self): self.correction() fresh = pms_repo.get_strategy('GRID') insert = pms_repo.insert_instruction def guarded(**kwargs): self.assertIsNotNone(db._TRANSACTION.get()) return insert(**kwargs) with patch.object(pms_repo, 'insert_instruction', side_effect=guarded): iid = runner._emit_instruction(fresh, DECISION) pid = runner._emit_proposal(fresh, DECISION) self.assertEqual(pms_repo.get_instruction(iid)['progress']['strategy_correction_id'], 'C1') self.assertEqual(pms_repo.get_proposal(pid)['hard_numbers']['strategy_correction_id'], 'C1') self.assertTrue(any("doc_id='account-execution' FOR UPDATE" in sql for sql in self.statements)) def test_pre_correction_proposal_cannot_be_accepted_after_correction(self): pid = runner._emit_proposal(self.original, DECISION) spec = pms_repo.get_proposal(pid)['hard_numbers'] self.correction() result = runner.emit_from_spec(spec) self.assertFalse(result['ok'], result) self.assertEqual(self.count('pms_instruction'), 0) self.assertNotIn('pending', pms_repo.get_strategy('GRID')['state']) def test_current_proposal_accepts_and_keeps_epoch_in_pending_state(self): self.correction() spec = dict(DECISION, strategy_id='GRID', strategy_correction_id='C1') result = runner.emit_from_spec(spec) self.assertTrue(result['ok'], result) fresh = pms_repo.get_strategy('GRID') self.assertEqual(fresh['state']['pending']['iid'], result['instruction_id']) self.assertEqual(runner._correction_id(fresh), 'C1') def test_legacy_proposal_before_any_correction_still_accepts(self): result = runner.emit_from_spec(dict(DECISION, strategy_id='GRID')) self.assertTrue(result['ok'], result) self.assertIsNone(pms_repo.get_instruction(result['instruction_id'])['progress']['strategy_correction_id']) def test_accept_state_write_failure_rolls_back_new_parent(self): with patch.object(pms_repo, 'update_strategy', side_effect=RuntimeError('模拟状态冲突')): with self.assertLogs('pms.strategy', level='ERROR'): result = runner.emit_from_spec(dict(DECISION, strategy_id='GRID')) self.assertFalse(result['ok'], result) self.assertEqual(self.count('pms_instruction'), 0) def tick(self, *, decision=True, corrected=False, paused=False, autonomy='auto', dry_run=False): pms_repo.update_strategy('GRID', autonomy=autonomy) def evaluate(st, pos, day, now, ctx): if corrected: self.correction(paused=paused) return copy.deepcopy(DECISION) if decision else None with patch.dict(runner.EVALUATORS, {'GRID': evaluate}): return runner.tick(dry_run=dry_run) def test_tick_loaded_before_correction_cannot_publish_or_overwrite(self): for autonomy in ('auto', 'confirm'): with self.assertLogs('pms.strategy', level='ERROR'): out = self.tick(corrected=True, paused=autonomy=='confirm', autonomy=autonomy) self.assertFalse(out['ok'], out) self.assertEqual(out['fired'], []) self.assertEqual(out['queued'], []) self.assertEqual(self.count('pms_instruction'), 0) self.assertEqual(self.count('pms_proposal'), 0) self.assertEqual(pms_repo.get_strategy('GRID')['state']['invested'], 1100) # 下一子例从新的、无更正标记的独立策略状态开始。 with self.engine.begin() as conn: conn.execute(text("UPDATE pms_strategy SET state_json=:body,status='ACTIVE'"), {'body':json.dumps(self.original['state'])}) def test_idle_tick_old_state_cannot_erase_correction(self): with self.assertLogs('pms.strategy', level='ERROR'): out = self.tick(decision=False, corrected=True) self.assertFalse(out['ok'], out) self.assertEqual(runner._correction_id(pms_repo.get_strategy('GRID')), 'C1') def test_tick_success_persists_pending_and_parent_together(self): self.correction() out = self.tick() self.assertTrue(out['ok'], out) self.assertEqual(len(out['fired']), 1) fresh = pms_repo.get_strategy('GRID') parent = pms_repo.get_instruction(fresh['state']['pending']['iid']) self.assertEqual(parent['progress']['strategy_correction_id'], 'C1') self.assertEqual(fresh['state']['invested'], 2100) def test_tick_state_failure_rolls_back_parent_and_reports_no_fired(self): original_update = pms_repo.update_strategy def fail_state(sid, **fields): if 'state' in fields: raise RuntimeError('模拟版本冲突') return original_update(sid, **fields) with patch.object(pms_repo, 'update_strategy', side_effect=fail_state): with self.assertLogs('pms.strategy', level='ERROR'): out = self.tick() self.assertFalse(out['ok'], out) self.assertEqual(out['fired'], []) self.assertEqual(self.count('pms_instruction'), 0) self.assertEqual(pms_repo.get_strategy('GRID')['state'], self.original['state']) def test_dry_run_keeps_strategy_and_does_not_lock_or_insert(self): self.statements.clear() out = self.tick(dry_run=True) self.assertTrue(out['ok'], out) self.assertEqual(pms_repo.get_strategy('GRID')['state'], self.original['state']) self.assertFalse(any('account-execution' in sql for sql in self.statements)) self.assertEqual(self.count('pms_instruction'), 0) def test_soft_archived_parent_does_not_archive_current_strategy(self): iid = runner._emit_instruction(self.original, DECISION) with self.engine.begin() as conn: conn.execute(text("UPDATE pms_instruction SET status='CONFIRMED',archived_at='2026-11-05' WHERE instruction_id=:iid"), {'iid': iid}) self.correction() self.now += dt.timedelta(minutes=1) result = runner.emit_from_spec(dict(DECISION, strategy_id='GRID', strategy_correction_id='C1', leg='grid_sell:1', side='sell', action='TRIM')) self.assertTrue(result['ok'], result) self.assertEqual(self.count('pms_instruction'), 2) if __name__ == '__main__': unittest.main()