Author: Honza_Kral
Date: 2009-06-01 10:42:29 -0500 (Mon, 01 Jun 2009)
New Revision: 10879

Modified:
   
django/branches/soc2009/model-validation/tests/modeltests/validation/models.py
   django/branches/soc2009/model-validation/tests/modeltests/validation/tests.py
Log:
[soc2009/model-validation] Added simple tests for ForeignKey validation.

Modified: 
django/branches/soc2009/model-validation/tests/modeltests/validation/models.py
===================================================================
--- 
django/branches/soc2009/model-validation/tests/modeltests/validation/models.py  
    2009-06-01 15:42:13 UTC (rev 10878)
+++ 
django/branches/soc2009/model-validation/tests/modeltests/validation/models.py  
    2009-06-01 15:42:29 UTC (rev 10879)
@@ -8,6 +8,7 @@
     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()

Modified: 
django/branches/soc2009/model-validation/tests/modeltests/validation/tests.py
===================================================================
--- 
django/branches/soc2009/model-validation/tests/modeltests/validation/tests.py   
    2009-06-01 15:42:13 UTC (rev 10878)
+++ 
django/branches/soc2009/model-validation/tests/modeltests/validation/tests.py   
    2009-06-01 15:42:29 UTC (rev 10879)
@@ -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 @@
     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())
+


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to