validators.py 811 B

12345678910111213141516171819
  1. from django.utils.unittest import TestCase
  2. from modeltests.validation import ValidationTestCase
  3. from models import *
  4. class TestModelsWithValidators(ValidationTestCase):
  5. def test_custom_validator_passes_for_correct_value(self):
  6. mtv = ModelToValidate(number=10, name='Some Name', f_with_custom_validator=42)
  7. self.assertEqual(None, mtv.full_clean())
  8. def test_custom_validator_raises_error_for_incorrect_value(self):
  9. mtv = ModelToValidate(number=10, name='Some Name', f_with_custom_validator=12)
  10. self.assertFailsValidation(mtv.full_clean, ['f_with_custom_validator'])
  11. self.assertFieldFailsValidationWithMessage(
  12. mtv.full_clean,
  13. 'f_with_custom_validator',
  14. [u'This is not the answer to life, universe and everything!']
  15. )