Пример #1
0
 def pack_command(self, *args):
     args_output = SYM_EMPTY.join([
         SYM_EMPTY.join((SYM_DOLLAR, b(str(len(k))), SYM_CRLF, k, SYM_CRLF))
         for k in imap(self.encode, args)])
     output = SYM_EMPTY.join(
         (SYM_STAR, b(str(len(args))), SYM_CRLF, args_output))
     return output
Пример #2
0
 def test_zunionstore_with_weight(self, r):
     r.zadd('a', a1=1, a2=1, a3=1)
     r.zadd('b', a1=2, a2=2, a3=2)
     r.zadd('c', a1=6, a3=5, a4=4)
     assert r.zunionstore('d', {'a': 1, 'b': 2, 'c': 3}) == 4
     assert r.zrange('d', 0, -1, withscores=True) == \
         [(b('a2'), 5), (b('a4'), 12), (b('a3'), 20), (b('a1'), 23)]
Пример #3
0
 def test_zunionstore_max(self, r):
     r.zadd('a', a1=1, a2=1, a3=1)
     r.zadd('b', a1=2, a2=2, a3=2)
     r.zadd('c', a1=6, a3=5, a4=4)
     assert r.zunionstore('d', ['a', 'b', 'c'], aggregate='MAX') == 4
     assert r.zrange('d', 0, -1, withscores=True) == \
         [(b('a2'), 2), (b('a4'), 4), (b('a3'), 5), (b('a1'), 6)]
Пример #4
0
    def test_sort_all_options(self, r):
        r["user:1:username"] = "******"
        r["user:2:username"] = "******"
        r["user:3:username"] = "******"
        r["user:4:username"] = "******"
        r["user:5:username"] = "******"
        r["user:6:username"] = "******"
        r["user:7:username"] = "******"
        r["user:8:username"] = "******"

        r["user:1:favorite_drink"] = "yuengling"
        r["user:2:favorite_drink"] = "rum"
        r["user:3:favorite_drink"] = "vodka"
        r["user:4:favorite_drink"] = "milk"
        r["user:5:favorite_drink"] = "pinot noir"
        r["user:6:favorite_drink"] = "water"
        r["user:7:favorite_drink"] = "gin"
        r["user:8:favorite_drink"] = "apple juice"

        r.rpush("gods", "5", "8", "3", "1", "2", "7", "6", "4")
        num = r.sort(
            "gods",
            start=2,
            num=4,
            by="user:*:username",
            get="user:*:favorite_drink",
            desc=True,
            alpha=True,
            store="sorted",
        )
        assert num == 4
        assert r.lrange("sorted", 0, 10) == [b("vodka"), b("milk"), b("gin"), b("apple juice")]
Пример #5
0
 def test_zunionstore_min(self, r):
     r.zadd('a', a1=1, a2=2, a3=3)
     r.zadd('b', a1=2, a2=2, a3=4)
     r.zadd('c', a1=6, a3=5, a4=4)
     assert r.zunionstore('d', ['a', 'b', 'c'], aggregate='MIN') == 4
     assert r.zrange('d', 0, -1, withscores=True) == \
         [(b('a1'), 1), (b('a2'), 2), (b('a3'), 3), (b('a4'), 4)]
Пример #6
0
 def test_zinterstore_min(self, r):
     r.zadd('a', a1=1, a2=2, a3=3)
     r.zadd('b', a1=2, a2=3, a3=5)
     r.zadd('c', a1=6, a3=5, a4=4)
     assert r.zinterstore('d', ['a', 'b', 'c'], aggregate='MIN') == 2
     assert r.zrange('d', 0, -1, withscores=True) == \
         [(b('a1'), 1), (b('a3'), 3)]
Пример #7
0
 def test_zunionstore_sum(self, r):
     r.zadd('a', a1=1, a2=1, a3=1)
     r.zadd('b', a1=2, a2=2, a3=2)
     r.zadd('c', a1=6, a3=5, a4=4)
     assert r.zunionstore('d', ['a', 'b', 'c']) == 4
     assert r.zrange('d', 0, -1, withscores=True) == \
         [(b('a2'), 3), (b('a4'), 4), (b('a3'), 8), (b('a1'), 9)]
Пример #8
0
 def test_sdiffstore(self, r):
     r.sadd('a', '1', '2', '3')
     assert r.sdiffstore('c', 'a', 'b') == 3
     assert r.smembers('c') == set([b('1'), b('2'), b('3')])
     r.sadd('b', '2', '3')
     assert r.sdiffstore('c', 'a', 'b') == 1
     assert r.smembers('c') == set([b('1')])
Пример #9
0
 def test_sinterstore(self, r):
     r.sadd('a', '1', '2', '3')
     assert r.sinterstore('c', 'a', 'b') == 0
     assert r.smembers('c') == set()
     r.sadd('b', '2', '3')
     assert r.sinterstore('c', 'a', 'b') == 2
     assert r.smembers('c') == set([b('2'), b('3')])
Пример #10
0
 def test_incr(self, r):
     assert r.incr('a') == 1
     assert r['a'] == b('1')
     assert r.incr('a') == 2
     assert r['a'] == b('2')
     assert r.incr('a', amount=5) == 7
     assert r['a'] == b('7')
Пример #11
0
 def test_zscan(self, r):
     r.zadd('a', 'a', 1, 'b', 2, 'c', 3)
     cursor, pairs = r.zscan('a')
     assert cursor == b('0')
     assert set(pairs) == set([(b('a'), 1), (b('b'), 2), (b('c'), 3)])
     _, pairs = r.zscan('a', match='a')
     assert set(pairs) == set([(b('a'), 1)])
Пример #12
0
    def test_sort_all_options(self, r):
        r['user:1:username'] = '******'
        r['user:2:username'] = '******'
        r['user:3:username'] = '******'
        r['user:4:username'] = '******'
        r['user:5:username'] = '******'
        r['user:6:username'] = '******'
        r['user:7:username'] = '******'
        r['user:8:username'] = '******'

        r['user:1:favorite_drink'] = 'yuengling'
        r['user:2:favorite_drink'] = 'rum'
        r['user:3:favorite_drink'] = 'vodka'
        r['user:4:favorite_drink'] = 'milk'
        r['user:5:favorite_drink'] = 'pinot noir'
        r['user:6:favorite_drink'] = 'water'
        r['user:7:favorite_drink'] = 'gin'
        r['user:8:favorite_drink'] = 'apple juice'

        r.rpush('gods', '5', '8', '3', '1', '2', '7', '6', '4')
        num = r.sort('gods', start=2, num=4, by='user:*:username',
                     get='user:*:favorite_drink', desc=True, alpha=True,
                     store='sorted')
        assert num == 4
        assert r.lrange('sorted', 0, 10) == \
            [b('vodka'), b('milk'), b('gin'), b('apple juice')]
Пример #13
0
 def test_decr(self, r):
     assert r.decr('a') == -1
     assert r['a'] == b('-1')
     assert r.decr('a') == -2
     assert r['a'] == b('-2')
     assert r.decr('a', amount=5) == -7
     assert r['a'] == b('-7')
Пример #14
0
 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'))
Пример #15
0
    def pack_command(self, *args):
        "Pack a series of arguments into the Redis protocol"
        output = []
        # the client might have included 1 or more literal arguments in
        # the command name, e.g., 'CONFIG GET'. The Redis server expects these
        # arguments to be sent separately, so split the first argument
        # manually. All of these arguements get wrapped in the Token class
        # to prevent them from being encoded.
        command = args[0]
        if ' ' in command:
            args = tuple([Token(s) for s in command.split(' ')]) + args[1:]
        else:
            args = (Token(command),) + args[1:]

        buff = SYM_EMPTY.join(
            (SYM_STAR, b(str(len(args))), SYM_CRLF))

        for arg in imap(self.encode, args):
            # to avoid large string mallocs, chunk the command into the
            # output list if we're sending large values
            if len(buff) > self._buffer_cutoff or \
               len(arg) > self._buffer_cutoff:
                buff = SYM_EMPTY.join(
                    (buff, SYM_DOLLAR, b(str(len(arg))), SYM_CRLF))
                output.append(buff)
                output.append(arg)
                buff = SYM_CRLF
            else:
                buff = SYM_EMPTY.join((buff, SYM_DOLLAR, b(str(len(arg))),
                                       SYM_CRLF, arg, SYM_CRLF))
        output.append(buff)
        return output
Пример #16
0
    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')
Пример #17
0
    def test_script_object_in_pipeline(self, r):
        multiply = r.register_script(multiply_script)
        assert not multiply.sha
        pipe = r.pipeline()
        pipe.set('a', 2)
        pipe.get('a')
        multiply(keys=['a'], args=[3], client=pipe)
        # even though the pipeline wasn't executed yet, we made sure the
        # script was loaded and got a valid sha
        assert multiply.sha
        assert r.script_exists(multiply.sha) == [True]
        # [SET worked, GET 'a', result of multiple script]
        assert pipe.execute() == [True, b('2'), 6]

        # purge the script from redis's cache and re-run the pipeline
        # the multiply script object knows it's sha, so it shouldn't get
        # reloaded until pipe.execute()
        r.script_flush()
        pipe = r.pipeline()
        pipe.set('a', 2)
        pipe.get('a')
        assert multiply.sha
        multiply(keys=['a'], args=[3], client=pipe)
        assert r.script_exists(multiply.sha) == [False]
        # [SET worked, GET 'a', result of multiple script]
        assert pipe.execute() == [True, b('2'), 6]
Пример #18
0
 def test_sscan(self, r):
     r.sadd('a', 1, 2, 3)
     cursor, members = r.sscan('a')
     assert cursor == 0
     assert set(members) == set([b('1'), b('2'), b('3')])
     _, members = r.sscan('a', match=b('1'))
     assert set(members) == set([b('1')])
Пример #19
0
 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')
Пример #20
0
 def test_zinterstore_with_weight(self, r):
     r.zadd('a{foo}', a1=1, a2=1, a3=1)
     r.zadd('b{foo}', a1=2, a2=2, a3=2)
     r.zadd('c{foo}', a1=6, a3=5, a4=4)
     assert r.zinterstore('d{foo}', {'a{foo}': 1, 'b{foo}': 2, 'c{foo}': 3}) == 2
     assert r.zrange('d{foo}', 0, -1, withscores=True) == \
         [(b('a3'), 20), (b('a1'), 23)]
Пример #21
0
    def test_pubsub_numsub(self, r):
        r.pubsub(ignore_subscribe_messages=True).subscribe('foo', 'bar', 'baz')
        r.pubsub(ignore_subscribe_messages=True).subscribe('bar', 'baz')
        r.pubsub(ignore_subscribe_messages=True).subscribe('baz')

        channels = [(b('bar'), 2), (b('baz'), 3), (b('foo'), 1)]
        assert channels == sorted(r.pubsub_numsub('foo', 'bar', 'baz'))
Пример #22
0
 def test_zinterstore_sum(self, r):
     r.zadd('a{foo}', a1=1, a2=1, a3=1)
     r.zadd('b{foo}', a1=2, a2=2, a3=2)
     r.zadd('c{foo}', a1=6, a3=5, a4=4)
     assert r.zinterstore('d{foo}', ['a{foo}', 'b{foo}', 'c{foo}']) == 2
     assert r.zrange('d{foo}', 0, -1, withscores=True) == \
         [(b('a3'), 8), (b('a1'), 9)]
Пример #23
0
 def test_sinterstore(self, r):
     r.sadd("a", "1", "2", "3")
     assert r.sinterstore("c", "a", "b") == 0
     assert r.smembers("c") == set()
     r.sadd("b", "2", "3")
     assert r.sinterstore("c", "a", "b") == 2
     assert r.smembers("c") == set([b("2"), b("3")])
Пример #24
0
 def test_zscan(self, r):
     r.zadd("a", "a", 1, "b", 2, "c", 3)
     cursor, pairs = r.zscan("a")
     assert cursor == 0
     assert set(pairs) == set([(b("a"), 1), (b("b"), 2), (b("c"), 3)])
     _, pairs = r.zscan("a", match="a")
     assert set(pairs) == set([(b("a"), 1)])
Пример #25
0
 def test_sdiffstore(self, r):
     r.sadd("a", "1", "2", "3")
     assert r.sdiffstore("c", "a", "b") == 3
     assert r.smembers("c") == set([b("1"), b("2"), b("3")])
     r.sadd("b", "2", "3")
     assert r.sdiffstore("c", "a", "b") == 1
     assert r.smembers("c") == set([b("1")])
Пример #26
0
 def test_incr(self, r):
     assert r.incr("a") == 1
     assert r["a"] == b("1")
     assert r.incr("a") == 2
     assert r["a"] == b("2")
     assert r.incr("a", amount=5) == 7
     assert r["a"] == b("7")
Пример #27
0
 def test_sscan(self, r):
     r.sadd("a", 1, 2, 3)
     cursor, members = r.sscan("a")
     assert cursor == 0
     assert set(members) == set([b("1"), b("2"), b("3")])
     _, members = r.sscan("a", match=b("1"))
     assert set(members) == set([b("1")])
Пример #28
0
    def test_script_object_in_pipeline(self, r):
        multiply = r.register_script(multiply_script)
        precalculated_sha = multiply.sha
        assert precalculated_sha
        pipe = r.pipeline()
        pipe.set('a', 2)
        pipe.get('a')
        multiply(keys=['a'], args=[3], client=pipe)
        assert r.script_exists(multiply.sha) == [False]
        # [SET worked, GET 'a', result of multiple script]
        assert pipe.execute() == [True, b('2'), 6]
        # The script should have been loaded by pipe.execute()
        assert r.script_exists(multiply.sha) == [True]
        # The precalculated sha should have been the correct one
        assert multiply.sha == precalculated_sha

        # purge the script from redis's cache and re-run the pipeline
        # the multiply script should be reloaded by pipe.execute()
        r.script_flush()
        pipe = r.pipeline()
        pipe.set('a', 2)
        pipe.get('a')
        multiply(keys=['a'], args=[3], client=pipe)
        assert r.script_exists(multiply.sha) == [False]
        # [SET worked, GET 'a', result of multiple script]
        assert pipe.execute() == [True, b('2'), 6]
        assert r.script_exists(multiply.sha) == [True]
Пример #29
0
 def test_decr(self, r):
     assert r.decr("a") == -1
     assert r["a"] == b("-1")
     assert r.decr("a") == -2
     assert r["a"] == b("-2")
     assert r.decr("a", amount=5) == -7
     assert r["a"] == b("-7")
Пример #30
0
 def test_zinterstore_max(self, r):
     r.zadd('a{foo}', a1=1, a2=1, a3=1)
     r.zadd('b{foo}', a1=2, a2=2, a3=2)
     r.zadd('c{foo}', a1=6, a3=5, a4=4)
     assert r.zinterstore('d{foo}', ['a{foo}', 'b{foo}', 'c{foo}'], aggregate='MAX') == 2
     assert r.zrange('d{foo}', 0, -1, withscores=True) == \
         [(b('a3'), 5), (b('a1'), 6)]
Пример #31
0
 def test_sort_limited(self, r):
     r.rpush('a', '3', '2', '1', '4')
     assert r.sort('a', start=1, num=2) == [b('2'), b('3')]
Пример #32
0
 def test_append(self, r):
     assert r.append('a', 'a1') == 2
     assert r['a'] == b('a1')
     assert r.append('a', 'a2') == 4
     assert r['a'] == b('a1a2')
Пример #33
0
 def test_sort_store(self, r):
     r.rpush('a', '2', '3', '1')
     assert r.sort('a', store='sorted_values') == 3
     assert r.lrange('sorted_values', 0, -1) == [b('1'), b('2'), b('3')]
Пример #34
0
 def test_sort_alpha(self, r):
     r.rpush('a', 'e', 'c', 'b', 'd', 'a')
     assert r.sort('a', alpha=True) == \
         [b('a'), b('b'), b('c'), b('d'), b('e')]
Пример #35
0
 def test_sort_desc(self, r):
     r.rpush('a', '2', '3', '1')
     assert r.sort('a', desc=True) == [b('3'), b('2'), b('1')]
Пример #36
0
 def test_sort_get(self, r):
     r['user:1'] = 'u1'
     r['user:2'] = 'u2'
     r['user:3'] = 'u3'
     r.rpush('a', '2', '3', '1')
     assert r.sort('a', get='user:*') == [b('u1'), b('u2'), b('u3')]
Пример #37
0
 def test_sort_by(self, r):
     r['score:1'] = 8
     r['score:2'] = 3
     r['score:3'] = 5
     r.rpush('a', '3', '2', '1')
     assert r.sort('a', by='score:*') == [b('2'), b('3'), b('1')]
Пример #38
0
 def test_hvals(self, r):
     h = {b('a1'): b('1'), b('a2'): b('2'), b('a3'): b('3')}
     r.hmset('a', h)
     local_vals = list(itervalues(h))
     remote_vals = r.hvals('a')
     assert sorted(local_vals) == sorted(remote_vals)
Пример #39
0
 def test_hmset(self, r):
     h = {b('a'): b('1'), b('b'): b('2'), b('c'): b('3')}
     assert r.hmset('a', h)
     assert r.hgetall('a') == h
Пример #40
0
 def test_sort_basic(self, r):
     r.rpush('a', '3', '2', '1', '4')
     assert r.sort('a') == [b('1'), b('2'), b('3'), b('4')]
Пример #41
0
 def test_hkeys(self, r):
     h = {b('a1'): b('1'), b('a2'): b('2'), b('a3'): b('3')}
     r.hmset('a', h)
     local_keys = list(iterkeys(h))
     remote_keys = r.hkeys('a')
     assert (sorted(local_keys) == sorted(remote_keys))
Пример #42
0
 def test_hsetnx(self, r):
     # Initially set the hash field
     assert r.hsetnx('a', '1', 1)
     assert r.hget('a', '1') == b('1')
     assert not r.hsetnx('a', '1', 2)
     assert r.hget('a', '1') == b('1')
Пример #43
0
    HIREDIS_SUPPORTS_BYTE_BUFFER = \
        hiredis_version >= StrictVersion('0.1.4')

    if not HIREDIS_SUPPORTS_BYTE_BUFFER:
        msg = ("redis-py works best with hiredis >= 0.1.4. You're running "
               "hiredis %s. Please consider upgrading." % hiredis.__version__)
        warnings.warn(msg)

    HIREDIS_USE_BYTE_BUFFER = True
    # only use byte buffer if hiredis supports it and the Python version
    # is >= 2.7
    if not HIREDIS_SUPPORTS_BYTE_BUFFER or (sys.version_info[0] == 2
                                            and sys.version_info[1] < 7):
        HIREDIS_USE_BYTE_BUFFER = False

SYM_STAR = b('*')
SYM_DOLLAR = b('$')
SYM_CRLF = b('\r\n')
SYM_EMPTY = b('')

SERVER_CLOSED_CONNECTION_ERROR = "Connection closed by server."


class Token(object):
    """
    Literal strings in Redis commands, such as the command names and any
    hard-coded arguments are wrapped in this class so we know not to apply
    and encoding rules on them.
    """
    def __init__(self, value):
        if isinstance(value, Token):
Пример #44
0
 def test_hmget(self, r):
     assert r.hmset('a', {'a': 1, 'b': 2, 'c': 3})
     assert r.hmget('a', 'a', 'b', 'c') == [b('1'), b('2'), b('3')]
Пример #45
0
    def test_zrevrangebyscore(self, r):
        r.zadd('a', a1=1, a2=2, a3=3, a4=4, a5=5)
        assert r.zrevrangebyscore('a', 4, 2) == [b('a4'), b('a3'), b('a2')]

        # slicing with start/num
        assert r.zrevrangebyscore('a', 4, 2, start=1, num=2) == \
            [b('a3'), b('a2')]

        # withscores
        assert r.zrevrangebyscore('a', 4, 2, withscores=True) == \
            [(b('a4'), 4.0), (b('a3'), 3.0), (b('a2'), 2.0)]

        # custom score function
        assert r.zrevrangebyscore('a', 4, 2, withscores=True,
                                  score_cast_func=int) == \
            [(b('a4'), 4), (b('a3'), 3), (b('a2'), 2)]
Пример #46
0
    def sort(self,
             name,
             start=None,
             num=None,
             by=None,
             get=None,
             desc=False,
             alpha=False,
             store=None,
             groups=None):
        """Sort and return the list, set or sorted set at ``name``.

        :start: and :num:
            allow for paging through the sorted data

        :by:
            allows using an external key to weight and sort the items.
            Use an "*" to indicate where in the key the item value is located

        :get:
            allows for returning items from external keys rather than the
            sorted data itself.  Use an "*" to indicate where int he key
            the item value is located

        :desc:
            allows for reversing the sort

        :alpha:
            allows for sorting lexicographically rather than numerically

        :store:
            allows for storing the result of the sort into the key `store`

        ClusterImpl:
            A full implementation of the server side sort mechanics because many of the
            options work on multiple keys that can exist on multiple servers.
        """
        if (start is None and num is not None) or \
           (start is not None and num is None):
            raise RedisError(
                "RedisError: ``start`` and ``num`` must both be specified")
        try:
            data_type = b(self.type(name))

            if data_type == b("none"):
                return []
            elif data_type == b("set"):
                data = list(self.smembers(name))[:]
            elif data_type == b("list"):
                data = self.lrange(name, 0, -1)
            else:
                raise RedisClusterException(
                    "Unable to sort data type : {0}".format(data_type))
            if by is not None:
                # _sort_using_by_arg mutates data so we don't
                # need need a return value.
                self._sort_using_by_arg(data, by, alpha)
            elif not alpha:
                data.sort(key=self._strtod_key_func)
            else:
                data.sort()
            if desc:
                data = data[::-1]
            if not (start is None and num is None):
                data = data[start:start + num]

            if get:
                data = self._retrive_data_from_sort(data, get)

            if store is not None:
                if data_type == b("set"):
                    self.delete(store)
                    self.rpush(store, *data)
                elif data_type == b("list"):
                    self.delete(store)
                    self.rpush(store, *data)
                else:
                    raise RedisClusterException(
                        "Unable to store sorted data for data type : {0}".
                        format(data_type))

                return len(data)

            if groups:
                if not get or isinstance(get, basestring) or len(get) < 2:
                    raise DataError('when using "groups" the "get" argument '
                                    'must be specified and contain at least '
                                    'two keys')
                n = len(get)
                return list(izip(*[data[i::n] for i in range(n)]))
            else:
                return data
        except KeyError:
            return []
Пример #47
0
 def test_zremrangebyrank(self, r):
     r.zadd('a', a1=1, a2=2, a3=3, a4=4, a5=5)
     assert r.zremrangebyrank('a', 1, 3) == 3
     assert r.zrange('a', 0, 5) == [b('a1'), b('a5')]
Пример #48
0
 def __init__(self, value):
     if isinstance(value, Token):
         value = value.value
     self.value = value
     self.encoded_value = b(value)
Пример #49
0
 def test_zrem(self, r):
     r.zadd('a', a1=1, a2=2, a3=3)
     assert r.zrem('a', 'a2') == 1
     assert r.zrange('a', 0, -1) == [b('a1'), b('a3')]
     assert r.zrem('a', 'b') == 0
     assert r.zrange('a', 0, -1) == [b('a1'), b('a3')]
Пример #50
0
 def test_zremrangebyscore(self, r):
     r.zadd('a', a1=1, a2=2, a3=3, a4=4, a5=5)
     assert r.zremrangebyscore('a', 2, 4) == 3
     assert r.zrange('a', 0, -1) == [b('a1'), b('a5')]
     assert r.zremrangebyscore('a', 2, 4) == 0
     assert r.zrange('a', 0, -1) == [b('a1'), b('a5')]
Пример #51
0
 def test_echo(self, r):
     for server, res in r.echo('foo bar').items():
         assert res == b('foo bar')
Пример #52
0
 def test_zrem_multiple_keys(self, r):
     r.zadd('a', a1=1, a2=2, a3=3)
     assert r.zrem('a', 'a1', 'a2') == 2
     assert r.zrange('a', 0, 5) == [b('a3')]
Пример #53
0
 def test_sunionstore(self, r):
     r.sadd('a{foo}', '1', '2')
     r.sadd('b{foo}', '2', '3')
     assert r.sunionstore('c{foo}', 'a{foo}', 'b{foo}') == 3
     assert r.smembers('c{foo}') == set([b('1'), b('2'), b('3')])
Пример #54
0
 def test_zrangebylex(self, r):
     r.zadd('a', a=0, b=0, c=0, d=0, e=0, f=0, g=0)
     assert r.zrangebylex('a', '-', '[c') == [b('a'), b('b'), b('c')]
     assert r.zrangebylex('a', '-', '(c') == [b('a'), b('b')]
     assert r.zrangebylex('a', '[aaa', '(g') == \
         [b('b'), b('c'), b('d'), b('e'), b('f')]
     assert r.zrangebylex('a', '[f', '+') == [b('f'), b('g')]
     assert r.zrangebylex('a', '-', '+', start=3, num=2) == [b('d'), b('e')]
Пример #55
0
 def test_srem(self, r):
     r.sadd('a', '1', '2', '3', '4')
     assert r.srem('a', '5') == 0
     assert r.srem('a', '2', '4') == 2
     assert r.smembers('a') == set([b('1'), b('3')])
Пример #56
0
 def test_zadd(self, r):
     r.zadd('a', a1=1, a2=2, a3=3)
     assert r.zrange('a', 0, -1) == [b('a1'), b('a2'), b('a3')]
Пример #57
0
 def test_srandmember(self, r):
     s = [b('1'), b('2'), b('3')]
     r.sadd('a', *s)
     assert r.srandmember('a') in s
Пример #58
0
 def test_sunion(self, r):
     r.sadd('a{foo}', '1', '2')
     r.sadd('b{foo}', '2', '3')
     assert r.sunion('a{foo}', 'b{foo}') == set([b('1'), b('2'), b('3')])
Пример #59
0
 def test_spop(self, r):
     s = [b('1'), b('2'), b('3')]
     r.sadd('a', *s)
     value = r.spop('a')
     assert value in s
     assert r.smembers('a') == set(s) - set([value])
Пример #60
0
 def test_srandmember_multi_value(self, r):
     s = [b('1'), b('2'), b('3')]
     r.sadd('a', *s)
     randoms = r.srandmember('a', number=2)
     assert len(randoms) == 2
     assert set(randoms).intersection(s) == set(randoms)