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.
6
First, let's make sure we're dealing with a blank slate (and that DEBUG is on so
7
that queries get logged)::
9
>>> from django.conf import settings
10
>>> settings.DEBUG = True
12
>>> from django.contrib.contenttypes.models import ContentType
13
>>> ContentType.objects.clear_cache()
15
>>> from django import db
16
>>> db.reset_queries()
18
At this point, a lookup for a ContentType should hit the DB::
20
>>> ContentType.objects.get_for_model(ContentType)
21
<ContentType: content type>
23
>>> len(db.connection.queries)
26
A second hit, though, won't hit the DB, nor will a lookup by ID::
28
>>> ct = ContentType.objects.get_for_model(ContentType)
29
>>> len(db.connection.queries)
31
>>> ContentType.objects.get_for_id(ct.id)
32
<ContentType: content type>
33
>>> len(db.connection.queries)
36
Once we clear the cache, another lookup will again hit the DB::
38
>>> ContentType.objects.clear_cache()
39
>>> ContentType.objects.get_for_model(ContentType)
40
<ContentType: content type>
41
>>> len(db.connection.queries)
44
Don't forget to reset DEBUG!
46
>>> settings.DEBUG = False
b'\\ No newline at end of file'
2
from django.conf import settings
3
from django.contrib.contenttypes.models import ContentType
4
from django.contrib.sites.models import Site
5
from django.contrib.contenttypes.views import shortcut
6
from django.core.exceptions import ObjectDoesNotExist
7
from django.http import HttpRequest
8
from django.test import TestCase
11
class ContentTypesTests(TestCase):
14
# First, let's make sure we're dealing with a blank slate (and that
15
# DEBUG is on so that queries get logged)
16
self.old_DEBUG = settings.DEBUG
17
self.old_Site_meta_installed = Site._meta.installed
19
ContentType.objects.clear_cache()
23
settings.DEBUG = self.old_DEBUG
24
Site._meta.installed = self.old_Site_meta_installed
26
def test_lookup_cache(self):
28
Make sure that the content type cache (see ContentTypeManager)
29
works correctly. Lookups for a particular content type -- by model or
30
by ID -- should hit the database only on the first lookup.
33
# At this point, a lookup for a ContentType should hit the DB
34
ContentType.objects.get_for_model(ContentType)
35
self.assertEqual(1, len(db.connection.queries))
37
# A second hit, though, won't hit the DB, nor will a lookup by ID
38
ct = ContentType.objects.get_for_model(ContentType)
39
self.assertEqual(1, len(db.connection.queries))
40
ContentType.objects.get_for_id(ct.id)
41
self.assertEqual(1, len(db.connection.queries))
43
# Once we clear the cache, another lookup will again hit the DB
44
ContentType.objects.clear_cache()
45
ContentType.objects.get_for_model(ContentType)
46
len(db.connection.queries)
47
self.assertEqual(2, len(db.connection.queries))
49
def test_shortcut_view(self):
51
Check that the shortcut view (used for the admin "view on site"
52
functionality) returns a complete URL regardless of whether the sites
53
framework is installed
56
request = HttpRequest()
58
"SERVER_NAME": "Example.com",
61
from django.contrib.auth.models import User
62
user_ct = ContentType.objects.get_for_model(User)
63
obj = User.objects.create(username="john")
65
if Site._meta.installed:
66
response = shortcut(request, user_ct.id, obj.id)
67
self.assertEqual("http://example.com/users/john/", response._headers.get("location")[1])
69
Site._meta.installed = False
70
response = shortcut(request, user_ct.id, obj.id)
71
self.assertEqual("http://Example.com/users/john/", response._headers.get("location")[1])