Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cast of message type in command interpreter #75

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion tests/adapters/interpreters/command/test_command_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
from typing import Callable, Optional, Sequence

import pytest
from mock import AsyncMock, MagicMock
from mock import AsyncMock, MagicMock, patch

from tickit.adapters.interpreters.command import CommandInterpreter
from tickit.adapters.interpreters.command.command_interpreter import Command
from tickit.core.adapter import Adapter

_GET_TYPE_HINTS = (
"tickit.adapters.interpreters.command.command_interpreter.get_type_hints"
)


@pytest.fixture
def command_interpreter():
Expand All @@ -33,6 +37,10 @@ def parse(self, data: bytes) -> Optional[Sequence[str]]:
return TestCommand


@patch(
_GET_TYPE_HINTS,
lambda _: {"arg1": str, "arg2": str},
)
@pytest.mark.asyncio
async def test_command_interpreter_handle_calls_func_with_args(
command_interpreter: CommandInterpreter,
Expand All @@ -51,6 +59,10 @@ async def test_command_interpreter_handle_calls_func_with_args(
test_adapter.test_method.assert_awaited_once_with("arg1", "arg2")


@patch(
_GET_TYPE_HINTS,
lambda _: {"arg1": str, "arg2": str},
)
@pytest.mark.asyncio
async def test_command_interpreter_handle_returns_iterable_reply(
command_interpreter: CommandInterpreter,
Expand All @@ -70,6 +82,10 @@ async def test_command_interpreter_handle_returns_iterable_reply(
assert reply == (await command_interpreter.handle(test_adapter, b"\x01"))[0]


@patch(
_GET_TYPE_HINTS,
lambda _: {"arg1": str, "arg2": str},
)
@pytest.mark.asyncio
async def test_command_interpreter_handle_wraps_non_iterable_reply(
command_interpreter: CommandInterpreter,
Expand All @@ -94,6 +110,10 @@ async def test_command_interpreter_handle_wraps_non_iterable_reply(
)


@patch(
_GET_TYPE_HINTS,
lambda _: {"arg1": str, "arg2": str},
)
@pytest.mark.asyncio
@pytest.mark.parametrize("interrupt", [True, False])
async def test_command_interpreter_handle_returns_interupt(
Expand Down
6 changes: 5 additions & 1 deletion tickit/adapters/interpreters/command/command_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import abstractmethod
from inspect import getmembers
from typing import AnyStr, AsyncIterable, Optional, Sequence, Tuple
from typing import AnyStr, AsyncIterable, Optional, Sequence, Tuple, get_type_hints

from tickit.core.adapter import Adapter, Interpreter
from tickit.utils.compat.typing_compat import Protocol, runtime_checkable
Expand Down Expand Up @@ -90,6 +90,10 @@ async def handle(
args = command.parse(message)
if args is None:
continue
args = (
argtype(arg)
for arg, argtype in zip(args, get_type_hints(method).values())
)
resp = await method(*args)
if not isinstance(resp, AsyncIterable):
resp = CommandInterpreter._wrap(resp)
Expand Down