2017-01-07 11:11:46 +00:00
|
|
|
from io import StringIO
|
|
|
|
|
2010-06-07 02:11:58 +00:00
|
|
|
from django.contrib.auth.models import User
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.core import management
|
2010-06-07 02:11:58 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import (
|
|
|
|
Car, CarDriver, Driver, Group, Membership, Person, UserMembership,
|
|
|
|
)
|
2010-06-07 02:11:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class M2MThroughTestCase(TestCase):
|
2015-03-22 15:42:59 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.bob = Person.objects.create(name="Bob")
|
|
|
|
cls.jim = Person.objects.create(name="Jim")
|
2013-06-04 06:09:29 +00:00
|
|
|
|
2015-03-22 15:42:59 +00:00
|
|
|
cls.rock = Group.objects.create(name="Rock")
|
|
|
|
cls.roll = Group.objects.create(name="Roll")
|
2010-06-07 02:11:58 +00:00
|
|
|
|
2015-03-22 15:42:59 +00:00
|
|
|
cls.frank = User.objects.create_user("frank", "frank@example.com", "password")
|
|
|
|
cls.jane = User.objects.create_user("jane", "jane@example.com", "password")
|
2010-06-07 02:11:58 +00:00
|
|
|
|
2015-03-22 15:42:59 +00:00
|
|
|
# normal intermediate model
|
|
|
|
cls.bob_rock = Membership.objects.create(person=cls.bob, group=cls.rock)
|
|
|
|
cls.bob_roll = Membership.objects.create(person=cls.bob, group=cls.roll, price=50)
|
|
|
|
cls.jim_rock = Membership.objects.create(person=cls.jim, group=cls.rock, price=50)
|
2010-06-07 02:11:58 +00:00
|
|
|
|
2015-03-22 15:42:59 +00:00
|
|
|
# intermediate model with custom id column
|
|
|
|
cls.frank_rock = UserMembership.objects.create(user=cls.frank, group=cls.rock)
|
|
|
|
cls.frank_roll = UserMembership.objects.create(user=cls.frank, group=cls.roll)
|
|
|
|
cls.jane_rock = UserMembership.objects.create(user=cls.jane, group=cls.rock)
|
2010-06-07 02:11:58 +00:00
|
|
|
|
2015-03-22 15:42:59 +00:00
|
|
|
def test_retrieve_reverse_m2m_items(self):
|
2010-06-07 02:11:58 +00:00
|
|
|
self.assertQuerysetEqual(
|
2015-03-22 15:42:59 +00:00
|
|
|
self.bob.group_set.all(), [
|
2010-06-07 02:11:58 +00:00
|
|
|
"<Group: Rock>",
|
|
|
|
"<Group: Roll>",
|
2012-12-13 11:33:11 +00:00
|
|
|
],
|
|
|
|
ordered=False
|
2010-06-07 02:11:58 +00:00
|
|
|
)
|
|
|
|
|
2015-03-22 15:42:59 +00:00
|
|
|
def test_retrieve_forward_m2m_items(self):
|
2010-06-07 02:11:58 +00:00
|
|
|
self.assertQuerysetEqual(
|
2015-03-22 15:42:59 +00:00
|
|
|
self.roll.members.all(), [
|
2010-06-07 02:11:58 +00:00
|
|
|
"<Person: Bob>",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2015-03-22 15:42:59 +00:00
|
|
|
def test_retrieve_reverse_m2m_items_via_custom_id_intermediary(self):
|
2010-06-07 02:11:58 +00:00
|
|
|
self.assertQuerysetEqual(
|
2015-03-22 15:42:59 +00:00
|
|
|
self.frank.group_set.all(), [
|
2010-06-07 02:11:58 +00:00
|
|
|
"<Group: Rock>",
|
|
|
|
"<Group: Roll>",
|
2012-12-13 11:33:11 +00:00
|
|
|
],
|
|
|
|
ordered=False
|
2010-06-07 02:11:58 +00:00
|
|
|
)
|
|
|
|
|
2015-03-22 15:42:59 +00:00
|
|
|
def test_retrieve_forward_m2m_items_via_custom_id_intermediary(self):
|
2010-06-07 02:11:58 +00:00
|
|
|
self.assertQuerysetEqual(
|
2015-03-22 15:42:59 +00:00
|
|
|
self.roll.user_members.all(), [
|
2010-06-07 02:11:58 +00:00
|
|
|
"<User: frank>",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2015-03-22 15:42:59 +00:00
|
|
|
def test_join_trimming_forwards(self):
|
2016-10-27 07:53:39 +00:00
|
|
|
"""
|
|
|
|
Too many copies of the intermediate table aren't involved when doing a
|
|
|
|
join (#8046, #8254).
|
|
|
|
"""
|
2015-03-22 15:42:59 +00:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.rock.members.filter(membership__price=50), [
|
|
|
|
"<Person: Jim>",
|
|
|
|
]
|
|
|
|
)
|
2010-06-07 02:11:58 +00:00
|
|
|
|
2015-03-22 15:42:59 +00:00
|
|
|
def test_join_trimming_reverse(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.bob.group_set.filter(membership__price=50), [
|
|
|
|
"<Group: Roll>",
|
|
|
|
]
|
|
|
|
)
|
2010-06-23 20:03:41 +00:00
|
|
|
|
2015-03-22 15:42:59 +00:00
|
|
|
|
|
|
|
class M2MThroughSerializationTestCase(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.bob = Person.objects.create(name="Bob")
|
|
|
|
cls.roll = Group.objects.create(name="Roll")
|
|
|
|
cls.bob_roll = Membership.objects.create(person=cls.bob, group=cls.roll)
|
|
|
|
|
|
|
|
def test_serialization(self):
|
|
|
|
"m2m-through models aren't serialized as m2m fields. Refs #8134"
|
|
|
|
pks = {"p_pk": self.bob.pk, "g_pk": self.roll.pk, "m_pk": self.bob_roll.pk}
|
2010-06-07 02:11:58 +00:00
|
|
|
|
2012-08-11 16:56:14 +00:00
|
|
|
out = StringIO()
|
2010-06-07 02:11:58 +00:00
|
|
|
management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertJSONEqual(
|
|
|
|
out.getvalue().strip(),
|
|
|
|
'[{"pk": %(m_pk)s, "model": "m2m_through_regress.membership", "fields": {"person": %(p_pk)s, "price": '
|
|
|
|
'100, "group": %(g_pk)s}}, {"pk": %(p_pk)s, "model": "m2m_through_regress.person", "fields": {"name": '
|
|
|
|
'"Bob"}}, {"pk": %(g_pk)s, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]'
|
|
|
|
% pks
|
|
|
|
)
|
2010-06-07 02:11:58 +00:00
|
|
|
|
2012-08-11 16:56:14 +00:00
|
|
|
out = StringIO()
|
2016-04-08 02:04:45 +00:00
|
|
|
management.call_command("dumpdata", "m2m_through_regress", format="xml", indent=2, stdout=out)
|
2012-09-28 07:20:01 +00:00
|
|
|
self.assertXMLEqual(out.getvalue().strip(), """
|
2010-06-07 02:11:58 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<django-objects version="1.0">
|
2010-06-23 20:03:41 +00:00
|
|
|
<object pk="%(m_pk)s" model="m2m_through_regress.membership">
|
|
|
|
<field to="m2m_through_regress.person" name="person" rel="ManyToOneRel">%(p_pk)s</field>
|
|
|
|
<field to="m2m_through_regress.group" name="group" rel="ManyToOneRel">%(g_pk)s</field>
|
2010-06-07 02:11:58 +00:00
|
|
|
<field type="IntegerField" name="price">100</field>
|
|
|
|
</object>
|
2010-06-23 20:03:41 +00:00
|
|
|
<object pk="%(p_pk)s" model="m2m_through_regress.person">
|
2010-06-07 02:11:58 +00:00
|
|
|
<field type="CharField" name="name">Bob</field>
|
|
|
|
</object>
|
2010-06-23 20:03:41 +00:00
|
|
|
<object pk="%(g_pk)s" model="m2m_through_regress.group">
|
2010-06-07 02:11:58 +00:00
|
|
|
<field type="CharField" name="name">Roll</field>
|
|
|
|
</object>
|
|
|
|
</django-objects>
|
2010-06-23 20:03:41 +00:00
|
|
|
""".strip() % pks)
|
2010-06-07 02:11:58 +00:00
|
|
|
|
2011-01-26 19:10:08 +00:00
|
|
|
|
|
|
|
class ToFieldThroughTests(TestCase):
|
2018-11-24 01:59:38 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.car = Car.objects.create(make="Toyota")
|
|
|
|
cls.driver = Driver.objects.create(name="Ryan Briscoe")
|
|
|
|
CarDriver.objects.create(car=cls.car, driver=cls.driver)
|
2012-10-28 14:47:07 +00:00
|
|
|
# We are testing if wrong objects get deleted due to using wrong
|
|
|
|
# field value in m2m queries. So, it is essential that the pk
|
|
|
|
# numberings do not match.
|
|
|
|
# Create one intentionally unused driver to mix up the autonumbering
|
2018-11-24 01:59:38 +00:00
|
|
|
cls.unused_driver = Driver.objects.create(name="Barney Gumble")
|
2012-10-28 14:47:07 +00:00
|
|
|
# And two intentionally unused cars.
|
2018-11-24 01:59:38 +00:00
|
|
|
cls.unused_car1 = Car.objects.create(make="Trabant")
|
|
|
|
cls.unused_car2 = Car.objects.create(make="Wartburg")
|
2011-01-26 19:10:08 +00:00
|
|
|
|
|
|
|
def test_to_field(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.car.drivers.all(),
|
|
|
|
["<Driver: Ryan Briscoe>"]
|
2012-10-28 14:47:07 +00:00
|
|
|
)
|
2011-01-26 19:10:08 +00:00
|
|
|
|
|
|
|
def test_to_field_reverse(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.driver.car_set.all(),
|
|
|
|
["<Car: Toyota>"]
|
2012-10-28 14:47:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_to_field_clear_reverse(self):
|
|
|
|
self.driver.car_set.clear()
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-26 19:15:03 +00:00
|
|
|
self.driver.car_set.all(), [])
|
2012-10-28 14:47:07 +00:00
|
|
|
|
|
|
|
def test_to_field_clear(self):
|
|
|
|
self.car.drivers.clear()
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-26 19:15:03 +00:00
|
|
|
self.car.drivers.all(), [])
|
2012-10-28 14:47:07 +00:00
|
|
|
|
|
|
|
# Low level tests for _add_items and _remove_items. We test these methods
|
|
|
|
# because .add/.remove aren't available for m2m fields with through, but
|
|
|
|
# through is the only way to set to_field currently. We do want to make
|
|
|
|
# sure these methods are ready if the ability to use .add or .remove with
|
|
|
|
# to_field relations is added some day.
|
|
|
|
def test_add(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.car.drivers.all(),
|
|
|
|
["<Driver: Ryan Briscoe>"]
|
|
|
|
)
|
|
|
|
# Yikes - barney is going to drive...
|
|
|
|
self.car.drivers._add_items('car', 'driver', self.unused_driver)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.car.drivers.all(),
|
2012-11-24 13:52:00 +00:00
|
|
|
["<Driver: Barney Gumble>", "<Driver: Ryan Briscoe>"]
|
2012-10-28 14:47:07 +00:00
|
|
|
)
|
|
|
|
|
2016-12-23 20:32:30 +00:00
|
|
|
def test_m2m_relations_unusable_on_null_to_field(self):
|
2016-12-23 13:28:18 +00:00
|
|
|
nullcar = Car(make=None)
|
|
|
|
msg = (
|
2016-12-21 00:13:12 +00:00
|
|
|
'"<Car: None>" needs to have a value for field "make" before this '
|
2016-12-23 13:28:18 +00:00
|
|
|
'many-to-many relationship can be used.'
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
nullcar.drivers.all()
|
|
|
|
|
2016-12-23 20:32:30 +00:00
|
|
|
def test_m2m_relations_unusable_on_null_pk_obj(self):
|
2016-12-23 13:28:18 +00:00
|
|
|
msg = (
|
|
|
|
"'Car' instance needs to have a primary key value before a "
|
|
|
|
"many-to-many relationship can be used."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
Car(make='Ford').drivers.all()
|
2012-10-28 14:47:07 +00:00
|
|
|
|
|
|
|
def test_add_related_null(self):
|
|
|
|
nulldriver = Driver.objects.create(name=None)
|
2016-12-23 13:28:18 +00:00
|
|
|
msg = 'Cannot add "<Driver: None>": the value for field "driver" is None'
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2012-10-28 14:47:07 +00:00
|
|
|
self.car.drivers._add_items('car', 'driver', nulldriver)
|
|
|
|
|
|
|
|
def test_add_reverse(self):
|
|
|
|
car2 = Car.objects.create(make="Honda")
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.driver.car_set.all(),
|
|
|
|
["<Car: Toyota>"]
|
|
|
|
)
|
|
|
|
self.driver.car_set._add_items('driver', 'car', car2)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.driver.car_set.all(),
|
2012-12-13 11:33:11 +00:00
|
|
|
["<Car: Toyota>", "<Car: Honda>"],
|
|
|
|
ordered=False
|
2012-10-28 14:47:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_add_null_reverse(self):
|
|
|
|
nullcar = Car.objects.create(make=None)
|
2016-12-23 13:28:18 +00:00
|
|
|
msg = 'Cannot add "<Car: None>": the value for field "car" is None'
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2012-10-28 14:47:07 +00:00
|
|
|
self.driver.car_set._add_items('driver', 'car', nullcar)
|
|
|
|
|
|
|
|
def test_add_null_reverse_related(self):
|
|
|
|
nulldriver = Driver.objects.create(name=None)
|
2016-12-23 13:28:18 +00:00
|
|
|
msg = (
|
2016-12-21 00:13:12 +00:00
|
|
|
'"<Driver: None>" needs to have a value for field "name" before '
|
2016-12-23 13:28:18 +00:00
|
|
|
'this many-to-many relationship can be used.'
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2012-10-28 14:47:07 +00:00
|
|
|
nulldriver.car_set._add_items('driver', 'car', self.car)
|
|
|
|
|
|
|
|
def test_remove(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.car.drivers.all(),
|
|
|
|
["<Driver: Ryan Briscoe>"]
|
|
|
|
)
|
|
|
|
self.car.drivers._remove_items('car', 'driver', self.driver)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-26 19:15:03 +00:00
|
|
|
self.car.drivers.all(), [])
|
2012-10-28 14:47:07 +00:00
|
|
|
|
|
|
|
def test_remove_reverse(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.driver.car_set.all(),
|
|
|
|
["<Car: Toyota>"]
|
|
|
|
)
|
|
|
|
self.driver.car_set._remove_items('driver', 'car', self.car)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-26 19:15:03 +00:00
|
|
|
self.driver.car_set.all(), [])
|
2012-10-28 14:47:07 +00:00
|
|
|
|
2011-01-26 19:10:08 +00:00
|
|
|
|
2010-06-07 02:11:58 +00:00
|
|
|
class ThroughLoadDataTestCase(TestCase):
|
|
|
|
fixtures = ["m2m_through"]
|
|
|
|
|
|
|
|
def test_sequence_creation(self):
|
2015-09-11 23:33:12 +00:00
|
|
|
"""
|
|
|
|
Sequences on an m2m_through are created for the through model, not a
|
|
|
|
phantom auto-generated m2m table (#11107).
|
|
|
|
"""
|
2012-08-11 16:56:14 +00:00
|
|
|
out = StringIO()
|
2010-06-07 02:11:58 +00:00
|
|
|
management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
|
2015-09-11 23:33:12 +00:00
|
|
|
self.assertJSONEqual(
|
|
|
|
out.getvalue().strip(),
|
|
|
|
'[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user"'
|
|
|
|
': 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, '
|
|
|
|
'"model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]'
|
|
|
|
)
|