"""逐笔批次凭据、原子入账失败回滚与修正仓储的隔离检查。""" import unit_test_guard import copy 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.db import session as db from app.core import strategy_book as books, ws_codec from app.repo import pms_repo, rotation_repo, trade_correction_repo as repo from app.services import ledger_service, rotation_execution, trade_lineage from config.settings import settings class BookingTests(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory(); self.addCleanup(self.tmp.cleanup) self.engine = create_engine('sqlite:///'+self.tmp.name+'/book.sqlite') self.addCleanup(self.engine.dispose) @event.listens_for(self.engine, 'connect') def connect(connection, _): connection.create_function('GREATEST', -1, max) @event.listens_for(self.engine, 'before_cursor_execute', retval=True) def dialect(conn, cursor, statement, parameters, context, many): db.assert_single_table(statement) statement = statement.replace(' FOR UPDATE', '') if ' ON DUPLICATE KEY UPDATE ' in statement: statement = statement.split(' ON DUPLICATE KEY UPDATE ')[0]+' ON CONFLICT DO NOTHING' parameters = parameters[:statement.count('?')] return statement, parameters for obj, name, kwargs in [ (db, 'get_engine', {'return_value': self.engine}), (db, '_check_cooldown', {}), (settings, 'INFRA_STRATEGY_BOOK_SCHEMA', {'new': True}), (ledger_service.market, 'get_price', {'return_value': 10.0}), (ledger_service.param_store, 'get_float', {'side_effect': lambda k, default=0: default})]: p = patch.object(obj, name, **kwargs); p.start(); self.addCleanup(p.stop) with self.engine.begin() as c: statements = [ 'CREATE TABLE pms_rotation_document(doc_id TEXT PRIMARY KEY,kind TEXT,payload_json TEXT,revision INTEGER,updated_at TEXT)', 'CREATE TABLE pms_qmt_inbox(seq INTEGER PRIMARY KEY,processed INTEGER,processed_at TEXT,process_note TEXT)', 'CREATE TABLE pms_instruction(instruction_id TEXT,book_id TEXT,ts_code TEXT,side TEXT,qty INTEGER,exec_qty INTEGER,exec_avg_price REAL,status TEXT,origin_type TEXT,origin_id TEXT,progress_json TEXT,updated_at TEXT)', 'CREATE TABLE pms_lot(id INTEGER PRIMARY KEY AUTOINCREMENT,book_id TEXT,ts_code TEXT,lot_type TEXT,qty INTEGER,open_price REAL,open_date TEXT,closed_qty INTEGER,close_avg_price REAL,realized_pnl REAL,status TEXT,instruction_id TEXT,note TEXT,created_at TEXT,updated_at TEXT)', 'CREATE TABLE pms_position(book_id TEXT,ts_code TEXT,status TEXT,total_qty INTEGER DEFAULT 0,avail_qty INTEGER DEFAULT 0,base_qty INTEGER,fill_qty INTEGER,add_qty INTEGER,dca_qty INTEGER,t0_qty INTEGER,avg_cost REAL,realized_t_profit REAL DEFAULT 0,cushion_pct REAL,cushion_peak REAL DEFAULT 0,cushion_state TEXT,pct_of_scale REAL,target_pct REAL,opened_date TEXT,updated_at TEXT,UNIQUE(book_id,ts_code))'] for sql in statements: c.execute(text(sql)) c.execute(text("INSERT INTO pms_instruction VALUES('P','original','600000.SH','buy',100,0,NULL,'CANCELLED','system',NULL,'{}','old')")) c.execute(text('INSERT INTO pms_qmt_inbox(seq,processed) VALUES(1,0)')) c.execute(text("INSERT INTO pms_rotation_document VALUES('account-execution','funds_reservation','{}',0,'old')")) account = {'cash': {'original': 10000., 'micro30': 2000.}, 'units': {'original': 10000., 'micro30': 2000.}, 'net_capital': {'original': 10000., 'micro30': 2000.}, 'fees': {'original': 0., 'micro30': 0.}, 'fills': {}, 'transfers': {}} c.execute(text("INSERT INTO pms_rotation_document VALUES('capital:live','capital',:body,1,'old')"), {'body': json.dumps(account)}) self.act = {'instruction_id': 'P', 'child_instruction_id': 'P_D01', 'kind': 'BUY', 'ts_code': '600000.SH', 'qty': 100, 'price': 10., 'lot_type': 'BASE', 'order_id': 'T1', 'source_payload': {'trade_no': 'T1', 'instruction_id': 'P_D01', 'qty': 100, 'price': 10., 'fee': None}} def state(self): with books.scope('all'): return {'positions': db.fetch_all('SELECT * FROM pms_position'), 'lots': db.fetch_all('SELECT * FROM pms_lot'), 'parents': db.fetch_all('SELECT * FROM pms_instruction'), 'inbox': db.fetch_all('SELECT * FROM pms_qmt_inbox'), 'docs': db.fetch_all('SELECT * FROM pms_rotation_document ORDER BY doc_id')} def test_real_booking_retains_exact_lot_and_original_digest(self): self.assertTrue(rotation_execution.book_trade(self.act, [], 1)) receipt, _ = rotation_repo.get(trade_lineage.key('T1')) self.assertEqual(receipt['book_id'], 'original') self.assertEqual(receipt['instruction_id'], 'P_D01') self.assertEqual(receipt['lot_id'], self.state()['lots'][0]['id']) self.assertEqual(receipt['changes'][0]['before'], None) self.assertEqual(len(receipt['original_payload_digest']), 64) self.assertEqual(self.act['source_payload']['fee'], None) self.assertEqual(self.state()['parents'][0]['status'], 'CONFIRMED') capital, _ = rotation_repo.get('capital:live') self.assertEqual(capital['cash'], {'original': 9000., 'micro30': 2000.}) def test_duplicate_does_not_repeat_cash_lots_or_lineage(self): rotation_execution.book_trade(self.act, [], 1) before = self.state() self.assertFalse(rotation_execution.book_trade(self.act, [], 1)) self.assertEqual(self.state(), before) def test_lineage_write_failure_rolls_back_entire_booking(self): before = self.state() with patch.object(trade_lineage.rotation_repo, 'put_once', side_effect=RuntimeError('storage fault')): with self.assertRaises(RuntimeError): rotation_execution.book_trade(self.act, [], 1) self.assertEqual(self.state(), before) def test_conflicting_existing_lineage_does_not_overwrite_or_book(self): rotation_repo.put_once(trade_lineage.key('T1'), 'trade_lineage', {'book_id': 'micro30'}) before = self.state() with self.assertRaises(ValueError): rotation_execution.book_trade(self.act, [], 1) self.assertEqual(self.state(), before) def test_missing_new_lot_mapping_rolls_back_cash(self): before = self.state() with patch.object(trade_lineage, 'lots', return_value=[]): with self.assertRaises(ValueError): rotation_execution.book_trade(self.act, [], 1) self.assertEqual(self.state(), before) class EvidenceTests(unittest.TestCase): def row(self, seq, payload, **fields): return {'seq': seq, 'payload_json': json.dumps(payload), **fields} def test_full_history_limit_is_not_silent_truncation(self): with patch.object(repo.db, 'fetch_all', return_value=[self.row(1, {'instruction_id': 'I'})]): with self.assertRaises(ValueError): repo.trades_for_orders(['I'], limit=1) def test_trade_identity_is_checked_despite_key_truncation(self): with patch.object(repo.db, 'fetch_one', return_value=self.row(1, {'trade_no': 'another'}, msg_type='trade')): with self.assertRaises(ValueError): repo.get_trade('T') def test_amendment_conflict_on_other_child_is_not_hidden(self): first = self.row(1, {'correction_id': 'C', 'instruction_id': 'I'}) second = self.row(2, {'correction_id': 'C', 'instruction_id': 'wrong'}, processed=3) with patch.object(repo.db, 'fetch_all', side_effect=[[first], [second]]): self.assertEqual([r['seq'] for r in repo.amendments('C', 'I')], [1, 2]) def test_original_payload_child_beats_historical_envelope_corr_id(self): rows = [self.row(1, {'instruction_id': 'I'}, corr_id='old-correlation', dedup_key='trade:T'), self.row(2, {'instruction_id': 'I'}, corr_id='I', dedup_key=None)] with patch.object(repo.db, 'fetch_all', return_value=rows): self.assertEqual([r['seq'] for r in repo.trades_for_orders(['I'])], [1]) def test_bad_correction_is_returned_with_seq_for_individual_hold(self): rows = [self.row(1, ['bad']), self.row(2, {'correction_id': 'valid'})] with patch.object(repo.db, 'fetch_all', return_value=rows): found = repo.pending() self.assertEqual(found[0]['seq'], 1) self.assertIsNone(found[0]['payload']) self.assertEqual(found[1]['payload']['correction_id'], 'valid') def test_unrelated_bad_correction_does_not_poison_valid_pair_lookup(self): rows = [self.row(1, ['bad'], processed=3), {'seq': 2, 'payload_json': '{bad', 'processed': 3}, self.row(3, {'correction_id': 'valid', 'instruction_id': 'I'})] with patch.object(repo.db, 'fetch_all', return_value=rows): self.assertEqual([r['seq'] for r in repo.corrections('valid', 'I')], [3]) def test_unrelated_bad_amendment_does_not_poison_global_conflict_lookup(self): first = self.row(3, {'correction_id': 'valid', 'instruction_id': 'I'}) conflicts = [self.row(1, ['bad'], processed=3), {'seq': 2, 'payload_json': '{bad', 'processed': 3}] with patch.object(repo.db, 'fetch_all', side_effect=[[first], conflicts]): self.assertEqual([r['seq'] for r in repo.amendments('valid', 'I')], [3]) def test_bad_payload_with_matching_business_key_remains_conflict_evidence(self): for kind in ('trade_correction', 'order_update'): key = ws_codec.dedup_key(kind, {'correction_id': 'C', 'amended': True}) bad = {'seq': 1, 'payload_json': '{bad', 'processed': 3, 'dedup_key': key} valid = self.row(2, {'correction_id': 'C', 'instruction_id': 'I'}) with self.subTest(kind=kind): if kind == 'trade_correction': with patch.object(repo.db, 'fetch_all', return_value=[bad, valid]): found = repo.corrections('C', 'I') else: with patch.object(repo.db, 'fetch_all', side_effect=[[valid], [bad]]): found = repo.amendments('C', 'I') self.assertEqual([r['seq'] for r in found], [1, 2]) self.assertIsNone(found[0]['payload']) if __name__ == '__main__': unittest.main()