feat(crm): add quote approver setting id helpers and unit tests
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
10c3c5470c
commit
3aafae6a9d
2 files changed
+60
No files matched your search
@@ -0,0 +1,34 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, Iterable, List, Optional, Set
|
||||
|
||||
|
||||
def parse_approver_ids(raw: Optional[str]) -> List[str]:
|
||||
if not raw:
|
||||
return []
|
||||
out: List[str] = []
|
||||
for part in str(raw).split(","):
|
||||
pid = part.strip()
|
||||
if pid:
|
||||
out.append(pid)
|
||||
return out
|
||||
|
||||
|
||||
def join_approver_ids(ids: Iterable[str]) -> str:
|
||||
return ",".join([str(i).strip() for i in ids if str(i).strip()])
|
||||
|
||||
|
||||
def prefill_approver_ids(
|
||||
setting: Optional[Dict[str, Any]],
|
||||
*,
|
||||
valid_ids: Optional[Set[str]] = None,
|
||||
) -> List[str]:
|
||||
"""返回可预填的审批人 ID 列表;不可用则空列表。"""
|
||||
if not setting:
|
||||
return []
|
||||
if str(setting.get("status") or "") != "0":
|
||||
return []
|
||||
ids = list(setting.get("approverIds") or [])
|
||||
if valid_ids is not None:
|
||||
ids = [i for i in ids if str(i) in valid_ids]
|
||||
return [str(i) for i in ids if str(i).strip()]
|
||||
@@ -0,0 +1,26 @@
|
||||
from app.crud.crm.quote_approver_setting import (
|
||||
join_approver_ids,
|
||||
parse_approver_ids,
|
||||
prefill_approver_ids,
|
||||
)
|
||||
|
||||
|
||||
def test_parse_and_join_roundtrip():
|
||||
assert parse_approver_ids("a,b,c") == ["a", "b", "c"]
|
||||
assert parse_approver_ids(" a,, b ") == ["a", "b"]
|
||||
assert parse_approver_ids("") == []
|
||||
assert parse_approver_ids(None) == []
|
||||
assert join_approver_ids(["a", "b", ""]) == "a,b"
|
||||
|
||||
|
||||
def test_prefill_requires_enabled_and_nonempty():
|
||||
assert prefill_approver_ids(None) == []
|
||||
assert prefill_approver_ids({"status": "1", "approverIds": ["u1"]}) == []
|
||||
assert prefill_approver_ids({"status": "0", "approverIds": []}) == []
|
||||
assert prefill_approver_ids({"status": "0", "approverIds": ["u1", "u2"]}) == ["u1", "u2"]
|
||||
|
||||
|
||||
def test_prefill_filters_against_valid_ids():
|
||||
setting = {"status": "0", "approverIds": ["u1", "gone", "u2"]}
|
||||
assert prefill_approver_ids(setting, valid_ids={"u1", "u2"}) == ["u1", "u2"]
|
||||
assert prefill_approver_ids(setting, valid_ids=set()) == []
|
||||
Reference in new issue
Block a user