1
0
mirror of https://github.com/django/django.git synced 2025-07-07 11:19:12 +00:00

[soc2009/model-validation] Added simple tests for ForeignKey validation.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10879 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Honza Král 2009-06-01 15:42:29 +00:00
parent afcb3c8872
commit eae2b9c149
2 changed files with 19 additions and 1 deletions

View File

@ -8,6 +8,7 @@ class ModelToValidate(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
created = models.DateTimeField(default=datetime.now) created = models.DateTimeField(default=datetime.now)
number = models.IntegerField() number = models.IntegerField()
parent = models.ForeignKey('self', blank=True, null=True)
def validate(self): def validate(self):
super(ModelToValidate, self).validate() super(ModelToValidate, self).validate()

View File

@ -1,4 +1,4 @@
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
from django.test import TestCase from django.test import TestCase
from models import ModelToValidate from models import ModelToValidate
@ -19,4 +19,21 @@ class BaseModelValidationTests(TestCase):
def test_custom_validate_method_is_called(self): def test_custom_validate_method_is_called(self):
mtv = ModelToValidate(number=11) mtv = ModelToValidate(number=11)
self.assertRaises(ValidationError, mtv.clean) self.assertRaises(ValidationError, mtv.clean)
try:
mtv.clean()
except ValidationError, e:
self.assertEquals(sorted([NON_FIELD_ERRORS, 'name']), sorted(e.message_dict.keys()))
def test_wrong_FK_value_raises_error(self):
mtv=ModelToValidate(number=10, name='Some Name', parent_id=3)
self.assertRaises(ValidationError, mtv.clean)
try:
mtv.clean()
except ValidationError, e:
self.assertEquals(['parent'], e.message_dict.keys())
def test_correct_FK_value_cleans(self):
parent = ModelToValidate.objects.create(number=10, name='Some Name')
mtv=ModelToValidate(number=10, name='Some Name', parent_id=parent.pk)
self.assertEqual(None, mtv.clean())