diff options
| author | Honza Král <honza.kral@gmail.com> | 2009-06-01 15:38:58 +0000 |
|---|---|---|
| committer | Honza Král <honza.kral@gmail.com> | 2009-06-01 15:38:58 +0000 |
| commit | 6152164303b6b8ac7705873fbebac72177efcb39 (patch) | |
| tree | c9398f1955be9493ea6f8145b168914b449b6117 /tests | |
| parent | b59bfbd128a882778f4d96ac9a29880791f728b9 (diff) | |
[soc2009/model-validation] Added clean() and validate() methods to Model.
This also called for ValidationError to also accept dicts as message.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10869 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/validation/__init__.py | 0 | ||||
| -rw-r--r-- | tests/modeltests/validation/models.py | 28 |
2 files changed, 28 insertions, 0 deletions
diff --git a/tests/modeltests/validation/__init__.py b/tests/modeltests/validation/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/modeltests/validation/__init__.py diff --git a/tests/modeltests/validation/models.py b/tests/modeltests/validation/models.py new file mode 100644 index 0000000000..d536495da7 --- /dev/null +++ b/tests/modeltests/validation/models.py @@ -0,0 +1,28 @@ +from datetime import datetime + +from django.core.exceptions import ValidationError +from django.db import models +from django.test import TestCase + +class ModelToValidate(models.Model): + name = models.CharField(max_length=100) + created = models.DateTimeField(default=datetime.now) + number = models.IntegerField() + + def validate(self): + super(ModelToValidate, self).validate() + if self.number == 11: + raise ValidationError('Invalid number supplied!') + +class BaseModelValidationTests(TestCase): + def test_missing_required_field_raises_error(self): + mtv = ModelToValidate() + self.assertRaises(ValidationError, mtv.clean) + + def test_with_correct_value_model_validates(self): + mtv = ModelToValidate(number=10) + self.assertEqual(None, mtv.clean()) + + def test_custom_validate_method_is_called(self): + mtv = ModelToValidate(number=11) + self.assertRaises(ValidationError, mtv.clean) |
