summaryrefslogtreecommitdiff
path: root/tests/m2o_recursive
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/m2o_recursive
parentb3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff)
downloaddjango-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/m2o_recursive')
-rw-r--r--tests/m2o_recursive/__init__.py0
-rw-r--r--tests/m2o_recursive/models.py32
-rw-r--r--tests/m2o_recursive/tests.py42
3 files changed, 74 insertions, 0 deletions
diff --git a/tests/m2o_recursive/__init__.py b/tests/m2o_recursive/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/m2o_recursive/__init__.py
diff --git a/tests/m2o_recursive/models.py b/tests/m2o_recursive/models.py
new file mode 100644
index 0000000000..2775d713dd
--- /dev/null
+++ b/tests/m2o_recursive/models.py
@@ -0,0 +1,32 @@
+"""
+11. Relating an object to itself, many-to-one
+
+To define a many-to-one relationship between a model and itself, use
+``ForeignKey('self')``.
+
+In this example, a ``Category`` is related to itself. That is, each
+``Category`` has a parent ``Category``.
+
+Set ``related_name`` to designate what the reverse relationship is called.
+"""
+
+from django.db import models
+from django.utils.encoding import python_2_unicode_compatible
+
+
+@python_2_unicode_compatible
+class Category(models.Model):
+ name = models.CharField(max_length=20)
+ parent = models.ForeignKey('self', blank=True, null=True, related_name='child_set')
+
+ def __str__(self):
+ return self.name
+
+@python_2_unicode_compatible
+class Person(models.Model):
+ full_name = models.CharField(max_length=20)
+ mother = models.ForeignKey('self', null=True, related_name='mothers_child_set')
+ father = models.ForeignKey('self', null=True, related_name='fathers_child_set')
+
+ def __str__(self):
+ return self.full_name
diff --git a/tests/m2o_recursive/tests.py b/tests/m2o_recursive/tests.py
new file mode 100644
index 0000000000..fa04c74cca
--- /dev/null
+++ b/tests/m2o_recursive/tests.py
@@ -0,0 +1,42 @@
+from __future__ import absolute_import
+
+from django.test import TestCase
+
+from .models import Category, Person
+
+
+class ManyToOneRecursiveTests(TestCase):
+
+ def setUp(self):
+ self.r = Category(id=None, name='Root category', parent=None)
+ self.r.save()
+ self.c = Category(id=None, name='Child category', parent=self.r)
+ self.c.save()
+
+ def test_m2o_recursive(self):
+ self.assertQuerysetEqual(self.r.child_set.all(),
+ ['<Category: Child category>'])
+ self.assertEqual(self.r.child_set.get(name__startswith='Child').id, self.c.id)
+ self.assertEqual(self.r.parent, None)
+ self.assertQuerysetEqual(self.c.child_set.all(), [])
+ self.assertEqual(self.c.parent.id, self.r.id)
+
+class MultipleManyToOneRecursiveTests(TestCase):
+
+ def setUp(self):
+ self.dad = Person(full_name='John Smith Senior', mother=None, father=None)
+ self.dad.save()
+ self.mom = Person(full_name='Jane Smith', mother=None, father=None)
+ self.mom.save()
+ self.kid = Person(full_name='John Smith Junior', mother=self.mom, father=self.dad)
+ self.kid.save()
+
+ def test_m2o_recursive2(self):
+ self.assertEqual(self.kid.mother.id, self.mom.id)
+ self.assertEqual(self.kid.father.id, self.dad.id)
+ self.assertQuerysetEqual(self.dad.fathers_child_set.all(),
+ ['<Person: John Smith Junior>'])
+ self.assertQuerysetEqual(self.mom.mothers_child_set.all(),
+ ['<Person: John Smith Junior>'])
+ self.assertQuerysetEqual(self.kid.mothers_child_set.all(), [])
+ self.assertQuerysetEqual(self.kid.fathers_child_set.all(), [])