summaryrefslogtreecommitdiff
path: root/tests/pipeline.py
blob: da85e556b923137df64421407d212914c75d45e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import redis
import unittest

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):
        pipe = self.client.pipeline()
        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,
                'a1',
                True,
                True,
                2.0,
                [('z1', 2.0), ('z2', 4)]
            ]
            )
            
    def test_invalid_command_in_pipeline(self):
        # all commands but the invalid one should be excuted correctly
        self.client['c'] = 'a'
        pipe = self.client.pipeline()
        pipe.set('a', 1).set('b', 2).lpush('c', 3).set('d', 4)
        result = pipe.execute()
        
        self.assertEquals(result[0], True)
        self.assertEquals(self.client['a'], '1')
        self.assertEquals(result[1], True)
        self.assertEquals(self.client['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'], 'a')
        self.assertEquals(result[3], True)
        self.assertEquals(self.client['d'], '4')
        
        # make sure the pipe was restored to a working state
        self.assertEquals(pipe.set('z', 'zzz').execute(), [True])
        self.assertEquals(self.client['z'], 'zzz')
        
    def test_pipe_cannot_select(self):
        pipe = self.client.pipeline()
        self.assertRaises(redis.RedisError,
            pipe.select, 'localhost', 6379, db=9)