2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
from operator import attrgetter
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2011-10-13 18:04:12 +00:00
|
|
|
from .models import Person
|
2010-10-09 16:26:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RecursiveM2MTests(TestCase):
|
2014-02-10 10:21:42 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.a, self.b, self.c, self.d = [
|
2010-10-09 16:26:48 +00:00
|
|
|
Person.objects.create(name=name)
|
|
|
|
for name in ["Anne", "Bill", "Chuck", "David"]
|
|
|
|
]
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Anne is friends with Bill and Chuck
|
2014-02-10 10:21:42 +00:00
|
|
|
self.a.friends.add(self.b, self.c)
|
2010-10-09 16:26:48 +00:00
|
|
|
|
|
|
|
# David is friends with Anne and Chuck - add in reverse direction
|
2014-02-10 10:21:42 +00:00
|
|
|
self.d.friends.add(self.a, self.c)
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2014-02-10 10:21:42 +00:00
|
|
|
def test_recursive_m2m_all(self):
|
|
|
|
""" Test that m2m relations are reported correctly """
|
2010-10-09 16:26:48 +00:00
|
|
|
# Who is friends with Anne?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.a.friends.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"Bill",
|
|
|
|
"Chuck",
|
|
|
|
"David"
|
|
|
|
],
|
2012-12-13 11:33:11 +00:00
|
|
|
attrgetter("name"),
|
|
|
|
ordered=False
|
2010-10-09 16:26:48 +00:00
|
|
|
)
|
|
|
|
# Who is friends with Bill?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.b.friends.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"Anne",
|
|
|
|
],
|
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
# Who is friends with Chuck?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.c.friends.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"Anne",
|
|
|
|
"David"
|
|
|
|
],
|
2012-12-13 11:33:11 +00:00
|
|
|
attrgetter("name"),
|
|
|
|
ordered=False
|
2010-10-09 16:26:48 +00:00
|
|
|
)
|
|
|
|
# Who is friends with David?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.d.friends.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"Anne",
|
|
|
|
"Chuck",
|
|
|
|
],
|
2012-12-13 11:33:11 +00:00
|
|
|
attrgetter("name"),
|
|
|
|
ordered=False
|
2010-10-09 16:26:48 +00:00
|
|
|
)
|
2014-02-10 10:21:42 +00:00
|
|
|
|
|
|
|
def test_recursive_m2m_reverse_add(self):
|
|
|
|
""" Test reverse m2m relation is consistent """
|
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Bill is already friends with Anne - add Anne again, but in the
|
|
|
|
# reverse direction
|
2014-02-10 10:21:42 +00:00
|
|
|
self.b.friends.add(self.a)
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Who is friends with Anne?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.a.friends.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"Bill",
|
|
|
|
"Chuck",
|
|
|
|
"David",
|
|
|
|
],
|
2012-12-13 11:33:11 +00:00
|
|
|
attrgetter("name"),
|
|
|
|
ordered=False
|
2010-10-09 16:26:48 +00:00
|
|
|
)
|
|
|
|
# Who is friends with Bill?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.b.friends.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"Anne",
|
|
|
|
],
|
|
|
|
attrgetter("name")
|
|
|
|
)
|
2014-02-10 10:21:42 +00:00
|
|
|
|
|
|
|
def test_recursive_m2m_remove(self):
|
|
|
|
""" Test that we can remove items from an m2m relationship """
|
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Remove Anne from Bill's friends
|
2014-02-10 10:21:42 +00:00
|
|
|
self.b.friends.remove(self.a)
|
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Who is friends with Anne?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.a.friends.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"Chuck",
|
|
|
|
"David",
|
|
|
|
],
|
2012-12-13 11:33:11 +00:00
|
|
|
attrgetter("name"),
|
|
|
|
ordered=False
|
2010-10-09 16:26:48 +00:00
|
|
|
)
|
|
|
|
# Who is friends with Bill?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.b.friends.all(), []
|
2010-10-09 16:26:48 +00:00
|
|
|
)
|
|
|
|
|
2014-02-10 10:21:42 +00:00
|
|
|
def test_recursive_m2m_clear(self):
|
|
|
|
""" Tests the clear method works as expected on m2m fields """
|
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Clear Anne's group of friends
|
2014-02-10 10:21:42 +00:00
|
|
|
self.a.friends.clear()
|
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Who is friends with Anne?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.a.friends.all(), []
|
2010-10-09 16:26:48 +00:00
|
|
|
)
|
2014-02-10 10:21:42 +00:00
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Reverse relationships should also be gone
|
|
|
|
# Who is friends with Chuck?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.c.friends.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"David",
|
|
|
|
],
|
|
|
|
attrgetter("name")
|
|
|
|
)
|
2014-02-10 10:21:42 +00:00
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Who is friends with David?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.d.friends.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"Chuck",
|
|
|
|
],
|
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
2014-02-10 10:21:42 +00:00
|
|
|
def test_recursive_m2m_add_via_related_name(self):
|
|
|
|
""" Tests that we can add m2m relations via the related_name attribute """
|
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# David is idolized by Anne and Chuck - add in reverse direction
|
2014-02-10 10:21:42 +00:00
|
|
|
self.d.stalkers.add(self.a)
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Who are Anne's idols?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.a.idols.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"David",
|
|
|
|
],
|
2012-12-13 11:33:11 +00:00
|
|
|
attrgetter("name"),
|
|
|
|
ordered=False
|
2010-10-09 16:26:48 +00:00
|
|
|
)
|
|
|
|
# Who is stalking Anne?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.a.stalkers.all(), [],
|
2010-10-09 16:26:48 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
2014-02-10 10:21:42 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Who are Anne's idols?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.a.idols.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"David",
|
|
|
|
],
|
2012-12-13 11:33:11 +00:00
|
|
|
attrgetter("name"),
|
|
|
|
ordered=False
|
2010-10-09 16:26:48 +00:00
|
|
|
)
|
2014-03-02 14:25:53 +00:00
|
|
|
# As the assertQuerysetEqual uses a set for comparison,
|
2014-02-10 10:21:42 +00:00
|
|
|
# check we've only got David listed once
|
|
|
|
self.assertEqual(self.a.idols.all().count(), 1)
|
|
|
|
|
|
|
|
def test_recursive_m2m_related_to_self(self):
|
2014-03-02 14:25:53 +00:00
|
|
|
""" Check the expected behavior when an instance is related to itself """
|
2014-02-10 10:21:42 +00:00
|
|
|
|
|
|
|
# Ann idolizes herself
|
|
|
|
self.a.idols.add(self.a)
|
|
|
|
|
2010-10-09 16:26:48 +00:00
|
|
|
# Who are Anne's idols?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.a.idols.all(), [
|
|
|
|
"Anne",
|
2010-10-09 16:26:48 +00:00
|
|
|
],
|
2012-12-13 11:33:11 +00:00
|
|
|
attrgetter("name"),
|
|
|
|
ordered=False
|
2010-10-09 16:26:48 +00:00
|
|
|
)
|
|
|
|
# Who is stalking Anne?
|
|
|
|
self.assertQuerysetEqual(
|
2014-02-10 10:21:42 +00:00
|
|
|
self.a.stalkers.all(), [
|
2010-10-09 16:26:48 +00:00
|
|
|
"Anne",
|
|
|
|
],
|
|
|
|
attrgetter("name")
|
|
|
|
)
|