Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Convert m2m_recursive tests to unittests. We have always been at war …

…with doctests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14082 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2314fadabe00796ecb0d9ebd7a950f58a675db68 1 parent 18c3ea5
Alex Gaynor authored October 09, 2010
170  tests/modeltests/m2m_recursive/models.py
@@ -18,6 +18,7 @@
18 18
 
19 19
 from django.db import models
20 20
 
  21
+
21 22
 class Person(models.Model):
22 23
     name = models.CharField(max_length=20)
23 24
     friends = models.ManyToManyField('self')
@@ -25,172 +26,3 @@ class Person(models.Model):
25 26
 
26 27
     def __unicode__(self):
27 28
         return self.name
28  
-
29  
-__test__ = {'API_TESTS':"""
30  
->>> a = Person(name='Anne')
31  
->>> a.save()
32  
->>> b = Person(name='Bill')
33  
->>> b.save()
34  
->>> c = Person(name='Chuck')
35  
->>> c.save()
36  
->>> d = Person(name='David')
37  
->>> d.save()
38  
-
39  
-# Add some friends in the direction of field definition
40  
-# Anne is friends with Bill and Chuck
41  
->>> a.friends.add(b,c)
42  
-
43  
-# David is friends with Anne and Chuck - add in reverse direction
44  
->>> d.friends.add(a,c)
45  
-
46  
-# Who is friends with Anne?
47  
->>> a.friends.all()
48  
-[<Person: Bill>, <Person: Chuck>, <Person: David>]
49  
-
50  
-# Who is friends with Bill?
51  
->>> b.friends.all()
52  
-[<Person: Anne>]
53  
-
54  
-# Who is friends with Chuck?
55  
->>> c.friends.all()
56  
-[<Person: Anne>, <Person: David>]
57  
-
58  
-# Who is friends with David?
59  
->>> d.friends.all()
60  
-[<Person: Anne>, <Person: Chuck>]
61  
-
62  
-# Bill is already friends with Anne - add Anne again, but in the reverse direction
63  
->>> b.friends.add(a)
64  
-
65  
-# Who is friends with Anne?
66  
->>> a.friends.all()
67  
-[<Person: Bill>, <Person: Chuck>, <Person: David>]
68  
-
69  
-# Who is friends with Bill?
70  
->>> b.friends.all()
71  
-[<Person: Anne>]
72  
-
73  
-# Remove Anne from Bill's friends
74  
->>> b.friends.remove(a)
75  
-
76  
-# Who is friends with Anne?
77  
->>> a.friends.all()
78  
-[<Person: Chuck>, <Person: David>]
79  
-
80  
-# Who is friends with Bill?
81  
->>> b.friends.all()
82  
-[]
83  
-
84  
-# Clear Anne's group of friends
85  
->>> a.friends.clear()
86  
-
87  
-# Who is friends with Anne?
88  
->>> a.friends.all()
89  
-[]
90  
-
91  
-# Reverse relationships should also be gone
92  
-# Who is friends with Chuck?
93  
->>> c.friends.all()
94  
-[<Person: David>]
95  
-
96  
-# Who is friends with David?
97  
->>> d.friends.all()
98  
-[<Person: Chuck>]
99  
-
100  
-
101  
-# Add some idols in the direction of field definition
102  
-# Anne idolizes Bill and Chuck
103  
->>> a.idols.add(b,c)
104  
-
105  
-# Bill idolizes Anne right back
106  
->>> b.idols.add(a)
107  
-
108  
-# David is idolized by Anne and Chuck - add in reverse direction
109  
->>> d.stalkers.add(a,c)
110  
-
111  
-# Who are Anne's idols?
112  
->>> a.idols.all()
113  
-[<Person: Bill>, <Person: Chuck>, <Person: David>]
114  
-
115  
-# Who is stalking Anne?
116  
->>> a.stalkers.all()
117  
-[<Person: Bill>]
118  
-
119  
-# Who are Bill's idols?
120  
->>> b.idols.all()
121  
-[<Person: Anne>]
122  
-
123  
-# Who is stalking Bill?
124  
->>> b.stalkers.all()
125  
-[<Person: Anne>]
126  
-
127  
-# Who are Chuck's idols?
128  
->>> c.idols.all()
129  
-[<Person: David>]
130  
-
131  
-# Who is stalking Chuck?
132  
->>> c.stalkers.all()
133  
-[<Person: Anne>]
134  
-
135  
-# Who are David's idols?
136  
->>> d.idols.all()
137  
-[]
138  
-
139  
-# Who is stalking David
140  
->>> d.stalkers.all()
141  
-[<Person: Anne>, <Person: Chuck>]
142  
-
143  
-# Bill is already being stalked by Anne - add Anne again, but in the reverse direction
144  
->>> b.stalkers.add(a)
145  
-
146  
-# Who are Anne's idols?
147  
->>> a.idols.all()
148  
-[<Person: Bill>, <Person: Chuck>, <Person: David>]
149  
-
150  
-# Who is stalking Anne?
151  
-[<Person: Bill>]
152  
-
153  
-# Who are Bill's idols
154  
->>> b.idols.all()
155  
-[<Person: Anne>]
156  
-
157  
-# Who is stalking Bill?
158  
->>> b.stalkers.all()
159  
-[<Person: Anne>]
160  
-
161  
-# Remove Anne from Bill's list of stalkers
162  
->>> b.stalkers.remove(a)
163  
-
164  
-# Who are Anne's idols?
165  
->>> a.idols.all()
166  
-[<Person: Chuck>, <Person: David>]
167  
-
168  
-# Who is stalking Anne?
169  
->>> a.stalkers.all()
170  
-[<Person: Bill>]
171  
-
172  
-# Who are Bill's idols?
173  
->>> b.idols.all()
174  
-[<Person: Anne>]
175  
-
176  
-# Who is stalking Bill?
177  
->>> b.stalkers.all()
178  
-[]
179  
-
180  
-# Clear Anne's group of idols
181  
->>> a.idols.clear()
182  
-
183  
-# Who are Anne's idols
184  
->>> a.idols.all()
185  
-[]
186  
-
187  
-# Reverse relationships should also be gone
188  
-# Who is stalking Chuck?
189  
->>> c.stalkers.all()
190  
-[]
191  
-
192  
-# Who is friends with David?
193  
->>> d.stalkers.all()
194  
-[<Person: Chuck>]
195  
-
196  
-"""}
253  tests/modeltests/m2m_recursive/tests.py
... ...
@@ -0,0 +1,253 @@
  1
+from operator import attrgetter
  2
+
  3
+from django.test import TestCase
  4
+
  5
+from models import Person
  6
+
  7
+
  8
+class RecursiveM2MTests(TestCase):
  9
+    def test_recursive_m2m(self):
  10
+        a, b, c, d = [
  11
+            Person.objects.create(name=name)
  12
+            for name in ["Anne", "Bill", "Chuck", "David"]
  13
+        ]
  14
+        
  15
+        # Add some friends in the direction of field definition
  16
+        # Anne is friends with Bill and Chuck
  17
+        a.friends.add(b, c)
  18
+
  19
+        # David is friends with Anne and Chuck - add in reverse direction
  20
+        d.friends.add(a,c)
  21
+        
  22
+        # Who is friends with Anne?
  23
+        self.assertQuerysetEqual(
  24
+            a.friends.all(), [
  25
+                "Bill",
  26
+                "Chuck",
  27
+                "David"
  28
+            ],
  29
+            attrgetter("name")
  30
+        )
  31
+        # Who is friends with Bill?
  32
+        self.assertQuerysetEqual(
  33
+            b.friends.all(), [
  34
+                "Anne",
  35
+            ],
  36
+            attrgetter("name")
  37
+        )
  38
+        # Who is friends with Chuck?
  39
+        self.assertQuerysetEqual(
  40
+            c.friends.all(), [
  41
+                "Anne",
  42
+                "David"
  43
+            ],
  44
+            attrgetter("name")
  45
+        )
  46
+        # Who is friends with David?
  47
+        self.assertQuerysetEqual(
  48
+            d.friends.all(), [
  49
+                "Anne",
  50
+                "Chuck",
  51
+            ],
  52
+            attrgetter("name")
  53
+        )
  54
+        # Bill is already friends with Anne - add Anne again, but in the
  55
+        # reverse direction
  56
+        b.friends.add(a)
  57
+        
  58
+        # Who is friends with Anne?
  59
+        self.assertQuerysetEqual(
  60
+            a.friends.all(), [
  61
+                "Bill",
  62
+                "Chuck",
  63
+                "David",
  64
+            ],
  65
+            attrgetter("name")
  66
+        )
  67
+        # Who is friends with Bill?
  68
+        self.assertQuerysetEqual(
  69
+            b.friends.all(), [
  70
+                "Anne",
  71
+            ],
  72
+            attrgetter("name")
  73
+        )
  74
+        # Remove Anne from Bill's friends
  75
+        b.friends.remove(a)
  76
+        # Who is friends with Anne?
  77
+        self.assertQuerysetEqual(
  78
+            a.friends.all(), [
  79
+                "Chuck",
  80
+                "David",
  81
+            ],
  82
+            attrgetter("name")
  83
+        )
  84
+        # Who is friends with Bill?
  85
+        self.assertQuerysetEqual(
  86
+            b.friends.all(), []
  87
+        )
  88
+
  89
+        # Clear Anne's group of friends
  90
+        a.friends.clear()
  91
+        # Who is friends with Anne?
  92
+        self.assertQuerysetEqual(
  93
+            a.friends.all(), []
  94
+        )
  95
+        # Reverse relationships should also be gone
  96
+        # Who is friends with Chuck?
  97
+        self.assertQuerysetEqual(
  98
+            c.friends.all(), [
  99
+                "David",
  100
+            ],
  101
+            attrgetter("name")
  102
+        )
  103
+        # Who is friends with David?
  104
+        self.assertQuerysetEqual(
  105
+            d.friends.all(), [
  106
+                "Chuck",
  107
+            ],
  108
+            attrgetter("name")
  109
+        )
  110
+
  111
+        # Add some idols in the direction of field definition
  112
+        # Anne idolizes Bill and Chuck
  113
+        a.idols.add(b, c)
  114
+        # Bill idolizes Anne right back
  115
+        b.idols.add(a)
  116
+        # David is idolized by Anne and Chuck - add in reverse direction
  117
+        d.stalkers.add(a, c)
  118
+        
  119
+        # Who are Anne's idols?
  120
+        self.assertQuerysetEqual(
  121
+            a.idols.all(), [
  122
+                "Bill",
  123
+                "Chuck",
  124
+                "David",
  125
+            ],
  126
+            attrgetter("name")
  127
+        )
  128
+        # Who is stalking Anne?
  129
+        self.assertQuerysetEqual(
  130
+            a.stalkers.all(), [
  131
+                "Bill",
  132
+            ],
  133
+            attrgetter("name")
  134
+        )
  135
+        # Who are Bill's idols?
  136
+        self.assertQuerysetEqual(
  137
+            b.idols.all(), [
  138
+                "Anne",
  139
+            ],
  140
+            attrgetter("name")
  141
+        )
  142
+        # Who is stalking Bill?
  143
+        self.assertQuerysetEqual(
  144
+            b.stalkers.all(), [
  145
+                "Anne",
  146
+            ],
  147
+            attrgetter("name")
  148
+        )
  149
+        # Who are Chuck's idols?
  150
+        self.assertQuerysetEqual(
  151
+            c.idols.all(), [
  152
+                "David",
  153
+            ],
  154
+            attrgetter("name"),
  155
+        )
  156
+        # Who is stalking Chuck?
  157
+        self.assertQuerysetEqual(
  158
+            c.stalkers.all(), [
  159
+                "Anne",
  160
+            ],
  161
+            attrgetter("name")
  162
+        )
  163
+        # Who are David's idols?
  164
+        self.assertQuerysetEqual(
  165
+            d.idols.all(), []
  166
+        )
  167
+        # Who is stalking David
  168
+        self.assertQuerysetEqual(
  169
+            d.stalkers.all(), [
  170
+                "Anne",
  171
+                "Chuck",
  172
+            ],
  173
+            attrgetter("name")
  174
+        )
  175
+        # Bill is already being stalked by Anne - add Anne again, but in the
  176
+        # reverse direction
  177
+        b.stalkers.add(a)
  178
+        # Who are Anne's idols?
  179
+        self.assertQuerysetEqual(
  180
+            a.idols.all(), [
  181
+                "Bill",
  182
+                "Chuck",
  183
+                "David",
  184
+            ],
  185
+            attrgetter("name")
  186
+        )
  187
+        # Who is stalking Anne?
  188
+        self.assertQuerysetEqual(
  189
+            a.stalkers.all(), [
  190
+                "Bill",
  191
+            ],
  192
+            attrgetter("name")
  193
+        )
  194
+        # Who are Bill's idols
  195
+        self.assertQuerysetEqual(
  196
+            b.idols.all(), [
  197
+                "Anne",
  198
+            ],
  199
+            attrgetter("name")
  200
+        )
  201
+        # Who is stalking Bill?
  202
+        self.assertQuerysetEqual(
  203
+            b.stalkers.all(), [
  204
+                "Anne",
  205
+            ],
  206
+            attrgetter("name"),
  207
+        )
  208
+        # Remove Anne from Bill's list of stalkers
  209
+        b.stalkers.remove(a)
  210
+        # Who are Anne's idols?
  211
+        self.assertQuerysetEqual(
  212
+            a.idols.all(), [
  213
+                "Chuck",
  214
+                "David",
  215
+            ],
  216
+            attrgetter("name")
  217
+        )
  218
+        # Who is stalking Anne?
  219
+        self.assertQuerysetEqual(
  220
+            a.stalkers.all(), [
  221
+                "Bill",
  222
+            ],
  223
+            attrgetter("name")
  224
+        )
  225
+        # Who are Bill's idols?
  226
+        self.assertQuerysetEqual(
  227
+            b.idols.all(), [
  228
+                "Anne",
  229
+            ],
  230
+            attrgetter("name")
  231
+        )
  232
+        # Who is stalking Bill?
  233
+        self.assertQuerysetEqual(
  234
+            b.stalkers.all(), []
  235
+        )
  236
+        # Clear Anne's group of idols
  237
+        a.idols.clear()
  238
+        # Who are Anne's idols
  239
+        self.assertQuerysetEqual(
  240
+            a.idols.all(), []
  241
+        )
  242
+        # Reverse relationships should also be gone
  243
+        # Who is stalking Chuck?
  244
+        self.assertQuerysetEqual(
  245
+            c.stalkers.all(), []
  246
+        )
  247
+        # Who is friends with David?
  248
+        self.assertQuerysetEqual(
  249
+            d.stalkers.all(), [
  250
+                "Chuck",
  251
+            ],
  252
+            attrgetter("name")
  253
+        )

0 notes on commit 2314fad

Please sign in to comment.
Something went wrong with that request. Please try again.