app/django/contrib/contenttypes/tests.py
changeset 54 03e267d67478
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 """
       
     2 Make sure that the content type cache (see ContentTypeManager) works correctly.
       
     3 Lookups for a particular content type -- by model or by ID -- should hit the
       
     4 database only on the first lookup.
       
     5 
       
     6 First, let's make sure we're dealing with a blank slate (and that DEBUG is on so
       
     7 that queries get logged)::
       
     8 
       
     9     >>> from django.conf import settings
       
    10     >>> settings.DEBUG = True
       
    11 
       
    12     >>> from django.contrib.contenttypes.models import ContentType
       
    13     >>> ContentType.objects.clear_cache()
       
    14 
       
    15     >>> from django import db
       
    16     >>> db.reset_queries()
       
    17     
       
    18 At this point, a lookup for a ContentType should hit the DB::
       
    19 
       
    20     >>> ContentType.objects.get_for_model(ContentType)
       
    21     <ContentType: content type>
       
    22     
       
    23     >>> len(db.connection.queries)
       
    24     1
       
    25 
       
    26 A second hit, though, won't hit the DB, nor will a lookup by ID::
       
    27 
       
    28     >>> ct = ContentType.objects.get_for_model(ContentType)
       
    29     >>> len(db.connection.queries)
       
    30     1
       
    31     >>> ContentType.objects.get_for_id(ct.id)
       
    32     <ContentType: content type>
       
    33     >>> len(db.connection.queries)
       
    34     1
       
    35 
       
    36 Once we clear the cache, another lookup will again hit the DB::
       
    37 
       
    38     >>> ContentType.objects.clear_cache()
       
    39     >>> ContentType.objects.get_for_model(ContentType)
       
    40     <ContentType: content type>
       
    41     >>> len(db.connection.queries)
       
    42     2
       
    43 
       
    44 Don't forget to reset DEBUG!
       
    45 
       
    46     >>> settings.DEBUG = False
       
    47 """