test_validators.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import pytest
  2. from django.core.exceptions import ValidationError
  3. from ..validators import validate_image_square, validate_sluggable
  4. def test_sluggable_validator_raises_error_if_result_slug_will_be_empty():
  5. validator = validate_sluggable()
  6. with pytest.raises(ValidationError):
  7. validator("!#@! !@#@")
  8. def test_sluggable_validator_raises_custom_error_if_result_slug_will_be_empty():
  9. error_message = "I'm short custom error!"
  10. validator = validate_sluggable(error_short=error_message)
  11. with pytest.raises(ValidationError) as e:
  12. validator("!#@! !@#@")
  13. assert error_message in str(e.value)
  14. def test_sluggable_validator_raises_error_if_result_slug_will_be_too_long():
  15. validator = validate_sluggable()
  16. with pytest.raises(ValidationError):
  17. validator("a" * 256)
  18. def test_sluggable_validator_raises_custom_error_if_result_slug_will_be_too_long():
  19. error_message = "I'm long custom error!"
  20. validator = validate_sluggable(error_long=error_message)
  21. with pytest.raises(ValidationError) as e:
  22. validator("a" * 256)
  23. assert error_message in str(e.value)
  24. def test_square_square_validator_validates_square_image(mocker):
  25. image = mocker.Mock(width=100, height=100)
  26. validate_image_square(image)
  27. def test_square_square_validator_raises_error_if_image_is_not_square(mocker):
  28. image = mocker.Mock(width=100, height=200)
  29. with pytest.raises(ValidationError):
  30. validate_image_square(image)