Commits

Hynek Cernoch committed b85833a

Fixed an unused view. Added tests. Fixed text of warning

Comments (0)

Files changed (3)

keyedcache/__init__.py

 
     if not cache.key_prefix and (hasattr(settings, 'CACHE_PREFIX') or settings.SITE_ID != 1):
         if hasattr(settings, 'CACHE_PREFIX'):
-            warn("""The setting "CACHE_PREFIX" is obsoleted and ignored by keyedcache."""
-                 """ Use "CACHES = {'default': {... 'KEY_PREFIX': '...'}}" instead.""")
+            warn("The setting `CACHE_PREFIX` is obsoleted and is ignored by keyedcache.\n"
+                 """Use "CACHES = {'default': {... 'KEY_PREFIX': '...'}}" instead.""")
         if settings.SITE_ID != 1:
-            warn("If you use multiple sites, an explicit KEY_PREFIX should be defined"
-                 " in \"CACHES = {'default': {... 'KEY_PREFIX': '...'}}\" in order to"
-                 " differentiate caches or to explicitely confirm they should be shared."
-                 " An easy solution is \"'CACHE_PREFIX': str(settings.SITE_ID)\".")
+            hint = ("Use \"CACHES = {'default': {... 'KEY_PREFIX': '...'}}\" in order to\n"
+                    "differentiate caches or to explicitely confirm they should be shared.\n"
+                    " An easy solution is \"'CACHE_PREFIX': str(settings.SITE_ID)\".")
+            warn("An explicit KEY_PREFIX should be defined if you use multiple sites.\n%s" % hint)
         if not cache.__module__.split('.')[-1] in ('locmem', 'dummy'):
             raise ImproperlyConfigured(
                     "Setting KEY_PREFIX is obligatory for production caches. See the previous warning.")

keyedcache/tests.py

 import keyedcache
 import random
 from django.test import TestCase
+from django.contrib.auth.models import User
+from django.core.urlresolvers import reverse
+from keyedcache.views import stats_page, view_page, delete_page
+
 import time
 
 CACHE_HIT=0
         self.assertEqual(v, 'test::3::more::yes')
 
 
-
+class TestClient(TestCase):
+    def test_basic_views(self):
+        # Authentized user is not enough
+        user = User.objects.create_user('alice', 'alice@example.com', 'secret')
+        user.save()
+        self.client.login(username='alice', password='secret')
+        response = self.client.get(reverse(stats_page))
+        self.assertEqual(response.status_code, 302)
+        self.assertTrue('/login/' in response._headers['location'][1])
+        # User must be in staff
+        user.is_staff = True
+        user.save()
+        response = self.client.get(reverse(stats_page))
+        self.assertContains(response, 'Cache Hit Rate')
+        response = self.client.get(reverse(view_page))
+        self.assertContains(response, 'Cache Keys')
+        response = self.client.get(reverse(delete_page))
+        self.assertContains(response, 'Key to delete:')

keyedcache/views.py

         'cache_count' : len(keyedcache.CACHED_KEYS),
         'cache_running' : running,
         'cache_time' : keyedcache.CACHE_TIMEOUT,
-        'cache_backend' : keyedcache.CACHE_BACKEND,
+        'cache_backend' : keyedcache.cache.__module__,
         'cache_calls' : keyedcache.CACHE_CALLS,
         'cache_hits' : keyedcache.CACHE_HITS,
         'hit_rate' : "%02.1f" % rate