Skip to content

Commit 3beb59d

Browse files
🧪 Add comprehensive tests for async_.generator
Added tests to `tests/unit/test_async/test_generator.py` to cover: - Generator correctly closing when an exception is raised inside the context manager - Exception propagation when the generator itself raises an error - Proper handling of positional and keyword arguments passed to the wrapped generator Co-authored-by: rnovatorov <20299819+rnovatorov@users.noreply.github.com>
1 parent 4e14276 commit 3beb59d

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

‎tests/unit/test_async/test_generator.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,46 @@ async def agen():
1616

1717
with pytest.raises(StopAsyncIteration):
1818
await g.__anext__()
19+
20+
21+
async def test_generator_closes_on_exception_in_context() -> None:
22+
closed = False
23+
24+
@enapter.async_.generator
25+
async def agen():
26+
nonlocal closed
27+
try:
28+
yield 1
29+
yield 2 # pragma: no cover
30+
finally:
31+
closed = True
32+
33+
with pytest.raises(ValueError, match="Test error"):
34+
async with agen() as g:
35+
assert await g.__anext__() == 1
36+
raise ValueError("Test error")
37+
38+
assert closed
39+
40+
41+
async def test_generator_raises_exception() -> None:
42+
@enapter.async_.generator
43+
async def agen():
44+
yield 1
45+
raise ValueError("Generator error")
46+
47+
async with agen() as g:
48+
assert await g.__anext__() == 1
49+
with pytest.raises(ValueError, match="Generator error"):
50+
await g.__anext__()
51+
52+
53+
async def test_generator_with_args() -> None:
54+
@enapter.async_.generator
55+
async def agen(a: int, b: int = 0):
56+
yield a
57+
yield b
58+
59+
async with agen(10, b=20) as g:
60+
assert await g.__anext__() == 10
61+
assert await g.__anext__() == 20

0 commit comments

Comments
 (0)