Ticket #17256: ticket-17256-with-tests.diff
File ticket-17256-with-tests.diff, 2.6 KB (added by , 13 years ago) |
---|
-
django/contrib/contenttypes/tests.py
44 44 def test_lookup_cache(self): 45 45 """ 46 46 Make sure that the content type cache (see ContentTypeManager) 47 works correctly. Lookups for a particular content type -- by model or48 by ID-- should hit the database only on the first lookup.47 works correctly. Lookups for a particular content type -- by model, ID 48 or natural key -- should hit the database only on the first lookup. 49 49 """ 50 50 51 51 # At this point, a lookup for a ContentType should hit the DB … … 53 53 ContentType.objects.get_for_model(ContentType) 54 54 55 55 # A second hit, though, won't hit the DB, nor will a lookup by ID 56 # or natural key 56 57 with self.assertNumQueries(0): 57 58 ct = ContentType.objects.get_for_model(ContentType) 58 59 with self.assertNumQueries(0): 59 60 ContentType.objects.get_for_id(ct.id) 61 with self.assertNumQueries(0): 62 ContentType.objects.get_by_natural_key('contenttypes', 63 'contenttype') 60 64 61 65 # Once we clear the cache, another lookup will again hit the DB 62 66 ContentType.objects.clear_cache() 63 67 with self.assertNumQueries(1): 64 68 ContentType.objects.get_for_model(ContentType) 65 69 70 # The same should happen with a lookup by natural key 71 ContentType.objects.clear_cache() 72 with self.assertNumQueries(1): 73 ContentType.objects.get_by_natural_key('contenttypes', 74 'contenttype') 75 # And a second hit shouldn't hit the DB 76 with self.assertNumQueries(0): 77 ContentType.objects.get_by_natural_key('contenttypes', 78 'contenttype') 79 66 80 def test_get_for_models_empty_cache(self): 67 81 # Empty cache. 68 82 with self.assertNumQueries(1): -
django/contrib/contenttypes/models.py
13 13 ct = self.__class__._cache[self.db][(app_label, model)] 14 14 except KeyError: 15 15 ct = self.get(app_label=app_label, model=model) 16 self._add_to_cache(self.db, ct) 16 17 return ct 17 18 18 19 def _get_opts(self, model):