test_decompose_helpers.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """decompose 入口二的纯逻辑离线测:slugify + expand_queries 跨 query 去重。"""
  2. from __future__ import annotations
  3. import scripts.decompose as d
  4. from creation_knowledge.config import PgConfig, Settings
  5. def _settings() -> Settings:
  6. return Settings(
  7. pg=PgConfig(host="h", port=5432, user="u", password="p", database="d"),
  8. crawler_base_url="x", crawler_key="", crawler_timeout=30,
  9. video_model="m", gemini_api_key="",
  10. openrouter_base_url="b", openrouter_api_key="k",
  11. llm_model="m", max_cards=12, frames_dir="f", douyin_ratio="540p", data_dir="")
  12. def test_slugify():
  13. assert d.slugify("分镜脚本") == "分镜脚本" # 中文保留
  14. assert d.slugify("A B!c") == "a-b-c" # 空格/标点→- 且小写
  15. assert d.slugify("") == "run" # 空→run
  16. assert d.slugify("!!!") == "run" # 全标点→run
  17. def test_expand_queries_dedup(monkeypatch):
  18. ret = {"q1": ["a", "b"], "q2": ["b", "c"]} # q1/q2 共享 id "b"
  19. def fake_search(keyword, **kw):
  20. return ret[keyword]
  21. monkeypatch.setattr(d, "search_keyword", fake_search)
  22. srcs = d.expand_queries(
  23. [{"query": "q1", "platform": "xiaohongshu"},
  24. {"query": "q2", "platform": "xiaohongshu"}], _settings())
  25. assert [s["cid"] for s in srcs] == ["a", "b", "c"] # b 去重,只出现一次
  26. assert all(s["from"] == "live" for s in srcs)
  27. assert srcs[0]["query"] == "q1" and srcs[2]["query"] == "q2" # b 归属首次命中的 q1
  28. def test_expand_queries_skips_failed(monkeypatch):
  29. def fake_search(keyword, **kw):
  30. if keyword == "bad":
  31. raise RuntimeError("search down")
  32. return ["x"]
  33. monkeypatch.setattr(d, "search_keyword", fake_search)
  34. srcs = d.expand_queries(
  35. [{"query": "bad", "platform": "xiaohongshu"},
  36. {"query": "ok", "platform": "xiaohongshu"}], _settings())
  37. assert [s["cid"] for s in srcs] == ["x"] # bad 整条跳过,不阻断 ok