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:
parent
afcb3c8872
commit
eae2b9c149
@ -8,6 +8,7 @@ class ModelToValidate(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
created = models.DateTimeField(default=datetime.now)
|
||||
number = models.IntegerField()
|
||||
parent = models.ForeignKey('self', blank=True, null=True)
|
||||
|
||||
def validate(self):
|
||||
super(ModelToValidate, self).validate()
|
||||
|
@ -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 models import ModelToValidate
|
||||
@ -19,4 +19,21 @@ class BaseModelValidationTests(TestCase):
|
||||
def test_custom_validate_method_is_called(self):
|
||||
mtv = ModelToValidate(number=11)
|
||||
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())
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user