Ejemplo n.º 1
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.º 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 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
Ejemplo n.º 6
0
def redis_read(context: hiredis.redisContext) -> ReplyValue:
    """Reads a reply value from the redis receive buffer.

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

    out = hiredis.redisReplyOut()
    hiredis.redisGetReplyOL(context, out)
    if out.ret == REDIS_ERR:
        raise_context_error(context)
        raise ContextError('redisGetReply error and no error code is set.')
    rep = out.reply
    raise_empty_reply_error(context, rep)
    ret = reduce_reply(rep)
    hiredis.freeReplyObject(rep)
    return ret
Ejemplo n.º 7
0
def test_redisGetReply(context):
    KEY = 'testkey'
    VALUE = 'testvalue'
    commands = (f'SET {KEY} {VALUE}', f'GET {KEY}', f'DEL {KEY}')
    for command in commands:
        assert (hiredis.redisAppendCommand(context,
                                           command) == hiredis.REDIS_OK)

    r = hiredis.redisReplyOut()
    hiredis.redisGetReplyOL(context, r)
    assert r.ret == hiredis.REDIS_OK
    assert r.reply.str == 'OK'
    hiredis.freeReplyObject(r.reply)

    hiredis.redisGetReplyOL(context, r)
    assert r.ret == hiredis.REDIS_OK
    assert r.reply.str == VALUE
    hiredis.freeReplyObject(r.reply)

    hiredis.redisGetReplyOL(context, r)
    assert r.ret == hiredis.REDIS_OK
    assert r.reply.integer == 1
    hiredis.freeReplyObject(r.reply)