diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2010-09-13 05:47:52 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2010-09-13 05:47:52 +0000 |
| commit | 11f77de5337ba692985907d41e2186ac34ea87e5 (patch) | |
| tree | 04a7823ae08d742347afcdb7d2b6a3331a61810c | |
| parent | 21fccbc13e28bf63c7998c79f15fa85537caa2db (diff) | |
[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
| -rw-r--r-- | tests/modeltests/reverse_lookup/models.py | 31 | ||||
| -rw-r--r-- | tests/modeltests/reverse_lookup/tests.py | 49 |
2 files changed, 49 insertions, 31 deletions
diff --git a/tests/modeltests/reverse_lookup/models.py b/tests/modeltests/reverse_lookup/models.py index ef385b4b18..2ffdc39b3b 100644 --- a/tests/modeltests/reverse_lookup/models.py +++ b/tests/modeltests/reverse_lookup/models.py @@ -26,34 +26,3 @@ class Choice(models.Model): def __unicode__(self): return self.name - -__test__ = {'API_TESTS':""" ->>> john = User(name="John Doe") ->>> john.save() ->>> jim = User(name="Jim Bo") ->>> jim.save() ->>> first_poll = Poll(question="What's the first question?", creator=john) ->>> first_poll.save() ->>> second_poll = Poll(question="What's the second question?", creator=jim) ->>> second_poll.save() ->>> new_choice = Choice(poll=first_poll, related_poll=second_poll, name="This is the answer.") ->>> new_choice.save() - ->>> # Reverse lookups by field name: ->>> User.objects.get(poll__question__exact="What's the first question?") -<User: John Doe> ->>> User.objects.get(poll__question__exact="What's the second question?") -<User: Jim Bo> - ->>> # Reverse lookups by related_name: ->>> Poll.objects.get(poll_choice__name__exact="This is the answer.") -<Poll: What's the first question?> ->>> Poll.objects.get(related_choice__name__exact="This is the answer.") -<Poll: What's the second question?> - ->>> # If a related_name is given you can't use the field name instead: ->>> Poll.objects.get(choice__name__exact="This is the answer") -Traceback (most recent call last): - ... -FieldError: Cannot resolve keyword 'choice' into field. Choices are: creator, id, poll_choice, question, related_choice -"""} diff --git a/tests/modeltests/reverse_lookup/tests.py b/tests/modeltests/reverse_lookup/tests.py new file mode 100644 index 0000000000..9a6e3068fa --- /dev/null +++ b/tests/modeltests/reverse_lookup/tests.py @@ -0,0 +1,49 @@ +from django.test import TestCase +from django.core.exceptions import FieldError + +from models import User, Poll, Choice + +class ReverseLookupTests(TestCase): + + def setUp(self): + john = User.objects.create(name="John Doe") + jim = User.objects.create(name="Jim Bo") + first_poll = Poll.objects.create( + question="What's the first question?", + creator=john + ) + second_poll = Poll.objects.create( + question="What's the second question?", + creator=jim + ) + new_choice = Choice.objects.create( + poll=first_poll, + related_poll=second_poll, + name="This is the answer." + ) + + def test_reverse_by_field(self): + u1 = User.objects.get( + poll__question__exact="What's the first question?" + ) + self.assertEqual(u1.name, "John Doe") + + u2 = User.objects.get( + poll__question__exact="What's the second question?" + ) + self.assertEqual(u2.name, "Jim Bo") + + def test_reverse_by_related_name(self): + p1 = Poll.objects.get(poll_choice__name__exact="This is the answer.") + self.assertEqual(p1.question, "What's the first question?") + + p2 = Poll.objects.get( + related_choice__name__exact="This is the answer.") + self.assertEqual(p2.question, "What's the second question?") + + def test_reverse_field_name_disallowed(self): + """ + If a related_name is given you can't use the field name instead + """ + self.assertRaises(FieldError, Poll.objects.get, + choice__name__exact="This is the answer") |
