Skip to content

Commit

Permalink
Add wrapper function to interpreter utils and include tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPrit committed Sep 6, 2022
1 parent 5882a2e commit bbc9e97
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
25 changes: 25 additions & 0 deletions tests/adapters/interpreters/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest

from tickit.adapters.interpreters.utils import (
wrap_as_async_iterable,
wrap_messages_as_async_iterable,
)


@pytest.mark.asyncio
async def test_wrap_list_correctly():
messages = ["Hello", "World"]
wrapped = wrap_messages_as_async_iterable(messages)
assert "Hello" == await wrapped.__anext__()
assert "World" == await wrapped.__anext__()
with pytest.raises(StopAsyncIteration):
await wrapped.__anext__()


@pytest.mark.asyncio
async def test_wrap_message_correctly():
message = b"Hello World"
wrapped = wrap_as_async_iterable(message)
assert b"Hello World" == await wrapped.__anext__()
with pytest.raises(StopAsyncIteration):
await wrapped.__anext__()
17 changes: 16 additions & 1 deletion tickit/adapters/interpreters/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import AnyStr, AsyncIterable
from typing import AnyStr, AsyncIterable, List


async def wrap_as_async_iterable(message: AnyStr) -> AsyncIterable[AnyStr]:
Expand All @@ -11,3 +11,18 @@ async def wrap_as_async_iterable(message: AnyStr) -> AsyncIterable[AnyStr]:
AsyncIterable[AnyStr]: An asynchronous iterable containing the message.
"""
yield message


async def wrap_messages_as_async_iterable(
messages: List[AnyStr],
) -> AsyncIterable[AnyStr]:
"""Wraps a message in an asynchronous iterable.
Args:
message (AnyStr): A singular message.
Returns:
AsyncIterable[AnyStr]: An asynchronous iterable containing the message.
"""
for message in messages:
yield message

0 comments on commit bbc9e97

Please sign in to comment.