"""组织树关键词检索:匹配子节点须带上祖先才能组树。""" from app.crud.dept import _build_tree, _collect_with_ancestors def _item(dept_id: str, name: str, parent_id: str = "0") -> dict: return { "id": dept_id, "parentId": parent_id, "deptName": name, "orderNum": 0, } def test_collect_with_ancestors_includes_parent_chain(): flat = [ _item("root", "市场营销部门"), _item("child", "采集部", "root"), ] expanded = _collect_with_ancestors(flat, {"child"}) ids = {x["id"] for x in expanded} assert ids == {"root", "child"} def test_build_tree_shows_child_under_parent_after_keyword_expand(): flat = [ _item("root", "市场营销部门"), _item("child", "采集部", "root"), ] expanded = _collect_with_ancestors(flat, {"child"}) tree = _build_tree(expanded, "0") assert len(tree) == 1 assert tree[0]["deptName"] == "市场营销部门" assert len(tree[0]["children"]) == 1 assert tree[0]["children"][0]["deptName"] == "采集部"