Skip to content

Commit

Permalink
Add: Add class for mocking an AsyncIterator from an Iterable
Browse files Browse the repository at this point in the history
Add a mock class for creating an AsyncIterator from an Iterable.
  • Loading branch information
bjoernricks committed Oct 25, 2022
1 parent ff45bc3 commit edc4c22
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import builtins
import sys
import unittest
from typing import Any
from typing import Any, AsyncIterator, Awaitable, Iterable
from unittest import TestCase
from unittest.mock import MagicMock

Expand Down Expand Up @@ -81,4 +81,15 @@ def assert_not_awaited(self):
pass


class AsyncIteratorMock(AsyncIterator):
def __init__(self, iterable: Iterable[Any]) -> None:
self.iterator = iter(iterable)

async def __anext__(self) -> Awaitable[Any]:
try:
return next(self.iterator)
except StopIteration:
raise StopAsyncIteration() from None


__all__ = ["IsolatedAsyncioTestCase", "AsyncMock"]

0 comments on commit edc4c22

Please sign in to comment.