2010-10-12 02:14:49 +02:00
|
|
|
from django.test import TestCase
|
2011-10-13 20:04:12 +02:00
|
|
|
|
|
|
|
from .models import Category, Person
|
|
|
|
|
2010-10-12 02:14:49 +02:00
|
|
|
|
|
|
|
class ManyToOneRecursiveTests(TestCase):
|
2018-11-24 02:59:38 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2018-11-24 12:28:28 +01:00
|
|
|
cls.r = Category.objects.create(id=None, name="Root category", parent=None)
|
|
|
|
cls.c = Category.objects.create(id=None, name="Child category", parent=cls.r)
|
2010-10-12 02:14:49 +02:00
|
|
|
|
|
|
|
def test_m2o_recursive(self):
|
2020-10-18 18:29:52 +02:00
|
|
|
self.assertSequenceEqual(self.r.child_set.all(), [self.c])
|
2010-10-12 02:14:49 +02:00
|
|
|
self.assertEqual(self.r.child_set.get(name__startswith="Child").id, self.c.id)
|
2016-06-16 20:19:18 +02:00
|
|
|
self.assertIsNone(self.r.parent)
|
2020-10-18 18:29:52 +02:00
|
|
|
self.assertSequenceEqual(self.c.child_set.all(), [])
|
2010-10-12 02:14:49 +02:00
|
|
|
self.assertEqual(self.c.parent.id, self.r.id)
|
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2010-10-12 02:14:49 +02:00
|
|
|
class MultipleManyToOneRecursiveTests(TestCase):
|
2018-11-24 02:59:38 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2018-11-24 12:28:28 +01:00
|
|
|
cls.dad = Person.objects.create(
|
|
|
|
full_name="John Smith Senior", mother=None, father=None
|
|
|
|
)
|
|
|
|
cls.mom = Person.objects.create(
|
|
|
|
full_name="Jane Smith", mother=None, father=None
|
|
|
|
)
|
|
|
|
cls.kid = Person.objects.create(
|
|
|
|
full_name="John Smith Junior", mother=cls.mom, father=cls.dad
|
|
|
|
)
|
2010-10-12 02:14:49 +02:00
|
|
|
|
|
|
|
def test_m2o_recursive2(self):
|
|
|
|
self.assertEqual(self.kid.mother.id, self.mom.id)
|
|
|
|
self.assertEqual(self.kid.father.id, self.dad.id)
|
2020-10-18 18:29:52 +02:00
|
|
|
self.assertSequenceEqual(self.dad.fathers_child_set.all(), [self.kid])
|
|
|
|
self.assertSequenceEqual(self.mom.mothers_child_set.all(), [self.kid])
|
|
|
|
self.assertSequenceEqual(self.kid.mothers_child_set.all(), [])
|
|
|
|
self.assertSequenceEqual(self.kid.fathers_child_set.all(), [])
|