test_oa_dept_select.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import unittest
  2. from app.services.oa_dept_select import (
  3. dept_contains_match,
  4. extract_dept_ids,
  5. validate_dept_form_values,
  6. )
  7. class TestValidateDeptFormValues(unittest.TestCase):
  8. def test_multiple_ok(self):
  9. schema = {
  10. "rule": [
  11. {
  12. "type": "dept",
  13. "field": "depts",
  14. "title": "部门",
  15. "$required": True,
  16. "props": {"multiple": True, "max": 2},
  17. }
  18. ]
  19. }
  20. data = {
  21. "depts": [
  22. {"id": "d1", "name": "中科兴皖 / 研发"},
  23. {"id": "d2", "name": "中科兴皖 / 质量"},
  24. ]
  25. }
  26. self.assertEqual(validate_dept_form_values(schema, data), [])
  27. def test_required_empty_array(self):
  28. schema = {
  29. "rule": [
  30. {
  31. "type": "dept",
  32. "field": "depts",
  33. "$required": True,
  34. "props": {"multiple": True},
  35. }
  36. ]
  37. }
  38. errs = validate_dept_form_values(schema, {"depts": []})
  39. self.assertTrue(any("必填" in e or "部门" in e for e in errs))
  40. def test_single_must_be_object(self):
  41. schema = {
  42. "rule": [
  43. {
  44. "type": "dept",
  45. "field": "dept",
  46. "props": {"multiple": False},
  47. }
  48. ]
  49. }
  50. errs = validate_dept_form_values(schema, {"dept": [{"id": "d1", "name": "A"}]})
  51. self.assertTrue(len(errs) >= 1)
  52. def test_max_exceeded(self):
  53. schema = {
  54. "rule": [
  55. {
  56. "type": "dept",
  57. "field": "depts",
  58. "props": {"multiple": True, "max": 1},
  59. }
  60. ]
  61. }
  62. data = {
  63. "depts": [
  64. {"id": "d1", "name": "A"},
  65. {"id": "d2", "name": "B"},
  66. ]
  67. }
  68. errs = validate_dept_form_values(schema, data)
  69. self.assertTrue(any("最大" in e or "上限" in e for e in errs))
  70. def test_max_exceeded_string_max(self):
  71. schema = {
  72. "rule": [
  73. {
  74. "type": "dept",
  75. "field": "depts",
  76. "props": {"multiple": True, "max": "1"},
  77. }
  78. ]
  79. }
  80. data = {
  81. "depts": [
  82. {"id": "d1", "name": "A"},
  83. {"id": "d2", "name": "B"},
  84. ]
  85. }
  86. errs = validate_dept_form_values(schema, data)
  87. self.assertTrue(any("最大" in e or "上限" in e for e in errs))
  88. def test_nested_fc_row(self):
  89. schema = {
  90. "rule": [
  91. {
  92. "type": "fcRow",
  93. "children": [
  94. {
  95. "type": "dept",
  96. "field": "d",
  97. "required": True,
  98. "props": {"multiple": False},
  99. }
  100. ],
  101. }
  102. ]
  103. }
  104. errs = validate_dept_form_values(schema, {})
  105. self.assertTrue(len(errs) >= 1)
  106. def test_no_dept_fields_ok(self):
  107. schema = {"rule": [{"type": "input", "field": "t"}]}
  108. self.assertEqual(validate_dept_form_values(schema, {"t": "x"}), [])
  109. class TestOaDeptSelect(unittest.TestCase):
  110. def test_extract_and_contains(self):
  111. self.assertEqual(extract_dept_ids({"id": "d1", "name": "研发"}), {"d1"})
  112. self.assertEqual(
  113. extract_dept_ids([{"id": "d1", "name": "A"}, {"id": "d2", "name": "B"}]),
  114. {"d1", "d2"},
  115. )
  116. self.assertTrue(
  117. dept_contains_match(
  118. [{"id": "d1", "name": "A"}],
  119. ["d1", "d9"],
  120. )
  121. )
  122. self.assertFalse(
  123. dept_contains_match(
  124. [{"id": "d3", "name": "C"}],
  125. ["d1", "d2"],
  126. )
  127. )
  128. # 精确匹配:下级 id 不等于上级 id
  129. self.assertFalse(
  130. dept_contains_match(
  131. {"id": "d1-child", "name": "前端组"},
  132. ["d1"],
  133. )
  134. )
  135. if __name__ == "__main__":
  136. unittest.main()