Commit ac1da82b authored by Sean Bleier's avatar Sean Bleier

Let's raise exceptions when provided bad db and port values. Also Fixed tests…

Let's raise exceptions when provided bad db and port values. Also Fixed tests and bumped version.  Thanks to Lior Sion
parent 00ca1c07
......@@ -8,4 +8,7 @@
*.wsgi
local_settings.py
development_settings.py
*.egg-info
\ No newline at end of file
*.egg-info
.project
.pydevproject
.settings
\ No newline at end of file
......@@ -5,5 +5,4 @@ S. Angel / Twidi <http://github.com/twidi>
Noah Kantrowitz / coderanger <http://github.com/coderanger>
Martin Mahner / bartTC <http://github.com/bartTC>
Timothée Peignier / cyberdelia <https://github.com/cyberdelia>
Forked from http://github.com/sebleier/django-redis-cache
Lior Sion / liorsion <https://github.com/liorsion>
from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
from django.utils.encoding import smart_unicode, smart_str
from django.utils.datastructures import SortedDict
from django.conf import settings
try:
import cPickle as pickle
......@@ -15,6 +14,11 @@ except ImportError:
"Redis cache backend requires the 'redis-py' library")
class ImproperlyConfigured(Exception):
"Django Redis Cache is somehow improperly configured"
pass
class CacheConnectionPool(object):
_connection_pool = None
......@@ -63,13 +67,13 @@ class CacheClass(BaseCache):
try:
db = int(db)
except (ValueError, TypeError):
db = 1
raise ImproperlyConfigured("db value must be an integer")
if ':' in server:
host, port = server.split(':')
try:
port = int(port)
except (ValueError, TypeError):
port = 6379
raise ImproperlyConfigured("port value must be an integer")
else:
host = server or 'localhost'
port = 6379
......
......@@ -5,7 +5,7 @@ setup(
url = "http://github.com/sebleier/django-redis-cache/",
author = "Sean Bleier",
author_email = "sebleier@gmail.com",
version = "0.6.0",
version = "0.6.1",
packages = ["redis_cache"],
description = "Redis Cache Backend for Django",
install_requires=['redis>=2.4.5',],
......
......@@ -10,7 +10,7 @@ from django import VERSION
from django.core.cache import get_cache
from django.test import TestCase
from models import Poll, expensive_calculation
from redis_cache.cache import RedisCache
from redis_cache.cache import RedisCache, pool, ImproperlyConfigured
# functions/classes for complex data type tests
def f():
......@@ -26,11 +26,15 @@ class RedisCacheTests(TestCase):
"""
def setUp(self):
# use DB 16 for testing and hope there isn't any important data :->
self.reset_pool()
self.cache = self.get_cache()
def tearDown(self):
self.cache.clear()
def reset_pool(self):
pool._connection_pool = None
def get_cache(self, backend=None):
if VERSION[0] == 1 and VERSION[1] < 3:
cache = get_cache(backend or 'redis_cache.cache://127.0.0.1:6379?db=15')
......@@ -39,14 +43,13 @@ class RedisCacheTests(TestCase):
return cache
def test_bad_db_initialization(self):
self.cache = self.get_cache('redis_cache.cache://127.0.0.1:6379?db=not_a_number')
self.assertEqual(self.cache._cache.connection_pool.connection_kwargs['db'], 1)
self.assertRaises(ImproperlyConfigured, self.get_cache, 'redis_cache.cache://127.0.0.1:6379?db=not_a_number')
def test_bad_port_initialization(self):
self.cache = self.get_cache('redis_cache.cache://127.0.0.1:not_a_number?db=15')
self.assertEqual(self.cache._cache.connection_pool.connection_kwargs['port'], 6379)
self.assertRaises(ImproperlyConfigured, self.get_cache, 'redis_cache.cache://127.0.0.1:not_a_number?db=15')
def test_default_initialization(self):
self.reset_pool()
self.cache = self.get_cache('redis_cache.cache://127.0.0.1')
self.assertEqual(self.cache._cache.connection_pool.connection_kwargs['host'], '127.0.0.1')
self.assertEqual(self.cache._cache.connection_pool.connection_kwargs['db'], 1)
......@@ -303,8 +306,8 @@ class RedisCacheTests(TestCase):
self.assertEqual(self.cache.get(new_key), 'spam')
def test_connection_pool(self):
# First, let's make sure that one connection exists in the pool
self.assertEqual(self.cache._cache.connection_pool._created_connections, 1)
# First, let's make sure there are no connections in the pool
self.assertEqual(self.cache._cache.connection_pool._created_connections, 0)
# Now, let's tie up two connections in the pool.
c1 = self.cache._cache.connection_pool.get_connection("_")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment