test_role_list_bound_names.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. """角色列表 boundUserNames / boundPostNames enrichment。"""
  2. import inspect
  3. from app.crud import role as role_crud
  4. class _DummySession:
  5. def query(self, *args, **kwargs): # pragma: no cover
  6. raise AssertionError("空入参不应访问数据库")
  7. def test_bound_user_names_by_role_ids_empty():
  8. db = _DummySession()
  9. assert role_crud._bound_user_names_by_role_ids(db, [], "zkxw") == {}
  10. assert role_crud._bound_user_names_by_role_ids(db, ["r1"], "") == {}
  11. def test_bound_post_names_by_role_ids_empty():
  12. db = _DummySession()
  13. assert role_crud._bound_post_names_by_role_ids(db, [], "zkxw") == {}
  14. assert role_crud._bound_post_names_by_role_ids(db, ["r1"], "") == {}
  15. def test_user_display_name_prefers_nick():
  16. assert role_crud._user_display_name("张三", "zhangsan") == "张三"
  17. assert role_crud._user_display_name("", "zhangsan") == "zhangsan"
  18. assert role_crud._user_display_name(" ", "u1") == "u1"
  19. assert role_crud._user_display_name(None, None) == ""
  20. def test_list_roles_enriches_bound_names():
  21. source = inspect.getsource(role_crud.list_roles)
  22. assert "_bound_user_names_by_role_ids" in source
  23. assert "_bound_post_names_by_role_ids" in source
  24. assert "boundUserNames" in inspect.getsource(role_crud._row_to_dict) or "bound_user_names" in source