class RedisClusterException(Exception):
    pass class ResponseError(RedisError):
    pass class DataError(RedisError):
    pass class RedisError(Exception):
    pass def b(x):
    return x.encode('latin-1') if not isinstance(x, bytes) else x def iteritems(x):
    return iter(x.items()) def iterkeys(x):
    return iter(x.keys()) def itervalues(x):
    return iter(x.values()) def parse_info(response):
    """Parses the result of Redis's INFO command into a Python dict"""
    info = {}
    response = nativestr(response)

    def get_value(value):
        if ',' not in value or '=' not in value:
            try:
                if '.' in value:
                    return float(value)
                else:
                    return int(value)
            except ValueError:
                return value
        else:
            sub_dict = {}
            for item in value.split(','):
                k, v = item.rsplit('=', 1)
                sub_dict[k] = get_value(v)
            return sub_dict

    for line in response.splitlines():
        if line and not line.startswith('#'):
            if line.find(':') != -1:
                key, value = line.split(':', 1)
                info[key] = get_value(value)
            else:
                # if the line isn't splittable, append it to the "__raw__" key
                info.setdefault('__raw__', []).append(line)

    return info def skip_if_server_version_lt(min_version):
    """
    """
    versions = get_versions()
    for version in versions.values():
        if StrictVersion(version) < StrictVersion(min_version):
            return pytest.mark.skipif(True, reason="")
    return pytest.mark.skipif(False, reason="") def skip_if_redis_py_version_lt(min_version):
    """
    """
    import aredis
    version = aredis.__version__
    if StrictVersion(version) < StrictVersion(min_version):
        return pytest.mark.skipif(True, reason="")
    return pytest.mark.skipif(False, reason="")
import datetime
import re
import time
import pytest
from string import ascii_letters
from aredis.exceptions import RedisClusterException, ResponseError, DataError, RedisError
from aredis.utils import b, iteritems, iterkeys, itervalues
from aredis.commands.server import parse_info
from tests.cluster.conftest import skip_if_server_version_lt, skip_if_redis_py_version_lt
        await r.pfadd('a', *members)
        assert await r.pfcount('a') == len(members)
        members_b = set([b('2'), b('3'), b('4')])
        await r.pfadd('b', *members_b)
        assert await r.pfcount('b') == len(members_b)
        assert await r.pfcount('a', 'b') == len(members_b.union(members))

    @pytest.mark.asyncio
    async def test_pfmerge(self, r):
        await r.flushdb()
        mema = set([b('1'), b('2'), b('3')])
        memb = set([b('2'), b('3'), b('4')])
        memc = set([b('5'), b('6'), b('7')])
        await r.pfadd('a', *mema)
        await r.pfadd('b', *memb)
        await r.pfadd('c', *memc)
        await r.pfmerge('d', 'c', 'a')
        assert await r.pfcount('d') == 6
        await r.pfmerge('d', 'b')
        assert await r.pfcount('d') == 7

    # HASH COMMANDS
    @pytest.mark.asyncio
    async def test_hget_and_hset(self, r):
        await r.flushdb()
        await r.hmset('a', {'1': 1, '2': 2, '3': 3})
        assert await r.hget('a', '1') == b('1')
        assert await r.hget('a', '2') == b('2')
        assert await r.hget('a', '3') == b('3')

        # field was updated, redis returns 0
        assert await r.hset('a', '2', 5) == 0
        assert await r.hget('a', '2') == b('5')

        # field is new, redis returns 1
        assert await r.hset('a', '4', 4) == 1
        assert await r.hget('a', '4') == b('4')

        # key inside of hash that doesn't exist returns null value
        assert await r.hget('a', 'b') is None

    @pytest.mark.asyncio
    async def test_hdel(self, r):
        await r.flushdb()
        await r.hmset('a', {'1': 1, '2': 2, '3': 3})
        assert await r.hdel('a', '2') == 1
        assert await r.hget('a', '2') is None
        assert await r.hdel('a', '1', '3') == 2
        assert await r.hlen('a') == 0

    @pytest.mark.asyncio
    async def test_hexists(self, r):
        await r.flushdb()
        await r.hmset('a', {'1': 1, '2': 2, '3': 3})
        assert await r.hexists('a', '1')
        assert not await r.hexists('a', '4')

    @pytest.mark.asyncio
    async def test_hgetall(self, r):
        await r.flushdb()
        h = {b('a1'): b('1'), b('a2'): b('2'), b('a3'): b('3')}
        await r.hmset('a', h)
        assert await r.hgetall('a') == h

    @pytest.mark.asyncio
    async def test_hincrby(self, r):
        await r.flushdb()
        assert await r.hincrby('a', '1') == 1
        assert await r.hincrby('a', '1', amount=2) == 3
        assert await r.hincrby('a', '1', amount=-2) == 1

    @pytest.mark.asyncio
    async def test_hincrbyfloat(self, r):
        await r.flushdb()
        assert await r.hincrbyfloat('a', '1') == 1.0
        assert await r.hincrbyfloat('a', '1') == 2.0
        assert await r.hincrbyfloat('a', '1', 1.2) == 3.2

    @pytest.mark.asyncio
    async def test_hkeys(self, r):
        await r.flushdb()
        h = {b('a1'): b('1'), b('a2'): b('2'), b('a3'): b('3')}
        await r.hmset('a', h)
        local_keys = list(iterkeys(h))
        remote_keys = await r.hkeys('a')
        assert (sorted(local_keys) == sorted(remote_keys))

    @pytest.mark.asyncio
    async def test_hlen(self, r):
        await r.flushdb()
        await r.hmset('a', {'1': 1, '2': 2, '3': 3})
        assert await r.hlen('a') == 3

    @pytest.mark.asyncio
    async def test_hmget(self, r):
        await r.flushdb()
        assert await r.hmset('a', {'a': 1, 'b': 2, 'c': 3})
        assert await r.hmget('a', 'a', 'b', 'c') == [b('1'), b('2'), b('3')]

    @pytest.mark.asyncio
    async def test_hmset(self, r):
        await r.flushdb()
        h = {b('a'): b('1'), b('b'): b('2'), b('c'): b('3')}
        assert await r.hmset('a', h)
        assert await r.hgetall('a') == h

    @pytest.mark.asyncio
    async def test_hsetnx(self, r):
        await r.flushdb()
        # Initially set the hash field
        assert await r.hsetnx('a', '1', 1)
        assert await r.hget('a', '1') == b('1')
        assert not await r.hsetnx('a', '1', 2)
        assert await r.hget('a', '1') == b('1')

    @pytest.mark.asyncio
    async def test_hvals(self, r):
        await r.flushdb()
        h = {b('a1'): b('1'), b('a2'): b('2'), b('a3'): b('3')}
        await r.hmset('a', h)

        local_vals = list(itervalues(h))