Skip to content

Commit

Permalink
Refactored RecursiveM2MTests into smaller pieces, refs #18586.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispbailey authored and timgraham committed Feb 10, 2014
1 parent c5395ee commit 0d98422
Showing 1 changed file with 65 additions and 134 deletions.
199 changes: 65 additions & 134 deletions tests/m2m_recursive/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@


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"
Expand All @@ -33,14 +34,14 @@ def test_recursive_m2m(self):
)
# 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"
],
Expand All @@ -49,20 +50,24 @@ def test_recursive_m2m(self):
)
# 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",
Expand All @@ -72,16 +77,21 @@ def test_recursive_m2m(self):
)
# 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",
],
Expand All @@ -90,175 +100,96 @@ def test_recursive_m2m(self):
)
# 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"),
ordered=False
)
# 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")
)

0 comments on commit 0d98422

Please sign in to comment.