Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 539af4deeceeda5c60265c9c2e107722a9ae0d28 1 parent fbc1fca
Russell Keith-Magee authored October 12, 2010
26  tests/modeltests/m2o_recursive/models.py
@@ -19,22 +19,10 @@ class Category(models.Model):
19 19
     def __unicode__(self):
20 20
         return self.name
21 21
 
22  
-__test__ = {'API_TESTS':"""
23  
-# Create a few Category objects.
24  
->>> r = Category(id=None, name='Root category', parent=None)
25  
->>> r.save()
26  
->>> c = Category(id=None, name='Child category', parent=r)
27  
->>> c.save()
28  
-
29  
->>> r.child_set.all()
30  
-[<Category: Child category>]
31  
->>> r.child_set.get(name__startswith='Child')
32  
-<Category: Child category>
33  
->>> print r.parent
34  
-None
35  
-
36  
->>> c.child_set.all()
37  
-[]
38  
->>> c.parent
39  
-<Category: Root category>
40  
-"""}
  22
+class Person(models.Model):
  23
+    full_name = models.CharField(max_length=20)
  24
+    mother = models.ForeignKey('self', null=True, related_name='mothers_child_set')
  25
+    father = models.ForeignKey('self', null=True, related_name='fathers_child_set')
  26
+
  27
+    def __unicode__(self):
  28
+        return self.full_name
38  tests/modeltests/m2o_recursive/tests.py
... ...
@@ -0,0 +1,38 @@
  1
+from django.test import TestCase
  2
+from models import Category, Person
  3
+
  4
+class ManyToOneRecursiveTests(TestCase):
  5
+
  6
+    def setUp(self):
  7
+        self.r = Category(id=None, name='Root category', parent=None)
  8
+        self.r.save()
  9
+        self.c = Category(id=None, name='Child category', parent=self.r)
  10
+        self.c.save()
  11
+
  12
+    def test_m2o_recursive(self):
  13
+        self.assertQuerysetEqual(self.r.child_set.all(),
  14
+                                 ['<Category: Child category>'])
  15
+        self.assertEqual(self.r.child_set.get(name__startswith='Child').id, self.c.id)
  16
+        self.assertEqual(self.r.parent, None)
  17
+        self.assertQuerysetEqual(self.c.child_set.all(), [])
  18
+        self.assertEqual(self.c.parent.id, self.r.id)
  19
+
  20
+class MultipleManyToOneRecursiveTests(TestCase):
  21
+
  22
+    def setUp(self):
  23
+        self.dad = Person(full_name='John Smith Senior', mother=None, father=None)
  24
+        self.dad.save()
  25
+        self.mom = Person(full_name='Jane Smith', mother=None, father=None)
  26
+        self.mom.save()
  27
+        self.kid = Person(full_name='John Smith Junior', mother=self.mom, father=self.dad)
  28
+        self.kid.save()
  29
+
  30
+    def test_m2o_recursive2(self):
  31
+        self.assertEqual(self.kid.mother.id, self.mom.id)
  32
+        self.assertEqual(self.kid.father.id, self.dad.id)
  33
+        self.assertQuerysetEqual(self.dad.fathers_child_set.all(),
  34
+                                 ['<Person: John Smith Junior>'])
  35
+        self.assertQuerysetEqual(self.mom.mothers_child_set.all(),
  36
+                                 ['<Person: John Smith Junior>'])
  37
+        self.assertQuerysetEqual(self.kid.mothers_child_set.all(), [])
  38
+        self.assertQuerysetEqual(self.kid.fathers_child_set.all(), [])
0  tests/modeltests/m2o_recursive2/__init__.py
No changes.
43  tests/modeltests/m2o_recursive2/models.py
... ...
@@ -1,43 +0,0 @@
1  
-"""
2  
-12. Relating a model to another model more than once
3  
-
4  
-In this example, a ``Person`` can have a ``mother`` and ``father`` -- both of
5  
-which are other ``Person`` objects.
6  
-
7  
-Set ``related_name`` to designate what the reverse relationship is called.
8  
-"""
9  
-
10  
-from django.db import models
11  
-
12  
-class Person(models.Model):
13  
-    full_name = models.CharField(max_length=20)
14  
-    mother = models.ForeignKey('self', null=True, related_name='mothers_child_set')
15  
-    father = models.ForeignKey('self', null=True, related_name='fathers_child_set')
16  
-
17  
-    def __unicode__(self):
18  
-        return self.full_name
19  
-
20  
-__test__ = {'API_TESTS':"""
21  
-# Create two Person objects -- the mom and dad in our family.
22  
->>> dad = Person(full_name='John Smith Senior', mother=None, father=None)
23  
->>> dad.save()
24  
->>> mom = Person(full_name='Jane Smith', mother=None, father=None)
25  
->>> mom.save()
26  
-
27  
-# Give mom and dad a kid.
28  
->>> kid = Person(full_name='John Smith Junior', mother=mom, father=dad)
29  
->>> kid.save()
30  
-
31  
->>> kid.mother
32  
-<Person: Jane Smith>
33  
->>> kid.father
34  
-<Person: John Smith Senior>
35  
->>> dad.fathers_child_set.all()
36  
-[<Person: John Smith Junior>]
37  
->>> mom.mothers_child_set.all()
38  
-[<Person: John Smith Junior>]
39  
->>> kid.mothers_child_set.all()
40  
-[]
41  
->>> kid.fathers_child_set.all()
42  
-[]
43  
-"""}

0 notes on commit 539af4d

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