test_dept_tree_search.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. """组织树关键词检索:匹配子节点须带上祖先才能组树。"""
  2. from app.crud.dept import _build_tree, _collect_with_ancestors
  3. def _item(dept_id: str, name: str, parent_id: str = "0") -> dict:
  4. return {
  5. "id": dept_id,
  6. "parentId": parent_id,
  7. "deptName": name,
  8. "orderNum": 0,
  9. }
  10. def test_collect_with_ancestors_includes_parent_chain():
  11. flat = [
  12. _item("root", "市场营销部门"),
  13. _item("child", "采集部", "root"),
  14. ]
  15. expanded = _collect_with_ancestors(flat, {"child"})
  16. ids = {x["id"] for x in expanded}
  17. assert ids == {"root", "child"}
  18. def test_build_tree_shows_child_under_parent_after_keyword_expand():
  19. flat = [
  20. _item("root", "市场营销部门"),
  21. _item("child", "采集部", "root"),
  22. ]
  23. expanded = _collect_with_ancestors(flat, {"child"})
  24. tree = _build_tree(expanded, "0")
  25. assert len(tree) == 1
  26. assert tree[0]["deptName"] == "市场营销部门"
  27. assert len(tree[0]["children"]) == 1
  28. assert tree[0]["children"][0]["deptName"] == "采集部"