akg-factor-bridge/test_build_readiness.py

97 lines
5.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""本地离线:python -m unittest test_build_readiness;来源与写入全部替身。"""
import datetime as dt
import socket
import sys
import types
import unittest
from unittest.mock import patch
import pandas as pd
for name in ('psycopg', 'pymysql'):
try:
__import__(name)
except ImportError:
sys.modules[name] = types.ModuleType(name)
import build_readiness as readiness
import run
NOW = dt.datetime.fromisoformat('2026-10-19T01:40:00+00:00')
class ReadinessTest(unittest.TestCase):
def setUp(self):
self.net = patch.object(socket.socket, 'connect', side_effect=AssertionError('禁止联网'))
self.net.start()
self.addCleanup(self.net.stop)
def test_monday_uses_friday(self):
self.assertEqual(readiness.expected_data_day(NOW), dt.date(2026, 10, 16))
def test_beijing_midnight_not_utc_date(self):
now = dt.datetime.fromisoformat('2026-10-18T17:50:00+00:00')
self.assertEqual(readiness.expected_data_day(now), dt.date(2026, 10, 16))
def test_holiday_and_weekend(self):
with patch('chinese_calendar.is_holiday', side_effect=lambda d: dt.date(2026, 10, 1) <= d <= dt.date(2026, 10, 7)):
now = dt.datetime.fromisoformat('2026-10-08T07:10:00+08:00')
self.assertEqual(readiness.expected_data_day(now), dt.date(2026, 9, 30))
def test_aligned_sources_pass(self):
frame = pd.DataFrame({'d': [dt.date(2026, 10, 16)]})
with patch.object(readiness.db, 'read_pg', return_value=frame), patch.object(readiness.db, 'read_mysql', return_value=frame):
self.assertEqual(readiness.check(NOW), '2026-10-16')
def test_stale_and_empty_sources_fail(self):
for value in ['2026-10-15', None, pd.NaT, '2026-10-19']:
with self.subTest(value=value), patch.object(readiness.db, 'read_pg', return_value=pd.DataFrame({'d': [value]})), patch.object(readiness.db, 'read_mysql', return_value=pd.DataFrame({'d': ['2026-10-16']})):
with self.assertRaisesRegex(RuntimeError, '应为 2026-10-16.*传导日期'):
readiness.check(NOW)
def test_missing_heat_fails_even_if_scan_current(self):
with patch.object(readiness.db, 'read_pg', return_value=pd.DataFrame({'d': ['2026-10-16']})), patch.object(readiness.db, 'read_mysql', side_effect=lambda source, sql: pd.DataFrame({'d': ['2026-10-15' if source == 'heat' else '2026-10-16']})):
with self.assertRaisesRegex(RuntimeError, '资金热度日期 2026-10-15'):
readiness.check(NOW)
def test_unavailable_source_does_not_guess_today(self):
with patch.object(readiness.db, 'read_pg', side_effect=OSError('secret-connection')), patch.object(readiness.db, 'read_mysql', return_value=pd.DataFrame({'d': ['2026-10-16']})):
with self.assertRaisesRegex(RuntimeError, '传导读取失败') as result:
readiness.check(NOW)
self.assertNotIn('secret-connection', str(result.exception))
def test_stale_default_stops_before_any_writes(self):
with patch.object(readiness, 'check', side_effect=RuntimeError('上游停更')), patch.object(run.common, 'write_factor') as write:
with self.assertRaisesRegex(RuntimeError, '上游停更'):
run.cmd_build('all', 'daily', None, None, None)
write.assert_not_called()
def test_explicit_history_keeps_existing_entry(self):
with patch.object(readiness, 'check', side_effect=AssertionError('不限制显式维护日期')), patch.object(run.factors, 'BUILDERS', {'test': lambda a,b: pd.DataFrame()}), patch.object(run.factors, 'FACTORS', {'test': 'test_table'}), patch.object(run.common, 'write_factor') as write:
run.cmd_build('test', 'daily', None, None, '2026-10-15', do_freeze=False)
write.assert_called_once()
def test_partial_build_failure_is_not_success(self):
def fail(a,b): raise ValueError('failed')
frozen=types.ModuleType('freeze'); frozen.snapshot=lambda *a,**kw: self.fail('不可冻结部分结果')
with patch.dict(sys.modules, {'freeze': frozen}), patch.object(run.factors, 'BUILDERS', {'good': lambda a,b: pd.DataFrame(), 'bad': fail}), patch.object(run.factors, 'FACTORS', {'good': 'g', 'bad': 'b'}), patch.object(run.common, 'write_factor') as write:
with self.assertRaisesRegex(RuntimeError, '因子构建失败:bad'):
run.cmd_build('all', 'daily', None, None, '2026-10-16')
write.assert_called_once()
def test_freeze_failure_is_not_success(self):
frozen=types.ModuleType('freeze')
def fail(*a, **kw): raise OSError('disk full')
frozen.snapshot=fail
with patch.dict(sys.modules, {'freeze': frozen}), patch.object(run.factors, 'BUILDERS', {'test': lambda a,b: pd.DataFrame()}), patch.object(run.factors, 'FACTORS', {'test': 't'}), patch.object(run.common, 'write_factor'):
with self.assertRaisesRegex(RuntimeError, '输入冻结失败'):
run.cmd_build('test', 'daily', None, None, '2026-10-16')
def test_refresh_missing_morning_fails_cli(self):
mod=types.ModuleType('plan_refresh')
for status in ('no_morning', 'no_morning_today', 'outside_window', 'unchanged'):
mod.refresh=lambda *a, **kw: {'status': status}
with self.subTest(status=status), patch.dict(sys.modules, {'plan_refresh': mod}), patch.object(sys, 'argv', ['run.py','plan-refresh']):
if status.startswith('no_morning'):
with self.assertRaisesRegex(SystemExit, '今日完整晨版缺失'): run.main()
else: run.main()
if __name__ == '__main__':
unittest.main()