test_description_validation.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """
  2. Unit tests for App description validation functions.
  3. This test module validates the 400-character limit enforcement
  4. for App descriptions across all creation and editing endpoints.
  5. """
  6. import os
  7. import sys
  8. import pytest
  9. # Add the API root to Python path for imports
  10. sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", ".."))
  11. class TestAppDescriptionValidationUnit:
  12. """Unit tests for description validation function"""
  13. def test_validate_description_length_function(self):
  14. """Test the _validate_description_length function directly"""
  15. from controllers.console.app.app import _validate_description_length
  16. # Test valid descriptions
  17. assert _validate_description_length("") == ""
  18. assert _validate_description_length("x" * 400) == "x" * 400
  19. assert _validate_description_length(None) is None
  20. # Test invalid descriptions
  21. with pytest.raises(ValueError) as exc_info:
  22. _validate_description_length("x" * 401)
  23. assert "Description cannot exceed 400 characters." in str(exc_info.value)
  24. with pytest.raises(ValueError) as exc_info:
  25. _validate_description_length("x" * 500)
  26. assert "Description cannot exceed 400 characters." in str(exc_info.value)
  27. with pytest.raises(ValueError) as exc_info:
  28. _validate_description_length("x" * 1000)
  29. assert "Description cannot exceed 400 characters." in str(exc_info.value)
  30. def test_validation_consistency_with_dataset(self):
  31. """Test that App and Dataset validation functions are consistent"""
  32. from controllers.console.app.app import _validate_description_length as app_validate
  33. from controllers.console.datasets.datasets import _validate_description_length as dataset_validate
  34. from controllers.service_api.dataset.dataset import _validate_description_length as service_dataset_validate
  35. # Test same valid inputs
  36. valid_desc = "x" * 400
  37. assert app_validate(valid_desc) == dataset_validate(valid_desc) == service_dataset_validate(valid_desc)
  38. assert app_validate("") == dataset_validate("") == service_dataset_validate("")
  39. assert app_validate(None) == dataset_validate(None) == service_dataset_validate(None)
  40. # Test same invalid inputs produce same error
  41. invalid_desc = "x" * 401
  42. app_error = None
  43. dataset_error = None
  44. service_dataset_error = None
  45. try:
  46. app_validate(invalid_desc)
  47. except ValueError as e:
  48. app_error = str(e)
  49. try:
  50. dataset_validate(invalid_desc)
  51. except ValueError as e:
  52. dataset_error = str(e)
  53. try:
  54. service_dataset_validate(invalid_desc)
  55. except ValueError as e:
  56. service_dataset_error = str(e)
  57. assert app_error == dataset_error == service_dataset_error
  58. assert app_error == "Description cannot exceed 400 characters."
  59. def test_boundary_values(self):
  60. """Test boundary values for description validation"""
  61. from controllers.console.app.app import _validate_description_length
  62. # Test exact boundary
  63. exactly_400 = "x" * 400
  64. assert _validate_description_length(exactly_400) == exactly_400
  65. # Test just over boundary
  66. just_over_400 = "x" * 401
  67. with pytest.raises(ValueError):
  68. _validate_description_length(just_over_400)
  69. # Test just under boundary
  70. just_under_400 = "x" * 399
  71. assert _validate_description_length(just_under_400) == just_under_400
  72. def test_edge_cases(self):
  73. """Test edge cases for description validation"""
  74. from controllers.console.app.app import _validate_description_length
  75. # Test None input
  76. assert _validate_description_length(None) is None
  77. # Test empty string
  78. assert _validate_description_length("") == ""
  79. # Test single character
  80. assert _validate_description_length("a") == "a"
  81. # Test unicode characters
  82. unicode_desc = "测试" * 200 # 400 characters in Chinese
  83. assert _validate_description_length(unicode_desc) == unicode_desc
  84. # Test unicode over limit
  85. unicode_over = "测试" * 201 # 402 characters
  86. with pytest.raises(ValueError):
  87. _validate_description_length(unicode_over)
  88. def test_whitespace_handling(self):
  89. """Test how validation handles whitespace"""
  90. from controllers.console.app.app import _validate_description_length
  91. # Test description with spaces
  92. spaces_400 = " " * 400
  93. assert _validate_description_length(spaces_400) == spaces_400
  94. # Test description with spaces over limit
  95. spaces_401 = " " * 401
  96. with pytest.raises(ValueError):
  97. _validate_description_length(spaces_401)
  98. # Test mixed content
  99. mixed_400 = "a" * 200 + " " * 200
  100. assert _validate_description_length(mixed_400) == mixed_400
  101. # Test mixed over limit
  102. mixed_401 = "a" * 200 + " " * 201
  103. with pytest.raises(ValueError):
  104. _validate_description_length(mixed_401)
  105. if __name__ == "__main__":
  106. # Run tests directly
  107. import traceback
  108. test_instance = TestAppDescriptionValidationUnit()
  109. test_methods = [method for method in dir(test_instance) if method.startswith("test_")]
  110. passed = 0
  111. failed = 0
  112. for test_method in test_methods:
  113. try:
  114. print(f"Running {test_method}...")
  115. getattr(test_instance, test_method)()
  116. print(f"✅ {test_method} PASSED")
  117. passed += 1
  118. except Exception as e:
  119. print(f"❌ {test_method} FAILED: {str(e)}")
  120. traceback.print_exc()
  121. failed += 1
  122. print(f"\n📊 Test Results: {passed} passed, {failed} failed")
  123. if failed == 0:
  124. print("🎉 All tests passed!")
  125. else:
  126. print("💥 Some tests failed!")
  127. sys.exit(1)