1
0
mirror of https://github.com/django/django.git synced 2025-10-25 14:46:09 +00:00
Files
django/tests/modeltests/validation/models.py
Honza Král 6152164303 [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
2009-06-01 15:38:58 +00:00

29 lines
962 B
Python

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)