118 lines
3.4 KiB
Python
118 lines
3.4 KiB
Python
import unittest
|
|
|
|
from app.services.oa_dept_select import 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"}), [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|