test_oa_dept_select.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import unittest
  2. from app.services.oa_dept_select import validate_dept_form_values
  3. class TestValidateDeptFormValues(unittest.TestCase):
  4. def test_multiple_ok(self):
  5. schema = {
  6. "rule": [
  7. {
  8. "type": "dept",
  9. "field": "depts",
  10. "title": "部门",
  11. "$required": True,
  12. "props": {"multiple": True, "max": 2},
  13. }
  14. ]
  15. }
  16. data = {
  17. "depts": [
  18. {"id": "d1", "name": "中科兴皖 / 研发"},
  19. {"id": "d2", "name": "中科兴皖 / 质量"},
  20. ]
  21. }
  22. self.assertEqual(validate_dept_form_values(schema, data), [])
  23. def test_required_empty_array(self):
  24. schema = {
  25. "rule": [
  26. {
  27. "type": "dept",
  28. "field": "depts",
  29. "$required": True,
  30. "props": {"multiple": True},
  31. }
  32. ]
  33. }
  34. errs = validate_dept_form_values(schema, {"depts": []})
  35. self.assertTrue(any("必填" in e or "部门" in e for e in errs))
  36. def test_single_must_be_object(self):
  37. schema = {
  38. "rule": [
  39. {
  40. "type": "dept",
  41. "field": "dept",
  42. "props": {"multiple": False},
  43. }
  44. ]
  45. }
  46. errs = validate_dept_form_values(schema, {"dept": [{"id": "d1", "name": "A"}]})
  47. self.assertTrue(len(errs) >= 1)
  48. def test_max_exceeded(self):
  49. schema = {
  50. "rule": [
  51. {
  52. "type": "dept",
  53. "field": "depts",
  54. "props": {"multiple": True, "max": 1},
  55. }
  56. ]
  57. }
  58. data = {
  59. "depts": [
  60. {"id": "d1", "name": "A"},
  61. {"id": "d2", "name": "B"},
  62. ]
  63. }
  64. errs = validate_dept_form_values(schema, data)
  65. self.assertTrue(any("最大" in e or "上限" in e for e in errs))
  66. def test_max_exceeded_string_max(self):
  67. schema = {
  68. "rule": [
  69. {
  70. "type": "dept",
  71. "field": "depts",
  72. "props": {"multiple": True, "max": "1"},
  73. }
  74. ]
  75. }
  76. data = {
  77. "depts": [
  78. {"id": "d1", "name": "A"},
  79. {"id": "d2", "name": "B"},
  80. ]
  81. }
  82. errs = validate_dept_form_values(schema, data)
  83. self.assertTrue(any("最大" in e or "上限" in e for e in errs))
  84. def test_nested_fc_row(self):
  85. schema = {
  86. "rule": [
  87. {
  88. "type": "fcRow",
  89. "children": [
  90. {
  91. "type": "dept",
  92. "field": "d",
  93. "required": True,
  94. "props": {"multiple": False},
  95. }
  96. ],
  97. }
  98. ]
  99. }
  100. errs = validate_dept_form_values(schema, {})
  101. self.assertTrue(len(errs) >= 1)
  102. def test_no_dept_fields_ok(self):
  103. schema = {"rule": [{"type": "input", "field": "t"}]}
  104. self.assertEqual(validate_dept_form_values(schema, {"t": "x"}), [])
  105. if __name__ == "__main__":
  106. unittest.main()