Example #1
0
def test_script_error(redis):
    with assert_raises(ScriptError) as error:
        run_code(
            client=redis,
            content="""
            local a = 1;
            local b = 2;
            c = 3; -- This is line 4.
            local d = 4;
            """,
        )

    assert_equal(4, error.exception.line)
Example #2
0
def test_argument_types(redis):
    args = {
        's': 'foo',
        'i': 42,
        'b': False,
        'l': [1, 2, 'b', None, '5', 3.14],
        'd': {'a': 'alpha', 'b': 5, 'c': None, 'd': 2.45},
    }
    result = run_code(
        client=redis,
        content="""
        %arg s string
        %arg i integer
        %arg b boolean
        %arg l list
        %arg d dict
        %return dict

        return cjson.encode({
            s=s,
            i=i,
            b=b,
            l=l,
            d=d,
        })
        """,
        kwargs=args,
    )

    assert_equal(args, result)
Example #3
0
def test_run_code():
    client = MagicMock()
    result = run_code(
        client=client,
        content="""
        %include "sum"
        return sum(3, 4);
        """,
        path=LUA_SEARCH_PATH,
    )
    assert_equal(client.evalsha(), result)
Example #4
0
def test_run_code():
    client = MagicMock()
    result = run_code(
        client=client,
        content="""
        %include "sum"
        return sum(3, 4);
        """,
        path=LUA_SEARCH_PATH,
    )
    assert_equal(client.evalsha(), result)
Example #5
0
def test_multiple_inclusion(redis):
    cache = {}
    parse_script(
        name='a',
        content="""
redis.call('INCR', key_a)
""".strip(),
        cache=cache,
    )
    parse_script(
        name='b',
        content="""
%pragma once
redis.call('INCR', key_b)
""".strip(),
        cache=cache,
    )
    result = run_code(
        client=redis,
        content="""
%key key_a
%key key_b
%include "a"
%include "b"
%include "a"
%include "b"
%return dict

local a = tonumber(redis.call('GET', key_a))
local b = tonumber(redis.call('GET', key_b))

return cjson.encode({
    a=a,
    b=b,
})
""".strip(),
        kwargs={
            'key_a': 'key_a',
            'key_b': 'key_b',
        },
        cache=cache,
    )

    assert_equal(
        {
            'a': 2,
            'b': 1,
        },
        result,
    )
Example #6
0
def test_run_code_with_args():
    client = MagicMock()
    result = run_code(
        client=client,
        content="""
        %arg a int
        %arg b int
        %include "sum"
        return sum(a, b);
        """,
        path=LUA_SEARCH_PATH,
        kwargs={'a': 1, 'b': 2},
    )
    assert_equal(client.evalsha(), result)
Example #7
0
def test_sum(redis):
    result = run_code(
        client=redis,
        content="""
        %arg a integer
        %arg b integer

        return a + b;
        """,
        kwargs={
            'a': 3,
            'b': 4,
        },
    )

    assert_equal(7, result)
Example #8
0
def test_run_code_with_args():
    client = MagicMock()
    result = run_code(
        client=client,
        content="""
        %arg a int
        %arg b int
        %include "sum"
        return sum(a, b);
        """,
        path=LUA_SEARCH_PATH,
        kwargs={
            'a': 1,
            'b': 2
        },
    )
    assert_equal(client.evalsha(), result)
Example #9
0
def test_sum_pipeline(redis):
    with redis.pipeline() as pipeline:
        result = run_code(
            client=pipeline,
            content="""
            %arg a integer
            %arg b integer
            %return string

            return a + b;
            """,
            kwargs={
                'a': 3,
                'b': 4,
            },
        )
        values = pipeline.execute()

    assert_equal([7], values)
    assert_equal("7", result(values[0]))
Example #10
0
def test_doc_example(redis):
    result = run_code(
        client=redis,
        content="""
%arg size integer
%arg members list
%return dict

local foo_id = redis.call('INCR', 'foo:last_id')
local foo_root_key = string.format('foo:%s', foo_id)
local foo_members_key = foo_root_key .. ':members'
local foo_size_key = foo_root_key .. ':size'

redis.call('SET', foo_size_key, size)
redis.call('SADD', foo_members_key, unpack(members))

return cjson.encode({
    id=foo_id,
    members=members,
    size=size,
})
""".strip(),
        kwargs={
            'members': {'john', 'susan', 'bob'},
            'size': 5,
        },
    )

    # This transformation makes the result more easily comparable.
    result['members'] = set(result['members'])

    assert_equal(
        {
            'id': 1,
            'members': {'john', 'susan', 'bob'},
            'size': 5,
        },
        result,
    )