2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from operator import attrgetter
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import (
|
|
|
|
CustomMembership, Employee, Event, Friendship, Group, Ingredient,
|
|
|
|
Invitation, Membership, Person, PersonSelfRefM2M, Recipe, RecipeIngredient,
|
|
|
|
Relationship,
|
|
|
|
)
|
2010-11-02 02:00:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class M2mThroughTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.bob = Person.objects.create(name='Bob')
|
|
|
|
self.jim = Person.objects.create(name='Jim')
|
|
|
|
self.jane = Person.objects.create(name='Jane')
|
|
|
|
self.rock = Group.objects.create(name='Rock')
|
|
|
|
self.roll = Group.objects.create(name='Roll')
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_retrieve_intermediate_items(self):
|
2013-10-19 12:31:38 +00:00
|
|
|
Membership.objects.create(person=self.jim, group=self.rock)
|
|
|
|
Membership.objects.create(person=self.jane, group=self.rock)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
expected = ['Jane', 'Jim']
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
self.rock.members.all(),
|
|
|
|
expected,
|
2010-11-02 02:00:16 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
def test_get_on_intermediate_model(self):
|
|
|
|
Membership.objects.create(person=self.jane, group=self.rock)
|
|
|
|
|
|
|
|
queryset = Membership.objects.get(person=self.jane, group=self.rock)
|
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
repr(queryset),
|
2010-11-02 02:00:16 +00:00
|
|
|
'<Membership: Jane is a member of Rock>'
|
|
|
|
)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
def test_filter_on_intermediate_model(self):
|
|
|
|
Membership.objects.create(person=self.jim, group=self.rock)
|
|
|
|
Membership.objects.create(person=self.jane, group=self.rock)
|
|
|
|
|
|
|
|
queryset = Membership.objects.filter(group=self.rock)
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
'<Membership: Jim is a member of Rock>',
|
|
|
|
'<Membership: Jane is a member of Rock>',
|
|
|
|
]
|
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
queryset,
|
|
|
|
expected
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
def test_cannot_use_add_on_m2m_with_intermediary_model(self):
|
|
|
|
msg = 'Cannot use add() on a ManyToManyField which specifies an intermediary model'
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
self.rock.members.add(self.bob)
|
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.rock.members.all(),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_cannot_use_create_on_m2m_with_intermediary_model(self):
|
|
|
|
msg = 'Cannot use create() on a ManyToManyField which specifies an intermediary model'
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
self.rock.members.create(name='Annie')
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.rock.members.all(),
|
|
|
|
[]
|
2014-02-09 13:54:46 +00:00
|
|
|
)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
def test_cannot_use_remove_on_m2m_with_intermediary_model(self):
|
|
|
|
Membership.objects.create(person=self.jim, group=self.rock)
|
|
|
|
msg = 'Cannot use remove() on a ManyToManyField which specifies an intermediary model'
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
self.rock.members.remove(self.jim)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.rock.members.all(),
|
|
|
|
['Jim', ],
|
|
|
|
attrgetter("name")
|
2014-02-09 13:54:46 +00:00
|
|
|
)
|
2010-11-02 02:00:16 +00:00
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_cannot_use_setattr_on_m2m_with_intermediary_model(self):
|
|
|
|
msg = 'Cannot set values on a ManyToManyField which specifies an intermediary model'
|
|
|
|
members = list(Person.objects.filter(name__in=['Bob', 'Jim']))
|
2010-11-02 02:00:16 +00:00
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
2015-10-08 21:17:10 +00:00
|
|
|
self.rock.members.set(members)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.rock.members.all(),
|
|
|
|
[]
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
def test_clear_removes_all_the_m2m_relationships(self):
|
|
|
|
Membership.objects.create(person=self.jim, group=self.rock)
|
|
|
|
Membership.objects.create(person=self.jane, group=self.rock)
|
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.rock.members.clear()
|
2014-09-24 20:55:16 +00:00
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.rock.members.all(),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_retrieve_reverse_intermediate_items(self):
|
|
|
|
Membership.objects.create(person=self.jim, group=self.rock)
|
|
|
|
Membership.objects.create(person=self.jim, group=self.roll)
|
2014-02-09 13:54:46 +00:00
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
expected = ['Rock', 'Roll']
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
self.jim.group_set.all(),
|
|
|
|
expected,
|
2010-11-02 02:00:16 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_cannot_use_add_on_reverse_m2m_with_intermediary_model(self):
|
|
|
|
msg = 'Cannot use add() on a ManyToManyField which specifies an intermediary model'
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
self.bob.group_set.add(self.bob)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.bob.group_set.all(),
|
|
|
|
[]
|
2014-02-09 13:54:46 +00:00
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_cannot_use_create_on_reverse_m2m_with_intermediary_model(self):
|
|
|
|
msg = 'Cannot use create() on a ManyToManyField which specifies an intermediary model'
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
self.bob.group_set.create(name='Funk')
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.bob.group_set.all(),
|
|
|
|
[]
|
2014-02-09 13:54:46 +00:00
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_cannot_use_remove_on_reverse_m2m_with_intermediary_model(self):
|
|
|
|
Membership.objects.create(person=self.bob, group=self.rock)
|
|
|
|
msg = 'Cannot use remove() on a ManyToManyField which specifies an intermediary model'
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
self.bob.group_set.remove(self.rock)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.bob.group_set.all(),
|
|
|
|
['Rock', ],
|
|
|
|
attrgetter('name')
|
2014-02-09 13:54:46 +00:00
|
|
|
)
|
2010-11-02 02:00:16 +00:00
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_cannot_use_setattr_on_reverse_m2m_with_intermediary_model(self):
|
|
|
|
msg = 'Cannot set values on a ManyToManyField which specifies an intermediary model'
|
|
|
|
members = list(Group.objects.filter(name__in=['Rock', 'Roll']))
|
2010-11-02 02:00:16 +00:00
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
2015-10-08 21:17:10 +00:00
|
|
|
self.bob.group_set.set(members)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.bob.group_set.all(),
|
|
|
|
[]
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
def test_clear_on_reverse_removes_all_the_m2m_relationships(self):
|
|
|
|
Membership.objects.create(person=self.jim, group=self.rock)
|
|
|
|
Membership.objects.create(person=self.jim, group=self.roll)
|
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.jim.group_set.clear()
|
2014-09-24 20:55:16 +00:00
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.jim.group_set.all(),
|
|
|
|
[]
|
|
|
|
)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
def test_query_model_by_attribute_name_of_related_model(self):
|
|
|
|
Membership.objects.create(person=self.jim, group=self.rock)
|
|
|
|
Membership.objects.create(person=self.jane, group=self.rock)
|
|
|
|
Membership.objects.create(person=self.bob, group=self.roll)
|
|
|
|
Membership.objects.create(person=self.jim, group=self.roll)
|
|
|
|
Membership.objects.create(person=self.jane, group=self.roll)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Group.objects.filter(members__name='Bob'),
|
|
|
|
['Roll', ],
|
2010-11-02 02:00:16 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_query_first_model_by_intermediate_model_attribute(self):
|
|
|
|
Membership.objects.create(
|
|
|
|
person=self.jane, group=self.roll,
|
|
|
|
invite_reason="She was just awesome."
|
|
|
|
)
|
|
|
|
Membership.objects.create(
|
|
|
|
person=self.jim, group=self.roll,
|
|
|
|
invite_reason="He is good."
|
|
|
|
)
|
|
|
|
Membership.objects.create(person=self.bob, group=self.roll)
|
|
|
|
|
|
|
|
qs = Group.objects.filter(
|
|
|
|
membership__invite_reason="She was just awesome."
|
|
|
|
)
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
qs,
|
|
|
|
['Roll'],
|
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_query_second_model_by_intermediate_model_attribute(self):
|
|
|
|
Membership.objects.create(
|
|
|
|
person=self.jane, group=self.roll,
|
|
|
|
invite_reason="She was just awesome."
|
|
|
|
)
|
|
|
|
Membership.objects.create(
|
|
|
|
person=self.jim, group=self.roll,
|
|
|
|
invite_reason="He is good."
|
|
|
|
)
|
|
|
|
Membership.objects.create(person=self.bob, group=self.roll)
|
|
|
|
|
|
|
|
qs = Person.objects.filter(
|
|
|
|
membership__invite_reason="She was just awesome."
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
qs,
|
|
|
|
['Jane'],
|
|
|
|
attrgetter("name")
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_query_model_by_related_model_name(self):
|
|
|
|
Membership.objects.create(person=self.jim, group=self.rock)
|
|
|
|
Membership.objects.create(person=self.jane, group=self.rock)
|
|
|
|
Membership.objects.create(person=self.bob, group=self.roll)
|
|
|
|
Membership.objects.create(person=self.jim, group=self.roll)
|
|
|
|
Membership.objects.create(person=self.jane, group=self.roll)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Person.objects.filter(group__name="Rock"),
|
|
|
|
['Jane', 'Jim'],
|
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_query_model_by_custom_related_name(self):
|
2013-10-19 12:31:38 +00:00
|
|
|
CustomMembership.objects.create(person=self.bob, group=self.rock)
|
|
|
|
CustomMembership.objects.create(person=self.jim, group=self.rock)
|
2010-11-02 02:00:16 +00:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
Person.objects.filter(custom__name="Rock"),
|
|
|
|
['Bob', 'Jim'],
|
2010-11-02 02:00:16 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
def test_query_model_by_intermediate_can_return_non_unique_queryset(self):
|
|
|
|
Membership.objects.create(person=self.jim, group=self.rock)
|
|
|
|
Membership.objects.create(
|
|
|
|
person=self.jane, group=self.rock,
|
|
|
|
date_joined=datetime(2006, 1, 1)
|
|
|
|
)
|
|
|
|
Membership.objects.create(
|
|
|
|
person=self.bob, group=self.roll,
|
|
|
|
date_joined=datetime(2004, 1, 1))
|
|
|
|
Membership.objects.create(person=self.jim, group=self.roll)
|
|
|
|
Membership.objects.create(
|
|
|
|
person=self.jane, group=self.roll,
|
|
|
|
date_joined=datetime(2004, 1, 1))
|
|
|
|
|
|
|
|
qs = Person.objects.filter(
|
|
|
|
membership__date_joined__gt=datetime(2004, 1, 1)
|
|
|
|
)
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
qs,
|
|
|
|
['Jane', 'Jim', 'Jim'],
|
2010-11-02 02:00:16 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
2014-09-24 20:55:16 +00:00
|
|
|
|
|
|
|
def test_custom_related_name_forward_empty_qs(self):
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
self.rock.custom_members.all(),
|
|
|
|
[]
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_custom_related_name_reverse_empty_qs(self):
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
self.bob.custom.all(),
|
2010-11-02 02:00:16 +00:00
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_custom_related_name_forward_non_empty_qs(self):
|
|
|
|
CustomMembership.objects.create(person=self.bob, group=self.rock)
|
|
|
|
CustomMembership.objects.create(person=self.jim, group=self.rock)
|
2010-11-02 02:00:16 +00:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
self.rock.custom_members.all(),
|
|
|
|
['Bob', 'Jim'],
|
2010-11-02 02:00:16 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_custom_related_name_reverse_non_empty_qs(self):
|
|
|
|
CustomMembership.objects.create(person=self.bob, group=self.rock)
|
|
|
|
CustomMembership.objects.create(person=self.jim, group=self.rock)
|
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
self.bob.custom.all(),
|
|
|
|
['Rock'],
|
2010-11-02 02:00:16 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_custom_related_name_doesnt_conflict_with_fky_related_name(self):
|
|
|
|
CustomMembership.objects.create(person=self.bob, group=self.rock)
|
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
self.bob.custom_person_related_name.all(),
|
|
|
|
['<CustomMembership: Bob is a member of Rock>']
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
|
|
|
|
2014-02-19 18:01:55 +00:00
|
|
|
def test_through_fields(self):
|
|
|
|
"""
|
|
|
|
Tests that relations with intermediary tables with multiple FKs
|
|
|
|
to the M2M's ``to`` model are possible.
|
|
|
|
"""
|
|
|
|
event = Event.objects.create(title='Rockwhale 2014')
|
|
|
|
Invitation.objects.create(event=event, inviter=self.bob, invitee=self.jim)
|
|
|
|
Invitation.objects.create(event=event, inviter=self.bob, invitee=self.jane)
|
2014-09-24 20:55:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
event.invitees.all(),
|
|
|
|
['Jane', 'Jim'],
|
|
|
|
attrgetter('name')
|
|
|
|
)
|
2014-02-19 18:01:55 +00:00
|
|
|
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
class M2mThroughReferentialTests(TestCase):
|
|
|
|
def test_self_referential_empty_qs(self):
|
|
|
|
tony = PersonSelfRefM2M.objects.create(name="Tony")
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
tony.friends.all(),
|
|
|
|
[]
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
|
|
|
|
2015-12-02 23:55:50 +00:00
|
|
|
def test_self_referential_non_symmetrical_first_side(self):
|
2014-09-24 20:55:16 +00:00
|
|
|
tony = PersonSelfRefM2M.objects.create(name="Tony")
|
|
|
|
chris = PersonSelfRefM2M.objects.create(name="Chris")
|
|
|
|
Friendship.objects.create(
|
|
|
|
first=tony, second=chris, date_friended=datetime.now()
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
tony.friends.all(),
|
|
|
|
['Chris'],
|
2010-11-02 02:00:16 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
2015-12-02 23:55:50 +00:00
|
|
|
def test_self_referential_non_symmetrical_second_side(self):
|
2014-09-24 20:55:16 +00:00
|
|
|
tony = PersonSelfRefM2M.objects.create(name="Tony")
|
|
|
|
chris = PersonSelfRefM2M.objects.create(name="Chris")
|
|
|
|
Friendship.objects.create(
|
|
|
|
first=tony, second=chris, date_friended=datetime.now()
|
|
|
|
)
|
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
chris.friends.all(),
|
|
|
|
[]
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
|
|
|
|
2015-12-02 23:55:50 +00:00
|
|
|
def test_self_referential_non_symmetrical_clear_first_side(self):
|
2014-09-24 20:55:16 +00:00
|
|
|
tony = PersonSelfRefM2M.objects.create(name="Tony")
|
|
|
|
chris = PersonSelfRefM2M.objects.create(name="Chris")
|
|
|
|
Friendship.objects.create(
|
|
|
|
first=tony, second=chris, date_friended=datetime.now()
|
|
|
|
)
|
|
|
|
|
|
|
|
chris.friends.clear()
|
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
chris.friends.all(),
|
|
|
|
[]
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
# Since this isn't a symmetrical relation, Tony's friend link still exists.
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
tony.friends.all(),
|
|
|
|
['Chris'],
|
2010-11-02 02:00:16 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
2015-12-02 23:55:50 +00:00
|
|
|
def test_self_referential_symmetrical(self):
|
2014-09-24 20:55:16 +00:00
|
|
|
tony = PersonSelfRefM2M.objects.create(name="Tony")
|
|
|
|
chris = PersonSelfRefM2M.objects.create(name="Chris")
|
|
|
|
Friendship.objects.create(
|
|
|
|
first=tony, second=chris, date_friended=datetime.now()
|
|
|
|
)
|
|
|
|
Friendship.objects.create(
|
|
|
|
first=chris, second=tony, date_friended=datetime.now()
|
|
|
|
)
|
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
tony.friends.all(),
|
|
|
|
['Chris'],
|
2010-11-02 02:00:16 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
chris.friends.all(),
|
|
|
|
['Tony'],
|
2010-11-02 02:00:16 +00:00
|
|
|
attrgetter("name")
|
|
|
|
)
|
|
|
|
|
2014-09-24 20:55:16 +00:00
|
|
|
def test_through_fields_self_referential(self):
|
|
|
|
john = Employee.objects.create(name='john')
|
|
|
|
peter = Employee.objects.create(name='peter')
|
|
|
|
mary = Employee.objects.create(name='mary')
|
|
|
|
harry = Employee.objects.create(name='harry')
|
|
|
|
|
|
|
|
Relationship.objects.create(source=john, target=peter, another=None)
|
|
|
|
Relationship.objects.create(source=john, target=mary, another=None)
|
|
|
|
Relationship.objects.create(source=john, target=harry, another=peter)
|
|
|
|
|
2010-11-02 02:00:16 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-24 20:55:16 +00:00
|
|
|
john.subordinates.all(),
|
|
|
|
['peter', 'mary', 'harry'],
|
|
|
|
attrgetter('name')
|
2010-11-02 02:00:16 +00:00
|
|
|
)
|
2014-11-16 16:21:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class M2mThroughToFieldsTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.pea = Ingredient.objects.create(iname='pea')
|
|
|
|
self.potato = Ingredient.objects.create(iname='potato')
|
|
|
|
self.tomato = Ingredient.objects.create(iname='tomato')
|
|
|
|
self.curry = Recipe.objects.create(rname='curry')
|
|
|
|
RecipeIngredient.objects.create(recipe=self.curry, ingredient=self.potato)
|
|
|
|
RecipeIngredient.objects.create(recipe=self.curry, ingredient=self.pea)
|
|
|
|
RecipeIngredient.objects.create(recipe=self.curry, ingredient=self.tomato)
|
|
|
|
|
|
|
|
def test_retrieval(self):
|
|
|
|
# Forward retrieval
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.curry.ingredients.all(),
|
|
|
|
[self.pea, self.potato, self.tomato], lambda x: x
|
|
|
|
)
|
|
|
|
# Backward retrieval
|
|
|
|
self.assertEqual(self.tomato.recipes.get(), self.curry)
|
|
|
|
|
|
|
|
def test_choices(self):
|
|
|
|
field = Recipe._meta.get_field('ingredients')
|
|
|
|
self.assertEqual(
|
|
|
|
[choice[0] for choice in field.get_choices(include_blank=False)],
|
|
|
|
['pea', 'potato', 'tomato']
|
|
|
|
)
|