Refactored RecursiveM2MTests into smaller pieces, refs #18586.
This commit is contained in:
parent
c5395eef76
commit
0d98422b13
|
@ -8,22 +8,23 @@ from .models import Person
|
||||||
|
|
||||||
|
|
||||||
class RecursiveM2MTests(TestCase):
|
class RecursiveM2MTests(TestCase):
|
||||||
def test_recursive_m2m(self):
|
def setUp(self):
|
||||||
a, b, c, d = [
|
self.a, self.b, self.c, self.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"]
|
||||||
]
|
]
|
||||||
|
|
||||||
# Add some friends in the direction of field definition
|
|
||||||
# Anne is friends with Bill and Chuck
|
# Anne is friends with Bill and Chuck
|
||||||
a.friends.add(b, c)
|
self.a.friends.add(self.b, self.c)
|
||||||
|
|
||||||
# David is friends with Anne and Chuck - add in reverse direction
|
# David is friends with Anne and Chuck - add in reverse direction
|
||||||
d.friends.add(a, c)
|
self.d.friends.add(self.a, self.c)
|
||||||
|
|
||||||
|
def test_recursive_m2m_all(self):
|
||||||
|
""" Test that m2m relations are reported correctly """
|
||||||
# Who is friends with Anne?
|
# Who is friends with Anne?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
a.friends.all(), [
|
self.a.friends.all(), [
|
||||||
"Bill",
|
"Bill",
|
||||||
"Chuck",
|
"Chuck",
|
||||||
"David"
|
"David"
|
||||||
|
@ -33,14 +34,14 @@ class RecursiveM2MTests(TestCase):
|
||||||
)
|
)
|
||||||
# Who is friends with Bill?
|
# Who is friends with Bill?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
b.friends.all(), [
|
self.b.friends.all(), [
|
||||||
"Anne",
|
"Anne",
|
||||||
],
|
],
|
||||||
attrgetter("name")
|
attrgetter("name")
|
||||||
)
|
)
|
||||||
# Who is friends with Chuck?
|
# Who is friends with Chuck?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
c.friends.all(), [
|
self.c.friends.all(), [
|
||||||
"Anne",
|
"Anne",
|
||||||
"David"
|
"David"
|
||||||
],
|
],
|
||||||
|
@ -49,20 +50,24 @@ class RecursiveM2MTests(TestCase):
|
||||||
)
|
)
|
||||||
# Who is friends with David?
|
# Who is friends with David?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
d.friends.all(), [
|
self.d.friends.all(), [
|
||||||
"Anne",
|
"Anne",
|
||||||
"Chuck",
|
"Chuck",
|
||||||
],
|
],
|
||||||
attrgetter("name"),
|
attrgetter("name"),
|
||||||
ordered=False
|
ordered=False
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_recursive_m2m_reverse_add(self):
|
||||||
|
""" Test reverse m2m relation is consistent """
|
||||||
|
|
||||||
# Bill is already friends with Anne - add Anne again, but in the
|
# Bill is already friends with Anne - add Anne again, but in the
|
||||||
# reverse direction
|
# reverse direction
|
||||||
b.friends.add(a)
|
self.b.friends.add(self.a)
|
||||||
|
|
||||||
# Who is friends with Anne?
|
# Who is friends with Anne?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
a.friends.all(), [
|
self.a.friends.all(), [
|
||||||
"Bill",
|
"Bill",
|
||||||
"Chuck",
|
"Chuck",
|
||||||
"David",
|
"David",
|
||||||
|
@ -72,16 +77,21 @@ class RecursiveM2MTests(TestCase):
|
||||||
)
|
)
|
||||||
# Who is friends with Bill?
|
# Who is friends with Bill?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
b.friends.all(), [
|
self.b.friends.all(), [
|
||||||
"Anne",
|
"Anne",
|
||||||
],
|
],
|
||||||
attrgetter("name")
|
attrgetter("name")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_recursive_m2m_remove(self):
|
||||||
|
""" Test that we can remove items from an m2m relationship """
|
||||||
|
|
||||||
# Remove Anne from Bill's friends
|
# Remove Anne from Bill's friends
|
||||||
b.friends.remove(a)
|
self.b.friends.remove(self.a)
|
||||||
|
|
||||||
# Who is friends with Anne?
|
# Who is friends with Anne?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
a.friends.all(), [
|
self.a.friends.all(), [
|
||||||
"Chuck",
|
"Chuck",
|
||||||
"David",
|
"David",
|
||||||
],
|
],
|
||||||
|
@ -90,44 +100,46 @@ class RecursiveM2MTests(TestCase):
|
||||||
)
|
)
|
||||||
# Who is friends with Bill?
|
# Who is friends with Bill?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
b.friends.all(), []
|
self.b.friends.all(), []
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_recursive_m2m_clear(self):
|
||||||
|
""" Tests the clear method works as expected on m2m fields """
|
||||||
|
|
||||||
# Clear Anne's group of friends
|
# Clear Anne's group of friends
|
||||||
a.friends.clear()
|
self.a.friends.clear()
|
||||||
|
|
||||||
# Who is friends with Anne?
|
# Who is friends with Anne?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
a.friends.all(), []
|
self.a.friends.all(), []
|
||||||
)
|
)
|
||||||
|
|
||||||
# Reverse relationships should also be gone
|
# Reverse relationships should also be gone
|
||||||
# Who is friends with Chuck?
|
# Who is friends with Chuck?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
c.friends.all(), [
|
self.c.friends.all(), [
|
||||||
"David",
|
"David",
|
||||||
],
|
],
|
||||||
attrgetter("name")
|
attrgetter("name")
|
||||||
)
|
)
|
||||||
|
|
||||||
# Who is friends with David?
|
# Who is friends with David?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
d.friends.all(), [
|
self.d.friends.all(), [
|
||||||
"Chuck",
|
"Chuck",
|
||||||
],
|
],
|
||||||
attrgetter("name")
|
attrgetter("name")
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add some idols in the direction of field definition
|
def test_recursive_m2m_add_via_related_name(self):
|
||||||
# Anne idolizes Bill and Chuck
|
""" Tests that we can add m2m relations via the related_name attribute """
|
||||||
a.idols.add(b, c)
|
|
||||||
# Bill idolizes Anne right back
|
|
||||||
b.idols.add(a)
|
|
||||||
# David is idolized by Anne and Chuck - add in reverse direction
|
# David is idolized by Anne and Chuck - add in reverse direction
|
||||||
d.stalkers.add(a, c)
|
self.d.stalkers.add(self.a)
|
||||||
|
|
||||||
# Who are Anne's idols?
|
# Who are Anne's idols?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
a.idols.all(), [
|
self.a.idols.all(), [
|
||||||
"Bill",
|
|
||||||
"Chuck",
|
|
||||||
"David",
|
"David",
|
||||||
],
|
],
|
||||||
attrgetter("name"),
|
attrgetter("name"),
|
||||||
|
@ -135,130 +147,49 @@ class RecursiveM2MTests(TestCase):
|
||||||
)
|
)
|
||||||
# Who is stalking Anne?
|
# Who is stalking Anne?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
a.stalkers.all(), [
|
self.a.stalkers.all(), [],
|
||||||
"Bill",
|
|
||||||
],
|
|
||||||
attrgetter("name")
|
attrgetter("name")
|
||||||
)
|
)
|
||||||
# Who are Bill's idols?
|
|
||||||
self.assertQuerysetEqual(
|
def test_recursive_m2m_add_in_both_directions(self):
|
||||||
b.idols.all(), [
|
""" Check that adding the same relation twice results in a single relation """
|
||||||
"Anne",
|
|
||||||
],
|
# Ann idolizes David
|
||||||
attrgetter("name")
|
self.a.idols.add(self.d)
|
||||||
)
|
|
||||||
# Who is stalking Bill?
|
# David is idolized by Anne
|
||||||
self.assertQuerysetEqual(
|
self.d.stalkers.add(self.a)
|
||||||
b.stalkers.all(), [
|
|
||||||
"Anne",
|
|
||||||
],
|
|
||||||
attrgetter("name")
|
|
||||||
)
|
|
||||||
# Who are Chuck's idols?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
c.idols.all(), [
|
|
||||||
"David",
|
|
||||||
],
|
|
||||||
attrgetter("name"),
|
|
||||||
)
|
|
||||||
# Who is stalking Chuck?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
c.stalkers.all(), [
|
|
||||||
"Anne",
|
|
||||||
],
|
|
||||||
attrgetter("name")
|
|
||||||
)
|
|
||||||
# Who are David's idols?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
d.idols.all(), []
|
|
||||||
)
|
|
||||||
# Who is stalking David
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
d.stalkers.all(), [
|
|
||||||
"Anne",
|
|
||||||
"Chuck",
|
|
||||||
],
|
|
||||||
attrgetter("name"),
|
|
||||||
ordered=False
|
|
||||||
)
|
|
||||||
# Bill is already being stalked by Anne - add Anne again, but in the
|
|
||||||
# reverse direction
|
|
||||||
b.stalkers.add(a)
|
|
||||||
# Who are Anne's idols?
|
# Who are Anne's idols?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
a.idols.all(), [
|
self.a.idols.all(), [
|
||||||
"Bill",
|
|
||||||
"Chuck",
|
|
||||||
"David",
|
"David",
|
||||||
],
|
],
|
||||||
attrgetter("name"),
|
attrgetter("name"),
|
||||||
ordered=False
|
ordered=False
|
||||||
)
|
)
|
||||||
# Who is stalking Anne?
|
# As the assertQuerysetEqual uses a set for comparrison,
|
||||||
self.assertQuerysetEqual(
|
# check we've only got David listed once
|
||||||
a.stalkers.all(), [
|
self.assertEqual(self.a.idols.all().count(), 1)
|
||||||
"Bill",
|
|
||||||
],
|
def test_recursive_m2m_related_to_self(self):
|
||||||
attrgetter("name")
|
""" Check the expected behaviour when an instance is related to itself """
|
||||||
)
|
|
||||||
# Who are Bill's idols
|
# Ann idolizes herself
|
||||||
self.assertQuerysetEqual(
|
self.a.idols.add(self.a)
|
||||||
b.idols.all(), [
|
|
||||||
"Anne",
|
|
||||||
],
|
|
||||||
attrgetter("name")
|
|
||||||
)
|
|
||||||
# Who is stalking Bill?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
b.stalkers.all(), [
|
|
||||||
"Anne",
|
|
||||||
],
|
|
||||||
attrgetter("name"),
|
|
||||||
)
|
|
||||||
# Remove Anne from Bill's list of stalkers
|
|
||||||
b.stalkers.remove(a)
|
|
||||||
# Who are Anne's idols?
|
# Who are Anne's idols?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
a.idols.all(), [
|
self.a.idols.all(), [
|
||||||
"Chuck",
|
"Anne",
|
||||||
"David",
|
|
||||||
],
|
],
|
||||||
attrgetter("name"),
|
attrgetter("name"),
|
||||||
ordered=False
|
ordered=False
|
||||||
)
|
)
|
||||||
# Who is stalking Anne?
|
# Who is stalking Anne?
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
a.stalkers.all(), [
|
self.a.stalkers.all(), [
|
||||||
"Bill",
|
|
||||||
],
|
|
||||||
attrgetter("name")
|
|
||||||
)
|
|
||||||
# Who are Bill's idols?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
b.idols.all(), [
|
|
||||||
"Anne",
|
"Anne",
|
||||||
],
|
],
|
||||||
attrgetter("name")
|
attrgetter("name")
|
||||||
)
|
)
|
||||||
# Who is stalking Bill?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
b.stalkers.all(), []
|
|
||||||
)
|
|
||||||
# Clear Anne's group of idols
|
|
||||||
a.idols.clear()
|
|
||||||
# Who are Anne's idols
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
a.idols.all(), []
|
|
||||||
)
|
|
||||||
# Reverse relationships should also be gone
|
|
||||||
# Who is stalking Chuck?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
c.stalkers.all(), []
|
|
||||||
)
|
|
||||||
# Who is friends with David?
|
|
||||||
self.assertQuerysetEqual(
|
|
||||||
d.stalkers.all(), [
|
|
||||||
"Chuck",
|
|
||||||
],
|
|
||||||
attrgetter("name")
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue