akg-factor-bridge/test_plan_review_source.py

89 lines
3.5 KiB
Python
Raw 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.

"""周报底本改读当日快照(工作包丙,2026-09-17 拍板):先读快照、无快照回落现算、开关 live 回旧。
离线,不连库。跑法:python3 test_plan_review_source.py,预期最后一行是 ALL OK。
"""
import json
import os
import sys
import tempfile
import types
# 离线不连库:缺席的驱动打空壳,pandas 本机装得上用真的。
for _n in ("psycopg", "pymysql", "dotenv"):
if _n not in sys.modules:
try:
__import__(_n)
except Exception: # noqa: BLE001
sys.modules[_n] = types.ModuleType(_n)
import pandas as pd # noqa: E402,F401 —— 只为触发真 pandas 导入,plan_review 要用
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import config # noqa: E402
import plan # noqa: E402
import plan_review # noqa: E402
FAILED = []
def t(name, cond, extra=""):
print((" ok " if cond else " FAIL ") + name + ((" " + str(extra)) if not cond else ""))
if not cond:
FAILED.append(name)
def main():
day = "2026-09-16"
snap = {"date": day,
"main": [{"code": "600000.SH", "rank": 1}, {"code": "000001.SZ", "rank": 2}],
"observe": [{"code": "600519.SH", "rank": 1}],
"candidates": [{"code": "600000.SH"}], "watch": [{"code": "000001.SZ"}]}
old_dir, old_src, old_collect = config.PLAN_SNAPSHOT_DIR, config.PLAN_REVIEW_SOURCE, plan.collect
calls = {"n": 0}
def fake_collect(d, **kw):
calls["n"] += 1
return {"date": d, "_full": {"main": [{"code": "X", "rank": 1}], "observe": []},
"candidates": [], "watch": []}
try:
with tempfile.TemporaryDirectory() as tmp:
config.PLAN_SNAPSHOT_DIR = tmp
plan.collect = fake_collect
with open(os.path.join(tmp, f"plan_{day}.json"), "w", encoding="utf-8") as f:
json.dump(snap, f)
# 1. 有快照:读快照,不现算
config.PLAN_REVIEW_SOURCE = "snapshot"
data, benben = plan_review._plan_for_review(day)
t("有快照时底本为快照", benben == "快照", benben)
t("_full.main 来自快照", [r["code"] for r in data["_full"]["main"]] == ["600000.SH", "000001.SZ"])
t("observe 来自快照", [r["code"] for r in data["_full"]["observe"]] == ["600519.SH"])
t("candidates 来自快照", [r["code"] for r in data["candidates"]] == ["600000.SH"])
t("读快照不触发现算", calls["n"] == 0, calls["n"])
# 2. 无快照:回落现算
_d2, benben2 = plan_review._plan_for_review("2026-09-15")
t("无快照时底本为现算", benben2 == "现算", benben2)
t("回落走了 plan.collect 一次", calls["n"] == 1, calls["n"])
# 3. 开关 live:即使有快照也现算,逐字回旧
config.PLAN_REVIEW_SOURCE = "live"
_d3, benben3 = plan_review._plan_for_review(day)
t("live 时底本为现算(有快照也不读)", benben3 == "现算", benben3)
t("live 又现算一次", calls["n"] == 2, calls["n"])
finally:
config.PLAN_SNAPSHOT_DIR, config.PLAN_REVIEW_SOURCE, plan.collect = old_dir, old_src, old_collect
print("-" * 70)
if FAILED:
print("FAILED %d: %s" % (len(FAILED), "; ".join(FAILED)))
return 1
print("ALL OK — 周报底本:有快照读快照 / 无快照回落现算 / 开关 live 回旧")
return 0
if __name__ == "__main__":
sys.exit(main())