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