test_description_validation.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import pytest
  2. from controllers.console.app.app import _validate_description_length as app_validate
  3. from controllers.console.datasets.datasets import _validate_description_length as dataset_validate
  4. from controllers.service_api.dataset.dataset import _validate_description_length as service_dataset_validate
  5. class TestDescriptionValidationUnit:
  6. """Unit tests for description validation functions in App and Dataset APIs"""
  7. def test_app_validate_description_length_valid(self):
  8. """Test App validation function with valid descriptions"""
  9. # Empty string should be valid
  10. assert app_validate("") == ""
  11. # None should be valid
  12. assert app_validate(None) is None
  13. # Short description should be valid
  14. short_desc = "Short description"
  15. assert app_validate(short_desc) == short_desc
  16. # Exactly 400 characters should be valid
  17. exactly_400 = "x" * 400
  18. assert app_validate(exactly_400) == exactly_400
  19. # Just under limit should be valid
  20. just_under = "x" * 399
  21. assert app_validate(just_under) == just_under
  22. def test_app_validate_description_length_invalid(self):
  23. """Test App validation function with invalid descriptions"""
  24. # 401 characters should fail
  25. just_over = "x" * 401
  26. with pytest.raises(ValueError) as exc_info:
  27. app_validate(just_over)
  28. assert "Description cannot exceed 400 characters." in str(exc_info.value)
  29. # 500 characters should fail
  30. way_over = "x" * 500
  31. with pytest.raises(ValueError) as exc_info:
  32. app_validate(way_over)
  33. assert "Description cannot exceed 400 characters." in str(exc_info.value)
  34. # 1000 characters should fail
  35. very_long = "x" * 1000
  36. with pytest.raises(ValueError) as exc_info:
  37. app_validate(very_long)
  38. assert "Description cannot exceed 400 characters." in str(exc_info.value)
  39. def test_dataset_validate_description_length_valid(self):
  40. """Test Dataset validation function with valid descriptions"""
  41. # Empty string should be valid
  42. assert dataset_validate("") == ""
  43. # Short description should be valid
  44. short_desc = "Short description"
  45. assert dataset_validate(short_desc) == short_desc
  46. # Exactly 400 characters should be valid
  47. exactly_400 = "x" * 400
  48. assert dataset_validate(exactly_400) == exactly_400
  49. # Just under limit should be valid
  50. just_under = "x" * 399
  51. assert dataset_validate(just_under) == just_under
  52. def test_dataset_validate_description_length_invalid(self):
  53. """Test Dataset validation function with invalid descriptions"""
  54. # 401 characters should fail
  55. just_over = "x" * 401
  56. with pytest.raises(ValueError) as exc_info:
  57. dataset_validate(just_over)
  58. assert "Description cannot exceed 400 characters." in str(exc_info.value)
  59. # 500 characters should fail
  60. way_over = "x" * 500
  61. with pytest.raises(ValueError) as exc_info:
  62. dataset_validate(way_over)
  63. assert "Description cannot exceed 400 characters." in str(exc_info.value)
  64. def test_service_dataset_validate_description_length_valid(self):
  65. """Test Service Dataset validation function with valid descriptions"""
  66. # Empty string should be valid
  67. assert service_dataset_validate("") == ""
  68. # None should be valid
  69. assert service_dataset_validate(None) is None
  70. # Short description should be valid
  71. short_desc = "Short description"
  72. assert service_dataset_validate(short_desc) == short_desc
  73. # Exactly 400 characters should be valid
  74. exactly_400 = "x" * 400
  75. assert service_dataset_validate(exactly_400) == exactly_400
  76. # Just under limit should be valid
  77. just_under = "x" * 399
  78. assert service_dataset_validate(just_under) == just_under
  79. def test_service_dataset_validate_description_length_invalid(self):
  80. """Test Service Dataset validation function with invalid descriptions"""
  81. # 401 characters should fail
  82. just_over = "x" * 401
  83. with pytest.raises(ValueError) as exc_info:
  84. service_dataset_validate(just_over)
  85. assert "Description cannot exceed 400 characters." in str(exc_info.value)
  86. # 500 characters should fail
  87. way_over = "x" * 500
  88. with pytest.raises(ValueError) as exc_info:
  89. service_dataset_validate(way_over)
  90. assert "Description cannot exceed 400 characters." in str(exc_info.value)
  91. def test_app_dataset_validation_consistency(self):
  92. """Test that App and Dataset validation functions behave identically"""
  93. test_cases = [
  94. "", # Empty string
  95. "Short description", # Normal description
  96. "x" * 100, # Medium description
  97. "x" * 400, # Exactly at limit
  98. ]
  99. # Test valid cases produce same results
  100. for test_desc in test_cases:
  101. assert app_validate(test_desc) == dataset_validate(test_desc) == service_dataset_validate(test_desc)
  102. # Test invalid cases produce same errors
  103. invalid_cases = [
  104. "x" * 401, # Just over limit
  105. "x" * 500, # Way over limit
  106. "x" * 1000, # Very long
  107. ]
  108. for invalid_desc in invalid_cases:
  109. app_error = None
  110. dataset_error = None
  111. service_dataset_error = None
  112. # Capture App validation error
  113. try:
  114. app_validate(invalid_desc)
  115. except ValueError as e:
  116. app_error = str(e)
  117. # Capture Dataset validation error
  118. try:
  119. dataset_validate(invalid_desc)
  120. except ValueError as e:
  121. dataset_error = str(e)
  122. # Capture Service Dataset validation error
  123. try:
  124. service_dataset_validate(invalid_desc)
  125. except ValueError as e:
  126. service_dataset_error = str(e)
  127. # All should produce errors
  128. assert app_error is not None, f"App validation should fail for {len(invalid_desc)} characters"
  129. assert dataset_error is not None, f"Dataset validation should fail for {len(invalid_desc)} characters"
  130. error_msg = f"Service Dataset validation should fail for {len(invalid_desc)} characters"
  131. assert service_dataset_error is not None, error_msg
  132. # Errors should be identical
  133. error_msg = f"Error messages should be identical for {len(invalid_desc)} characters"
  134. assert app_error == dataset_error == service_dataset_error, error_msg
  135. assert app_error == "Description cannot exceed 400 characters."
  136. def test_boundary_values(self):
  137. """Test boundary values around the 400 character limit"""
  138. boundary_tests = [
  139. (0, True), # Empty
  140. (1, True), # Minimum
  141. (399, True), # Just under limit
  142. (400, True), # Exactly at limit
  143. (401, False), # Just over limit
  144. (402, False), # Over limit
  145. (500, False), # Way over limit
  146. ]
  147. for length, should_pass in boundary_tests:
  148. test_desc = "x" * length
  149. if should_pass:
  150. # Should not raise exception
  151. assert app_validate(test_desc) == test_desc
  152. assert dataset_validate(test_desc) == test_desc
  153. assert service_dataset_validate(test_desc) == test_desc
  154. else:
  155. # Should raise ValueError
  156. with pytest.raises(ValueError):
  157. app_validate(test_desc)
  158. with pytest.raises(ValueError):
  159. dataset_validate(test_desc)
  160. with pytest.raises(ValueError):
  161. service_dataset_validate(test_desc)
  162. def test_special_characters(self):
  163. """Test validation with special characters, Unicode, etc."""
  164. # Unicode characters
  165. unicode_desc = "测试描述" * 100 # Chinese characters
  166. if len(unicode_desc) <= 400:
  167. assert app_validate(unicode_desc) == unicode_desc
  168. assert dataset_validate(unicode_desc) == unicode_desc
  169. assert service_dataset_validate(unicode_desc) == unicode_desc
  170. # Special characters
  171. special_desc = "Special chars: !@#$%^&*()_+-=[]{}|;':\",./<>?" * 10
  172. if len(special_desc) <= 400:
  173. assert app_validate(special_desc) == special_desc
  174. assert dataset_validate(special_desc) == special_desc
  175. assert service_dataset_validate(special_desc) == special_desc
  176. # Mixed content
  177. mixed_desc = "Mixed content: 测试 123 !@# " * 15
  178. if len(mixed_desc) <= 400:
  179. assert app_validate(mixed_desc) == mixed_desc
  180. assert dataset_validate(mixed_desc) == mixed_desc
  181. assert service_dataset_validate(mixed_desc) == mixed_desc
  182. elif len(mixed_desc) > 400:
  183. with pytest.raises(ValueError):
  184. app_validate(mixed_desc)
  185. with pytest.raises(ValueError):
  186. dataset_validate(mixed_desc)
  187. with pytest.raises(ValueError):
  188. service_dataset_validate(mixed_desc)
  189. def test_whitespace_handling(self):
  190. """Test validation with various whitespace scenarios"""
  191. # Leading/trailing whitespace
  192. whitespace_desc = " Description with whitespace "
  193. if len(whitespace_desc) <= 400:
  194. assert app_validate(whitespace_desc) == whitespace_desc
  195. assert dataset_validate(whitespace_desc) == whitespace_desc
  196. assert service_dataset_validate(whitespace_desc) == whitespace_desc
  197. # Newlines and tabs
  198. multiline_desc = "Line 1\nLine 2\tTabbed content"
  199. if len(multiline_desc) <= 400:
  200. assert app_validate(multiline_desc) == multiline_desc
  201. assert dataset_validate(multiline_desc) == multiline_desc
  202. assert service_dataset_validate(multiline_desc) == multiline_desc
  203. # Only whitespace over limit
  204. only_spaces = " " * 401
  205. with pytest.raises(ValueError):
  206. app_validate(only_spaces)
  207. with pytest.raises(ValueError):
  208. dataset_validate(only_spaces)
  209. with pytest.raises(ValueError):
  210. service_dataset_validate(only_spaces)