Code

Ticket #13700: django-m2m-through-test-cleanup.diff

File django-m2m-through-test-cleanup.diff, 6.1 KB (added by Alex, 4 years ago)
Line 
1diff --git a/tests/regressiontests/m2m_through_regress/models.py b/tests/regressiontests/m2m_through_regress/models.py
2index 56aecd6..ec87985 100644
3--- a/tests/regressiontests/m2m_through_regress/models.py
4+++ b/tests/regressiontests/m2m_through_regress/models.py
5@@ -1,8 +1,10 @@
6 from datetime import datetime
7+
8 from django.contrib.auth.models import User
9 from django.core import management
10 from django.db import models
11 
12+
13 # Forward declared intermediate model
14 class Membership(models.Model):
15     person = models.ForeignKey('Person')
16@@ -51,159 +53,3 @@ class Through(ThroughBase):
17 class B(models.Model):
18     b_text = models.CharField(max_length=20)
19     a_list = models.ManyToManyField(A, through=Through)
20-
21-
22-__test__ = {'API_TESTS':"""
23-# Create some dummy data
24->>> bob = Person.objects.create(name='Bob')
25->>> jim = Person.objects.create(name='Jim')
26-
27->>> rock = Group.objects.create(name='Rock')
28->>> roll = Group.objects.create(name='Roll')
29-
30->>> frank = User.objects.create_user('frank','frank@example.com','password')
31->>> jane = User.objects.create_user('jane','jane@example.com','password')
32-
33-# Now test that the forward declared Membership works
34->>> Membership.objects.create(person=bob, group=rock)
35-<Membership: Bob is a member of Rock>
36-
37->>> Membership.objects.create(person=bob, group=roll)
38-<Membership: Bob is a member of Roll>
39-
40->>> Membership.objects.create(person=jim, group=rock)
41-<Membership: Jim is a member of Rock>
42-
43->>> bob.group_set.all()
44-[<Group: Rock>, <Group: Roll>]
45-
46->>> roll.members.all()
47-[<Person: Bob>]
48-
49-# Error messages use the model name, not repr of the class name
50->>> bob.group_set = []
51-Traceback (most recent call last):
52-...
53-AttributeError: Cannot set values on a ManyToManyField which specifies an intermediary model.  Use m2m_through_regress.Membership's Manager instead.
54-
55->>> roll.members = []
56-Traceback (most recent call last):
57-...
58-AttributeError: Cannot set values on a ManyToManyField which specifies an intermediary model.  Use m2m_through_regress.Membership's Manager instead.
59-
60->>> rock.members.create(name='Anne')
61-Traceback (most recent call last):
62-...
63-AttributeError: Cannot use create() on a ManyToManyField which specifies an intermediary model.  Use m2m_through_regress.Membership's Manager instead.
64-
65->>> bob.group_set.create(name='Funk')
66-Traceback (most recent call last):
67-...
68-AttributeError: Cannot use create() on a ManyToManyField which specifies an intermediary model.  Use m2m_through_regress.Membership's Manager instead.
69-
70-# Now test that the intermediate with a relationship outside
71-# the current app (i.e., UserMembership) workds
72->>> UserMembership.objects.create(user=frank, group=rock)
73-<UserMembership: frank is a user and member of Rock>
74-
75->>> UserMembership.objects.create(user=frank, group=roll)
76-<UserMembership: frank is a user and member of Roll>
77-
78->>> UserMembership.objects.create(user=jane, group=rock)
79-<UserMembership: jane is a user and member of Rock>
80-
81->>> frank.group_set.all()
82-[<Group: Rock>, <Group: Roll>]
83-
84->>> roll.user_members.all()
85-[<User: frank>]
86-
87-# Regression test for #8134 --
88-# m2m-through models shouldn't be serialized as m2m fields on the model.
89-
90-# First, clean up a lot of objects we don't need.
91-# The serialization test only requires three objects to work -
92-# one for each end of the m2m, plus the through model.
93-
94->>> User.objects.all().delete()
95->>> UserMembership.objects.all().delete()
96->>> frank.delete()
97->>> rock.delete()
98->>> jim.delete()
99-
100-# Dump the current contents of the database as a JSON fixture
101->>> management.call_command('dumpdata', 'm2m_through_regress', format='json', indent=2)
102-[
103-  {
104-    "pk": 2,
105-    "model": "m2m_through_regress.membership",
106-    "fields": {
107-      "person": 1,
108-      "price": 100,
109-      "group": 2
110-    }
111-  },
112-  {
113-    "pk": 1,
114-    "model": "m2m_through_regress.person",
115-    "fields": {
116-      "name": "Bob"
117-    }
118-  },
119-  {
120-    "pk": 2,
121-    "model": "m2m_through_regress.group",
122-    "fields": {
123-      "name": "Roll"
124-    }
125-  }
126-]
127-
128-# Check the XML serializer too, since it doesn't use the common implementation
129->>> management.call_command('dumpdata', 'm2m_through_regress', format='xml', indent=2)
130-<?xml version="1.0" encoding="utf-8"?>
131-<django-objects version="1.0">
132-  <object pk="2" model="m2m_through_regress.membership">
133-    <field to="m2m_through_regress.person" name="person" rel="ManyToOneRel">1</field>
134-    <field to="m2m_through_regress.group" name="group" rel="ManyToOneRel">2</field>
135-    <field type="IntegerField" name="price">100</field>
136-  </object>
137-  <object pk="1" model="m2m_through_regress.person">
138-    <field type="CharField" name="name">Bob</field>
139-  </object>
140-  <object pk="2" model="m2m_through_regress.group">
141-    <field type="CharField" name="name">Roll</field>
142-  </object>
143-</django-objects>
144-
145-## Regression test for #8046:
146-Check that we don't involve too many copies of the intermediate table when
147-doing a join.
148-
149->>> bob = Person.objects.create(name='Bob')
150->>> jim = Person.objects.create(name='Jim')
151->>> rock = Group.objects.create(name='Rock')
152->>> roll = Group.objects.create(name='Roll')
153->>> _ = Membership.objects.create(person=bob, group=rock)
154->>> _ = Membership.objects.create(person=jim, group=rock, price=50)
155->>> _ = Membership.objects.create(person=bob, group=roll, price=50)
156->>> rock.members.filter(membership__price=50)
157-[<Person: Jim>]
158-
159-## Regression test for #8254
160->>> bob.group_set.filter(membership__price=50)
161-[<Group: Roll>]
162-
163-## Regression test for #9804
164-# Flush the database, just to make sure we can.
165->>> management.call_command('flush', verbosity=0, interactive=False)
166-
167-## Regression test for #11107
168-Ensure that sequences on m2m_through tables are being created for the through
169-model, not for a phantom auto-generated m2m table.
170-
171->>> management.call_command('loaddata', 'm2m_through', verbosity=0)
172->>> management.call_command('dumpdata', 'm2m_through_regress', format='json')
173-[{"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"}}]
174-
175-"""}