1. from django.test import SimpleTestCase
    
  2. 
    
  3. from . import ValidationAssertions
    
  4. from .models import ModelToValidate
    
  5. 
    
  6. 
    
  7. class TestModelsWithValidators(ValidationAssertions, SimpleTestCase):
    
  8.     def test_custom_validator_passes_for_correct_value(self):
    
  9.         mtv = ModelToValidate(
    
  10.             number=10,
    
  11.             name="Some Name",
    
  12.             f_with_custom_validator=42,
    
  13.             f_with_iterable_of_validators=42,
    
  14.         )
    
  15.         self.assertIsNone(mtv.full_clean())
    
  16. 
    
  17.     def test_custom_validator_raises_error_for_incorrect_value(self):
    
  18.         mtv = ModelToValidate(
    
  19.             number=10,
    
  20.             name="Some Name",
    
  21.             f_with_custom_validator=12,
    
  22.             f_with_iterable_of_validators=42,
    
  23.         )
    
  24.         self.assertFailsValidation(mtv.full_clean, ["f_with_custom_validator"])
    
  25.         self.assertFieldFailsValidationWithMessage(
    
  26.             mtv.full_clean,
    
  27.             "f_with_custom_validator",
    
  28.             ["This is not the answer to life, universe and everything!"],
    
  29.         )
    
  30. 
    
  31.     def test_field_validators_can_be_any_iterable(self):
    
  32.         mtv = ModelToValidate(
    
  33.             number=10,
    
  34.             name="Some Name",
    
  35.             f_with_custom_validator=42,
    
  36.             f_with_iterable_of_validators=12,
    
  37.         )
    
  38.         self.assertFailsValidation(mtv.full_clean, ["f_with_iterable_of_validators"])
    
  39.         self.assertFieldFailsValidationWithMessage(
    
  40.             mtv.full_clean,
    
  41.             "f_with_iterable_of_validators",
    
  42.             ["This is not the answer to life, universe and everything!"],
    
  43.         )