test_validators.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from unittest.mock import Mock
  2. from django.core.exceptions import ValidationError
  3. from django.test import TestCase
  4. from ..validators import validate_post_length, validate_thread_title
  5. class ValidatePostLengthTests(TestCase):
  6. def test_valid_post_length_passes_validation(self):
  7. """valid post passes validation"""
  8. settings = Mock(post_length_min=1, post_length_max=50)
  9. validate_post_length(settings, "Lorem ipsum dolor met sit amet elit.")
  10. def test_for_empty_post_validation_error_is_raised(self):
  11. """empty post is rejected"""
  12. settings = Mock(post_length_min=3)
  13. with self.assertRaises(ValidationError):
  14. validate_post_length(settings, "")
  15. def test_for_too_short_post_validation_error_is_raised(self):
  16. """too short post is rejected"""
  17. settings = Mock(post_length_min=3)
  18. with self.assertRaises(ValidationError):
  19. validate_post_length(settings, "a")
  20. def test_for_too_long_post_validation_error_is_raised(self):
  21. """too long post is rejected"""
  22. settings = Mock(post_length_min=1, post_length_max=2)
  23. with self.assertRaises(ValidationError):
  24. validate_post_length(settings, "abc")
  25. class ValidateThreadTitleTests(TestCase):
  26. def test_valid_thread_titles_pass_validation(self):
  27. """validate_thread_title is ok with valid titles"""
  28. settings = Mock(thread_title_length_min=1, thread_title_length_max=50)
  29. VALID_TITLES = ["Lorem ipsum dolor met", "123 456 789 112", "Ugabugagagagagaga"]
  30. for title in VALID_TITLES:
  31. validate_thread_title(settings, title)
  32. def test_for_empty_thread_title_validation_error_is_raised(self):
  33. """empty title is rejected"""
  34. settings = Mock(thread_title_length_min=3)
  35. with self.assertRaises(ValidationError):
  36. validate_thread_title(settings, "")
  37. def test_for_too_short_thread_title_validation_error_is_raised(self):
  38. """too short title is rejected"""
  39. settings = Mock(thread_title_length_min=3)
  40. with self.assertRaises(ValidationError):
  41. validate_thread_title(settings, "a")
  42. def test_for_too_long_thread_title_validation_error_is_raised(self):
  43. """too long title is rejected"""
  44. settings = Mock(thread_title_length_min=1, thread_title_length_max=2)
  45. with self.assertRaises(ValidationError):
  46. validate_thread_title(settings, "abc")
  47. def test_for_unsluggable_thread_title_valdiation_error_is_raised(self):
  48. """unsluggable title is rejected"""
  49. settings = Mock(thread_title_length_min=1, thread_title_length_max=9)
  50. with self.assertRaises(ValidationError):
  51. validate_thread_title(settings, "-#%^&-")