PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/m2m_recursive/tests.py

https://code.google.com/p/mango-py/
Python | 253 lines | 195 code | 11 blank | 47 comment | 2 complexity | 078fbf7477c50cdddf0c5dc2230d7599 MD5 | raw file
  1from operator import attrgetter
  2
  3from django.test import TestCase
  4
  5from models import Person
  6
  7
  8class 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        )