Merge branch 'feature/equipment-acceptance-sync' into dev
This commit is contained in:
commit
91a4bcbb8b
48 files changed
+6196
-82
No files matched your search
@@ -0,0 +1,98 @@
|
||||
"""
|
||||
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"]
|
||||
@@ -0,0 +1,32 @@
|
||||
"""
|
||||
tests/test_equipment_acceptance_config.py
|
||||
|
||||
用途:锁定设备验收预警四档天数的保存前校验。
|
||||
依赖:config_service.validate_reminder_days、ValidationError。
|
||||
逻辑:不连数据库。默认 30/37/44/51 通过;相等、倒序、非整数和空岗位以外的天数形状被拒绝。
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from app.core.errors import ValidationError
|
||||
from app.services.equipment_acceptance.config_service import validate_reminder_days
|
||||
|
||||
|
||||
def test_default_reminder_days_pass():
|
||||
"""冻结默认值可以保存。"""
|
||||
assert validate_reminder_days([30, 37, 44, 51]) == (30, 37, 44, 51)
|
||||
|
||||
|
||||
def test_days_must_increase():
|
||||
"""后一档不大于前一档时拒绝,避免两档在同一天触发。"""
|
||||
with pytest.raises(ValidationError) as caught:
|
||||
validate_reminder_days([30, 30, 44, 51])
|
||||
assert caught.value.code == "EQUIPMENT_ACCEPTANCE.SETTINGS_INVALID"
|
||||
|
||||
|
||||
def test_days_reject_bool_and_wrong_length():
|
||||
"""布尔值在 Python 里也是 int,不能当成 1 天;档数必须是四档。"""
|
||||
with pytest.raises(ValidationError):
|
||||
validate_reminder_days([True, 37, 44, 51])
|
||||
with pytest.raises(ValidationError):
|
||||
validate_reminder_days([30, 37, 44])
|
||||
@@ -0,0 +1,257 @@
|
||||
"""
|
||||
backend/tests/test_equipment_acceptance_snapshot.py
|
||||
|
||||
用途:设备验收同步的状态、分组和 SQL 组装单测。
|
||||
依赖:snapshot_rules、source_group、query_sql。不连接 OA,也不启动 Flask。
|
||||
逻辑:停表规则、按 requestid 分组、SQL 不查 isremark,发送记录快照字段原样返回。
|
||||
"""
|
||||
|
||||
from datetime import date, datetime
|
||||
|
||||
import pytest
|
||||
|
||||
pytestmark = pytest.mark.pure_unit
|
||||
|
||||
from app.services.equipment_acceptance.query_sql import build_acceptance_sql, resolve_catalogs
|
||||
from app.services.equipment_acceptance.snapshot_rules import (
|
||||
EXCLUDED,
|
||||
NOT_STARTED,
|
||||
STOPPED_ARCHIVED,
|
||||
STOPPED_CANNOT_ACCEPT,
|
||||
STOPPED_FORCE_FINISHED,
|
||||
STOPPED_RETURNED,
|
||||
STOPPED_REVOKED,
|
||||
STOPPED_VOIDED,
|
||||
WATCHING,
|
||||
can_accept_from_oa,
|
||||
classify_monitor_status,
|
||||
dwell_days,
|
||||
is_returned_from_oa,
|
||||
)
|
||||
from app.services.equipment_acceptance.source_group import group_acceptance_rows
|
||||
|
||||
|
||||
def test_oa_yes_no_is_flipped_before_storage():
|
||||
"""OA 0 是、1 否。验收空值保持空;退回空值收成否。其他值不猜测。"""
|
||||
assert can_accept_from_oa(0) == 1
|
||||
assert can_accept_from_oa(1) == 0
|
||||
assert can_accept_from_oa(None) is None
|
||||
assert can_accept_from_oa(2) == 2
|
||||
assert is_returned_from_oa(0) == 1
|
||||
assert is_returned_from_oa(1) == 0
|
||||
assert is_returned_from_oa(None) == 0
|
||||
assert is_returned_from_oa(2) == 2
|
||||
|
||||
|
||||
def test_returned_yes_stops_even_while_approving():
|
||||
"""is_returned=1 是退回,审批中也停表。"""
|
||||
status = classify_monitor_status(
|
||||
can_accept=1, is_returned=1, flow_status=1, timer_started=True
|
||||
)
|
||||
assert status == STOPPED_RETURNED
|
||||
|
||||
|
||||
def test_cannot_accept_stops_and_null_can_accept_is_not_watching():
|
||||
"""can_accept=0 是不可以,停表;空值不是可以验收。"""
|
||||
stopped = classify_monitor_status(
|
||||
can_accept=0, is_returned=0, flow_status=1, timer_started=True
|
||||
)
|
||||
excluded = classify_monitor_status(
|
||||
can_accept=None, is_returned=0, flow_status=1, timer_started=True
|
||||
)
|
||||
assert stopped == STOPPED_CANNOT_ACCEPT
|
||||
assert excluded == EXCLUDED
|
||||
|
||||
|
||||
def test_not_returned_and_can_accept_watches():
|
||||
"""未退回且可以验收、审批中、已有计时起点时继续计时。"""
|
||||
status = classify_monitor_status(
|
||||
can_accept=1, is_returned=0, flow_status=1, timer_started=True
|
||||
)
|
||||
assert status == WATCHING
|
||||
|
||||
|
||||
def test_terminal_flow_status_stops():
|
||||
"""3/4/5 分别停表。7、8、99、-99 都是撤销,没有计时起点也不再提醒。"""
|
||||
assert classify_monitor_status(can_accept=1, is_returned=0, flow_status=3, timer_started=True) == STOPPED_ARCHIVED
|
||||
assert classify_monitor_status(can_accept=1, is_returned=0, flow_status=4, timer_started=True) == STOPPED_FORCE_FINISHED
|
||||
assert classify_monitor_status(can_accept=1, is_returned=0, flow_status=5, timer_started=True) == STOPPED_VOIDED
|
||||
for flow_status in (7, 8, 99, -99):
|
||||
assert classify_monitor_status(
|
||||
can_accept=1, is_returned=0, flow_status=flow_status, timer_started=True
|
||||
) == STOPPED_REVOKED
|
||||
assert classify_monitor_status(
|
||||
can_accept=1, is_returned=0, flow_status=flow_status, timer_started=False
|
||||
) == STOPPED_REVOKED
|
||||
|
||||
|
||||
def test_missing_timer_is_not_started_when_no_stop_reason():
|
||||
"""没有计时起点且没有停表原因时是未开始。"""
|
||||
status = classify_monitor_status(
|
||||
can_accept=1, is_returned=0, flow_status=1, timer_started=False
|
||||
)
|
||||
assert status == NOT_STARTED
|
||||
|
||||
|
||||
def test_unknown_returned_is_excluded():
|
||||
"""0、1 以外的退回值既不判退回也不当未退回。"""
|
||||
status = classify_monitor_status(
|
||||
can_accept=1, is_returned=2, flow_status=1, timer_started=True
|
||||
)
|
||||
assert status == EXCLUDED
|
||||
|
||||
|
||||
def test_dwell_days_freeze_on_archive_date():
|
||||
"""归档后的天数截止到归档日,不随同步日继续增加。"""
|
||||
days = dwell_days(
|
||||
timer_started_at=datetime(2026, 9, 1, 8, 0, 0),
|
||||
flow_status=3,
|
||||
finished_at=datetime(2026, 9, 11, 18, 0, 0),
|
||||
as_of=date(2026, 10, 8),
|
||||
)
|
||||
assert days == 10
|
||||
|
||||
|
||||
def test_dwell_days_use_sync_date_while_watching():
|
||||
"""计时中按同步日减起点的自然日。"""
|
||||
days = dwell_days(
|
||||
timer_started_at=date(2026, 10, 1),
|
||||
flow_status=1,
|
||||
finished_at=None,
|
||||
as_of=date(2026, 10, 8),
|
||||
)
|
||||
assert days == 7
|
||||
|
||||
|
||||
def test_group_splits_requests_and_dedupes_operators():
|
||||
"""不同 requestid 各成一张单。同一节点同一人只留一行,即使源行带了不同的 isremark。没有 requestid 的行跳过。"""
|
||||
drafts, skipped = group_acceptance_rows(
|
||||
[
|
||||
{
|
||||
"ysddh": "YS-1",
|
||||
"cgdh": "CG-1",
|
||||
"htbh": "HT-1",
|
||||
"sbsyrjssj": "2026-10-01 09:00:00",
|
||||
"sfkyzcys": 0,
|
||||
"ycsm": " 到货规格与请购不符 ",
|
||||
"sfzfth": None,
|
||||
"requestid": 10,
|
||||
"flowstatus": 3,
|
||||
"nodeid": 1,
|
||||
"nodename": "旧节点",
|
||||
"userid": 7,
|
||||
"isremark": 2,
|
||||
},
|
||||
{
|
||||
"ysddh": "YS-2",
|
||||
"requestid": 20,
|
||||
"flowstatus": 1,
|
||||
"nodeid": 2,
|
||||
"nodename": "使用部门担当",
|
||||
"userid": 8,
|
||||
"isremark": 0,
|
||||
"handler_name": "吴敏",
|
||||
"handler_job_num": "10086",
|
||||
},
|
||||
{
|
||||
"requestid": 20,
|
||||
"flowstatus": 1,
|
||||
"nodeid": 2,
|
||||
"nodename": "使用部门担当",
|
||||
"userid": 9,
|
||||
"isremark": 2,
|
||||
},
|
||||
{
|
||||
"requestid": 20,
|
||||
"flowstatus": 1,
|
||||
"nodeid": 2,
|
||||
"nodename": "使用部门担当",
|
||||
"userid": 8,
|
||||
"isremark": 9,
|
||||
"handler_name": "吴敏",
|
||||
"handler_job_num": "10086",
|
||||
},
|
||||
{"ysddh": "NO-REQUEST"},
|
||||
]
|
||||
)
|
||||
assert skipped == 1
|
||||
by_request = {draft.request_id: draft for draft in drafts}
|
||||
assert set(by_request) == {10, 20}
|
||||
assert by_request[10].acceptance_no == "YS-1"
|
||||
assert by_request[10].contract_no == "HT-1"
|
||||
assert by_request[10].can_accept == 1
|
||||
assert by_request[10].cannot_accept_note == "到货规格与请购不符"
|
||||
assert by_request[10].is_returned == 0
|
||||
assert by_request[10].flow_status_raw == 3
|
||||
current = by_request[20]
|
||||
assert current.request_match_count == 1
|
||||
assert current.flow_status_raw == 1
|
||||
assert [node.node_name for node in current.nodes] == ["使用部门担当"]
|
||||
assert sorted(item.user_id for item in current.operators) == [8, 9]
|
||||
assert not hasattr(current.operators[0], "isremark")
|
||||
|
||||
|
||||
def test_sql_uses_dictionary_catalogs_and_does_not_select_isremark():
|
||||
"""实例同时有字典库时用 weaver_wfc / weaver_wfp,SQL 不查询 isremark。"""
|
||||
catalogs = resolve_catalogs({"ecology10", "eteams", "weaver_wfc", "weaver_wfp"})
|
||||
sql = build_acceptance_sql(catalogs)
|
||||
assert catalogs.request == "weaver_wfc"
|
||||
assert catalogs.node == "weaver_wfp"
|
||||
assert "[weaver_wfc].[dbo].[wfc_operate]" in sql
|
||||
assert "[weaver_wfp].[dbo].[wfp_node]" in sql
|
||||
assert "isremark" not in sql.lower()
|
||||
assert "ycsm" in sql
|
||||
assert "workflow_requestbase" not in sql
|
||||
assert "[weaver_wfc].[dbo].[wfc_form_data]" in sql
|
||||
assert "d.dataid = f.FORM_DATA_ID" in sql
|
||||
assert "lcbh" not in sql
|
||||
assert "htbh1" not in sql
|
||||
assert "ysrq" not in sql
|
||||
assert "requestmark" not in sql
|
||||
assert "operateround" not in sql.lower()
|
||||
assert "optgroupid" not in sql.lower()
|
||||
assert "batch_number" not in sql.lower()
|
||||
assert "operate_log_id" not in sql.lower()
|
||||
|
||||
|
||||
def test_sql_falls_back_when_dictionary_catalog_is_absent():
|
||||
"""看不到 weaver_wfc 时,流程表改用 ecology10。人员库缺失则失败。"""
|
||||
catalogs = resolve_catalogs({"ecology10", "eteams"})
|
||||
assert catalogs.request == "ecology10"
|
||||
assert catalogs.node == "ecology10"
|
||||
with pytest.raises(ValueError):
|
||||
resolve_catalogs({"ecology10"})
|
||||
|
||||
|
||||
def test_sql_rejects_unsafe_catalog_override():
|
||||
"""库名覆盖值不能带分号或空格。"""
|
||||
with pytest.raises(ValueError):
|
||||
resolve_catalogs({"ecology10", "eteams"}, request="weaver_wfc;drop")
|
||||
|
||||
|
||||
def test_send_record_payload_keeps_written_names():
|
||||
"""发送记录接口用写入时的单号和主送抄送,不回读当前岗位。"""
|
||||
from app.models.equipment_acceptance import EquipmentAcceptanceSendRecord
|
||||
from app.services.equipment_acceptance.send_record_service import send_record_payload
|
||||
|
||||
row = EquipmentAcceptanceSendRecord(
|
||||
id=3,
|
||||
order_id=9,
|
||||
acceptance_no="YS-1",
|
||||
purchase_order_no="CG-1",
|
||||
tier=2,
|
||||
threshold_days=37,
|
||||
to_text="经副理甲",
|
||||
cc_text="COO、采购经副理",
|
||||
status="sent",
|
||||
sent_at=datetime(2026, 10, 9, 10, 0, 0),
|
||||
error_message=None,
|
||||
created_at=datetime(2026, 10, 9, 9, 0, 0),
|
||||
)
|
||||
payload = send_record_payload(row)
|
||||
assert payload["acceptance_no"] == "YS-1"
|
||||
assert payload["to_text"] == "经副理甲"
|
||||
assert payload["cc_text"] == "COO、采购经副理"
|
||||
assert payload["threshold_days"] == 37
|
||||
assert payload["status"] == "sent"
|
||||
assert payload["sent_at"]
|
||||
Reference in new issue
Block a user