diff options
author | andy <andy@whiskeymedia.com> | 2013-06-06 11:30:23 -0700 |
---|---|---|
committer | andy <andy@whiskeymedia.com> | 2013-06-06 11:30:23 -0700 |
commit | 6a69aa50797f95b8fd426651ac0b27d7718036d9 (patch) | |
tree | 33bc095766d7193aabc2c7a903c00c52b64c0a8f | |
parent | a207d76939b640634095a49c4790003839a0a669 (diff) | |
download | redis-py-6a69aa50797f95b8fd426651ac0b27d7718036d9.tar.gz |
encoding tests
-rw-r--r-- | redis/connection.py | 11 | ||||
-rw-r--r-- | redis/utils.py | 10 | ||||
-rw-r--r-- | tests/conftest.py | 12 | ||||
-rw-r--r-- | tests/encoding.py | 65 | ||||
-rw-r--r-- | tests/test_encoding.py | 34 |
5 files changed, 52 insertions, 80 deletions
diff --git a/redis/connection.py b/redis/connection.py index 343c838..7e8cb44 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -16,12 +16,9 @@ from redis.exceptions import ( NoScriptError, ExecAbortError, ) - -try: +from redis.utils import HIREDIS_AVAILABLE +if HIREDIS_AVAILABLE: import hiredis - hiredis_available = True -except ImportError: - hiredis_available = False SYM_STAR = b('*') @@ -145,7 +142,7 @@ class PythonParser(object): class HiredisParser(object): "Parser class for connections using Hiredis" def __init__(self): - if not hiredis_available: + if not HIREDIS_AVAILABLE: raise RedisError("Hiredis is not installed") def __del__(self): @@ -189,7 +186,7 @@ class HiredisParser(object): response = self._reader.gets() return response -if hiredis_available: +if HIREDIS_AVAILABLE: DefaultParser = HiredisParser else: DefaultParser = PythonParser diff --git a/redis/utils.py b/redis/utils.py index 7c04a71..ee681bf 100644 --- a/redis/utils.py +++ b/redis/utils.py @@ -1,10 +1,16 @@ -from redis.client import Redis +try: + import hiredis + HIREDIS_AVAILABLE = True +except ImportError: + HIREDIS_AVAILABLE = False def from_url(url, db=None, **kwargs): - """Returns an active Redis client generated from the given database URL. + """ + Returns an active Redis client generated from the given database URL. Will attempt to extract the database id from the path url fragment, if none is provided. """ + from redis.client import Redis return Redis.from_url(url, db, **kwargs) diff --git a/tests/conftest.py b/tests/conftest.py index b3a2e5b..89b9b58 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,8 +2,8 @@ import pytest import redis -def _get_client(cls, request=None): - client = cls(host='localhost', port=6379, db=9) +def _get_client(cls, request=None, **kwargs): + client = cls(host='localhost', port=6379, db=9, **kwargs) client.flushdb() if request: request.addfinalizer(client.flushdb) @@ -17,10 +17,10 @@ def skip_if_server_version_lt(min_version): @pytest.fixture() -def r(request): - return _get_client(redis.Redis, request) +def r(request, **kwargs): + return _get_client(redis.Redis, request, **kwargs) @pytest.fixture() -def sr(request): - return _get_client(redis.StrictRedis, request) +def sr(request, **kwargs): + return _get_client(redis.StrictRedis, request, **kwargs) diff --git a/tests/encoding.py b/tests/encoding.py deleted file mode 100644 index b5aa58d..0000000 --- a/tests/encoding.py +++ /dev/null @@ -1,65 +0,0 @@ -from __future__ import with_statement -import unittest - -from redis._compat import unichr, u, unicode, b -from redis.client import list_or_args -from redis.connection import ConnectionPool, PythonParser, HiredisParser -import redis - - -class EncodingTestCase(unittest.TestCase): - def setUp(self): - self.client = redis.Redis( - host='localhost', port=6379, db=9, charset='utf-8') - self.client.flushdb() - - def tearDown(self): - self.client.flushdb() - - def test_simple_encoding(self): - unicode_string = unichr(3456) + u('abcd') + unichr(3421) - self.client.set('unicode-string', unicode_string) - cached_val = self.client.get('unicode-string') - self.assertEquals( - unicode.__name__, type(cached_val).__name__, - 'Cache returned value with type "%s", expected "%s"' % - (type(cached_val).__name__, unicode.__name__)) - self.assertEqual(unicode_string, cached_val) - - def test_list_encoding(self): - unicode_string = unichr(3456) + u('abcd') + unichr(3421) - result = [unicode_string, unicode_string, unicode_string] - for i in range(len(result)): - self.client.rpush('a', unicode_string) - self.assertEquals(self.client.lrange('a', 0, -1), result) - - def test_list_or_args(self): - bfoo = b('foo') - ufoo = u('foo') - # first record is a text instance - self.assertEquals(list_or_args(ufoo, []), [ufoo]) - self.assertEquals(list_or_args(ufoo, [ufoo]), [ufoo, ufoo]) - # first record is a list - self.assertEquals(list_or_args([ufoo], [ufoo]), [ufoo, ufoo]) - # first record is a binary instance - self.assertEquals(list_or_args(bfoo, []), [bfoo]) - self.assertEquals(list_or_args(bfoo, [bfoo]), [bfoo, bfoo]) - - -class PythonParserEncodingTestCase(EncodingTestCase): - def setUp(self): - pool = ConnectionPool( - host='localhost', port=6379, db=9, - encoding='utf-8', decode_responses=True, parser_class=PythonParser) - self.client = redis.Redis(connection_pool=pool) - self.client.flushdb() - - -class HiredisEncodingTestCase(EncodingTestCase): - def setUp(self): - pool = ConnectionPool( - host='localhost', port=6379, db=9, - encoding='utf-8', decode_responses=True, - parser_class=HiredisParser) - self.client = redis.Redis(connection_pool=pool) - self.client.flushdb() diff --git a/tests/test_encoding.py b/tests/test_encoding.py new file mode 100644 index 0000000..5a02736 --- /dev/null +++ b/tests/test_encoding.py @@ -0,0 +1,34 @@ +from __future__ import with_statement +import pytest + +from redis._compat import unichr, u, unicode +from redis.utils import HIREDIS_AVAILABLE +from .conftest import r as _redis_client + + +@pytest.fixture(scope="module") +def r(request): + return _redis_client(request=request, decode_responses=True) + + +class BaseEncodingTests(object): + def test_simple_encoding(self, r): + unicode_string = unichr(3456) + u('abcd') + unichr(3421) + r['unicode-string'] = unicode_string + cached_val = r['unicode-string'] + assert isinstance(cached_val, unicode) + assert unicode_string == cached_val + + def test_list_encoding(self, r): + unicode_string = unichr(3456) + u('abcd') + unichr(3421) + result = [unicode_string, unicode_string, unicode_string] + r.rpush('a', *result) + assert r.lrange('a', 0, -1) == result + + +class TestPythonParserEncoding(BaseEncodingTests): + pass + +if HIREDIS_AVAILABLE: + class TestHiredisParserEncoding(BaseEncodingTests): + pass |