mirror of
https://github.com/django/django.git
synced 2025-10-25 06:36:07 +00:00
We still have to decide what to do with fields that have duplicate errors: - required from form - blank on model This change resulted in some tests being rendered as invalid. For example excluding a required field or doing save_as_new on InlineFormset with non-existing instance. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10872 bcc190cf-cafb-0310-a4f2-bffc1f526a37
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
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)
|
|
try:
|
|
mtv.clean()
|
|
except ValidationError, e:
|
|
self.assertEquals(['name', 'number'], sorted(e.message_dict.keys()))
|
|
|
|
def test_with_correct_value_model_validates(self):
|
|
mtv = ModelToValidate(number=10, name='Some Name')
|
|
self.assertEqual(None, mtv.clean())
|
|
|
|
def test_custom_validate_method_is_called(self):
|
|
mtv = ModelToValidate(number=11)
|
|
self.assertRaises(ValidationError, mtv.clean)
|
|
|