From 8654c6a7329c06133b6c4b32f05e606132a338e5 Mon Sep 17 00:00:00 2001 From: Michael Blatherwick Date: Sun, 22 Mar 2015 15:42:59 +0000 Subject: [PATCH] Refs #18586 -- Split up tests.m2m_through_regress --- tests/m2m_through_regress/tests.py | 108 +++++++++++++++-------------- 1 file changed, 57 insertions(+), 51 deletions(-) diff --git a/tests/m2m_through_regress/tests.py b/tests/m2m_through_regress/tests.py index f283c658b8..434a84c0d6 100644 --- a/tests/m2m_through_regress/tests.py +++ b/tests/m2m_through_regress/tests.py @@ -11,67 +11,97 @@ from .models import ( class M2MThroughTestCase(TestCase): + @classmethod + def setUpTestData(cls): + cls.bob = Person.objects.create(name="Bob") + cls.jim = Person.objects.create(name="Jim") - def test_everything(self): - bob = Person.objects.create(name="Bob") - jim = Person.objects.create(name="Jim") + cls.rock = Group.objects.create(name="Rock") + cls.roll = Group.objects.create(name="Roll") - rock = Group.objects.create(name="Rock") - roll = Group.objects.create(name="Roll") + cls.frank = User.objects.create_user("frank", "frank@example.com", "password") + cls.jane = User.objects.create_user("jane", "jane@example.com", "password") - frank = User.objects.create_user("frank", "frank@example.com", "password") - jane = User.objects.create_user("jane", "jane@example.com", "password") + # 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) - Membership.objects.create(person=bob, group=rock) - Membership.objects.create(person=bob, group=roll) - Membership.objects.create(person=jim, group=rock) + # 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) + def test_retrieve_reverse_m2m_items(self): self.assertQuerysetEqual( - bob.group_set.all(), [ + self.bob.group_set.all(), [ "", "", ], ordered=False ) + def test_retrieve_forward_m2m_items(self): self.assertQuerysetEqual( - roll.members.all(), [ + self.roll.members.all(), [ "", ] ) - self.assertRaises(AttributeError, setattr, bob, "group_set", []) - self.assertRaises(AttributeError, setattr, roll, "members", []) + def test_cannot_use_setattr_on_reverse_m2m_with_intermediary_model(self): + self.assertRaises(AttributeError, setattr, self.bob, "group_set", []) - self.assertRaises(AttributeError, rock.members.create, name="Anne") - self.assertRaises(AttributeError, bob.group_set.create, name="Funk") + def test_cannot_use_setattr_on_forward_m2m_with_intermediary_model(self): + self.assertRaises(AttributeError, setattr, self.roll, "members", []) - UserMembership.objects.create(user=frank, group=rock) - UserMembership.objects.create(user=frank, group=roll) - UserMembership.objects.create(user=jane, group=rock) + def test_cannot_use_create_on_m2m_with_intermediary_model(self): + self.assertRaises(AttributeError, self.rock.members.create, name="Anne") + def test_cannot_use_create_on_reverse_m2m_with_intermediary_model(self): + self.assertRaises(AttributeError, self.bob.group_set.create, name="Funk") + + def test_retrieve_reverse_m2m_items_via_custom_id_intermediary(self): self.assertQuerysetEqual( - frank.group_set.all(), [ + self.frank.group_set.all(), [ "", "", ], ordered=False ) + def test_retrieve_forward_m2m_items_via_custom_id_intermediary(self): self.assertQuerysetEqual( - roll.user_members.all(), [ + self.roll.user_members.all(), [ "", ] ) + def test_join_trimming_forwards(self): + "Check that we don't involve too many copies of the intermediate table when doing a join. Refs #8046, #8254" + self.assertQuerysetEqual( + self.rock.members.filter(membership__price=50), [ + "", + ] + ) + + def test_join_trimming_reverse(self): + self.assertQuerysetEqual( + self.bob.group_set.filter(membership__price=50), [ + "", + ] + ) + + +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" - - p = Person.objects.create(name="Bob") - g = Group.objects.create(name="Roll") - m = Membership.objects.create(person=p, group=g) - - pks = {"p_pk": p.pk, "g_pk": g.pk, "m_pk": m.pk} + pks = {"p_pk": self.bob.pk, "g_pk": self.roll.pk, "m_pk": self.bob_roll.pk} out = StringIO() management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out) @@ -97,30 +127,6 @@ class M2MThroughTestCase(TestCase): """.strip() % pks) - def test_join_trimming(self): - "Check that we don't involve too many copies of the intermediate table when doing a join. Refs #8046, #8254" - bob = Person.objects.create(name="Bob") - jim = Person.objects.create(name="Jim") - - rock = Group.objects.create(name="Rock") - roll = Group.objects.create(name="Roll") - - Membership.objects.create(person=bob, group=rock) - Membership.objects.create(person=jim, group=rock, price=50) - Membership.objects.create(person=bob, group=roll, price=50) - - self.assertQuerysetEqual( - rock.members.filter(membership__price=50), [ - "", - ] - ) - - self.assertQuerysetEqual( - bob.group_set.filter(membership__price=50), [ - "", - ] - ) - class ToFieldThroughTests(TestCase): def setUp(self):