test_query.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """query demo 离线测:①②纯逻辑 + 树采样 + parse_suggest(不碰网络/LLM)。"""
  2. from __future__ import annotations
  3. import pytest
  4. from acquisition.query import (
  5. CARRIER_POS, CARRIER_POS_GROUPED, KTYPE_SUFFIX, _nonleaf_d4, sample_nodes,
  6. tactic_multiaxis,
  7. )
  8. from acquisition.suggest import SuggestError, parse_suggest
  9. def test_sample_nodes():
  10. topics = sample_nodes("实质", limit=8)
  11. forms = sample_nodes("形式", limit=8)
  12. assert 0 < len(topics) <= 8 and 0 < len(forms) <= 8
  13. assert all(isinstance(x, str) and x for x in topics + forms)
  14. def test_carrier_pos_and_form_sampling():
  15. # ② 的载体位置 = 载体×位置交叉;文章无封面;形式树(架构分支)可采样
  16. assert "短视频开头" in CARRIER_POS and "图片封面" in CARRIER_POS
  17. assert "文章封面" not in CARRIER_POS # 文章无封面
  18. assert CARRIER_POS_GROUPED["文章"] == ["开头", "中间", "收尾"]
  19. forms = sample_nodes("形式", under="架构", depths=(4,), limit=8)
  20. assert 0 < len(forms) <= 8 and all(isinstance(f, str) and f for f in forms)
  21. def test_parse_suggest_mines_tags_and_titles():
  22. resp = {"code": 0, "data": {"data": [
  23. {"title": "健身脚本怎么写 #健身 #跟练脚本", "body_text": "教程 #减脂",
  24. "topic_list": [{"name": "健身博主"}]},
  25. ]}}
  26. out = parse_suggest(resp, limit=10)
  27. assert "健身" in out and "跟练脚本" in out and "减脂" in out and "健身博主" in out
  28. def test_multiaxis_assembly():
  29. rows = tactic_multiaxis(n=12)
  30. assert len(rows) == 12
  31. r = rows[0]
  32. assert {"实质", "形式", "阶段", "动作", "作用", "知识类型", "query"} <= set(r)
  33. # 形式限架构:不应混进 剪辑/后期 等制作节点
  34. forms = _nonleaf_d4("形式", 6, under="架构")
  35. assert "剪辑组接" not in forms and "后期处理" not in forms
  36. # 组合 query = 各轴机械拼接,句尾是知识类型后缀
  37. assert r["query"].startswith(r["实质"] + " " + r["形式"])
  38. assert r["query"].endswith(KTYPE_SUFFIX[r["知识类型"]])
  39. def test_parse_suggest_business_error():
  40. with pytest.raises(SuggestError):
  41. parse_suggest({"code": 10000, "msg": "x"})