mirror of https://github.com/django/django.git
Simplified m2m_recursive.tests.
This commit is contained in:
parent
8543647306
commit
ea6e684f34
|
@ -1,5 +1,3 @@
|
||||||
from operator import attrgetter
|
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from .models import Person
|
from .models import Person
|
||||||
|
@ -10,173 +8,54 @@ class RecursiveM2MTests(TestCase):
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
cls.a, cls.b, cls.c, cls.d = [
|
cls.a, cls.b, cls.c, cls.d = [
|
||||||
Person.objects.create(name=name)
|
Person.objects.create(name=name)
|
||||||
for name in ["Anne", "Bill", "Chuck", "David"]
|
for name in ['Anne', 'Bill', 'Chuck', 'David']
|
||||||
]
|
]
|
||||||
|
|
||||||
# Anne is friends with Bill and Chuck
|
|
||||||
cls.a.friends.add(cls.b, cls.c)
|
cls.a.friends.add(cls.b, cls.c)
|
||||||
|
# Add m2m for Anne and Chuck in reverse direction.
|
||||||
# David is friends with Anne and Chuck - add in reverse direction
|
|
||||||
cls.d.friends.add(cls.a, cls.c)
|
cls.d.friends.add(cls.a, cls.c)
|
||||||
|
|
||||||
def test_recursive_m2m_all(self):
|
def test_recursive_m2m_all(self):
|
||||||
# Who is friends with Anne?
|
for person, friends in (
|
||||||
self.assertQuerysetEqual(
|
(self.a, [self.b, self.c, self.d]),
|
||||||
self.a.friends.all(), [
|
(self.b, [self.a]),
|
||||||
"Bill",
|
(self.c, [self.a, self.d]),
|
||||||
"Chuck",
|
(self.d, [self.a, self.c]),
|
||||||
"David"
|
):
|
||||||
],
|
with self.subTest(person=person):
|
||||||
attrgetter("name"),
|
self.assertSequenceEqual(person.friends.all(), friends)
|
||||||
ordered=False
|
|
||||||
)
|
|
||||||
# Who is friends with Bill?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.b.friends.all(), [
|
|
||||||
"Anne",
|
|
||||||
],
|
|
||||||
attrgetter("name")
|
|
||||||
)
|
|
||||||
# Who is friends with Chuck?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.c.friends.all(), [
|
|
||||||
"Anne",
|
|
||||||
"David"
|
|
||||||
],
|
|
||||||
attrgetter("name"),
|
|
||||||
ordered=False
|
|
||||||
)
|
|
||||||
# Who is friends with David?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.d.friends.all(), [
|
|
||||||
"Anne",
|
|
||||||
"Chuck",
|
|
||||||
],
|
|
||||||
attrgetter("name"),
|
|
||||||
ordered=False
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_recursive_m2m_reverse_add(self):
|
def test_recursive_m2m_reverse_add(self):
|
||||||
# Bill is already friends with Anne - add Anne again, but in the
|
# Add m2m for Anne in reverse direction.
|
||||||
# reverse direction
|
|
||||||
self.b.friends.add(self.a)
|
self.b.friends.add(self.a)
|
||||||
|
self.assertSequenceEqual(self.a.friends.all(), [self.b, self.c, self.d])
|
||||||
# Who is friends with Anne?
|
self.assertSequenceEqual(self.b.friends.all(), [self.a])
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.a.friends.all(), [
|
|
||||||
"Bill",
|
|
||||||
"Chuck",
|
|
||||||
"David",
|
|
||||||
],
|
|
||||||
attrgetter("name"),
|
|
||||||
ordered=False
|
|
||||||
)
|
|
||||||
# Who is friends with Bill?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.b.friends.all(), [
|
|
||||||
"Anne",
|
|
||||||
],
|
|
||||||
attrgetter("name")
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_recursive_m2m_remove(self):
|
def test_recursive_m2m_remove(self):
|
||||||
# Remove Anne from Bill's friends
|
|
||||||
self.b.friends.remove(self.a)
|
self.b.friends.remove(self.a)
|
||||||
|
self.assertSequenceEqual(self.a.friends.all(), [self.c, self.d])
|
||||||
# Who is friends with Anne?
|
self.assertSequenceEqual(self.b.friends.all(), [])
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.a.friends.all(), [
|
|
||||||
"Chuck",
|
|
||||||
"David",
|
|
||||||
],
|
|
||||||
attrgetter("name"),
|
|
||||||
ordered=False
|
|
||||||
)
|
|
||||||
# Who is friends with Bill?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.b.friends.all(), []
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_recursive_m2m_clear(self):
|
def test_recursive_m2m_clear(self):
|
||||||
# Clear Anne's group of friends
|
# Clear m2m for Anne.
|
||||||
self.a.friends.clear()
|
self.a.friends.clear()
|
||||||
|
self.assertSequenceEqual(self.a.friends.all(), [])
|
||||||
# Who is friends with Anne?
|
# Reverse m2m relationships should be removed.
|
||||||
self.assertQuerysetEqual(
|
self.assertSequenceEqual(self.c.friends.all(), [self.d])
|
||||||
self.a.friends.all(), []
|
self.assertSequenceEqual(self.d.friends.all(), [self.c])
|
||||||
)
|
|
||||||
|
|
||||||
# Reverse relationships should also be gone
|
|
||||||
# Who is friends with Chuck?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.c.friends.all(), [
|
|
||||||
"David",
|
|
||||||
],
|
|
||||||
attrgetter("name")
|
|
||||||
)
|
|
||||||
|
|
||||||
# Who is friends with David?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.d.friends.all(), [
|
|
||||||
"Chuck",
|
|
||||||
],
|
|
||||||
attrgetter("name")
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_recursive_m2m_add_via_related_name(self):
|
def test_recursive_m2m_add_via_related_name(self):
|
||||||
# David is idolized by Anne and Chuck - add in reverse direction
|
# Add m2m with custom related name for Anne in reverse direction.
|
||||||
self.d.stalkers.add(self.a)
|
self.d.stalkers.add(self.a)
|
||||||
|
self.assertSequenceEqual(self.a.idols.all(), [self.d])
|
||||||
# Who are Anne's idols?
|
self.assertSequenceEqual(self.a.stalkers.all(), [])
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.a.idols.all(), [
|
|
||||||
"David",
|
|
||||||
],
|
|
||||||
attrgetter("name"),
|
|
||||||
ordered=False
|
|
||||||
)
|
|
||||||
# Who is stalking Anne?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.a.stalkers.all(), [],
|
|
||||||
attrgetter("name")
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_recursive_m2m_add_in_both_directions(self):
|
def test_recursive_m2m_add_in_both_directions(self):
|
||||||
"""Adding the same relation twice results in a single relation."""
|
# Adding the same relation twice results in a single relation.
|
||||||
# Ann idolizes David
|
|
||||||
self.a.idols.add(self.d)
|
self.a.idols.add(self.d)
|
||||||
|
|
||||||
# David is idolized by Anne
|
|
||||||
self.d.stalkers.add(self.a)
|
self.d.stalkers.add(self.a)
|
||||||
|
self.assertSequenceEqual(self.a.idols.all(), [self.d])
|
||||||
# Who are Anne's idols?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.a.idols.all(), [
|
|
||||||
"David",
|
|
||||||
],
|
|
||||||
attrgetter("name"),
|
|
||||||
ordered=False
|
|
||||||
)
|
|
||||||
# As the assertQuerysetEqual uses a set for comparison,
|
|
||||||
# check we've only got David listed once
|
|
||||||
self.assertEqual(self.a.idols.all().count(), 1)
|
|
||||||
|
|
||||||
def test_recursive_m2m_related_to_self(self):
|
def test_recursive_m2m_related_to_self(self):
|
||||||
# Ann idolizes herself
|
|
||||||
self.a.idols.add(self.a)
|
self.a.idols.add(self.a)
|
||||||
|
self.assertSequenceEqual(self.a.idols.all(), [self.a])
|
||||||
# Who are Anne's idols?
|
self.assertSequenceEqual(self.a.stalkers.all(), [self.a])
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.a.idols.all(), [
|
|
||||||
"Anne",
|
|
||||||
],
|
|
||||||
attrgetter("name"),
|
|
||||||
ordered=False
|
|
||||||
)
|
|
||||||
# Who is stalking Anne?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
self.a.stalkers.all(), [
|
|
||||||
"Anne",
|
|
||||||
],
|
|
||||||
attrgetter("name")
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue