~ubuntu-branches/ubuntu/jaunty/python-django/jaunty

« back to all changes in this revision

Viewing changes to django/contrib/contenttypes/tests.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
"""
 
 
b'\\ No newline at end of file'