2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-10-12 00:14:49 +00:00
|
|
|
from django.test import TestCase
|
2011-10-13 18:04:12 +00:00
|
|
|
|
|
|
|
from .models import Category, Person
|
|
|
|
|
2010-10-12 00:14:49 +00:00
|
|
|
|
|
|
|
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)
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(self.r.parent)
|
2010-10-12 00:14:49 +00:00
|
|
|
self.assertQuerysetEqual(self.c.child_set.all(), [])
|
|
|
|
self.assertEqual(self.c.parent.id, self.r.id)
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2010-10-12 00:14:49 +00:00
|
|
|
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(), [])
|