2018-11-26 19:05:02 +00:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
from . import ValidationAssertions
|
2011-10-13 18:04:12 +00:00
|
|
|
from .models import ModelToValidate
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
|
2018-11-26 19:05:02 +00:00
|
|
|
class TestModelsWithValidators(ValidationAssertions, SimpleTestCase):
|
2010-01-05 03:56:19 +00:00
|
|
|
def test_custom_validator_passes_for_correct_value(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
mtv = ModelToValidate(
|
|
|
|
number=10,
|
|
|
|
name="Some Name",
|
|
|
|
f_with_custom_validator=42,
|
|
|
|
f_with_iterable_of_validators=42,
|
|
|
|
)
|
2015-04-27 14:59:16 +00:00
|
|
|
self.assertIsNone(mtv.full_clean())
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
def test_custom_validator_raises_error_for_incorrect_value(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
mtv = ModelToValidate(
|
|
|
|
number=10,
|
|
|
|
name="Some Name",
|
|
|
|
f_with_custom_validator=12,
|
|
|
|
f_with_iterable_of_validators=42,
|
|
|
|
)
|
|
|
|
self.assertFailsValidation(mtv.full_clean, ["f_with_custom_validator"])
|
2010-01-05 03:56:19 +00:00
|
|
|
self.assertFieldFailsValidationWithMessage(
|
2010-01-12 02:29:45 +00:00
|
|
|
mtv.full_clean,
|
2022-02-03 19:24:19 +00:00
|
|
|
"f_with_custom_validator",
|
|
|
|
["This is not the answer to life, universe and everything!"],
|
2010-01-05 03:56:19 +00:00
|
|
|
)
|
2016-04-22 00:18:43 +00:00
|
|
|
|
|
|
|
def test_field_validators_can_be_any_iterable(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
mtv = ModelToValidate(
|
|
|
|
number=10,
|
|
|
|
name="Some Name",
|
|
|
|
f_with_custom_validator=42,
|
|
|
|
f_with_iterable_of_validators=12,
|
|
|
|
)
|
|
|
|
self.assertFailsValidation(mtv.full_clean, ["f_with_iterable_of_validators"])
|
2016-04-22 00:18:43 +00:00
|
|
|
self.assertFieldFailsValidationWithMessage(
|
|
|
|
mtv.full_clean,
|
2022-02-03 19:24:19 +00:00
|
|
|
"f_with_iterable_of_validators",
|
|
|
|
["This is not the answer to life, universe and everything!"],
|
2016-04-22 00:18:43 +00:00
|
|
|
)
|