Refactored RecursiveM2MTests into smaller pieces, refs #18586.

This commit is contained in:
Chris Bailey 2014-02-10 05:21:42 -05:00 committed by Tim Graham
parent c5395eef76
commit 0d98422b13
1 changed files with 65 additions and 134 deletions

View File

@ -8,22 +8,23 @@ from .models import Person
class RecursiveM2MTests(TestCase):
def test_recursive_m2m(self):
a, b, c, d = [
def setUp(self):
self.a, self.b, self.c, self.d = [
Person.objects.create(name=name)
for name in ["Anne", "Bill", "Chuck", "David"]
]
# Add some friends in the direction of field definition
# 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
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?
self.assertQuerysetEqual(
a.friends.all(), [
self.a.friends.all(), [
"Bill",
"Chuck",
"David"
@ -33,14 +34,14 @@ class RecursiveM2MTests(TestCase):
)
# Who is friends with Bill?
self.assertQuerysetEqual(
b.friends.all(), [
self.b.friends.all(), [
"Anne",
],
attrgetter("name")
)
# Who is friends with Chuck?
self.assertQuerysetEqual(
c.friends.all(), [
self.c.friends.all(), [
"Anne",
"David"
],
@ -49,20 +50,24 @@ class RecursiveM2MTests(TestCase):
)
# Who is friends with David?
self.assertQuerysetEqual(
d.friends.all(), [
self.d.friends.all(), [
"Anne",
"Chuck",
],
attrgetter("name"),
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
# reverse direction
b.friends.add(a)
self.b.friends.add(self.a)
# Who is friends with Anne?
self.assertQuerysetEqual(
a.friends.all(), [
self.a.friends.all(), [
"Bill",
"Chuck",
"David",
@ -72,16 +77,21 @@ class RecursiveM2MTests(TestCase):
)
# Who is friends with Bill?
self.assertQuerysetEqual(
b.friends.all(), [
self.b.friends.all(), [
"Anne",
],
attrgetter("name")
)
def test_recursive_m2m_remove(self):
""" Test that we can remove items from an m2m relationship """
# Remove Anne from Bill's friends
b.friends.remove(a)
self.b.friends.remove(self.a)
# Who is friends with Anne?
self.assertQuerysetEqual(
a.friends.all(), [
self.a.friends.all(), [
"Chuck",
"David",
],
@ -90,44 +100,46 @@ class RecursiveM2MTests(TestCase):
)
# Who is friends with Bill?
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
a.friends.clear()
self.a.friends.clear()
# Who is friends with Anne?
self.assertQuerysetEqual(
a.friends.all(), []
self.a.friends.all(), []
)
# Reverse relationships should also be gone
# Who is friends with Chuck?
self.assertQuerysetEqual(
c.friends.all(), [
self.c.friends.all(), [
"David",
],
attrgetter("name")
)
# Who is friends with David?
self.assertQuerysetEqual(
d.friends.all(), [
self.d.friends.all(), [
"Chuck",
],
attrgetter("name")
)
# Add some idols in the direction of field definition
# Anne idolizes Bill and Chuck
a.idols.add(b, c)
# Bill idolizes Anne right back
b.idols.add(a)
def test_recursive_m2m_add_via_related_name(self):
""" Tests that we can add m2m relations via the related_name attribute """
# 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?
self.assertQuerysetEqual(
a.idols.all(), [
"Bill",
"Chuck",
self.a.idols.all(), [
"David",
],
attrgetter("name"),
@ -135,130 +147,49 @@ class RecursiveM2MTests(TestCase):
)
# Who is stalking Anne?
self.assertQuerysetEqual(
a.stalkers.all(), [
"Bill",
],
self.a.stalkers.all(), [],
attrgetter("name")
)
# Who are Bill's idols?
self.assertQuerysetEqual(
b.idols.all(), [
"Anne",
],
attrgetter("name")
)
# Who is stalking Bill?
self.assertQuerysetEqual(
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)
def test_recursive_m2m_add_in_both_directions(self):
""" Check that adding the same relation twice results in a single relation """
# Ann idolizes David
self.a.idols.add(self.d)
# David is idolized by Anne
self.d.stalkers.add(self.a)
# Who are Anne's idols?
self.assertQuerysetEqual(
a.idols.all(), [
"Bill",
"Chuck",
self.a.idols.all(), [
"David",
],
attrgetter("name"),
ordered=False
)
# Who is stalking Anne?
self.assertQuerysetEqual(
a.stalkers.all(), [
"Bill",
],
attrgetter("name")
)
# Who are Bill's idols
self.assertQuerysetEqual(
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)
# As the assertQuerysetEqual uses a set for comparrison,
# check we've only got David listed once
self.assertEqual(self.a.idols.all().count(), 1)
def test_recursive_m2m_related_to_self(self):
""" Check the expected behaviour when an instance is related to itself """
# Ann idolizes herself
self.a.idols.add(self.a)
# Who are Anne's idols?
self.assertQuerysetEqual(
a.idols.all(), [
"Chuck",
"David",
self.a.idols.all(), [
"Anne",
],
attrgetter("name"),
ordered=False
)
# Who is stalking Anne?
self.assertQuerysetEqual(
a.stalkers.all(), [
"Bill",
],
attrgetter("name")
)
# Who are Bill's idols?
self.assertQuerysetEqual(
b.idols.all(), [
self.a.stalkers.all(), [
"Anne",
],
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")
)