150 lines
4.3 KiB
Python
150 lines
4.3 KiB
Python
import unittest
|
|
|
|
from app.services.oa_dept_select import (
|
|
dept_contains_match,
|
|
extract_dept_ids,
|
|
validate_dept_form_values,
|
|
)
|
|
|
|
|
|
class TestValidateDeptFormValues(unittest.TestCase):
|
|
def test_multiple_ok(self):
|
|
schema = {
|
|
"rule": [
|
|
{
|
|
"type": "dept",
|
|
"field": "depts",
|
|
"title": "部门",
|
|
"$required": True,
|
|
"props": {"multiple": True, "max": 2},
|
|
}
|
|
]
|
|
}
|
|
data = {
|
|
"depts": [
|
|
{"id": "d1", "name": "中科兴皖 / 研发"},
|
|
{"id": "d2", "name": "中科兴皖 / 质量"},
|
|
]
|
|
}
|
|
self.assertEqual(validate_dept_form_values(schema, data), [])
|
|
|
|
def test_required_empty_array(self):
|
|
schema = {
|
|
"rule": [
|
|
{
|
|
"type": "dept",
|
|
"field": "depts",
|
|
"$required": True,
|
|
"props": {"multiple": True},
|
|
}
|
|
]
|
|
}
|
|
errs = validate_dept_form_values(schema, {"depts": []})
|
|
self.assertTrue(any("必填" in e or "部门" in e for e in errs))
|
|
|
|
def test_single_must_be_object(self):
|
|
schema = {
|
|
"rule": [
|
|
{
|
|
"type": "dept",
|
|
"field": "dept",
|
|
"props": {"multiple": False},
|
|
}
|
|
]
|
|
}
|
|
errs = validate_dept_form_values(schema, {"dept": [{"id": "d1", "name": "A"}]})
|
|
self.assertTrue(len(errs) >= 1)
|
|
|
|
def test_max_exceeded(self):
|
|
schema = {
|
|
"rule": [
|
|
{
|
|
"type": "dept",
|
|
"field": "depts",
|
|
"props": {"multiple": True, "max": 1},
|
|
}
|
|
]
|
|
}
|
|
data = {
|
|
"depts": [
|
|
{"id": "d1", "name": "A"},
|
|
{"id": "d2", "name": "B"},
|
|
]
|
|
}
|
|
errs = validate_dept_form_values(schema, data)
|
|
self.assertTrue(any("最大" in e or "上限" in e for e in errs))
|
|
|
|
def test_max_exceeded_string_max(self):
|
|
schema = {
|
|
"rule": [
|
|
{
|
|
"type": "dept",
|
|
"field": "depts",
|
|
"props": {"multiple": True, "max": "1"},
|
|
}
|
|
]
|
|
}
|
|
data = {
|
|
"depts": [
|
|
{"id": "d1", "name": "A"},
|
|
{"id": "d2", "name": "B"},
|
|
]
|
|
}
|
|
errs = validate_dept_form_values(schema, data)
|
|
self.assertTrue(any("最大" in e or "上限" in e for e in errs))
|
|
|
|
def test_nested_fc_row(self):
|
|
schema = {
|
|
"rule": [
|
|
{
|
|
"type": "fcRow",
|
|
"children": [
|
|
{
|
|
"type": "dept",
|
|
"field": "d",
|
|
"required": True,
|
|
"props": {"multiple": False},
|
|
}
|
|
],
|
|
}
|
|
]
|
|
}
|
|
errs = validate_dept_form_values(schema, {})
|
|
self.assertTrue(len(errs) >= 1)
|
|
|
|
def test_no_dept_fields_ok(self):
|
|
schema = {"rule": [{"type": "input", "field": "t"}]}
|
|
self.assertEqual(validate_dept_form_values(schema, {"t": "x"}), [])
|
|
|
|
|
|
class TestOaDeptSelect(unittest.TestCase):
|
|
def test_extract_and_contains(self):
|
|
self.assertEqual(extract_dept_ids({"id": "d1", "name": "研发"}), {"d1"})
|
|
self.assertEqual(
|
|
extract_dept_ids([{"id": "d1", "name": "A"}, {"id": "d2", "name": "B"}]),
|
|
{"d1", "d2"},
|
|
)
|
|
self.assertTrue(
|
|
dept_contains_match(
|
|
[{"id": "d1", "name": "A"}],
|
|
["d1", "d9"],
|
|
)
|
|
)
|
|
self.assertFalse(
|
|
dept_contains_match(
|
|
[{"id": "d3", "name": "C"}],
|
|
["d1", "d2"],
|
|
)
|
|
)
|
|
# 精确匹配:下级 id 不等于上级 id
|
|
self.assertFalse(
|
|
dept_contains_match(
|
|
{"id": "d1-child", "name": "前端组"},
|
|
["d1"],
|
|
)
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|