From 539af4deeceeda5c60265c9c2e107722a9ae0d28 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 12 Oct 2010 00:14:49 +0000 Subject: [PATCH] Migrated m2o_recursive and m2o_recursive2 tests, merging them into a single package. Thanks to George Sakkis for the patches. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14163 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/m2o_recursive/models.py | 24 +++--------- tests/modeltests/m2o_recursive/tests.py | 38 ++++++++++++++++++ tests/modeltests/m2o_recursive2/__init__.py | 0 tests/modeltests/m2o_recursive2/models.py | 43 --------------------- 4 files changed, 44 insertions(+), 61 deletions(-) create mode 100644 tests/modeltests/m2o_recursive/tests.py delete mode 100644 tests/modeltests/m2o_recursive2/__init__.py delete mode 100644 tests/modeltests/m2o_recursive2/models.py diff --git a/tests/modeltests/m2o_recursive/models.py b/tests/modeltests/m2o_recursive/models.py index f58f7fe0f5..ed9945a6c5 100644 --- a/tests/modeltests/m2o_recursive/models.py +++ b/tests/modeltests/m2o_recursive/models.py @@ -19,22 +19,10 @@ class Category(models.Model): def __unicode__(self): return self.name -__test__ = {'API_TESTS':""" -# Create a few Category objects. ->>> r = Category(id=None, name='Root category', parent=None) ->>> r.save() ->>> c = Category(id=None, name='Child category', parent=r) ->>> c.save() +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') ->>> r.child_set.all() -[] ->>> r.child_set.get(name__startswith='Child') - ->>> print r.parent -None - ->>> c.child_set.all() -[] ->>> c.parent - -"""} + def __unicode__(self): + return self.full_name diff --git a/tests/modeltests/m2o_recursive/tests.py b/tests/modeltests/m2o_recursive/tests.py new file mode 100644 index 0000000000..79dde8b5ea --- /dev/null +++ b/tests/modeltests/m2o_recursive/tests.py @@ -0,0 +1,38 @@ +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(), + ['']) + 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(), + ['']) + self.assertQuerysetEqual(self.mom.mothers_child_set.all(), + ['']) + self.assertQuerysetEqual(self.kid.mothers_child_set.all(), []) + self.assertQuerysetEqual(self.kid.fathers_child_set.all(), []) diff --git a/tests/modeltests/m2o_recursive2/__init__.py b/tests/modeltests/m2o_recursive2/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/modeltests/m2o_recursive2/models.py b/tests/modeltests/m2o_recursive2/models.py deleted file mode 100644 index 47af18ba0e..0000000000 --- a/tests/modeltests/m2o_recursive2/models.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -12. Relating a model to another model more than once - -In this example, a ``Person`` can have a ``mother`` and ``father`` -- both of -which are other ``Person`` objects. - -Set ``related_name`` to designate what the reverse relationship is called. -""" - -from django.db import models - -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 __unicode__(self): - return self.full_name - -__test__ = {'API_TESTS':""" -# Create two Person objects -- the mom and dad in our family. ->>> dad = Person(full_name='John Smith Senior', mother=None, father=None) ->>> dad.save() ->>> mom = Person(full_name='Jane Smith', mother=None, father=None) ->>> mom.save() - -# Give mom and dad a kid. ->>> kid = Person(full_name='John Smith Junior', mother=mom, father=dad) ->>> kid.save() - ->>> kid.mother - ->>> kid.father - ->>> dad.fathers_child_set.all() -[] ->>> mom.mothers_child_set.all() -[] ->>> kid.mothers_child_set.all() -[] ->>> kid.fathers_child_set.all() -[] -"""}