Ticket #17256: ticket-17256-with-tests.diff

File ticket-17256-with-tests.diff, 2.6 KB (added by Simon Charette, 13 years ago)

Updated patch with tests

  • django/contrib/contenttypes/tests.py

     
    4444    def test_lookup_cache(self):
    4545        """
    4646        Make sure that the content type cache (see ContentTypeManager)
    47         works correctly. Lookups for a particular content type -- by model or
    48         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.
    4949        """
    5050
    5151        # At this point, a lookup for a ContentType should hit the DB
     
    5353            ContentType.objects.get_for_model(ContentType)
    5454
    5555        # A second hit, though, won't hit the DB, nor will a lookup by ID
     56        # or natural key
    5657        with self.assertNumQueries(0):
    5758            ct = ContentType.objects.get_for_model(ContentType)
    5859        with self.assertNumQueries(0):
    5960            ContentType.objects.get_for_id(ct.id)
     61        with self.assertNumQueries(0):
     62            ContentType.objects.get_by_natural_key('contenttypes',
     63                                                   'contenttype')
    6064
    6165        # Once we clear the cache, another lookup will again hit the DB
    6266        ContentType.objects.clear_cache()
    6367        with self.assertNumQueries(1):
    6468            ContentType.objects.get_for_model(ContentType)
    6569
     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
    6680    def test_get_for_models_empty_cache(self):
    6781        # Empty cache.
    6882        with self.assertNumQueries(1):
  • django/contrib/contenttypes/models.py

     
    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):
Back to Top