summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAlex Grönholm <alex.gronholm+git@nextday.fi>2012-08-07 00:15:53 +0300
committerAlex Grönholm <alex.gronholm+git@nextday.fi>2012-08-07 00:15:53 +0300
commit0aaa404f3525641fe5bb3b50ca303d05a326cdce (patch)
treecd9bb0d8c37b01a98a5a3891891061605d4c30e2 /tests
parent826364f3bb19abca0e1e6a92266abb017f5481f4 (diff)
downloadredis-py-0aaa404f3525641fe5bb3b50ca303d05a326cdce.tar.gz
Fixed Python 3.2+ compatibility
Diffstat (limited to 'tests')
-rw-r--r--tests/__init__.py12
-rw-r--r--tests/encoding.py11
-rw-r--r--tests/lock.py4
-rw-r--r--tests/pipeline.py2
-rw-r--r--tests/pubsub.py17
-rw-r--r--tests/server_commands.py45
6 files changed, 47 insertions, 44 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
index 777e77a..fb73cde 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -1,11 +1,11 @@
import unittest
-from server_commands import ServerCommandsTestCase
-from connection_pool import ConnectionPoolTestCase
-from pipeline import PipelineTestCase
-from lock import LockTestCase
-from pubsub import PubSubTestCase, PubSubRedisDownTestCase
-from encoding import PythonParserEncodingTestCase, HiredisEncodingTestCase
+from tests.server_commands import ServerCommandsTestCase
+from tests.connection_pool import ConnectionPoolTestCase
+from tests.pipeline import PipelineTestCase
+from tests.lock import LockTestCase
+from tests.pubsub import PubSubTestCase, PubSubRedisDownTestCase
+from tests.encoding import PythonParserEncodingTestCase, HiredisEncodingTestCase
try:
import hiredis
diff --git a/tests/encoding.py b/tests/encoding.py
index 248e05e..8923f68 100644
--- a/tests/encoding.py
+++ b/tests/encoding.py
@@ -1,6 +1,7 @@
from __future__ import with_statement
import unittest
+from redis._compat import unichr, u, unicode
from redis.connection import ConnectionPool, PythonParser, HiredisParser
import redis
@@ -15,17 +16,17 @@ class EncodingTestCase(unittest.TestCase):
self.client.flushdb()
def test_simple_encoding(self):
- unicode_string = unichr(3456) + u'abcd' + unichr(3421)
+ 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', type(cached_val).__name__,
- 'Cache returned value with type "%s", expected "unicode"' %
- type(cached_val).__name__)
+ 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)
+ 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)
diff --git a/tests/lock.py b/tests/lock.py
index 9a9a397..fc2d336 100644
--- a/tests/lock.py
+++ b/tests/lock.py
@@ -17,7 +17,7 @@ class LockTestCase(unittest.TestCase):
def test_lock(self):
lock = self.client.lock('foo')
self.assert_(lock.acquire())
- self.assertEquals(self.client['foo'], str(Lock.LOCK_FOREVER))
+ self.assertEquals(self.client['foo'], str(Lock.LOCK_FOREVER).encode())
lock.release()
self.assertEquals(self.client.get('foo'), None)
@@ -51,7 +51,7 @@ class LockTestCase(unittest.TestCase):
def test_context_manager(self):
with self.client.lock('foo'):
- self.assertEquals(self.client['foo'], str(Lock.LOCK_FOREVER))
+ self.assertEquals(self.client['foo'], str(Lock.LOCK_FOREVER).encode())
self.assertEquals(self.client.get('foo'), None)
def test_float_timeout(self):
diff --git a/tests/pipeline.py b/tests/pipeline.py
index e20e48a..42ddad6 100644
--- a/tests/pipeline.py
+++ b/tests/pipeline.py
@@ -6,7 +6,7 @@ import redis
class PipelineTestCase(unittest.TestCase):
def setUp(self):
- self.client = redis.Redis(host='localhost', port=6379, db=9)
+ self.client = redis.Redis(host='localhost', port=6379, db=9, decode_responses=True)
self.client.flushdb()
def tearDown(self):
diff --git a/tests/pubsub.py b/tests/pubsub.py
index 090297f..90bf619 100644
--- a/tests/pubsub.py
+++ b/tests/pubsub.py
@@ -1,12 +1,13 @@
import unittest
+from redis._compat import next
from redis.exceptions import ConnectionError
import redis
class PubSubTestCase(unittest.TestCase):
def setUp(self):
- self.connection_pool = redis.ConnectionPool()
+ self.connection_pool = redis.ConnectionPool(decode_responses=True)
self.client = redis.Redis(connection_pool=self.connection_pool)
self.pubsub = self.client.pubsub()
@@ -24,7 +25,7 @@ class PubSubTestCase(unittest.TestCase):
# there should be now 2 messages in the buffer, a subscribe and the
# one we just published
self.assertEquals(
- self.pubsub.listen().next(),
+ next(self.pubsub.listen()),
{
'type': 'subscribe',
'pattern': None,
@@ -33,7 +34,7 @@ class PubSubTestCase(unittest.TestCase):
}
)
self.assertEquals(
- self.pubsub.listen().next(),
+ next(self.pubsub.listen()),
{
'type': 'message',
'pattern': None,
@@ -49,7 +50,7 @@ class PubSubTestCase(unittest.TestCase):
)
# unsubscribe message should be in the buffer
self.assertEquals(
- self.pubsub.listen().next(),
+ next(self.pubsub.listen()),
{
'type': 'unsubscribe',
'pattern': None,
@@ -69,7 +70,7 @@ class PubSubTestCase(unittest.TestCase):
# there should be now 2 messages in the buffer, a subscribe and the
# one we just published
self.assertEquals(
- self.pubsub.listen().next(),
+ next(self.pubsub.listen()),
{
'type': 'psubscribe',
'pattern': None,
@@ -78,7 +79,7 @@ class PubSubTestCase(unittest.TestCase):
}
)
self.assertEquals(
- self.pubsub.listen().next(),
+ next(self.pubsub.listen()),
{
'type': 'pmessage',
'pattern': 'f*',
@@ -94,7 +95,7 @@ class PubSubTestCase(unittest.TestCase):
)
# unsubscribe message should be in the buffer
self.assertEquals(
- self.pubsub.listen().next(),
+ next(self.pubsub.listen()),
{
'type': 'punsubscribe',
'pattern': None,
@@ -107,7 +108,7 @@ class PubSubTestCase(unittest.TestCase):
class PubSubRedisDownTestCase(unittest.TestCase):
def setUp(self):
self.connection_pool = redis.ConnectionPool(port=6390)
- self.client = redis.Redis(connection_pool=self.connection_pool)
+ self.client = redis.Redis(connection_pool=self.connection_pool, decode_responses=True)
self.pubsub = self.client.pubsub()
def tearDown(self):
diff --git a/tests/server_commands.py b/tests/server_commands.py
index bd935b9..cbc802b 100644
--- a/tests/server_commands.py
+++ b/tests/server_commands.py
@@ -1,16 +1,16 @@
-from string import letters
from distutils.version import StrictVersion
import unittest
import datetime
import time
+from redis._compat import unichr, u, b, ascii_letters, iteritems, dictkeys, dictvalues
from redis.client import parse_info
import redis
class ServerCommandsTestCase(unittest.TestCase):
def get_client(self, cls=redis.Redis):
- return cls(host='localhost', port=6379, db=9)
+ return cls(host='localhost', port=6379, db=9, decode_responses=True)
def setUp(self):
self.client = self.get_client()
@@ -39,17 +39,18 @@ class ServerCommandsTestCase(unittest.TestCase):
def test_get_and_set(self):
# get and set can't be tested independently of each other
- self.assertEquals(self.client.get('a'), None)
- byte_string = 'value'
+ client = redis.Redis(host='localhost', port=6379, db=9)
+ self.assertEquals(client.get('a'), None)
+ byte_string = b('value')
integer = 5
- unicode_string = unichr(3456) + u'abcd' + unichr(3421)
- self.assert_(self.client.set('byte_string', byte_string))
- self.assert_(self.client.set('integer', 5))
- self.assert_(self.client.set('unicode_string', unicode_string))
- self.assertEquals(self.client.get('byte_string'), byte_string)
- self.assertEquals(self.client.get('integer'), str(integer))
+ unicode_string = unichr(3456) + u('abcd') + unichr(3421)
+ self.assert_(client.set('byte_string', byte_string))
+ self.assert_(client.set('integer', 5))
+ self.assert_(client.set('unicode_string', unicode_string))
+ self.assertEquals(client.get('byte_string'), byte_string)
+ self.assertEquals(client.get('integer'), b(str(integer)))
self.assertEquals(
- self.client.get('unicode_string').decode('utf-8'),
+ client.get('unicode_string').decode('utf-8'),
unicode_string)
def test_getitem_and_setitem(self):
@@ -211,7 +212,7 @@ class ServerCommandsTestCase(unittest.TestCase):
def test_mset(self):
d = {'a': '1', 'b': '2', 'c': '3'}
self.assert_(self.client.mset(d))
- for k, v in d.iteritems():
+ for k, v in iteritems(d):
self.assertEquals(self.client[k], v)
def test_msetnx(self):
@@ -219,7 +220,7 @@ class ServerCommandsTestCase(unittest.TestCase):
self.assert_(self.client.msetnx(d))
d2 = {'a': 'x', 'd': '4'}
self.assert_(not self.client.msetnx(d2))
- for k, v in d.iteritems():
+ for k, v in iteritems(d):
self.assertEquals(self.client[k], v)
self.assertEquals(self.client.get('d'), None)
@@ -990,7 +991,7 @@ class ServerCommandsTestCase(unittest.TestCase):
# HASHES
def make_hash(self, key, d):
- for k, v in d.iteritems():
+ for k, v in iteritems(d):
self.client.hset(key, k, v)
def test_hget_and_hset(self):
@@ -1097,7 +1098,7 @@ class ServerCommandsTestCase(unittest.TestCase):
self.assertEquals(self.client.hincrby('a', 'a1'), 2)
self.assertEquals(self.client.hincrby('a', 'a1', amount=2), 4)
# negative values decrement
- self.assertEquals(self.client.hincrby('a', 'a1', amount=-3), 1)
+ self.assertEquals(self.client.hincrby('a', 'a1', amount= -3), 1)
# hash that exists, but key that doesn't
self.assertEquals(self.client.hincrby('a', 'a2', amount=3), 3)
# finally a key that's not an int
@@ -1114,7 +1115,7 @@ class ServerCommandsTestCase(unittest.TestCase):
# real logic
h = {'a1': '1', 'a2': '2', 'a3': '3'}
self.make_hash('a', h)
- keys = h.keys()
+ keys = dictkeys(h)
keys.sort()
remote_keys = self.client.hkeys('a')
remote_keys.sort()
@@ -1143,7 +1144,7 @@ class ServerCommandsTestCase(unittest.TestCase):
# real logic
h = {'a1': '1', 'a2': '2', 'a3': '3'}
self.make_hash('a', h)
- vals = h.values()
+ vals = dictvalues(h)
vals.sort()
remote_vals = self.client.hvals('a')
remote_vals.sort()
@@ -1296,15 +1297,15 @@ class ServerCommandsTestCase(unittest.TestCase):
'foo\tbar\x07': '789',
}
# fill in lists
- for key, value in mapping.iteritems():
+ for key, value in iteritems(mapping):
for c in value:
self.assertTrue(self.client.rpush(key, c))
# check that KEYS returns all the keys as they are
- self.assertEqual(sorted(self.client.keys('*')), sorted(mapping.keys()))
+ self.assertEqual(sorted(self.client.keys('*')), sorted(dictkeys(mapping)))
# check that it is possible to get list content by key name
- for key in mapping.keys():
+ for key in dictkeys(mapping):
self.assertEqual(self.client.lrange(key, 0, -1),
list(mapping[key]))
@@ -1346,8 +1347,8 @@ class ServerCommandsTestCase(unittest.TestCase):
"The PythonParser has some special cases for return values > 1MB"
# load up 5MB of data into a key
data = []
- for i in range(5000000 / len(letters)):
- data.append(letters)
+ for i in range(5000000 // len(ascii_letters)):
+ data.append(ascii_letters)
data = ''.join(data)
self.client.set('a', data)
self.assertEquals(self.client.get('a'), data)