Ejemplo n.º 1
0
def cleanup_keys(context, keys):
    """Test helper to cleanup keys from redis."""

    for key in keys:
        r = hiredisb.redisCommand_b(context, f'DEL {key}'.encode())
        if r is not None:
            hiredisb.freeReplyObject_b(r)
Ejemplo n.º 2
0
def redis_command(context: hiredisb.redisContext_b,
                  command: bytes) -> ReplyValue:
    """Bytes version of redis_command().

    Wrapper around hiredisb.redisCommand_b().
    """

    rep = hiredisb.redisCommand_b(context, command)
    raise_empty_reply_error(context, rep)
    ret = reduce_reply_b(rep)
    hiredisb.freeReplyObject_b(rep)
    return ret
Ejemplo n.º 3
0
def test_redisCommand_b_reply_nil(context):
    key = 'testkey'
    r = hiredisb.redisCommand_b(context, f'DEL {key}'.encode())
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_INTEGER
    assert r.integer in (0, 1)
    hiredisb.freeReplyObject_b(r)
    r = hiredisb.redisCommand_b(context, f'GET {key}'.encode())
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_NIL
    hiredisb.freeReplyObject_b(r)
    cleanup_keys(context, (key,))
Ejemplo n.º 4
0
def test_redisCommand_b_noargs(context):
    key = 'testkey'
    value = 'testvalue'
    r = hiredisb.redisCommand_b(context, f'SET {key} {value}'.encode())
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_STATUS
    assert r.str == b'OK'
    hiredisb.freeReplyObject_b(r)
    r = hiredisb.redisCommand_b(context, f'GET {key}'.encode())
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_STRING
    assert r.str == value.encode()
    hiredisb.freeReplyObject_b(r)
    cleanup_keys(context, (key,))
Ejemplo n.º 5
0
def redis_read(context: hiredisb.redisContext_b) -> ReplyValue:
    """Bytes version of redis_read().

    Wrapper around hiredisb.redisGetReplyOL()."""

    out = hiredisb.redisReplyOut_b()
    hiredisb.redisGetReplyOL_b(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_b(rep)
    hiredisb.freeReplyObject_b(rep)
    return ret
Ejemplo n.º 6
0
def test_redisCommand_b_reply_integer(context):
    key = 'testkey'
    value = 'foobar'
    value_bits = 26
    r = hiredisb.redisCommand_b(context, f'SET {key} {value}'.encode())
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_STATUS
    assert r.str == b'OK'
    hiredisb.freeReplyObject_b(r)
    r = hiredisb.redisCommand_b(context, f'BITCOUNT {key}'.encode())
    assert r is not None
    assert r.type == hiredis.REDIS_REPLY_INTEGER
    assert r.integer == value_bits
    hiredisb.freeReplyObject_b(r)
    cleanup_keys(context, (key,))
Ejemplo n.º 7
0
def test_redisGetReply_b(context):
    KEY = 'testkey'
    VALUE = 'testvalue'
    commands = (
        f'SET {KEY} {VALUE}',
        f'GET {KEY}',
        f'DEL {KEY}'
    )
    for command in commands:
        assert (hiredisb.redisAppendCommand_b(context, command.encode())
            == hiredis.REDIS_OK
        )

    r = hiredisb.redisReplyOut_b()
    hiredisb.redisGetReplyOL_b(context, r)
    assert r.ret == hiredis.REDIS_OK
    assert r.reply.str == b'OK'
    hiredisb.freeReplyObject_b(r.reply)

    hiredisb.redisGetReplyOL_b(context, r)
    assert r.ret == hiredis.REDIS_OK
    assert r.reply.str == VALUE.encode()
    hiredisb.freeReplyObject_b(r.reply)

    hiredisb.redisGetReplyOL_b(context, r)
    assert r.ret == hiredis.REDIS_OK
    assert r.reply.integer == 1
    hiredisb.freeReplyObject_b(r.reply)