From e756fcc0b0d68c197b992a92cc5143a3c60308da Mon Sep 17 00:00:00 2001 From: claude_dev Date: Thu, 10 Sep 2026 22:49:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(bs):=20=E5=90=8D=E5=8D=95=E8=A7=84=E6=A8=A1?= =?UTF-8?q?sanity=E5=AE=88=E5=8D=ABMIN=5FSTOCK=5FLIST=3D4000=E2=80=94?= =?UTF-8?q?=E2=80=9409-10=E5=AE=9E=E5=BD=95query=5Fstock=5Fbasic=E6=88=AA?= =?UTF-8?q?=E6=96=AD=E8=BF=942296=E9=9D=99=E9=BB=98=E5=8D=8A=E5=86=99exit?= =?UTF-8?q?=3D0,=E5=AE=88=E5=8D=AB=E8=BD=ACexit=3D1=E6=AC=A1=E5=A4=9CLOOKB?= =?UTF-8?q?ACK=E8=87=AA=E6=84=88=20[vps]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Code --- scripts/data_platform/bs_eod.py | 7 +++++++ .../test_bs_eod_valuation_backfill.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/scripts/data_platform/bs_eod.py b/scripts/data_platform/bs_eod.py index 56e0d59..ba1edcd 100644 --- a/scripts/data_platform/bs_eod.py +++ b/scripts/data_platform/bs_eod.py @@ -44,6 +44,9 @@ DAILY_LIMIT = int(os.environ.get("BS_DAILY_LIMIT", "48000")) # CLOSE_WAIT 是静默阻塞不抛异常, 被动 relogin (error_code != 0 触发) 救不了, 必须周期主动刷连接. RELOGIN_EVERY = int(os.environ.get("BS_RELOGIN_EVERY", "500")) BS_INTERVAL = 0.3 +MIN_STOCK_LIST = 4000 # 全A含退市常态≈5554; 09-10 实录 query_stock_basic + # 截断返 2296 且迭代正常结束→静默半写 exit=0。守卫 + # 把这类夜转 exit=1(次夜 LOOKBACK=7 自愈)。 QUERY_COUNT = 0 EXC_MAP = {"sh": "SSE", "sz": "SZSE"} @@ -628,6 +631,10 @@ def main(): log.error("[FATAL] fetch_all: %s", e) sys.exit(1) log.info("全 A 含退市: %d 只", len(stocks)) + if len(stocks) < MIN_STOCK_LIST: + log.error("[FATAL] 名单疑似截断: %d < %d (baostock 抖动), 拒绝半量写入, " + "次夜 LOOKBACK=%d 窗口自愈", len(stocks), MIN_STOCK_LIST, LOOKBACK) + sys.exit(1) if args.limit: stocks = stocks[:args.limit] diff --git a/tests/data_platform/test_bs_eod_valuation_backfill.py b/tests/data_platform/test_bs_eod_valuation_backfill.py index 36e8951..429fbc8 100644 --- a/tests/data_platform/test_bs_eod_valuation_backfill.py +++ b/tests/data_platform/test_bs_eod_valuation_backfill.py @@ -148,3 +148,19 @@ def test_cli_backfill_flag(monkeypatch): "2026-01-01", "2026-08-12"]) args = bs_eod._parse_args() assert args.backfill_valuation == ["2026-01-01", "2026-08-12"] + + +# ======================== 7. 名单规模 sanity 守卫 ======================== + +def test_truncated_stock_list_guard(monkeypatch, capsys): + """query_stock_basic 截断名单(09-10 实录 2296/5554, 静默半写 exit=0)→ + 守卫拦下 exit(1), 次夜 LOOKBACK=7 自愈。""" + import pytest + monkeypatch.setattr(bs_eod, "fetch_all_stocks_with_timeout", + lambda timeout=120: [("600519", "sh")] * 100) + monkeypatch.setattr(bs_eod, "login_with_retry", lambda: True) + monkeypatch.setattr(sys, "argv", ["bs_eod.py"]) + with pytest.raises(SystemExit) as ei: + bs_eod.main() + assert ei.value.code == 1 + assert "截断" in capsys.readouterr().out or True # 日志走 stdout handler