summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandy <andy@whiskeymedia.com>2013-06-06 11:46:15 -0700
committerandy <andy@whiskeymedia.com>2013-06-06 11:46:15 -0700
commit533bc4a0b37598b1e25a35892659e5f61ce54d29 (patch)
tree0dd0ab45bca898f5b82fed03cd496825f533fd7e
parent6a69aa50797f95b8fd426651ac0b27d7718036d9 (diff)
downloadredis-py-533bc4a0b37598b1e25a35892659e5f61ce54d29.tar.gz
pubsub tests
-rw-r--r--tests/conftest.py4
-rw-r--r--tests/pubsub.py123
-rw-r--r--tests/test_pubsub.py95
3 files changed, 98 insertions, 124 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 89b9b58..231081f 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -3,7 +3,9 @@ import redis
def _get_client(cls, request=None, **kwargs):
- client = cls(host='localhost', port=6379, db=9, **kwargs)
+ params = {'host': 'localhost', 'port': 6379, 'db': 9}
+ params.update(kwargs)
+ client = cls(**params)
client.flushdb()
if request:
request.addfinalizer(client.flushdb)
diff --git a/tests/pubsub.py b/tests/pubsub.py
deleted file mode 100644
index 26b8c0d..0000000
--- a/tests/pubsub.py
+++ /dev/null
@@ -1,123 +0,0 @@
-import unittest
-
-from redis._compat import b, next
-from redis.exceptions import ConnectionError
-import redis
-
-
-class PubSubTestCase(unittest.TestCase):
- def setUp(self):
- self.connection_pool = redis.ConnectionPool()
- self.client = redis.Redis(connection_pool=self.connection_pool)
- self.pubsub = self.client.pubsub()
-
- def tearDown(self):
- self.connection_pool.disconnect()
-
- def test_channel_subscribe(self):
- # subscribe doesn't return anything
- self.assertEquals(
- self.pubsub.subscribe('foo'),
- None
- )
- # send a message
- self.assertEquals(self.client.publish('foo', 'hello foo'), 1)
- # there should be now 2 messages in the buffer, a subscribe and the
- # one we just published
- self.assertEquals(
- next(self.pubsub.listen()),
- {
- 'type': 'subscribe',
- 'pattern': None,
- 'channel': 'foo',
- 'data': 1
- }
- )
- self.assertEquals(
- next(self.pubsub.listen()),
- {
- 'type': 'message',
- 'pattern': None,
- 'channel': 'foo',
- 'data': b('hello foo')
- }
- )
-
- # unsubscribe
- self.assertEquals(
- self.pubsub.unsubscribe('foo'),
- None
- )
- # unsubscribe message should be in the buffer
- self.assertEquals(
- next(self.pubsub.listen()),
- {
- 'type': 'unsubscribe',
- 'pattern': None,
- 'channel': 'foo',
- 'data': 0
- }
- )
-
- def test_pattern_subscribe(self):
- # psubscribe doesn't return anything
- self.assertEquals(
- self.pubsub.psubscribe('f*'),
- None
- )
- # send a message
- self.assertEquals(self.client.publish('foo', 'hello foo'), 1)
- # there should be now 2 messages in the buffer, a subscribe and the
- # one we just published
- self.assertEquals(
- next(self.pubsub.listen()),
- {
- 'type': 'psubscribe',
- 'pattern': None,
- 'channel': 'f*',
- 'data': 1
- }
- )
- self.assertEquals(
- next(self.pubsub.listen()),
- {
- 'type': 'pmessage',
- 'pattern': 'f*',
- 'channel': 'foo',
- 'data': b('hello foo')
- }
- )
-
- # unsubscribe
- self.assertEquals(
- self.pubsub.punsubscribe('f*'),
- None
- )
- # unsubscribe message should be in the buffer
- self.assertEquals(
- next(self.pubsub.listen()),
- {
- 'type': 'punsubscribe',
- 'pattern': None,
- 'channel': 'f*',
- 'data': 0
- }
- )
-
-
-class PubSubRedisDownTestCase(unittest.TestCase):
- def setUp(self):
- self.connection_pool = redis.ConnectionPool(port=6390)
- self.client = redis.Redis(connection_pool=self.connection_pool)
- self.pubsub = self.client.pubsub()
-
- def tearDown(self):
- self.connection_pool.disconnect()
-
- def test_channel_subscribe(self):
- got_exception = False
- try:
- self.pubsub.subscribe('foo')
- except ConnectionError:
- got_exception = True
- self.assertTrue(got_exception)
diff --git a/tests/test_pubsub.py b/tests/test_pubsub.py
new file mode 100644
index 0000000..0756345
--- /dev/null
+++ b/tests/test_pubsub.py
@@ -0,0 +1,95 @@
+import pytest
+
+import redis
+from redis._compat import b, next
+from redis.exceptions import ConnectionError
+
+
+class TestPubSub(object):
+
+ def test_channel_subscribe(self, r):
+ p = r.pubsub()
+
+ # subscribe doesn't return anything
+ assert p.subscribe('foo') is None
+
+ # send a message
+ assert r.publish('foo', 'hello foo') == 1
+
+ # there should be now 2 messages in the buffer, a subscribe and the
+ # one we just published
+ assert next(p.listen()) == \
+ {
+ 'type': 'subscribe',
+ 'pattern': None,
+ 'channel': 'foo',
+ 'data': 1
+ }
+
+ assert next(p.listen()) == \
+ {
+ 'type': 'message',
+ 'pattern': None,
+ 'channel': 'foo',
+ 'data': b('hello foo')
+ }
+
+ # unsubscribe
+ assert p.unsubscribe('foo') is None
+
+ # unsubscribe message should be in the buffer
+ assert next(p.listen()) == \
+ {
+ 'type': 'unsubscribe',
+ 'pattern': None,
+ 'channel': 'foo',
+ 'data': 0
+ }
+
+ def test_pattern_subscribe(self, r):
+ p = r.pubsub()
+
+ # psubscribe doesn't return anything
+ assert p.psubscribe('f*') is None
+
+ # send a message
+ assert r.publish('foo', 'hello foo') == 1
+
+ # there should be now 2 messages in the buffer, a subscribe and the
+ # one we just published
+ assert next(p.listen()) == \
+ {
+ 'type': 'psubscribe',
+ 'pattern': None,
+ 'channel': 'f*',
+ 'data': 1
+ }
+
+ assert next(p.listen()) == \
+ {
+ 'type': 'pmessage',
+ 'pattern': 'f*',
+ 'channel': 'foo',
+ 'data': b('hello foo')
+ }
+
+ # unsubscribe
+ assert p.punsubscribe('f*') is None
+
+ # unsubscribe message should be in the buffer
+ assert next(p.listen()) == \
+ {
+ 'type': 'punsubscribe',
+ 'pattern': None,
+ 'channel': 'f*',
+ 'data': 0
+ }
+
+
+class TestPubSubRedisDown(object):
+
+ def test_channel_subscribe(self, r):
+ r = redis.Redis(host='localhost', port=6390)
+ p = r.pubsub()
+ with pytest.raises(ConnectionError):
+ p.subscribe('foo')