"""独立限制价采集的离线业务边界。HTTP、数据库、日历和时钟均使用内存桩。""" import unit_test_guard # noqa: F401 import contextlib import copy import unittest from datetime import datetime, timedelta from unittest.mock import patch import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from app.core import price_limits as core from app.core.execution_policy import BEIJING from app.services import price_limit_collector as worker, param_store from config.settings import settings NOW = datetime(2026, 11, 3, 9, 0, tzinfo=BEIJING) DAY = '2026-11-03' A, B = '600000.SH', '000001.SZ' def row(code=A, day=DAY, high='11.00'): return {'ts_code': code, 'trade_date': day, 'pre_close': '10.00', 'up_limit': high, 'down_limit': '9.00'} class CollectorTests(unittest.TestCase): def setUp(self): self.stack = contextlib.ExitStack() self.addCleanup(self.stack.close) self.docs, self.params, self.rows, self.calls = {}, {}, [], [] self.seconds = 0. self.target = {'codes': [A, B], 'errors': [], 'sources': {A: ['持仓'], B: ['微盘候选']}} self.result = lambda code: core.normalize_rows([row(code)], DAY, [code]) self.stack.enter_context(patch.object(settings, 'PMS_TABLE_PREFIX', '')) self.stack.enter_context(patch.object(settings, 'PMS_TUSHARE_TOKEN', 'test-secret-never-print')) for name in ('get_bool', 'get_int', 'get_float'): self.stack.enter_context(patch.object(param_store, name, side_effect=lambda key, default=None: self.params.get(key, True if key == 'PMS_LIMIT_COLLECT_ENABLED' else default))) self.stack.enter_context(patch.object(worker.tradedays, 'calendar_degraded', return_value=False)) self.stack.enter_context(patch.object(worker.tradedays, 'is_trade_day', return_value=True)) self.stack.enter_context(patch.object(worker.time, 'monotonic', side_effect=lambda: self.seconds)) self.stack.enter_context(patch.object(worker.time, 'sleep', side_effect=self.advance)) original_get = param_store.get self.stack.enter_context(patch.object(param_store, 'get', side_effect=lambda key, default=None: original_get(key, default) if key in param_store.SECRET_KEYS else self.params.get(key, default))) self.collect = self.stack.enter_context(patch.object(worker.price_limit_targets, 'collect_targets', side_effect=lambda day: copy.deepcopy(self.target))) self.shared = self.stack.enter_context(patch.object(worker.db, 'fetch_all', side_effect=lambda *a, **kw: copy.deepcopy(self.rows))) self.stack.enter_context(patch.object(worker.store, 'get', side_effect=lambda key: (copy.deepcopy(self.docs.get(key)), 1))) self.stack.enter_context(patch.object(worker.store, 'locked', self.locked)) self.fetch = self.stack.enter_context(patch.object(worker.price_limit_provider, 'fetch_limit', side_effect=self.provider)) import requests self.forbidden = [self.stack.enter_context(patch.object(obj, name, side_effect=AssertionError('真实外部访问'))) for obj, name in [(requests, 'Session'), (worker.db, 'execute'), (worker.db, 'fetch_one')]] def tearDown(self): for method in self.forbidden: method.assert_not_called() @contextlib.contextmanager def locked(self, key, kind, initial): value = copy.deepcopy(self.docs.get(key, initial)) yield value self.docs[key] = value def advance(self, seconds): self.seconds += seconds def provider(self, code, day, token, timeout_sec): self.calls.append((code, day, self.seconds)) self.assertEqual(day, DAY) self.assertGreater(timeout_sec, 0) return self.result(code) def entry(self, code=A): return self.docs[worker.day_key(DAY)]['entries'][code] def run_round(self, now=NOW): return worker.run_once(now) def test_missing_targets_fetched_and_persisted_with_source(self): result = self.run_round() self.assertEqual((result['status'], result['valid_count'], result['requests']), ('COMPLETE', 2, 2)) self.assertEqual(self.entry()['source'], 'tushare.stk_limit') self.assertEqual(self.entry()['record']['trade_date'], DAY) self.assertEqual(self.docs[worker.day_key(DAY)]['target_sources'][A], ['持仓']) self.assertNotIn('owner', self.docs[worker.LEASE_KEY]) def test_single_code_requests_are_globally_spaced(self): self.run_round() self.assertEqual(len(self.calls), 2) self.assertGreaterEqual(self.calls[1][2]-self.calls[0][2], 1.2) for code, _, _ in self.calls: self.assertNotIn(',', code) def test_shared_current_values_avoid_redundant_http(self): self.rows = [row(A), row(B)] self.assertEqual(self.run_round()['valid_count'], 2) self.fetch.assert_not_called() self.assertEqual(self.entry()['source'], 'gp_stk_limit') sql, bindings = self.shared.call_args.args self.assertIn('ts_code IN', sql) self.assertEqual(set(bindings.values()), {DAY, A, B}) def test_target_failure_keeps_other_targets(self): self.target['errors'] = ['计划读取失败'] result = self.run_round() self.assertEqual((result['status'], result['valid_count']), ('PARTIAL', 2)) self.assertEqual(result['errors'], ['计划读取失败']) def test_bj_excluded_before_shared_and_http(self): self.target['codes'].append('920001.BJ') result = self.run_round() self.assertEqual(result['target_count'], 2) self.assertNotIn('920001.BJ', self.shared.call_args.args[1].values()) self.assertNotIn('920001.BJ', [c[0] for c in self.calls]) def test_wrong_day_shared_value_does_not_stop_provider(self): self.rows = [row(A, '2026-11-02')] self.run_round() self.assertEqual(self.entry()['status'], 'VALID') self.assertEqual(self.entry()['source'], 'tushare.stk_limit') def test_one_empty_response_keeps_other_valid_and_retries_later(self): self.result = lambda c: core.normalize_rows([] if c == A else [row(c)], DAY, [c]) r = self.run_round() self.assertEqual((r['valid_count'], r['missing_count']), (1, 1)) self.assertEqual(self.entry()['last_error'], 'NOT_PUBLISHED') self.calls.clear() self.run_round(NOW+timedelta(seconds=20)) self.assertFalse(self.calls) self.run_round(NOW+timedelta(seconds=70)) self.assertEqual([c[0] for c in self.calls], [A]) self.assertEqual(self.entry()['failures'], 2) def test_failed_first_does_not_starve_next_round(self): self.params['PMS_LIMIT_ROUND_MAX_CODES'] = 1 self.result = lambda c: core.normalize_rows([], DAY, [c]) self.run_round() first = self.calls[-1][0] self.run_round(NOW+timedelta(seconds=10)) self.assertNotEqual(first, self.calls[-1][0]) def test_max_failures_persist_across_rounds(self): self.params['PMS_LIMIT_MAX_FAILURES_PER_DAY'] = 1 self.result = lambda c: {'records': {}, 'invalid': {}, 'errors': ['HTTP_TIMEOUT']} self.run_round() self.calls.clear() self.run_round(NOW+timedelta(minutes=20)) self.assertFalse(self.calls) def test_transport_error_keeps_valid_content_and_original_timestamp(self): self.run_round() old = copy.deepcopy(self.entry()) self.result = lambda c: {'records': {}, 'invalid': {}, 'errors': ['HTTP_TIMEOUT']} self.run_round(NOW+timedelta(minutes=20)) self.assertEqual(self.entry()['record'], old['record']) self.assertEqual(self.entry()['fetched_at'], old['fetched_at']) self.assertEqual(self.entry()['status'], 'VALID') self.assertEqual(self.entry()['last_error'], 'HTTP_TIMEOUT') def test_explicit_bad_value_quarantines_existing_record(self): self.run_round() self.result = lambda c: core.normalize_rows([row(c, high='0')], DAY, [c]) self.run_round(NOW+timedelta(minutes=20)) self.assertEqual(self.entry()['status'], 'INVALID') self.assertEqual(self.entry()['last_error'], 'UNSUPPORTED') def test_revised_value_keeps_prior_version(self): self.run_round() old = self.entry()['record']['version'] self.result = lambda c: core.normalize_rows([row(c, high='11.20')], DAY, [c]) self.run_round(NOW+timedelta(minutes=20)) self.assertNotEqual(self.entry()['record']['version'], old) self.assertEqual(self.entry()['revisions'][0]['record']['version'], old) def test_later_shared_old_record_cannot_override_tushare(self): self.run_round() old = self.entry()['record'] self.rows = [row(A, high='11.10')] self.run_round(NOW+timedelta(seconds=30)) self.assertEqual(self.entry()['record'], old) def test_shared_failure_uses_provider(self): self.shared.side_effect = RuntimeError('never-print') r = self.run_round() self.assertEqual(r['valid_count'], 2) self.assertEqual(r['errors'], ['共享限制价读取失败']) def test_missing_token_still_uses_shared_data(self): self.stack.enter_context(patch.object(settings, 'PMS_TUSHARE_TOKEN', '')) self.rows = [row(A)] r = self.run_round() self.assertEqual(r['valid_count'], 1) self.assertIn('采集密钥未配置', r['errors']) self.fetch.assert_not_called() def test_real_prefix_disabled_and_calendar_window_guards(self): with patch.object(settings, 'PMS_TABLE_PREFIX', 'real_'): self.assertEqual(self.run_round()['status'], 'UNAUTHORIZED_INSTANCE') self.params['PMS_LIMIT_COLLECT_ENABLED'] = False self.assertEqual(self.run_round()['status'], 'DISABLED') self.params.clear() with patch.object(worker.tradedays, 'calendar_degraded', return_value=True): self.assertEqual(self.run_round()['status'], 'CALENDAR_WAIT') with patch.object(worker.tradedays, 'is_trade_day', return_value=False): self.assertEqual(self.run_round()['status'], 'CALENDAR_WAIT') self.assertEqual(self.run_round(NOW.replace(hour=8))['status'], 'OUTSIDE_WINDOW') self.params['PMS_LIMIT_COLLECT_START'] = 'bad' self.assertEqual(self.run_round()['status'], 'INVALID_WINDOW') self.collect.assert_not_called() def test_invalid_runtime_values_do_not_fetch(self): self.params['PMS_LIMIT_MIN_REQUEST_INTERVAL_SEC'] = 0 self.assertEqual(self.run_round()['status'], 'INVALID_CONFIG') self.collect.assert_not_called() def test_live_lease_skips_all_sources(self): self.docs[worker.LEASE_KEY] = {'owner': 'other', 'until': (NOW+timedelta(seconds=30)).isoformat()} self.assertEqual(self.run_round()['status'], 'BUSY') self.collect.assert_not_called() def test_expired_lease_is_recovered(self): self.docs[worker.LEASE_KEY] = {'owner': 'old', 'until': (NOW-timedelta(seconds=1)).isoformat()} self.assertEqual(self.run_round()['valid_count'], 2) def test_lost_lease_does_not_overwrite_new_owner(self): def replaced(c): self.docs[worker.LEASE_KEY] = {'owner': 'new', 'until': (NOW+timedelta(seconds=300)).isoformat()} return core.normalize_rows([row(c)], DAY, [c]) self.result = replaced self.assertEqual(self.run_round()['status'], 'LEASE_LOST') self.assertNotIn(worker.day_key(DAY), self.docs) self.assertEqual(self.docs[worker.LEASE_KEY]['owner'], 'new') def test_round_timeout_discards_late_response(self): def slow(c): self.advance(31) return core.normalize_rows([row(c)], DAY, [c]) self.result = slow r = self.run_round() self.assertEqual(r['valid_count'], 0) self.assertIn('本轮采集超时,迟到响应未采纳', r['errors']) self.assertEqual(self.entry()['status'], 'MISSING') def test_old_day_document_is_not_reused(self): self.docs[worker.day_key(DAY)] = {'trade_date': '2026-11-02', 'entries': {A: {'status': 'VALID'}}} self.run_round() self.assertEqual(self.entry()['record']['trade_date'], DAY) def test_shared_bad_row_cannot_reset_http_backoff(self): self.rows = [row(A, high='0')] self.result = lambda c: {'records': {}, 'invalid': {}, 'errors': ['HTTP_TIMEOUT']} self.run_round() self.calls.clear() self.run_round(NOW+timedelta(seconds=20)) self.assertFalse(self.calls) self.assertEqual(self.entry()['failures'], 1) def test_slow_first_request_does_not_starve_next_stock(self): def slow(c): self.advance(31) return core.normalize_rows([row(c)], DAY, [c]) self.result = slow self.run_round() first = self.calls[-1][0] self.run_round(NOW+timedelta(seconds=40)) self.assertNotEqual(first, self.calls[-1][0]) def test_empty_targets_no_market_wide_query(self): self.target = {'codes': [], 'errors': [], 'sources': {}} self.assertEqual(self.run_round()['target_count'], 0) self.shared.assert_not_called() self.fetch.assert_not_called() def test_one_bad_schedule_record_does_not_block_other_targets(self): self.docs[worker.day_key(DAY)] = {'trade_date': DAY, 'entries': { A: {'status': 'INVALID', 'failures': 'bad', 'last_attempt': [], 'revisions': {}, 'last_error': {'bad': True}}}} self.assertEqual(self.run_round()['valid_count'], 2) self.assertEqual(self.entry()['status'], 'VALID') def test_bad_entries_container_is_recovered(self): self.docs[worker.day_key(DAY)] = {'trade_date': DAY, 'entries': []} self.assertEqual(self.run_round()['valid_count'], 2) def test_secret_not_editable_or_readable_through_paramstore(self): self.assertIn('PMS_TUSHARE_TOKEN', param_store.SECRET_KEYS) self.assertNotIn('PMS_TUSHARE_TOKEN', param_store._editable_keys()) self.assertEqual(param_store.get('PMS_TUSHARE_TOKEN'), '') if __name__ == '__main__': unittest.main()