Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added notes to the testcases to clarify their intent

  • Loading branch information...
commit a0c0e9a5a256a2cf353ae13aceecb83a83796e7d 1 parent 37e69d3
Charles Leifer authored February 23, 2011

Showing 1 changed file with 70 additions and 1 deletion. Show diff stats Hide diff stats

  1. 71  genericm2m/genericm2m_tests/tests.py
71  genericm2m/genericm2m_tests/tests.py
@@ -33,6 +33,10 @@ def assertRelatedEqual(self, rel_qs, tups, from_field='parent',
33 33
         self.assertEqual(rel_tup, list(tups))
34 34
     
35 35
     def test_connect(self):
  36
+        """
  37
+        Connect model instances to various other model instances, then query
  38
+        the manager and check the queryset returned is correct
  39
+        """
36 40
         self.pizza.related.connect(self.soda)
37 41
         self.pizza.related.connect(self.beer)
38 42
         self.pizza.related.connect(self.mario)
@@ -75,6 +79,11 @@ def test_connect(self):
75 79
         ))
76 80
     
77 81
     def test_related_to(self):
  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
+        """
78 87
         self.pizza.related.connect(self.soda)
79 88
         self.pizza.related.connect(self.beer)
80 89
         self.pizza.related.connect(self.table)
@@ -106,24 +115,40 @@ def test_related_to(self):
106 115
         ))
107 116
 
108 117
     def test_manager_methods(self):
  118
+        """
  119
+        Since the RelatedObjectsDescriptor behaves like a dynamic manager (much
  120
+        the same as Django's ForeignRelatedObjectsDescriptor) test to ensure
  121
+        that the manager behaves as expected and correctly implements all the
  122
+        basic FK methods
  123
+        """
  124
+        # connect pizza to soda and grab the newly-created RelatedObject
109 125
         self.pizza.related.connect(self.soda)
110  
-        rel_obj = RelatedObject.objects.all()[0] # grab the new related obj
  126
+        rel_obj = RelatedObject.objects.all()[0]
111 127
         
  128
+        # connect cereal to milk (this is just to make sure that anything
  129
+        # modified on one Food object doesn't affect another Food object
112 130
         self.cereal.related.connect(self.milk)
113 131
         
  132
+        # create a new RelatedObject but do not save it yet -- note that it does
  133
+        # not have `parent_object` set
114 134
         new_rel_obj = RelatedObject(object=self.beer)
115 135
         
  136
+        # add this related object to pizza, parent_object gets set and it will
  137
+        # show up in the queryset as expected
116 138
         self.pizza.related.add(new_rel_obj)
117 139
         self.assertRelatedEqual(self.pizza.related.all(), (
118 140
             (self.pizza, self.beer),
119 141
             (self.pizza, self.soda),
120 142
         ))
121 143
         
  144
+        # remove the original RelatedObject `rel_obj`, which was the connection
  145
+        # from pizza -> soda
122 146
         self.pizza.related.remove(rel_obj)
123 147
         self.assertRelatedEqual(self.pizza.related.all(), (
124 148
             (self.pizza, self.beer),
125 149
         ))
126 150
         
  151
+        # make sure clearing pizza's related queryset works
127 152
         self.pizza.related.clear()
128 153
         self.assertRelatedEqual(self.pizza.related.all(), ())
129 154
         
@@ -132,9 +157,16 @@ def test_manager_methods(self):
132 157
             (self.cereal, self.milk),
133 158
         ))
134 159
         
  160
+        # there should be just one row in the table
135 161
         self.assertEqual(RelatedObject.objects.count(), 1)
136 162
     
137 163
     def test_model_level(self):
  164
+        """
  165
+        The RelatedObjectsDescriptor can work at the class-level as well and
  166
+        applies to all instances of the model - check that when connections are
  167
+        made between individual instances and then are queried via the class,
  168
+        that all connections are returned from that model type
  169
+        """
138 170
         self.pizza.related.connect(self.beer)
139 171
         self.cereal.related.connect(self.milk)
140 172
         
@@ -157,6 +189,10 @@ def test_model_level(self):
157 189
         ))
158 190
     
159 191
     def test_custom_connect(self):
  192
+        """
  193
+        Mimic the test_connect() method, but instead use the custom descriptor,
  194
+        `related_beverages` which goes through the RelatedBeverage model
  195
+        """
160 196
         self.pizza.related_beverages.connect(self.soda)
161 197
         self.pizza.related_beverages.connect(self.beer)
162 198
         
@@ -179,6 +215,10 @@ def test_custom_connect(self):
179 215
         self.assertRelatedEqual(related, ())
180 216
 
181 217
     def test_custom_model_manager(self):
  218
+        """
  219
+        Mimic the test_model_manager() method, but instead use the custom
  220
+        descriptor and through model
  221
+        """
182 222
         self.pizza.related_beverages.connect(self.soda)
183 223
         rel_obj = RelatedBeverage.objects.all()[0] # grab the new related obj
184 224
         
@@ -208,6 +248,10 @@ def test_custom_model_manager(self):
208 248
         self.assertEqual(RelatedBeverage.objects.count(), 1)
209 249
     
210 250
     def test_custom_model_level(self):
  251
+        """
  252
+        And lastly, test that the custom descriptor/through-model work as
  253
+        expected at the model-level (previous tests were instance-level)
  254
+        """
211 255
         self.pizza.related_beverages.connect(self.soda)
212 256
         self.pizza.related_beverages.connect(self.beer)
213 257
         self.sandwich.related_beverages.connect(self.soda)
@@ -221,17 +265,31 @@ def test_custom_model_level(self):
221 265
         ), 'food', 'beverage')
222 266
     
223 267
     def test_generic_traversal(self):
  268
+        """
  269
+        Ensure that the RelatedObjectsDescriptor returns a GFKOptimizedQuerySet
  270
+        when the through model contains a GFK -- also check that the queryset's
  271
+        optimized lookup works as expected
  272
+        """
224 273
         self.pizza.related.connect(self.beer)
225 274
         self.pizza.related.connect(self.soda)
226 275
         self.pizza.related.connect(self.mario)
227 276
         
  277
+        # the manager returns instances of GFKOptimizedQuerySet
228 278
         related = self.pizza.related.all()
229 279
         self.assertEqual(type(related), GFKOptimizedQuerySet)
230 280
         
  281
+        # check the queryset is using the right field
  282
+        self.assertEqual(related.get_gfk().name, 'object')
  283
+        
  284
+        # the custom queryset's optimized lookup works correctly
231 285
         objects = related.generic_objects()
232 286
         self.assertEqual(objects, [self.mario, self.soda, self.beer])
233 287
     
234 288
     def test_filtering(self):
  289
+        """
  290
+        Check that filtering on RelatedObject fields (or through model fields)
  291
+        works as expected
  292
+        """
235 293
         self.pizza.related.connect(self.beer, alias='bud lite')
236 294
         self.pizza.related.connect(self.soda, alias='pepsi')
237 295
         self.pizza.related.connect(self.mario)
@@ -253,6 +311,10 @@ def test_filtering(self):
253 311
         ))
254 312
         
255 313
     def test_custom_model_using_gfks(self):
  314
+        """
  315
+        Check that using a custom through model with GFKs works as expected
  316
+        (looking at models.py, Note uses `AnotherRelatedObject` as its through)
  317
+        """
256 318
         self.note_a = Note.objects.create(content='a')
257 319
         self.note_b = Note.objects.create(content='b')
258 320
         self.note_c = Note.objects.create(content='c')
@@ -260,9 +322,12 @@ def test_custom_model_using_gfks(self):
260 322
         self.note_a.related.connect(self.pizza)
261 323
         self.note_a.related.connect(self.note_b)
262 324
         
  325
+        # create some notes with custom attributes
263 326
         self.note_b.related.connect(self.cereal, alias='cereal note', description='lucky charms!')
264 327
         self.note_b.related.connect(self.milk, alias='milk note', description='goes good with cereal')
265 328
         
  329
+        # ensure that the queryset is using the correct model and automatically
  330
+        # determines that a GFKOptimizedQuerySet can be used
266 331
         queryset = self.note_a.related.all()
267 332
         self.assertEqual(queryset.model, AnotherRelatedObject)
268 333
         self.assertTrue(isinstance(queryset, GFKOptimizedQuerySet))
@@ -281,12 +346,14 @@ def test_custom_model_using_gfks(self):
281 346
         
282 347
         cereal_rel, milk_rel = related_b
283 348
         
  349
+        # check that the custom attributes were saved correctly
284 350
         self.assertEqual(cereal_rel.alias, 'cereal note')
285 351
         self.assertEqual(cereal_rel.description, 'lucky charms!')
286 352
         
287 353
         self.assertEqual(milk_rel.alias, 'milk note')
288 354
         self.assertEqual(milk_rel.description, 'goes good with cereal')
289 355
         
  356
+        # check that we can filter on fields as expected
290 357
         self.assertRelatedEqual(self.note_b.related.filter(alias='cereal note'), (
291 358
             (self.note_b, self.cereal),
292 359
         ))
@@ -294,6 +361,8 @@ def test_custom_model_using_gfks(self):
294 361
         related_c = self.note_c.related.all()
295 362
         self.assertRelatedEqual(related_c, ())
296 363
         
  364
+        # lastly, check that the GFKOptimizedQuerySet returns the expected
  365
+        # results when doing the optimized lookup
297 366
         self.assertEqual(related_a.generic_objects(), [
298 367
             self.pizza, self.note_b
299 368
         ])

0 notes on commit a0c0e9a

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