Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Migrated reverse_lookup doctests. Thanks to Eric Florenzano

Backport of r13829 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13838 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 11f77de5337ba692985907d41e2186ac34ea87e5 1 parent 21fccbc
Russell Keith-Magee authored September 13, 2010
31  tests/modeltests/reverse_lookup/models.py
@@ -26,34 +26,3 @@ class Choice(models.Model):
26 26
 
27 27
     def __unicode__(self):
28 28
         return self.name
29  
-
30  
-__test__ = {'API_TESTS':"""
31  
->>> john = User(name="John Doe")
32  
->>> john.save()
33  
->>> jim = User(name="Jim Bo")
34  
->>> jim.save()
35  
->>> first_poll = Poll(question="What's the first question?", creator=john)
36  
->>> first_poll.save()
37  
->>> second_poll = Poll(question="What's the second question?", creator=jim)
38  
->>> second_poll.save()
39  
->>> new_choice = Choice(poll=first_poll, related_poll=second_poll, name="This is the answer.")
40  
->>> new_choice.save()
41  
-
42  
->>> # Reverse lookups by field name:
43  
->>> User.objects.get(poll__question__exact="What's the first question?")
44  
-<User: John Doe>
45  
->>> User.objects.get(poll__question__exact="What's the second question?")
46  
-<User: Jim Bo>
47  
-
48  
->>> # Reverse lookups by related_name:
49  
->>> Poll.objects.get(poll_choice__name__exact="This is the answer.")
50  
-<Poll: What's the first question?>
51  
->>> Poll.objects.get(related_choice__name__exact="This is the answer.")
52  
-<Poll: What's the second question?>
53  
-
54  
->>> # If a related_name is given you can't use the field name instead:
55  
->>> Poll.objects.get(choice__name__exact="This is the answer")
56  
-Traceback (most recent call last):
57  
-    ...
58  
-FieldError: Cannot resolve keyword 'choice' into field. Choices are: creator, id, poll_choice, question, related_choice
59  
-"""}
49  tests/modeltests/reverse_lookup/tests.py
... ...
@@ -0,0 +1,49 @@
  1
+from django.test import TestCase
  2
+from django.core.exceptions import FieldError
  3
+
  4
+from models import User, Poll, Choice
  5
+
  6
+class ReverseLookupTests(TestCase):
  7
+
  8
+    def setUp(self):
  9
+        john = User.objects.create(name="John Doe")
  10
+        jim = User.objects.create(name="Jim Bo")
  11
+        first_poll = Poll.objects.create(
  12
+            question="What's the first question?",
  13
+            creator=john
  14
+        )
  15
+        second_poll = Poll.objects.create(
  16
+            question="What's the second question?",
  17
+            creator=jim
  18
+        )
  19
+        new_choice = Choice.objects.create(
  20
+            poll=first_poll,
  21
+            related_poll=second_poll,
  22
+            name="This is the answer."
  23
+        )
  24
+
  25
+    def test_reverse_by_field(self):
  26
+        u1 = User.objects.get(
  27
+            poll__question__exact="What's the first question?"
  28
+        )
  29
+        self.assertEqual(u1.name, "John Doe")
  30
+
  31
+        u2 = User.objects.get(
  32
+            poll__question__exact="What's the second question?"
  33
+        )
  34
+        self.assertEqual(u2.name, "Jim Bo")
  35
+
  36
+    def test_reverse_by_related_name(self):
  37
+        p1 = Poll.objects.get(poll_choice__name__exact="This is the answer.")
  38
+        self.assertEqual(p1.question, "What's the first question?")
  39
+
  40
+        p2 = Poll.objects.get(
  41
+            related_choice__name__exact="This is the answer.")
  42
+        self.assertEqual(p2.question, "What's the second question?")
  43
+
  44
+    def test_reverse_field_name_disallowed(self):
  45
+        """
  46
+        If a related_name is given you can't use the field name instead
  47
+        """
  48
+        self.assertRaises(FieldError, Poll.objects.get,
  49
+            choice__name__exact="This is the answer")

0 notes on commit 11f77de

Please sign in to comment.
Something went wrong with that request. Please try again.