summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHonza Král <honza.kral@gmail.com>2009-06-01 15:42:29 +0000
committerHonza Král <honza.kral@gmail.com>2009-06-01 15:42:29 +0000
commiteae2b9c149662e8797744262d4f11c66822d5bca (patch)
treed86b5395c45d6f1a0062d88f39f7a35c17928c3d
parentafcb3c887290fb4df73e72780f864b1ec82a2ac2 (diff)
[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
-rw-r--r--tests/modeltests/validation/models.py1
-rw-r--r--tests/modeltests/validation/tests.py19
2 files changed, 19 insertions, 1 deletions
diff --git a/tests/modeltests/validation/models.py b/tests/modeltests/validation/models.py
index 77b1b07cf6..61095fc52c 100644
--- a/tests/modeltests/validation/models.py
+++ b/tests/modeltests/validation/models.py
@@ -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()
diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py
index f1d558aa8d..4bd7e8abf1 100644
--- a/tests/modeltests/validation/tests.py
+++ b/tests/modeltests/validation/tests.py
@@ -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())