Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 435 lines (352 sloc) 16.285 kb
6985e7b @coleifer Beefing up test suite
authored
1 from django.contrib.contenttypes.models import ContentType
5314676 @coleifer Initial commit
authored
2 from django.test import TestCase
3
ab92043 @coleifer Adding a GFKOptimizedQuerySet that will allow efficient lookups across
authored
4 from genericm2m.models import RelatedObject, RelatedObjectsDescriptor, GFKOptimizedQuerySet
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
5 from genericm2m.genericm2m_tests.models import (
6 Food, Beverage, Person, RelatedBeverage, Boring, AnotherRelatedObject, Note
7 )
5314676 @coleifer Initial commit
authored
8
9
10 class RelationsTestCase(TestCase):
11 def setUp(self):
12 self.pizza = Food.objects.create(name='pizza')
13 self.sandwich = Food.objects.create(name='sandwich')
14 self.cereal = Food.objects.create(name='cereal')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
15
5314676 @coleifer Initial commit
authored
16 self.soda = Beverage.objects.create(name='soda')
17 self.beer = Beverage.objects.create(name='beer')
18 self.milk = Beverage.objects.create(name='milk')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
19
5314676 @coleifer Initial commit
authored
20 self.mario = Person.objects.create(name='mario')
21 self.sam = Person.objects.create(name='sam')
22 self.chocula = Person.objects.create(name='chocula')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
23
46720f5 @coleifer Updating the readme and adding a couple new lines to the tests to ensure
authored
24 self.table = Boring.objects.create(name='table')
25 self.chair = Boring.objects.create(name='chair')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
26
5314676 @coleifer Initial commit
authored
27 def assertRelatedEqual(self, rel_qs, tups, from_field='parent',
28 to_field='object'):
29 rel_tup = [
30 (getattr(rel_obj, from_field), getattr(rel_obj, to_field)) \
31 for rel_obj in rel_qs
32 ]
33 self.assertEqual(rel_tup, list(tups))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
34
5314676 @coleifer Initial commit
authored
35 def test_connect(self):
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
36 """
37 Connect model instances to various other model instances, then query
38 the manager and check the queryset returned is correct
39 """
5314676 @coleifer Initial commit
authored
40 self.pizza.related.connect(self.soda)
41 self.pizza.related.connect(self.beer)
42 self.pizza.related.connect(self.mario)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
43
5314676 @coleifer Initial commit
authored
44 self.soda.related.connect(self.pizza)
45 self.soda.related.connect(self.beer)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
46
5314676 @coleifer Initial commit
authored
47 related = self.pizza.related.all()
48 self.assertRelatedEqual(related, (
49 (self.pizza, self.mario),
50 (self.pizza, self.beer),
51 (self.pizza, self.soda),
52 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
53
5314676 @coleifer Initial commit
authored
54 self.sandwich.related.connect(self.soda)
55 self.sandwich.related.connect(self.milk)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
56
5314676 @coleifer Initial commit
authored
57 related = self.sandwich.related.all()
58 self.assertRelatedEqual(related, (
59 (self.sandwich, self.milk),
60 (self.sandwich, self.soda),
61 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
62
5314676 @coleifer Initial commit
authored
63 related = self.cereal.related.all()
64 self.assertRelatedEqual(related, ())
a6657a9 @coleifer Smarter handling of reverse lookups
authored
65
5314676 @coleifer Initial commit
authored
66 related = self.soda.related.all()
67 self.assertRelatedEqual(related, (
68 (self.soda, self.beer),
69 (self.soda, self.pizza),
70 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
71
46720f5 @coleifer Updating the readme and adding a couple new lines to the tests to ensure
authored
72 self.sandwich.related.connect(self.table)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
73
46720f5 @coleifer Updating the readme and adding a couple new lines to the tests to ensure
authored
74 related = self.sandwich.related.all()
75 self.assertRelatedEqual(related, (
76 (self.sandwich, self.table),
77 (self.sandwich, self.milk),
78 (self.sandwich, self.soda),
79 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
80
5314676 @coleifer Initial commit
authored
81 def test_related_to(self):
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
82 """
83 Check the back-side of the double-GFK, note: this only works on objects
84 that have a RelatedObjectsDescriptor() pointing to the same model
85 class, in this case the default `RelatedObject`
86 """
5314676 @coleifer Initial commit
authored
87 self.pizza.related.connect(self.soda)
88 self.pizza.related.connect(self.beer)
46720f5 @coleifer Updating the readme and adding a couple new lines to the tests to ensure
authored
89 self.pizza.related.connect(self.table)
5314676 @coleifer Initial commit
authored
90 self.sandwich.related.connect(self.soda)
91 self.sandwich.related.connect(self.milk)
92 self.mario.related.connect(self.soda)
93 self.soda.related.connect(self.pizza)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
94
5314676 @coleifer Initial commit
authored
95 related = self.soda.related.related_to()
96 self.assertRelatedEqual(related, (
97 (self.mario, self.soda),
98 (self.sandwich, self.soda),
99 (self.pizza, self.soda),
100 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
101
5314676 @coleifer Initial commit
authored
102 related = self.beer.related.related_to()
103 self.assertRelatedEqual(related, (
104 (self.pizza, self.beer),
105 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
106
5314676 @coleifer Initial commit
authored
107 related = self.milk.related.related_to()
108 self.assertRelatedEqual(related, (
109 (self.sandwich, self.milk),
110 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
111
5314676 @coleifer Initial commit
authored
112 related = self.pizza.related.related_to()
113 self.assertRelatedEqual(related, (
114 (self.soda, self.pizza),
115 ))
116
0d145fc @coleifer Adding a symmetrical method
authored
117 def test_symmetrical(self):
118 self.pizza.related.connect(self.soda)
119 self.pizza.related.connect(self.beer)
120 self.pizza.related.connect(self.table)
121 self.sandwich.related.connect(self.soda)
122 self.sandwich.related.connect(self.milk)
123 self.mario.related.connect(self.soda)
124 self.soda.related.connect(self.pizza)
125
126 related = self.soda.related.symmetrical().order_by('id')
127 self.assertRelatedEqual(related, (
128 (self.pizza, self.soda),
129 (self.sandwich, self.soda),
130 (self.mario, self.soda),
131 (self.soda, self.pizza),
132 ))
133
134 related = self.beer.related.symmetrical()
135 self.assertRelatedEqual(related, (
136 (self.pizza, self.beer),
137 ))
138
5314676 @coleifer Initial commit
authored
139 def test_manager_methods(self):
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
140 """
141 Since the RelatedObjectsDescriptor behaves like a dynamic manager (much
142 the same as Django's ForeignRelatedObjectsDescriptor) test to ensure
143 that the manager behaves as expected and correctly implements all the
144 basic FK methods
145 """
146 # connect pizza to soda and grab the newly-created RelatedObject
5314676 @coleifer Initial commit
authored
147 self.pizza.related.connect(self.soda)
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
148 rel_obj = RelatedObject.objects.all()[0]
a6657a9 @coleifer Smarter handling of reverse lookups
authored
149
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
150 # connect cereal to milk (this is just to make sure that anything
151 # modified on one Food object doesn't affect another Food object
5314676 @coleifer Initial commit
authored
152 self.cereal.related.connect(self.milk)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
153
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
154 # create a new RelatedObject but do not save it yet -- note that it does
155 # not have `parent_object` set
5314676 @coleifer Initial commit
authored
156 new_rel_obj = RelatedObject(object=self.beer)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
157
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
158 # add this related object to pizza, parent_object gets set and it will
159 # show up in the queryset as expected
5314676 @coleifer Initial commit
authored
160 self.pizza.related.add(new_rel_obj)
161 self.assertRelatedEqual(self.pizza.related.all(), (
162 (self.pizza, self.beer),
163 (self.pizza, self.soda),
164 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
165
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
166 # remove the original RelatedObject `rel_obj`, which was the connection
167 # from pizza -> soda
5314676 @coleifer Initial commit
authored
168 self.pizza.related.remove(rel_obj)
169 self.assertRelatedEqual(self.pizza.related.all(), (
170 (self.pizza, self.beer),
171 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
172
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
173 # make sure clearing pizza's related queryset works
5314676 @coleifer Initial commit
authored
174 self.pizza.related.clear()
175 self.assertRelatedEqual(self.pizza.related.all(), ())
a6657a9 @coleifer Smarter handling of reverse lookups
authored
176
5314676 @coleifer Initial commit
authored
177 # make sure clearing the pizza objects didn't affect cereal
178 self.assertRelatedEqual(self.cereal.related.all(), (
179 (self.cereal, self.milk),
180 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
181
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
182 # there should be just one row in the table
5314676 @coleifer Initial commit
authored
183 self.assertEqual(RelatedObject.objects.count(), 1)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
184
5314676 @coleifer Initial commit
authored
185 def test_model_level(self):
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
186 """
187 The RelatedObjectsDescriptor can work at the class-level as well and
188 applies to all instances of the model - check that when connections are
189 made between individual instances and then are queried via the class,
190 that all connections are returned from that model type
191 """
5314676 @coleifer Initial commit
authored
192 self.pizza.related.connect(self.beer)
193 self.cereal.related.connect(self.milk)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
194
5314676 @coleifer Initial commit
authored
195 self.mario.related.connect(self.pizza)
196 self.sam.related.connect(self.beer)
197 self.soda.related.connect(self.pizza)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
198
5314676 @coleifer Initial commit
authored
199 self.assertRelatedEqual(Food.related.all(), (
200 (self.cereal, self.milk),
201 (self.pizza, self.beer),
202 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
203
5314676 @coleifer Initial commit
authored
204 self.assertRelatedEqual(Beverage.related.all(), (
205 (self.soda, self.pizza),
206 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
207
5314676 @coleifer Initial commit
authored
208 self.assertRelatedEqual(Person.related.all(), (
209 (self.sam, self.beer),
210 (self.mario, self.pizza),
211 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
212
5314676 @coleifer Initial commit
authored
213 def test_custom_connect(self):
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
214 """
215 Mimic the test_connect() method, but instead use the custom descriptor,
216 `related_beverages` which goes through the RelatedBeverage model
217 """
5314676 @coleifer Initial commit
authored
218 self.pizza.related_beverages.connect(self.soda)
219 self.pizza.related_beverages.connect(self.beer)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
220
5314676 @coleifer Initial commit
authored
221 related = self.pizza.related_beverages.all()
222 self.assertRelatedEqual(related, (
223 (self.pizza, self.beer),
224 (self.pizza, self.soda),
225 ), 'food', 'beverage')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
226
5314676 @coleifer Initial commit
authored
227 self.sandwich.related_beverages.connect(self.soda)
228 self.sandwich.related_beverages.connect(self.milk)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
229
5314676 @coleifer Initial commit
authored
230 related = self.sandwich.related_beverages.all()
231 self.assertRelatedEqual(related, (
232 (self.sandwich, self.milk),
233 (self.sandwich, self.soda),
234 ), 'food', 'beverage')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
235
5314676 @coleifer Initial commit
authored
236 related = self.cereal.related_beverages.all()
237 self.assertRelatedEqual(related, ())
238
239 def test_custom_model_manager(self):
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
240 """
241 Mimic the test_model_manager() method, but instead use the custom
242 descriptor and through model
243 """
5314676 @coleifer Initial commit
authored
244 self.pizza.related_beverages.connect(self.soda)
245 rel_obj = RelatedBeverage.objects.all()[0] # grab the new related obj
a6657a9 @coleifer Smarter handling of reverse lookups
authored
246
5314676 @coleifer Initial commit
authored
247 self.cereal.related_beverages.connect(self.milk)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
248
5314676 @coleifer Initial commit
authored
249 new_rel_obj = RelatedBeverage(beverage=self.beer)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
250
5314676 @coleifer Initial commit
authored
251 self.pizza.related_beverages.add(new_rel_obj)
252 self.assertRelatedEqual(self.pizza.related_beverages.all(), (
253 (self.pizza, self.beer),
254 (self.pizza, self.soda),
255 ), 'food', 'beverage')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
256
5314676 @coleifer Initial commit
authored
257 self.pizza.related_beverages.remove(rel_obj)
258 self.assertRelatedEqual(self.pizza.related_beverages.all(), (
259 (self.pizza, self.beer),
260 ), 'food', 'beverage')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
261
5314676 @coleifer Initial commit
authored
262 self.pizza.related_beverages.clear()
263 self.assertRelatedEqual(self.pizza.related_beverages.all(), ())
a6657a9 @coleifer Smarter handling of reverse lookups
authored
264
5314676 @coleifer Initial commit
authored
265 # make sure clearing the pizza objects didn't affect cereal
266 self.assertRelatedEqual(self.cereal.related_beverages.all(), (
267 (self.cereal, self.milk),
268 ), 'food', 'beverage')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
269
5314676 @coleifer Initial commit
authored
270 self.assertEqual(RelatedBeverage.objects.count(), 1)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
271
5314676 @coleifer Initial commit
authored
272 def test_custom_model_level(self):
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
273 """
274 And lastly, test that the custom descriptor/through-model work as
275 expected at the model-level (previous tests were instance-level)
276 """
5314676 @coleifer Initial commit
authored
277 self.pizza.related_beverages.connect(self.soda)
278 self.pizza.related_beverages.connect(self.beer)
279 self.sandwich.related_beverages.connect(self.soda)
280 self.cereal.related_beverages.connect(self.milk)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
281
5314676 @coleifer Initial commit
authored
282 self.assertRelatedEqual(Food.related_beverages.all(), (
283 (self.cereal, self.milk),
284 (self.sandwich, self.soda),
285 (self.pizza, self.beer),
286 (self.pizza, self.soda),
287 ), 'food', 'beverage')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
288
ab92043 @coleifer Adding a GFKOptimizedQuerySet that will allow efficient lookups across
authored
289 def test_generic_traversal(self):
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
290 """
291 Ensure that the RelatedObjectsDescriptor returns a GFKOptimizedQuerySet
292 when the through model contains a GFK -- also check that the queryset's
293 optimized lookup works as expected
294 """
ab92043 @coleifer Adding a GFKOptimizedQuerySet that will allow efficient lookups across
authored
295 self.pizza.related.connect(self.beer)
296 self.pizza.related.connect(self.soda)
297 self.pizza.related.connect(self.mario)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
298
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
299 # the manager returns instances of GFKOptimizedQuerySet
ab92043 @coleifer Adding a GFKOptimizedQuerySet that will allow efficient lookups across
authored
300 related = self.pizza.related.all()
301 self.assertEqual(type(related), GFKOptimizedQuerySet)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
302
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
303 # check the queryset is using the right field
304 self.assertEqual(related.get_gfk().name, 'object')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
305
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
306 # the custom queryset's optimized lookup works correctly
ab92043 @coleifer Adding a GFKOptimizedQuerySet that will allow efficient lookups across
authored
307 objects = related.generic_objects()
308 self.assertEqual(objects, [self.mario, self.soda, self.beer])
a6657a9 @coleifer Smarter handling of reverse lookups
authored
309
310 # check the reverse does not hold, documenting existing behavior since
311 # it looks at only the "default" manager on the back-side
312 related = self.soda.related.related_to()
313 self.assertEqual(type(related), GFKOptimizedQuerySet)
314
315 # check the queryset is using the right field
316 self.assertEqual(related.get_gfk().name, 'parent')
317
318 # the custom queryset's optimized lookup works correctly
319 objects = related.generic_objects()
320 self.assertEqual(objects, [self.pizza])
321
6985e7b @coleifer Beefing up test suite
authored
322 def test_filtering(self):
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
323 """
324 Check that filtering on RelatedObject fields (or through model fields)
325 works as expected
326 """
6985e7b @coleifer Beefing up test suite
authored
327 self.pizza.related.connect(self.beer, alias='bud lite')
328 self.pizza.related.connect(self.soda, alias='pepsi')
329 self.pizza.related.connect(self.mario)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
330
6985e7b @coleifer Beefing up test suite
authored
331 rel_qs = self.pizza.related.filter(alias='bud lite')
332 self.assertRelatedEqual(rel_qs, (
333 (self.pizza, self.beer),
334 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
335
6985e7b @coleifer Beefing up test suite
authored
336 rel_qs = self.pizza.related.filter(object_type=ContentType.objects.get_for_model(Beverage))
337 self.assertRelatedEqual(rel_qs, (
338 (self.pizza, self.soda),
339 (self.pizza, self.beer),
340 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
341
6985e7b @coleifer Beefing up test suite
authored
342 rel_qs = self.beer.related.related_to().filter(alias='bud lite')
343 self.assertRelatedEqual(rel_qs, (
344 (self.pizza, self.beer),
345 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
346
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
347 def test_custom_model_using_gfks(self):
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
348 """
349 Check that using a custom through model with GFKs works as expected
350 (looking at models.py, Note uses `AnotherRelatedObject` as its through)
351 """
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
352 self.note_a = Note.objects.create(content='a')
353 self.note_b = Note.objects.create(content='b')
354 self.note_c = Note.objects.create(content='c')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
355
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
356 self.note_a.related.connect(self.pizza)
357 self.note_a.related.connect(self.note_b)
a6657a9 @coleifer Smarter handling of reverse lookups
authored
358
b3b56f7 @coleifer Illustrate when models are mixed using different related_model and the
authored
359 self.pizza.related.connect(self.note_b)
360
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
361 # create some notes with custom attributes
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
362 self.note_b.related.connect(self.cereal, alias='cereal note', description='lucky charms!')
363 self.note_b.related.connect(self.milk, alias='milk note', description='goes good with cereal')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
364
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
365 # ensure that the queryset is using the correct model and automatically
366 # determines that a GFKOptimizedQuerySet can be used
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
367 queryset = self.note_a.related.all()
368 self.assertEqual(queryset.model, AnotherRelatedObject)
369 self.assertTrue(isinstance(queryset, GFKOptimizedQuerySet))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
370
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
371 related_a = self.note_a.related.all()
372 self.assertRelatedEqual(related_a, (
373 (self.note_a, self.pizza),
374 (self.note_a, self.note_b),
375 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
376
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
377 related_b = self.note_b.related.all()
378 self.assertRelatedEqual(related_b, (
379 (self.note_b, self.cereal),
380 (self.note_b, self.milk),
381 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
382
b3b56f7 @coleifer Illustrate when models are mixed using different related_model and the
authored
383 related_to = self.note_b.related.related_to()
384 # note that pizza does not show up here even though it is related to note b
385 # this is because that relationship was stored in a different table (RelatedObject)
386 # as opposed to AnotherRelatedObject
387 self.assertEqual(related_to.generic_objects(), [self.note_a])
388
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
389 cereal_rel, milk_rel = related_b
a6657a9 @coleifer Smarter handling of reverse lookups
authored
390
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
391 # check that the custom attributes were saved correctly
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
392 self.assertEqual(cereal_rel.alias, 'cereal note')
393 self.assertEqual(cereal_rel.description, 'lucky charms!')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
394
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
395 self.assertEqual(milk_rel.alias, 'milk note')
396 self.assertEqual(milk_rel.description, 'goes good with cereal')
a6657a9 @coleifer Smarter handling of reverse lookups
authored
397
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
398 # check that we can filter on fields as expected
6985e7b @coleifer Beefing up test suite
authored
399 self.assertRelatedEqual(self.note_b.related.filter(alias='cereal note'), (
400 (self.note_b, self.cereal),
401 ))
a6657a9 @coleifer Smarter handling of reverse lookups
authored
402
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
403 related_c = self.note_c.related.all()
404 self.assertRelatedEqual(related_c, ())
a6657a9 @coleifer Smarter handling of reverse lookups
authored
405
a0c0e9a @coleifer Added notes to the testcases to clarify their intent
authored
406 # lastly, check that the GFKOptimizedQuerySet returns the expected
407 # results when doing the optimized lookup
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
408 self.assertEqual(related_a.generic_objects(), [
409 self.pizza, self.note_b
410 ])
a6657a9 @coleifer Smarter handling of reverse lookups
authored
411
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
412 self.assertEqual(related_b.generic_objects(), [
413 self.cereal, self.milk
414 ])
a6657a9 @coleifer Smarter handling of reverse lookups
authored
415
68dad53 @coleifer Beefing up the test suite to cover custom intermediary models
authored
416 self.assertEqual(related_c.generic_objects(), [])
0d5ccd1 @quiqueporta generic_objects can be filtered by model instance type. e.g.: pizza.rela...
quiqueporta authored
417
418 def test_generic_objects_filtered(self):
419 """
420 Get generic objects filtered by Model.
421 """
422 self.pizza.related.connect(self.beer)
423 self.pizza.related.connect(self.soda)
424 self.pizza.related.connect(self.mario)
425
426 # Get all generic related content
427 related = self.pizza.related.all()
428 objects = related.generic_objects()
429 self.assertEqual(objects, [self.mario, self.soda, self.beer])
430
431 # Get Person generic related content only.
432 related = self.pizza.related.all()
433 objects = related.generic_objects(Person)
434 self.assertEqual(objects, [self.mario])
Something went wrong with that request. Please try again.