99 lines
3.6 KiB
Python
99 lines
3.6 KiB
Python
"""
|
|
backend/tests/test_equipment_acceptance_alert_plan.py
|
|
|
|
用途:设备验收四档跨档和收件人组装的纯函数测试。
|
|
依赖:alert_plan。不连接数据库,不访问 OA,不发企业微信。
|
|
逻辑:超过阈值才进档。解析失败留在原角色上,不改发给别人。
|
|
"""
|
|
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
pytestmark = pytest.mark.pure_unit
|
|
|
|
from app.services.equipment_acceptance.alert_plan import (
|
|
assemble_tier,
|
|
due_tiers,
|
|
make_person,
|
|
resolve_account,
|
|
resolve_configured_user,
|
|
)
|
|
|
|
_DAYS = (30, 37, 44, 51)
|
|
|
|
|
|
def test_due_tiers_use_strictly_greater_than_threshold():
|
|
"""刚好等于 30 天还不发。超过 51 天才四档都到。"""
|
|
assert due_tiers(None, _DAYS) == []
|
|
assert due_tiers(30, _DAYS) == []
|
|
assert due_tiers(31, _DAYS) == [1]
|
|
assert due_tiers(51, _DAYS) == [1, 2, 3]
|
|
assert due_tiers(52, _DAYS) == [1, 2, 3, 4]
|
|
|
|
|
|
def test_tier_one_keeps_missing_supervisor_as_failure():
|
|
"""直接主管缺失时整档仍可发给处理人和采购担当,不把申请人改成主送。"""
|
|
handler = resolve_account(
|
|
slot="to",
|
|
label="当前处理人",
|
|
name="吴敏",
|
|
job_num="10086",
|
|
user=SimpleNamespace(id=7, wecom_userid="wx-handler", real_name="吴敏"),
|
|
missing_job_error="吴敏没有工号",
|
|
oa_employee_id=11,
|
|
)
|
|
supervisor = make_person(slot="cc", label="直接主管", name="吴敏", error="吴敏没有直接主管")
|
|
applicant = resolve_account(
|
|
slot="cc",
|
|
label="采购担当",
|
|
name="赵琴",
|
|
job_num="20001",
|
|
user=SimpleNamespace(id=8, wecom_userid="wx-buyer", real_name="赵琴"),
|
|
missing_job_error="没有申请人工号",
|
|
)
|
|
draft = assemble_tier(tier=1, threshold_days=30, to_people=[handler], cc_people=[supervisor, applicant])
|
|
assert draft["status"] == "pending"
|
|
assert draft["to_text"] == "吴敏"
|
|
assert draft["cc_text"] == "赵琴"
|
|
assert "吴敏没有直接主管" in draft["error_message"]
|
|
assert all(item["label"] != "采购担当" or item["slot"] == "cc" for item in draft["recipients"])
|
|
|
|
|
|
def test_duplicate_person_stays_only_on_the_primary_list():
|
|
"""同一个人既是主送又是抄送时只留主送。"""
|
|
person = resolve_account(
|
|
slot="to",
|
|
label="当前处理人",
|
|
name="吴敏",
|
|
job_num="10086",
|
|
user=SimpleNamespace(id=7, wecom_userid="wx-handler", real_name="吴敏"),
|
|
missing_job_error="吴敏没有工号",
|
|
)
|
|
copied = dict(person)
|
|
copied["slot"] = "cc"
|
|
copied["label"] = "直接主管"
|
|
draft = assemble_tier(tier=1, threshold_days=30, to_people=[person], cc_people=[copied])
|
|
assert draft["to_text"] == "吴敏"
|
|
assert draft["cc_text"] is None
|
|
assert len(draft["recipients"]) == 1
|
|
|
|
|
|
def test_unbound_wecom_and_missing_role_stay_failed():
|
|
"""没绑企微、没选 COO 都记失败,不写死姓名。"""
|
|
unbound = resolve_account(
|
|
slot="to",
|
|
label="当前处理人",
|
|
name="吴敏",
|
|
job_num="10086",
|
|
user=SimpleNamespace(id=7, wecom_userid="", real_name="吴敏"),
|
|
missing_job_error="吴敏没有工号",
|
|
)
|
|
assert unbound["error"] == "未绑定企业微信"
|
|
missing = resolve_configured_user(slot="to", label="COO", user=None, missing_error="未配置COO")
|
|
draft = assemble_tier(tier=3, threshold_days=44, to_people=[missing], cc_people=[unbound])
|
|
assert draft["status"] == "failed"
|
|
assert draft["to_text"] is None
|
|
assert "未配置COO" in draft["error_message"]
|
|
assert "未绑定企业微信" in draft["error_message"]
|