108 lines
4.8 KiB
Python
108 lines
4.8 KiB
Python
"""周报底本改读当日快照(工作包丙,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 common # noqa: E402
|
||
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
|
||
|
||
# 复审退回修补 (2026-09-17): pms_roster 的档位白名单是独立门, 两种模式都先过滤再定序; 空白名单不过滤。
|
||
roster = [
|
||
{"c": "600000.SH", "b": "main", "t": "强传导", "s": 90, "r": 3},
|
||
{"c": "000001.SZ", "b": "main", "t": "弱传导", "s": 100, "r": 1},
|
||
{"c": "600519.SH", "b": "main", "t": "强传导", "s": 80, "r": 2},
|
||
{"c": "000002.SZ", "b": "observe", "t": "强传导", "s": 70, "r": 1},
|
||
]
|
||
up = plan_review._roster_pick(roster, ["强传导"], "upstream", 10)
|
||
t("upstream: 先过强传导再按名次升序 (600519 r=2 在 600000 r=3 前; 弱传导与观察档不入)",
|
||
up == [common.to_prefix("600519.SH"), common.to_prefix("600000.SH")], up)
|
||
old = plan_review._roster_pick(roster, ["强传导"], "score", 10)
|
||
t("旧模式: 先过强传导再按分数降序 (600000 s=90 在 600519 s=80 前)",
|
||
old == [common.to_prefix("600000.SH"), common.to_prefix("600519.SH")], old)
|
||
nofilter = plan_review._roster_pick(roster, [], "upstream", 10)
|
||
t("白名单为空: 不按档过滤 (弱传导 000001 r=1 也入且在最前, 主榜三只全在)",
|
||
nofilter[0] == common.to_prefix("000001.SZ") and len(nofilter) == 3, nofilter)
|
||
|
||
print("-" * 70)
|
||
if FAILED:
|
||
print("FAILED %d: %s" % (len(FAILED), "; ".join(FAILED)))
|
||
return 1
|
||
print("ALL OK — 周报底本:有快照读快照 / 无快照回落现算 / 开关 live 回旧 / "
|
||
"pms_roster 档位白名单先过滤再按 rank_mode 定序")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|