Ticket #17256: master...ticket-17256-contentypemanager-get_by_natural_key-doesnt-cache.diff

File master...ticket-17256-contentypemanager-get_by_natural_key-doesnt-cache.diff, 4.4 KB (added by Simon Charette, 12 years ago)
  • django/contrib/contenttypes/models.py

    diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py
    index 1bb07e0..6d919b4 100644
    a b def get_by_natural_key(self, app_label, model):  
    1313            ct = self.__class__._cache[self.db][(app_label, model)]
    1414        except KeyError:
    1515            ct = self.get(app_label=app_label, model=model)
     16            self._add_to_cache(self.db, ct)
    1617        return ct
    1718
    1819    def _get_opts(self, model):
  • django/contrib/contenttypes/tests.py

    diff --git a/django/contrib/contenttypes/tests.py b/django/contrib/contenttypes/tests.py
    index f2a338e..3b7906c 100644
    a b def tearDown(self):  
    5151    def test_lookup_cache(self):
    5252        """
    5353        Make sure that the content type cache (see ContentTypeManager)
    54         works correctly. Lookups for a particular content type -- by model or
    55         by ID -- should hit the database only on the first lookup.
     54        works correctly. Lookups for a particular content type -- by model, ID
     55        or natural key -- should hit the database only on the first lookup.
    5656        """
    5757
    5858        # At this point, a lookup for a ContentType should hit the DB
    def test_lookup_cache(self):  
    6060            ContentType.objects.get_for_model(ContentType)
    6161
    6262        # A second hit, though, won't hit the DB, nor will a lookup by ID
     63        # or natural key
    6364        with self.assertNumQueries(0):
    6465            ct = ContentType.objects.get_for_model(ContentType)
    6566        with self.assertNumQueries(0):
    6667            ContentType.objects.get_for_id(ct.id)
     68        with self.assertNumQueries(0):
     69            ContentType.objects.get_by_natural_key('contenttypes',
     70                                                   'contenttype')
    6771
    6872        # Once we clear the cache, another lookup will again hit the DB
    6973        ContentType.objects.clear_cache()
    7074        with self.assertNumQueries(1):
    7175            ContentType.objects.get_for_model(ContentType)
    7276
     77        # The same should happen with a lookup by natural key
     78        ContentType.objects.clear_cache()
     79        with self.assertNumQueries(1):
     80            ContentType.objects.get_by_natural_key('contenttypes',
     81                                                   'contenttype')
     82        # And a second hit shouldn't hit the DB
     83        with self.assertNumQueries(0):
     84            ContentType.objects.get_by_natural_key('contenttypes',
     85                                                   'contenttype')
     86
    7387    def test_get_for_models_empty_cache(self):
    7488        # Empty cache.
    7589        with self.assertNumQueries(1):
  • tests/regressiontests/comment_tests/tests/templatetag_tests.py

    diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
    index de39767..0b036ce 100644
    a b def testNumberQueries(self):  
    130130        with self.assertNumQueries(4):
    131131            self.testRenderCommentListFromObject()
    132132
    133         # Force the CT to be cached
    134         ct = ContentType.objects.get_for_model(Article)
     133        # CT's should be cached
    135134        with self.assertNumQueries(3):
    136135            self.testRenderCommentListFromObject()
    137136
    def testNumberQueries(self):  
    141140        with self.assertNumQueries(4):
    142141            self.verifyGetCommentList()
    143142
    144         ct = ContentType.objects.get_for_model(Author)
    145143        with self.assertNumQueries(3):
    146144            self.verifyGetCommentList()
    147145
    def testNumberQueries(self):  
    151149        with self.assertNumQueries(3):
    152150            self.testRenderCommentForm()
    153151
    154         ct = ContentType.objects.get_for_model(Article)
    155152        with self.assertNumQueries(2):
    156153            self.testRenderCommentForm()
    157154
    def testNumberQueries(self):  
    161158        with self.assertNumQueries(3):
    162159            self.testGetCommentForm()
    163160
    164         ct = ContentType.objects.get_for_model(Article)
    165161        with self.assertNumQueries(2):
    166162            self.testGetCommentForm()
    167163
    def testNumberQueries(self):  
    171167        with self.assertNumQueries(3):
    172168            self.verifyGetCommentCount()
    173169
    174         ct = ContentType.objects.get_for_model(Article)
    175170        with self.assertNumQueries(2):
    176171            self.verifyGetCommentCount()
Back to Top