From 1a6da249c3a9bd353928f664bc6bd3f582b5eba1 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 7 Jun 2010 02:11:58 +0000 Subject: [PATCH] Fixed #13700 -- Converted m2m_through_regress tests to unittests, removing another flush call. Thanks to Alex Gaynor. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13324 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../m2m_through_regress/models.py | 158 +----------------- .../m2m_through_regress/tests.py | 126 ++++++++++++++ 2 files changed, 128 insertions(+), 156 deletions(-) create mode 100644 tests/regressiontests/m2m_through_regress/tests.py diff --git a/tests/regressiontests/m2m_through_regress/models.py b/tests/regressiontests/m2m_through_regress/models.py index 56aecd6975..ec87985765 100644 --- a/tests/regressiontests/m2m_through_regress/models.py +++ b/tests/regressiontests/m2m_through_regress/models.py @@ -1,8 +1,10 @@ from datetime import datetime + from django.contrib.auth.models import User from django.core import management from django.db import models + # Forward declared intermediate model class Membership(models.Model): person = models.ForeignKey('Person') @@ -51,159 +53,3 @@ class Through(ThroughBase): class B(models.Model): b_text = models.CharField(max_length=20) a_list = models.ManyToManyField(A, through=Through) - - -__test__ = {'API_TESTS':""" -# Create some dummy data ->>> bob = Person.objects.create(name='Bob') ->>> jim = Person.objects.create(name='Jim') - ->>> rock = Group.objects.create(name='Rock') ->>> roll = Group.objects.create(name='Roll') - ->>> frank = User.objects.create_user('frank','frank@example.com','password') ->>> jane = User.objects.create_user('jane','jane@example.com','password') - -# Now test that the forward declared Membership works ->>> Membership.objects.create(person=bob, group=rock) - - ->>> Membership.objects.create(person=bob, group=roll) - - ->>> Membership.objects.create(person=jim, group=rock) - - ->>> bob.group_set.all() -[, ] - ->>> roll.members.all() -[] - -# Error messages use the model name, not repr of the class name ->>> bob.group_set = [] -Traceback (most recent call last): -... -AttributeError: Cannot set values on a ManyToManyField which specifies an intermediary model. Use m2m_through_regress.Membership's Manager instead. - ->>> roll.members = [] -Traceback (most recent call last): -... -AttributeError: Cannot set values on a ManyToManyField which specifies an intermediary model. Use m2m_through_regress.Membership's Manager instead. - ->>> rock.members.create(name='Anne') -Traceback (most recent call last): -... -AttributeError: Cannot use create() on a ManyToManyField which specifies an intermediary model. Use m2m_through_regress.Membership's Manager instead. - ->>> bob.group_set.create(name='Funk') -Traceback (most recent call last): -... -AttributeError: Cannot use create() on a ManyToManyField which specifies an intermediary model. Use m2m_through_regress.Membership's Manager instead. - -# Now test that the intermediate with a relationship outside -# the current app (i.e., UserMembership) workds ->>> UserMembership.objects.create(user=frank, group=rock) - - ->>> UserMembership.objects.create(user=frank, group=roll) - - ->>> UserMembership.objects.create(user=jane, group=rock) - - ->>> frank.group_set.all() -[, ] - ->>> roll.user_members.all() -[] - -# Regression test for #8134 -- -# m2m-through models shouldn't be serialized as m2m fields on the model. - -# First, clean up a lot of objects we don't need. -# The serialization test only requires three objects to work - -# one for each end of the m2m, plus the through model. - ->>> User.objects.all().delete() ->>> UserMembership.objects.all().delete() ->>> frank.delete() ->>> rock.delete() ->>> jim.delete() - -# Dump the current contents of the database as a JSON fixture ->>> management.call_command('dumpdata', 'm2m_through_regress', format='json', indent=2) -[ - { - "pk": 2, - "model": "m2m_through_regress.membership", - "fields": { - "person": 1, - "price": 100, - "group": 2 - } - }, - { - "pk": 1, - "model": "m2m_through_regress.person", - "fields": { - "name": "Bob" - } - }, - { - "pk": 2, - "model": "m2m_through_regress.group", - "fields": { - "name": "Roll" - } - } -] - -# Check the XML serializer too, since it doesn't use the common implementation ->>> management.call_command('dumpdata', 'm2m_through_regress', format='xml', indent=2) - - - - 1 - 2 - 100 - - - Bob - - - Roll - - - -## Regression test for #8046: -Check that we don't involve too many copies of the intermediate table when -doing a join. - ->>> 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) ->>> rock.members.filter(membership__price=50) -[] - -## Regression test for #8254 ->>> bob.group_set.filter(membership__price=50) -[] - -## Regression test for #9804 -# Flush the database, just to make sure we can. ->>> management.call_command('flush', verbosity=0, interactive=False) - -## Regression test for #11107 -Ensure that sequences on m2m_through tables are being created for the through -model, not for a phantom auto-generated m2m table. - ->>> management.call_command('loaddata', 'm2m_through', verbosity=0) ->>> management.call_command('dumpdata', 'm2m_through_regress', format='json') -[{"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"}}] - -"""} diff --git a/tests/regressiontests/m2m_through_regress/tests.py b/tests/regressiontests/m2m_through_regress/tests.py new file mode 100644 index 0000000000..ff1d95020e --- /dev/null +++ b/tests/regressiontests/m2m_through_regress/tests.py @@ -0,0 +1,126 @@ +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + +from django.core import management +from django.contrib.auth.models import User +from django.test import TestCase + +from models import Person, Group, Membership, UserMembership + + +class M2MThroughTestCase(TestCase): + def test_everything(self): + bob = Person.objects.create(name="Bob") + jim = Person.objects.create(name="Jim") + + rock = Group.objects.create(name="Rock") + roll = Group.objects.create(name="Roll") + + frank = User.objects.create_user("frank", "frank@example.com", "password") + jane = User.objects.create_user("jane", "jane@example.com", "password") + + Membership.objects.create(person=bob, group=rock) + Membership.objects.create(person=bob, group=roll) + Membership.objects.create(person=jim, group=rock) + + self.assertQuerysetEqual( + bob.group_set.all(), [ + "", + "", + ] + ) + + self.assertQuerysetEqual( + roll.members.all(), [ + "", + ] + ) + + self.assertRaises(AttributeError, setattr, bob, "group_set", []) + self.assertRaises(AttributeError, setattr, roll, "members", []) + + self.assertRaises(AttributeError, rock.members.create, name="Anne") + self.assertRaises(AttributeError, bob.group_set.create, name="Funk") + + UserMembership.objects.create(user=frank, group=rock) + UserMembership.objects.create(user=frank, group=roll) + UserMembership.objects.create(user=jane, group=rock) + + self.assertQuerysetEqual( + frank.group_set.all(), [ + "", + "", + ] + ) + + self.assertQuerysetEqual( + roll.user_members.all(), [ + "", + ] + ) + + 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") + Membership.objects.create(person=p, group=g) + + out = StringIO() + management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out) + self.assertEqual(out.getvalue().strip(), """[{"pk": 1, "model": "m2m_through_regress.membership", "fields": {"person": 1, "price": 100, "group": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Bob"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]""") + + out = StringIO() + management.call_command("dumpdata", "m2m_through_regress", format="xml", + indent=2, stdout=out) + self.assertEqual(out.getvalue().strip(), """ + + + + 1 + 1 + 100 + + + Bob + + + Roll + + + """.strip()) + + 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 ThroughLoadDataTestCase(TestCase): + fixtures = ["m2m_through"] + + def test_sequence_creation(self): + "Check that sequences on an m2m_through are created for the through model, not a phantom auto-generated m2m table. Refs #11107" + out = StringIO() + management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out) + self.assertEqual(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"}}]""")