Code

Ticket #15146: tests.diff

File tests.diff, 1.6 KB (added by elbarto, 3 years ago)

Tests added.

Line 
1Index: tests/modeltests/reverse_lookup/tests.py
2===================================================================
3--- tests/modeltests/reverse_lookup/tests.py    (revisión: 15343)
4+++ tests/modeltests/reverse_lookup/tests.py    (copia de trabajo)
5@@ -1,7 +1,7 @@
6 from django.test import TestCase
7 from django.core.exceptions import FieldError
8 
9-from models import User, Poll, Choice
10+from models import User, Poll, Choice, Person
11 
12 class ReverseLookupTests(TestCase):
13 
14@@ -41,9 +41,19 @@
15             related_choice__name__exact="This is the answer.")
16         self.assertEqual(p2.question, "What's the second question?")
17 
18+    def test_reverse_by_related_name_without_saving(self):
19+        p1 = Person()
20+        p1.save()
21+        p2 = Person()
22+        self.assertEqual(p2.children.all(), "[]")
23+       
24+        p2.save()
25+        self.assertEqual(p2.children.all(), "[]")
26+
27     def test_reverse_field_name_disallowed(self):
28         """
29         If a related_name is given you can't use the field name instead
30         """
31         self.assertRaises(FieldError, Poll.objects.get,
32             choice__name__exact="This is the answer")
33+
34Index: tests/modeltests/reverse_lookup/models.py
35===================================================================
36--- tests/modeltests/reverse_lookup/models.py   (revisión: 15343)
37+++ tests/modeltests/reverse_lookup/models.py   (copia de trabajo)
38@@ -26,3 +26,7 @@
39 
40     def __unicode__(self):
41         return self.name
42+
43+class Person(models.Model):
44+    parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
45+