79 lines
2.2 KiB
Python
79 lines
2.2 KiB
Python
from datetime import date
|
|
|
|
from app.services.oa_confirmation_kit_sync import extract_writeback_values
|
|
|
|
|
|
def _kit_schema(fields: dict):
|
|
children = []
|
|
props = {}
|
|
for sem, fid in fields.items():
|
|
props[f"{sem}Field" if not sem.endswith("Field") else sem] = fid
|
|
# normalize: hireDate -> hireDateField already handled below
|
|
# rebuild properly
|
|
props = {}
|
|
children = []
|
|
for sem, fid in fields.items():
|
|
props[sem + "Field"] = fid
|
|
children.append(
|
|
{
|
|
"type": "input",
|
|
"field": fid,
|
|
"props": {"_fc_kit_field": sem},
|
|
}
|
|
)
|
|
return {
|
|
"rule": [
|
|
{
|
|
"type": "confirmationKit",
|
|
"props": props,
|
|
"children": children,
|
|
}
|
|
]
|
|
}
|
|
|
|
|
|
def test_extract_updates_six_fields():
|
|
schema = _kit_schema(
|
|
{
|
|
"hireDate": "f_hire",
|
|
"probationMonths": "f_prob",
|
|
"name": "f_name",
|
|
"email": "f_email",
|
|
"dept": "f_dept",
|
|
"phone": "f_phone",
|
|
}
|
|
)
|
|
data = {
|
|
"f_hire": "2026-01-15",
|
|
"f_prob": 3,
|
|
"f_name": "张三",
|
|
"f_email": "a@b.com",
|
|
"f_dept": {"id": "d1", "name": "研发"},
|
|
"f_phone": "13800138000",
|
|
}
|
|
out = extract_writeback_values(schema, data)
|
|
assert out["hire_date"] == date(2026, 1, 15)
|
|
assert out["probation_months"] == 3
|
|
assert out["nick_name"] == "张三"
|
|
assert out["email"] == "a@b.com"
|
|
assert out["dept_id"] == "d1"
|
|
assert out["phone_no"] == "13800138000"
|
|
|
|
|
|
def test_empty_fields_skipped():
|
|
schema = _kit_schema({"name": "f_name", "phone": "f_phone", "email": "f_email"})
|
|
data = {"f_name": "李四", "f_phone": "", "f_email": "bad"}
|
|
out = extract_writeback_values(schema, data)
|
|
assert out == {"nick_name": "李四"}
|
|
|
|
|
|
def test_invalid_phone_skipped():
|
|
schema = _kit_schema({"phone": "f_phone", "name": "f_name"})
|
|
out = extract_writeback_values(schema, {"f_phone": "123", "f_name": "王五"})
|
|
assert "phone_no" not in out
|
|
assert out["nick_name"] == "王五"
|
|
|
|
|
|
def test_no_kit_noop():
|
|
assert extract_writeback_values({"rule": [{"type": "input", "field": "x"}]}, {"x": 1}) == {}
|