~ubuntu-branches/ubuntu/precise/python-django/precise

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2011-02-17 13:34:07 UTC
  • mfrom: (1.1.13 upstream) (4.4.12 sid)
  • Revision ID: james.westby@ubuntu.com-20110217133407-rwr88elhhq6j7ba0
Tags: 1.2.5-1ubuntu1
* Merge from Debian for security fixes (LP: #719031). Remaining changes:
  - debian/control: don't Build-Depends on locales-all, which doesn't exist
    in natty
* Drop the following patches, now included upstream:
  - debian/patches/07_security_admin_infoleak.diff
  - debian/patches/08_security_pasword_reset_dos.diff

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'
 
1
from django import db
 
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
 
9
 
 
10
 
 
11
class ContentTypesTests(TestCase):
 
12
 
 
13
    def setUp(self):
 
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
 
18
        settings.DEBUG = True
 
19
        ContentType.objects.clear_cache()
 
20
        db.reset_queries()
 
21
 
 
22
    def tearDown(self):
 
23
        settings.DEBUG = self.old_DEBUG
 
24
        Site._meta.installed = self.old_Site_meta_installed
 
25
 
 
26
    def test_lookup_cache(self):
 
27
        """
 
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.
 
31
        """
 
32
 
 
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))
 
36
 
 
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))
 
42
 
 
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))
 
48
 
 
49
    def test_shortcut_view(self):
 
50
        """
 
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
 
54
        """
 
55
 
 
56
        request = HttpRequest()
 
57
        request.META = {
 
58
            "SERVER_NAME": "Example.com",
 
59
            "SERVER_PORT": "80",
 
60
        }
 
61
        from django.contrib.auth.models import User
 
62
        user_ct = ContentType.objects.get_for_model(User)
 
63
        obj = User.objects.create(username="john")
 
64
 
 
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])
 
68
 
 
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])