summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandy <andy@whiskeymedia.com>2013-06-04 23:01:11 -0400
committerandy <andy@whiskeymedia.com>2013-06-04 23:01:11 -0400
commit97de031fb67d691b10a15b8c176823b01c1299fe (patch)
treebae742e1eba69d3efafeb278b29835c9cd487d29
parent5e30cd66fabe06b67298c4946b412a0f08fdab5c (diff)
downloadredis-py-97de031fb67d691b10a15b8c176823b01c1299fe.tar.gz
pipeline tests converted to pytest
-rw-r--r--tests/pipeline.py202
-rw-r--r--tests/test_pipeline.py202
2 files changed, 202 insertions, 202 deletions
diff --git a/tests/pipeline.py b/tests/pipeline.py
deleted file mode 100644
index 198ee58..0000000
--- a/tests/pipeline.py
+++ /dev/null
@@ -1,202 +0,0 @@
-from __future__ import with_statement
-import unittest
-
-import redis
-from redis._compat import b
-
-
-class PipelineTestCase(unittest.TestCase):
- def setUp(self):
- self.client = redis.Redis(host='localhost', port=6379, db=9)
- self.client.flushdb()
-
- def tearDown(self):
- self.client.flushdb()
-
- def test_pipeline(self):
- with self.client.pipeline() as pipe:
- pipe.set('a', 'a1').get('a').zadd('z', z1=1).zadd('z', z2=4)
- pipe.zincrby('z', 'z1').zrange('z', 0, 5, withscores=True)
- self.assertEquals(
- pipe.execute(),
- [
- True,
- b('a1'),
- True,
- True,
- 2.0,
- [(b('z1'), 2.0), (b('z2'), 4)],
- ]
- )
-
- def test_pipeline_length(self):
- with self.client.pipeline() as pipe:
- # Initially empty.
- self.assertEquals(len(pipe), 0)
- self.assertFalse(pipe)
-
- # Fill 'er up!
- pipe.set('a', 'a1').set('b', 'b1').set('c', 'c1')
- self.assertEquals(len(pipe), 3)
- self.assertTrue(pipe)
-
- # Execute calls reset(), so empty once again.
- pipe.execute()
- self.assertEquals(len(pipe), 0)
- self.assertFalse(pipe)
-
- def test_pipeline_no_transaction(self):
- with self.client.pipeline(transaction=False) as pipe:
- pipe.set('a', 'a1').set('b', 'b1').set('c', 'c1')
- self.assertEquals(pipe.execute(), [True, True, True])
- self.assertEquals(self.client['a'], b('a1'))
- self.assertEquals(self.client['b'], b('b1'))
- self.assertEquals(self.client['c'], b('c1'))
-
- def test_pipeline_no_transaction_watch(self):
- self.client.set('a', 0)
-
- with self.client.pipeline(transaction=False) as pipe:
- pipe.watch('a')
- a = pipe.get('a')
-
- pipe.multi()
- pipe.set('a', int(a) + 1)
- result = pipe.execute()
- self.assertEquals(result, [True])
-
- def test_pipeline_no_transaction_watch_failure(self):
- self.client.set('a', 0)
-
- with self.client.pipeline(transaction=False) as pipe:
- pipe.watch('a')
- a = pipe.get('a')
-
- self.client.set('a', 'bad')
-
- pipe.multi()
- pipe.set('a', int(a) + 1)
- self.assertRaises(redis.WatchError, pipe.execute)
-
- def test_exec_error_in_response(self):
- # an invalid pipeline command at exec time adds the exception instance
- # to the list of returned values
- self.client['c'] = 'a'
- with self.client.pipeline() as pipe:
- pipe.set('a', 1).set('b', 2).lpush('c', 3).set('d', 4)
- result = pipe.execute(raise_on_error=False)
-
- self.assertEquals(result[0], True)
- self.assertEquals(self.client['a'], b('1'))
- self.assertEquals(result[1], True)
- self.assertEquals(self.client['b'], b('2'))
- # we can't lpush to a key that's a string value, so this should
- # be a ResponseError exception
- self.assert_(isinstance(result[2], redis.ResponseError))
- self.assertEquals(self.client['c'], b('a'))
- self.assertEquals(result[3], True)
- self.assertEquals(self.client['d'], b('4'))
-
- # make sure the pipe was restored to a working state
- self.assertEquals(pipe.set('z', 'zzz').execute(), [True])
- self.assertEquals(self.client['z'], b('zzz'))
-
- def test_exec_error_raised(self):
- self.client['c'] = 'a'
- with self.client.pipeline() as pipe:
- pipe.set('a', 1).set('b', 2).lpush('c', 3).set('d', 4)
- self.assertRaises(redis.ResponseError, pipe.execute)
-
- # make sure the pipe was restored to a working state
- self.assertEquals(pipe.set('z', 'zzz').execute(), [True])
- self.assertEquals(self.client['z'], b('zzz'))
-
- def test_parse_error_raised(self):
- with self.client.pipeline() as pipe:
- # the zrem is invalid because we don't pass any keys to it
- pipe.set('a', 1).zrem('b').set('b', 2)
- self.assertRaises(redis.ResponseError, pipe.execute)
-
- # make sure the pipe was restored to a working state
- self.assertEquals(pipe.set('z', 'zzz').execute(), [True])
- self.assertEquals(self.client['z'], b('zzz'))
-
- def test_watch_succeed(self):
- self.client.set('a', 1)
- self.client.set('b', 2)
-
- with self.client.pipeline() as pipe:
- pipe.watch('a', 'b')
- self.assertEquals(pipe.watching, True)
- a_value = pipe.get('a')
- b_value = pipe.get('b')
- self.assertEquals(a_value, b('1'))
- self.assertEquals(b_value, b('2'))
- pipe.multi()
-
- pipe.set('c', 3)
- self.assertEquals(pipe.execute(), [True])
- self.assertEquals(pipe.watching, False)
-
- def test_watch_failure(self):
- self.client.set('a', 1)
- self.client.set('b', 2)
-
- with self.client.pipeline() as pipe:
- pipe.watch('a', 'b')
- self.client.set('b', 3)
- pipe.multi()
- pipe.get('a')
- self.assertRaises(redis.WatchError, pipe.execute)
- self.assertEquals(pipe.watching, False)
-
- def test_unwatch(self):
- self.client.set('a', 1)
- self.client.set('b', 2)
-
- with self.client.pipeline() as pipe:
- pipe.watch('a', 'b')
- self.client.set('b', 3)
- pipe.unwatch()
- self.assertEquals(pipe.watching, False)
- pipe.get('a')
- self.assertEquals(pipe.execute(), [b('1')])
-
- def test_transaction_callable(self):
- self.client.set('a', 1)
- self.client.set('b', 2)
- has_run = []
-
- def my_transaction(pipe):
- a_value = pipe.get('a')
- self.assert_(a_value in (b('1'), b('2')))
- b_value = pipe.get('b')
- self.assertEquals(b_value, b('2'))
-
- # silly run-once code... incr's a so WatchError should be raised
- # forcing this all to run again
- if not has_run:
- self.client.incr('a')
- has_run.append('it has')
-
- pipe.multi()
- pipe.set('c', int(a_value) + int(b_value))
-
- result = self.client.transaction(my_transaction, 'a', 'b')
- self.assertEquals(result, [True])
- self.assertEquals(self.client.get('c'), b('4'))
-
- def test_error_in_simple_pipeline(self):
- self.client.hmset('x', {'a': 'b'})
- with self.client.pipeline(transaction=False) as pipe:
- pipe.llen('x')
- pipe.expire('x', 100)
- try:
- pipe.execute()
- except redis.ResponseError:
- pass
- else:
- raise
-
- ret = self.client.hgetall('x')
- self.assertEqual(ret, {b('a'): b('b')})
diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py
new file mode 100644
index 0000000..ee860e7
--- /dev/null
+++ b/tests/test_pipeline.py
@@ -0,0 +1,202 @@
+from __future__ import with_statement
+import pytest
+
+import redis
+from redis._compat import b
+
+
+class TestPipeline(object):
+ def test_pipeline(self, r):
+ with r.pipeline() as pipe:
+ pipe.set('a', 'a1').get('a').zadd('z', z1=1).zadd('z', z2=4)
+ pipe.zincrby('z', 'z1').zrange('z', 0, 5, withscores=True)
+ assert pipe.execute() == \
+ [
+ True,
+ b('a1'),
+ True,
+ True,
+ 2.0,
+ [(b('z1'), 2.0), (b('z2'), 4)],
+ ]
+
+ def test_pipeline_length(self, r):
+ with r.pipeline() as pipe:
+ # Initially empty.
+ assert len(pipe) == 0
+ assert not pipe
+
+ # Fill 'er up!
+ pipe.set('a', 'a1').set('b', 'b1').set('c', 'c1')
+ assert len(pipe) == 3
+ assert pipe
+
+ # Execute calls reset(), so empty once again.
+ pipe.execute()
+ assert len(pipe) == 0
+ assert not pipe
+
+ def test_pipeline_no_transaction(self, r):
+ with r.pipeline(transaction=False) as pipe:
+ pipe.set('a', 'a1').set('b', 'b1').set('c', 'c1')
+ assert pipe.execute() == [True, True, True]
+ assert r['a'] == b('a1')
+ assert r['b'] == b('b1')
+ assert r['c'] == b('c1')
+
+ def test_pipeline_no_transaction_watch(self, r):
+ r['a'] = 0
+
+ with r.pipeline(transaction=False) as pipe:
+ pipe.watch('a')
+ a = pipe.get('a')
+
+ pipe.multi()
+ pipe.set('a', int(a) + 1)
+ assert pipe.execute() == [True]
+
+ def test_pipeline_no_transaction_watch_failure(self, r):
+ r['a'] = 0
+
+ with r.pipeline(transaction=False) as pipe:
+ pipe.watch('a')
+ a = pipe.get('a')
+
+ r['a'] = 'bad'
+
+ pipe.multi()
+ pipe.set('a', int(a) + 1)
+
+ with pytest.raises(redis.WatchError):
+ pipe.execute()
+
+ assert r['a'] == b('bad')
+
+ def test_exec_error_in_response(self, r):
+ """
+ an invalid pipeline command at exec time adds the exception instance
+ to the list of returned values
+ """
+ r['c'] = 'a'
+ with r.pipeline() as pipe:
+ pipe.set('a', 1).set('b', 2).lpush('c', 3).set('d', 4)
+ result = pipe.execute(raise_on_error=False)
+
+ assert result[0]
+ assert r['a'] == b('1')
+ assert result[1]
+ assert r['b'] == b('2')
+
+ # we can't lpush to a key that's a string value, so this should
+ # be a ResponseError exception
+ assert isinstance(result[2], redis.ResponseError)
+ assert r['c'] == b('a')
+
+ # since this isn't a transaction, the other commands after the
+ # error are still executed
+ assert result[3]
+ assert r['d'] == b('4')
+
+ # make sure the pipe was restored to a working state
+ assert pipe.set('z', 'zzz').execute() == [True]
+ assert r['z'] == b('zzz')
+
+ def test_exec_error_raised(self, r):
+ r['c'] = 'a'
+ with r.pipeline() as pipe:
+ pipe.set('a', 1).set('b', 2).lpush('c', 3).set('d', 4)
+ with pytest.raises(redis.ResponseError):
+ pipe.execute()
+
+ # make sure the pipe was restored to a working state
+ assert pipe.set('z', 'zzz').execute() == [True]
+ assert r['z'] == b('zzz')
+
+ def test_parse_error_raised(self, r):
+ with r.pipeline() as pipe:
+ # the zrem is invalid because we don't pass any keys to it
+ pipe.set('a', 1).zrem('b').set('b', 2)
+ with pytest.raises(redis.ResponseError):
+ pipe.execute()
+
+ # make sure the pipe was restored to a working state
+ assert pipe.set('z', 'zzz').execute() == [True]
+ assert r['z'] == b('zzz')
+
+ def test_watch_succeed(self, r):
+ r['a'] = 1
+ r['b'] = 2
+
+ with r.pipeline() as pipe:
+ pipe.watch('a', 'b')
+ assert pipe.watching
+ a_value = pipe.get('a')
+ b_value = pipe.get('b')
+ assert a_value == b('1')
+ assert b_value == b('2')
+ pipe.multi()
+
+ pipe.set('c', 3)
+ assert pipe.execute() == [True]
+ assert not pipe.watching
+
+ def test_watch_failure(self, r):
+ r['a'] = 1
+ r['b'] = 2
+
+ with r.pipeline() as pipe:
+ pipe.watch('a', 'b')
+ r['b'] = 3
+ pipe.multi()
+ pipe.get('a')
+ with pytest.raises(redis.WatchError):
+ pipe.execute()
+
+ assert not pipe.watching
+
+ def test_unwatch(self, r):
+ r['a'] = 1
+ r['b'] = 2
+
+ with r.pipeline() as pipe:
+ pipe.watch('a', 'b')
+ r['b'] = 3
+ pipe.unwatch()
+ assert not pipe.watching
+ pipe.get('a')
+ assert pipe.execute() == [b('1')]
+
+ def test_transaction_callable(self, r):
+ r['a'] = 1
+ r['b'] = 2
+ has_run = []
+
+ def my_transaction(pipe):
+ a_value = pipe.get('a')
+ assert a_value in (b('1'), b('2'))
+ b_value = pipe.get('b')
+ assert b_value == b('2')
+
+ # silly run-once code... incr's "a" so WatchError should be raised
+ # forcing this all to run again. this should incr "a" once to "2"
+ if not has_run:
+ r.incr('a')
+ has_run.append('it has')
+
+ pipe.multi()
+ pipe.set('c', int(a_value) + int(b_value))
+
+ result = r.transaction(my_transaction, 'a', 'b')
+ assert result == [True]
+ assert r['c'] == b('4')
+
+ def test_exec_error_in_no_transaction_pipeline(self, r):
+ r['a'] = 1
+ with r.pipeline(transaction=False) as pipe:
+ pipe.llen('a')
+ pipe.expire('a', 100)
+
+ with pytest.raises(redis.ResponseError):
+ pipe.execute()
+
+ assert r['a'] == b('1')