75 lines
2.1 KiB
Python
75 lines
2.1 KiB
Python
import unittest
|
|
|
|
from app.services.oa_person_select import (
|
|
extract_person_ids,
|
|
person_contains_match,
|
|
validate_person_form_values,
|
|
)
|
|
|
|
|
|
class TestOaPersonSelect(unittest.TestCase):
|
|
def test_validate_multi_ok(self):
|
|
schema = {
|
|
"rule": [
|
|
{
|
|
"type": "person",
|
|
"field": "F1",
|
|
"title": "协同人",
|
|
"props": {"multiple": True},
|
|
}
|
|
]
|
|
}
|
|
data = {"F1": [{"id": "u1", "name": "张三"}, {"id": "u2", "name": "李四"}]}
|
|
self.assertEqual(validate_person_form_values(schema, data), [])
|
|
|
|
def test_validate_required_empty(self):
|
|
schema = {
|
|
"rule": [
|
|
{
|
|
"type": "person",
|
|
"field": "F1",
|
|
"title": "协同人",
|
|
"$required": True,
|
|
"props": {"multiple": True},
|
|
}
|
|
]
|
|
}
|
|
errs = validate_person_form_values(schema, {"F1": []})
|
|
self.assertTrue(any("必填" in e for e in errs))
|
|
|
|
def test_validate_max(self):
|
|
schema = {
|
|
"rule": [
|
|
{
|
|
"type": "person",
|
|
"field": "F1",
|
|
"title": "协同人",
|
|
"props": {"multiple": True, "max": 1},
|
|
}
|
|
]
|
|
}
|
|
errs = validate_person_form_values(
|
|
schema,
|
|
{"F1": [{"id": "u1", "name": "A"}, {"id": "u2", "name": "B"}]},
|
|
)
|
|
self.assertTrue(any("上限" in e for e in errs))
|
|
|
|
def test_extract_and_contains(self):
|
|
self.assertEqual(extract_person_ids({"id": "u1", "name": "A"}), {"u1"})
|
|
self.assertTrue(
|
|
person_contains_match(
|
|
[{"id": "u1", "name": "A"}],
|
|
["u1", "u2"],
|
|
)
|
|
)
|
|
self.assertFalse(
|
|
person_contains_match(
|
|
[{"id": "u3", "name": "C"}],
|
|
["u1", "u2"],
|
|
)
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|