unis_hrms/tests/test_derived_fields.py

85 lines
4.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""派生字段回归:``after_read`` 钩子写入的属性必须与 schema 字段同名。
通用 CRUD 路由用 ``spec.read.model_validate(obj)``(Pydantic from_attributes)序列化对象,
它读取的是**公开属性名**。若钩子把结果塞进 ``_roles`` / ``_user_count`` 这类下划线键,
接口就会静默返回 null —— 前端「角色」列变空、分配角色弹窗预填空,保存时清空用户全部角色。
本文件锁死这类缺陷,同时锁死播种写入的招聘负责人与司龄字段。
"""
from __future__ import annotations
def _items(client, headers, path, **params):
resp = client.get(path, headers=headers, params={"page_size": 100, **params})
assert resp.status_code == 200, f"{path} -> {resp.status_code} {resp.text[:300]}"
body = resp.json()
assert body["items"], f"{path} 返回空列表"
return body["items"]
def test_user_rows_carry_roles_and_role_ids(client, admin):
"""``GET /system/users``:角色与 role_ids 必须同时下发且互相自洽。"""
rows = _items(client, admin, "/api/v1/system/users")
for row in rows:
assert row["roles"] is not None, row["username"]
assert row["role_ids"] is not None, row["username"]
assert row["role_ids"] == [r["id"] for r in row["roles"]], row["username"]
for role in row["roles"]:
assert {"id", "code", "name", "data_scope"} <= set(role), role
by_name = {r["username"]: r for r in rows}
assert "super_admin" in {r["code"] for r in by_name["admin"]["roles"]}
assert by_name["hr"]["role_ids"], "hr 账号必须至少有一个角色"
def test_assign_roles_roundtrip_keeps_membership_visible(client, admin):
"""分配角色后重新读取仍能看到角色(避免「保存即清空」回归)。"""
rows = _items(client, admin, "/api/v1/system/users")
fixtures = {"admin", "hr", "payroll", "recruiter", "manager", "emp00001", "emp"}
victim = next(r for r in rows if r["username"] not in fixtures and r["role_ids"])
original = sorted(victim["role_ids"])
resp = client.post(f"/api/v1/system/users/{victim['id']}/roles",
headers=admin, json={"role_ids": original})
assert resp.status_code == 200, resp.text
again = client.get("/api/v1/system/users", headers=admin,
params={"page_size": 100}).json()["items"]
refreshed = next(r for r in again if r["id"] == victim["id"])
assert sorted(refreshed["role_ids"]) == original, refreshed
def test_role_rows_carry_user_and_permission_counts(client, admin):
"""``GET /system/roles``:两个计数都不能为 null,且与用户列表一致。"""
roles = _items(client, admin, "/api/v1/system/roles")
users = _items(client, admin, "/api/v1/system/users")
expected: dict[int, int] = {}
for user in users:
for role in user["roles"]:
expected[role["id"]] = expected.get(role["id"], 0) + 1
for role in roles:
assert role["user_count"] is not None, role["code"]
assert role["permission_count"] is not None, role["code"]
assert role["user_count"] == expected.get(role["id"], 0), role["code"]
assert any(r["code"] == "super_admin" and r["permission_count"] > 100 for r in roles)
def test_job_postings_have_owner_names(client, admin):
"""招聘负责人由播种写入,列表需带出可读姓名。"""
rows = _items(client, admin, "/api/v1/recruitment/jobs")
for row in rows:
assert row["owner_user_id"], row["code"]
assert row["owner_name"], row["code"]
assert len({r["owner_name"] for r in rows}) >= 1
def test_employees_expose_work_age(client, admin):
"""司龄是数值列(用于筛选/排序),必须真实落库而非只存在于自定义字段。"""
rows = _items(client, admin, "/api/v1/employees")
missing = [r["emp_no"] for r in rows if r.get("work_age_years") is None]
assert not missing, f"缺少 work_age_years: {missing[:5]}"
assert all(isinstance(r["work_age_years"], (int, float)) for r in rows)