# Path: aredis/exceptions.py
# class NoScriptError(ResponseError):
#     pass
# 
# class ResponseError(RedisError):
#     pass
# 
# Path: aredis/utils.py
# def b(x):
#     return x.encode('latin-1') if not isinstance(x, bytes) else x


import pytest
from aredis.exceptions import (NoScriptError,
                               ResponseError)
from aredis.utils import b
"""
msgpack_hello_script_broken = """
local message = cmsgpack.unpack(ARGV[1])
local names = message['name']
return "hello " .. name
"""


class TestScripting:

    @pytest.mark.asyncio(forbid_global_loop=True)
    async def test_eval(self, r):
        await r.flushdb()
        await r.set('a', 2)
        # 2 * 3 == 6
        assert await r.eval(multiply_script, 1, 'a', 3) == 6

    @pytest.mark.asyncio(forbid_global_loop=True)
    async def test_evalsha(self, r):
        await r.set('a', 2)
        sha = await r.script_load(multiply_script)
        # 2 * 3 == 6
        assert await r.evalsha(sha, 1, 'a', 3) == 6

    @pytest.mark.asyncio(forbid_global_loop=True)
    async def test_evalsha_script_not_loaded(self, r):
        await r.set('a', 2)
        sha = await r.script_load(multiply_script)
        # remove the script from Redis's cache
        await r.script_flush()

# Path: aredis/exceptions.py
# class NoScriptError(ResponseError):
#     pass
# 
# class ResponseError(RedisError):
#     pass
# 
# Path: aredis/utils.py
# def b(x):
#     return x.encode('latin-1') if not isinstance(x, bytes) else x


# Path: tests/client/test_scripting.py
import pytest
from aredis.exceptions import (NoScriptError,
                               ResponseError)
from aredis.utils import b

"""
msgpack_hello_script_broken = """
local message = cmsgpack.unpack(ARGV[1])
local names = message['name']
return "hello " .. name
"""


class TestScripting:

    @pytest.mark.asyncio(forbid_global_loop=True)
    async def test_eval(self, r):
        await r.flushdb()
        await r.set('a', 2)
        # 2 * 3 == 6
        assert await r.eval(multiply_script, 1, 'a', 3) == 6

    @pytest.mark.asyncio(forbid_global_loop=True)
    async def test_evalsha(self, r):
        await r.set('a', 2)
        sha = await r.script_load(multiply_script)
        # 2 * 3 == 6
        assert await r.evalsha(sha, 1, 'a', 3) == 6

    @pytest.mark.asyncio(forbid_global_loop=True)
    async def test_evalsha_script_not_loaded(self, r):
        await r.set('a', 2)
        sha = await r.script_load(multiply_script)
        # remove the script from Redis's cache
        await r.script_flush()

        with pytest.raises(NoScriptError):