Ejemplo n.º 1
0
def test_redisCommand_args(context):
    """Tests that using varargs is properly unsupported.

    The alternative is a seg fault.
    """
    key = 'testkey'
    value = 'testvalue'
    with pytest.raises(TypeError):
        hiredis.redisCommand(context, 'SET %s %s', key, value)
Ejemplo n.º 2
0
def test_redisCommand_reply_nil(context):
    key = 'testkey'
    r = hiredis.redisCommand(context, f'DEL {key}')
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_INTEGER
    assert r.integer in (0, 1)
    hiredis.freeReplyObject(r)
    r = hiredis.redisCommand(context, f'GET {key}')
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_NIL
    hiredis.freeReplyObject(r)
    cleanup_keys(context, (key, ))
Ejemplo n.º 3
0
def test_redisCommand_noargs(context):
    key = 'testkey'
    value = 'testvalue'
    r = hiredis.redisCommand(context, f'SET {key} {value}')
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_STATUS
    assert r.str == 'OK'
    hiredis.freeReplyObject(r)
    r = hiredis.redisCommand(context, f'GET {key}')
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_STRING
    assert r.str == value
    hiredis.freeReplyObject(r)
    cleanup_keys(context, (key, ))
Ejemplo n.º 4
0
def test_redisCommand_reply_integer(context):
    key = 'testkey'
    value = 'foobar'
    value_bits = 26
    r = hiredis.redisCommand(context, f'SET {key} {value}')
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_STATUS
    assert r.str == 'OK'
    hiredis.freeReplyObject(r)
    r = hiredis.redisCommand(context, f'BITCOUNT {key}')
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_INTEGER
    assert r.integer == value_bits
    hiredis.freeReplyObject(r)
    cleanup_keys(context, (key, ))
Ejemplo n.º 5
0
def cleanup_keys(context, keys):
    """Test helper to cleanup keys from redis."""

    for key in keys:
        r = hiredis.redisCommand(context, f'DEL {key}')
        if r is not None:
            hiredis.freeReplyObject(r)
Ejemplo n.º 6
0
def test_redisCommand_reply_array(context):
    keys = [f'testkey{i}' for i in range(5)]
    for key in keys:
        r = hiredis.redisCommand(context, f'SET {key} {key}')
        assert r is not None
        assert r.type == hiredis.REDIS_REPLY_STATUS
        assert r.str == 'OK'
    r = hiredis.redisCommand(context, f'KEYS testkey*')
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_ARRAY
    assert r.elements >= len(keys)
    returned_keys = set()
    rep = reduce_reply(r)
    assert isinstance(rep, tuple)
    assert len(rep) >= len(keys)
    for key in rep:
        assert isinstance(key, str)
        returned_keys.add(key)
    assert set(keys) <= returned_keys
    cleanup_keys(context, keys)
Ejemplo n.º 7
0
def redis_command(
        context: hiredis.redisContext,
        command: str
    ) -> ReplyValue:
    """Sends the command and retrieves the response (write and read).

    Wrapper around hiredis.redisCommand().
    Raises:
        * HiredisError (any type)
        * FastredisError
    """

    rep = hiredis.redisCommand(context, command)
    raise_empty_reply_error(context, rep)
    ret = reduce_reply(rep)
    hiredis.freeReplyObject(rep)
    return ret