summaryrefslogtreecommitdiff
path: root/tests/reverse_lookup
diff options
context:
space:
mode:
authorFlorian Apolloner <florian@apolloner.eu>2013-02-26 09:53:47 +0100
committerFlorian Apolloner <florian@apolloner.eu>2013-02-26 14:36:57 +0100
commit89f40e36246100df6a11316c31a76712ebc6c501 (patch)
tree6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/reverse_lookup
parentb3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff)
downloaddjango-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/reverse_lookup')
-rw-r--r--tests/reverse_lookup/__init__.py0
-rw-r--r--tests/reverse_lookup/models.py33
-rw-r--r--tests/reverse_lookup/tests.py52
3 files changed, 85 insertions, 0 deletions
diff --git a/tests/reverse_lookup/__init__.py b/tests/reverse_lookup/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/reverse_lookup/__init__.py
diff --git a/tests/reverse_lookup/models.py b/tests/reverse_lookup/models.py
new file mode 100644
index 0000000000..ed58177770
--- /dev/null
+++ b/tests/reverse_lookup/models.py
@@ -0,0 +1,33 @@
+"""
+25. Reverse lookups
+
+This demonstrates the reverse lookup features of the database API.
+"""
+
+from django.db import models
+from django.utils.encoding import python_2_unicode_compatible
+
+
+@python_2_unicode_compatible
+class User(models.Model):
+ name = models.CharField(max_length=200)
+
+ def __str__(self):
+ return self.name
+
+@python_2_unicode_compatible
+class Poll(models.Model):
+ question = models.CharField(max_length=200)
+ creator = models.ForeignKey(User)
+
+ def __str__(self):
+ return self.question
+
+@python_2_unicode_compatible
+class Choice(models.Model):
+ name = models.CharField(max_length=100)
+ poll = models.ForeignKey(Poll, related_name="poll_choice")
+ related_poll = models.ForeignKey(Poll, related_name="related_choice")
+
+ def __str__(self):
+ return self.name
diff --git a/tests/reverse_lookup/tests.py b/tests/reverse_lookup/tests.py
new file mode 100644
index 0000000000..549ee66392
--- /dev/null
+++ b/tests/reverse_lookup/tests.py
@@ -0,0 +1,52 @@
+from __future__ import absolute_import
+
+from django.core.exceptions import FieldError
+from django.test import TestCase
+
+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")