PageRenderTime 223ms CodeModel.GetById 217ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/contenttypes/tests.py

https://code.google.com/p/mango-py/
Python | 75 lines | 55 code | 9 blank | 11 comment | 0 complexity | 578da266362a412a08d54fd3e7395370 MD5 | raw file
 1from django import db
 2from django.conf import settings
 3from django.contrib.contenttypes.models import ContentType
 4from django.contrib.sites.models import Site
 5from django.contrib.contenttypes.views import shortcut
 6from django.core.exceptions import ObjectDoesNotExist
 7from django.http import HttpRequest
 8from django.test import TestCase
 9
10
11class 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        ContentType.objects.clear_cache()
26
27    def test_lookup_cache(self):
28        """
29        Make sure that the content type cache (see ContentTypeManager)
30        works correctly. Lookups for a particular content type -- by model or
31        by ID -- should hit the database only on the first lookup.
32        """
33
34        # At this point, a lookup for a ContentType should hit the DB
35        ContentType.objects.get_for_model(ContentType)
36        self.assertEqual(1, len(db.connection.queries))
37
38        # A second hit, though, won't hit the DB, nor will a lookup by ID
39        ct = ContentType.objects.get_for_model(ContentType)
40        self.assertEqual(1, len(db.connection.queries))
41        ContentType.objects.get_for_id(ct.id)
42        self.assertEqual(1, len(db.connection.queries))
43
44        # Once we clear the cache, another lookup will again hit the DB
45        ContentType.objects.clear_cache()
46        ContentType.objects.get_for_model(ContentType)
47        len(db.connection.queries)
48        self.assertEqual(2, len(db.connection.queries))
49
50    def test_shortcut_view(self):
51        """
52        Check that the shortcut view (used for the admin "view on site"
53        functionality) returns a complete URL regardless of whether the sites
54        framework is installed
55        """
56
57        request = HttpRequest()
58        request.META = {
59            "SERVER_NAME": "Example.com",
60            "SERVER_PORT": "80",
61        }
62        from django.contrib.auth.models import User
63        user_ct = ContentType.objects.get_for_model(User)
64        obj = User.objects.create(username="john")
65
66        if Site._meta.installed:
67            current_site = Site.objects.get_current()
68            response = shortcut(request, user_ct.id, obj.id)
69            self.assertEqual("http://%s/users/john/" % current_site.domain,
70                             response._headers.get("location")[1])
71
72        Site._meta.installed = False
73        response = shortcut(request, user_ct.id, obj.id)
74        self.assertEqual("http://Example.com/users/john/",
75                         response._headers.get("location")[1])