PageRenderTime 32ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/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
Possible License(s): BSD-3-Clause
  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. class ContentTypesTests(TestCase):
  10. def setUp(self):
  11. # First, let's make sure we're dealing with a blank slate (and that
  12. # DEBUG is on so that queries get logged)
  13. self.old_DEBUG = settings.DEBUG
  14. self.old_Site_meta_installed = Site._meta.installed
  15. settings.DEBUG = True
  16. ContentType.objects.clear_cache()
  17. db.reset_queries()
  18. def tearDown(self):
  19. settings.DEBUG = self.old_DEBUG
  20. Site._meta.installed = self.old_Site_meta_installed
  21. ContentType.objects.clear_cache()
  22. def test_lookup_cache(self):
  23. """
  24. Make sure that the content type cache (see ContentTypeManager)
  25. works correctly. Lookups for a particular content type -- by model or
  26. by ID -- should hit the database only on the first lookup.
  27. """
  28. # At this point, a lookup for a ContentType should hit the DB
  29. ContentType.objects.get_for_model(ContentType)
  30. self.assertEqual(1, len(db.connection.queries))
  31. # A second hit, though, won't hit the DB, nor will a lookup by ID
  32. ct = ContentType.objects.get_for_model(ContentType)
  33. self.assertEqual(1, len(db.connection.queries))
  34. ContentType.objects.get_for_id(ct.id)
  35. self.assertEqual(1, len(db.connection.queries))
  36. # Once we clear the cache, another lookup will again hit the DB
  37. ContentType.objects.clear_cache()
  38. ContentType.objects.get_for_model(ContentType)
  39. len(db.connection.queries)
  40. self.assertEqual(2, len(db.connection.queries))
  41. def test_shortcut_view(self):
  42. """
  43. Check that the shortcut view (used for the admin "view on site"
  44. functionality) returns a complete URL regardless of whether the sites
  45. framework is installed
  46. """
  47. request = HttpRequest()
  48. request.META = {
  49. "SERVER_NAME": "Example.com",
  50. "SERVER_PORT": "80",
  51. }
  52. from django.contrib.auth.models import User
  53. user_ct = ContentType.objects.get_for_model(User)
  54. obj = User.objects.create(username="john")
  55. if Site._meta.installed:
  56. current_site = Site.objects.get_current()
  57. response = shortcut(request, user_ct.id, obj.id)
  58. self.assertEqual("http://%s/users/john/" % current_site.domain,
  59. response._headers.get("location")[1])
  60. Site._meta.installed = False
  61. response = shortcut(request, user_ct.id, obj.id)
  62. self.assertEqual("http://Example.com/users/john/",
  63. response._headers.get("location")[1])