Skip to content
Merged
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
43 changes: 43 additions & 0 deletions tests/unit/test_async/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,46 @@ async def agen():

with pytest.raises(StopAsyncIteration):
await g.__anext__()


async def test_generator_closes_on_exception_in_context() -> None:
closed = False

@enapter.async_.generator
async def agen():
nonlocal closed
try:
yield 1
yield 2 # pragma: no cover
finally:
closed = True

with pytest.raises(ValueError, match="Test error"):
async with agen() as g:
assert await g.__anext__() == 1
raise ValueError("Test error")

assert closed


async def test_generator_raises_exception() -> None:
@enapter.async_.generator
async def agen():
yield 1
raise ValueError("Generator error")

async with agen() as g:
assert await g.__anext__() == 1
with pytest.raises(ValueError, match="Generator error"):
await g.__anext__()


async def test_generator_with_args() -> None:
@enapter.async_.generator
async def agen(a: int, b: int = 0):
yield a
yield b

async with agen(10, b=20) as g:
assert await g.__anext__() == 10
assert await g.__anext__() == 20
Loading