pipeline.py
Go to the documentation of this file.
00001 from __future__ import with_statement
00002 import unittest
00003 
00004 import rocon_python_redis as redis
00005 from redis._compat import b
00006 
00007 class PipelineTestCase(unittest.TestCase):
00008     def setUp(self):
00009         self.client = redis.Redis(host='localhost', port=6379, db=9)
00010         self.client.flushdb()
00011 
00012     def tearDown(self):
00013         self.client.flushdb()
00014 
00015     def test_pipeline(self):
00016         with self.client.pipeline() as pipe:
00017             pipe.set('a', 'a1').get('a').zadd('z', z1=1).zadd('z', z2=4)
00018             pipe.zincrby('z', 'z1').zrange('z', 0, 5, withscores=True)
00019             self.assertEquals(
00020                 pipe.execute(),
00021                 [
00022                     True,
00023                     b('a1'),
00024                     True,
00025                     True,
00026                     2.0,
00027                     [(b('z1'), 2.0), (b('z2'), 4)],
00028                 ]
00029             )
00030 
00031     def test_pipeline_no_transaction(self):
00032         with self.client.pipeline(transaction=False) as pipe:
00033             pipe.set('a', 'a1').set('b', 'b1').set('c', 'c1')
00034             self.assertEquals(pipe.execute(), [True, True, True])
00035             self.assertEquals(self.client['a'], b('a1'))
00036             self.assertEquals(self.client['b'], b('b1'))
00037             self.assertEquals(self.client['c'], b('c1'))
00038 
00039     def test_pipeline_no_transaction_watch(self):
00040         self.client.set('a', 0)
00041 
00042         with self.client.pipeline(transaction=False) as pipe:
00043             pipe.watch('a')
00044             a = pipe.get('a')
00045 
00046             pipe.multi()
00047             pipe.set('a', int(a) + 1)
00048             result = pipe.execute()
00049             self.assertEquals(result, [True])
00050 
00051     def test_pipeline_no_transaction_watch_failure(self):
00052         self.client.set('a', 0)
00053 
00054         with self.client.pipeline(transaction=False) as pipe:
00055             pipe.watch('a')
00056             a = pipe.get('a')
00057 
00058             self.client.set('a', 'bad')
00059 
00060             pipe.multi()
00061             pipe.set('a', int(a) + 1)
00062             self.assertRaises(redis.WatchError, pipe.execute)
00063 
00064     def test_invalid_command_in_pipeline(self):
00065         # all commands but the invalid one should be excuted correctly
00066         self.client['c'] = 'a'
00067         with self.client.pipeline() as pipe:
00068             pipe.set('a', 1).set('b', 2).lpush('c', 3).set('d', 4)
00069             result = pipe.execute()
00070 
00071             self.assertEquals(result[0], True)
00072             self.assertEquals(self.client['a'], b('1'))
00073             self.assertEquals(result[1], True)
00074             self.assertEquals(self.client['b'], b('2'))
00075             # we can't lpush to a key that's a string value, so this should
00076             # be a ResponseError exception
00077             self.assert_(isinstance(result[2], redis.ResponseError))
00078             self.assertEquals(self.client['c'], b('a'))
00079             self.assertEquals(result[3], True)
00080             self.assertEquals(self.client['d'], b('4'))
00081 
00082             # make sure the pipe was restored to a working state
00083             self.assertEquals(pipe.set('z', 'zzz').execute(), [True])
00084             self.assertEquals(self.client['z'], b('zzz'))
00085 
00086     def test_watch_succeed(self):
00087         self.client.set('a', 1)
00088         self.client.set('b', 2)
00089 
00090         with self.client.pipeline() as pipe:
00091             pipe.watch('a', 'b')
00092             self.assertEquals(pipe.watching, True)
00093             a_value = pipe.get('a')
00094             b_value = pipe.get('b')
00095             self.assertEquals(a_value, b('1'))
00096             self.assertEquals(b_value, b('2'))
00097             pipe.multi()
00098 
00099             pipe.set('c', 3)
00100             self.assertEquals(pipe.execute(), [True])
00101             self.assertEquals(pipe.watching, False)
00102 
00103     def test_watch_failure(self):
00104         self.client.set('a', 1)
00105         self.client.set('b', 2)
00106 
00107         with self.client.pipeline() as pipe:
00108             pipe.watch('a', 'b')
00109             self.client.set('b', 3)
00110             pipe.multi()
00111             pipe.get('a')
00112             self.assertRaises(redis.WatchError, pipe.execute)
00113             self.assertEquals(pipe.watching, False)
00114 
00115     def test_unwatch(self):
00116         self.client.set('a', 1)
00117         self.client.set('b', 2)
00118 
00119         with self.client.pipeline() as pipe:
00120             pipe.watch('a', 'b')
00121             self.client.set('b', 3)
00122             pipe.unwatch()
00123             self.assertEquals(pipe.watching, False)
00124             pipe.get('a')
00125             self.assertEquals(pipe.execute(), [b('1')])
00126 
00127     def test_transaction_callable(self):
00128         self.client.set('a', 1)
00129         self.client.set('b', 2)
00130         has_run = []
00131 
00132         def my_transaction(pipe):
00133             a_value = pipe.get('a')
00134             self.assert_(a_value in (b('1'), b('2')))
00135             b_value = pipe.get('b')
00136             self.assertEquals(b_value, b('2'))
00137 
00138             # silly run-once code... incr's a so WatchError should be raised
00139             # forcing this all to run again
00140             if not has_run:
00141                 self.client.incr('a')
00142                 has_run.append('it has')
00143 
00144             pipe.multi()
00145             pipe.set('c', int(a_value) + int(b_value))
00146 
00147         result = self.client.transaction(my_transaction, 'a', 'b')
00148         self.assertEquals(result, [True])
00149         self.assertEquals(self.client.get('c'), b('4'))


rocon_python_redis
Author(s): Andy McCurdy
autogenerated on Fri May 2 2014 10:35:49