|
| 1 | +from loguru import logger |
| 2 | +import asyncio |
| 3 | +import sys |
| 4 | + |
| 5 | +logger.remove() |
| 6 | + |
| 7 | +# We're truly only testing whether the tests succeed, we do not care about the formatting. |
| 8 | +# These should be regular Pytest test cases, but that is not possible because the syntax is not valid in Python 3.5. |
| 9 | +logger.add(lambda m: None, format="", diagnose=True, backtrace=True, colorize=True) |
| 10 | + |
| 11 | +def test_decorate_async_generator(): |
| 12 | + @logger.catch(reraise=True) |
| 13 | + async def generator(x, y): |
| 14 | + yield x |
| 15 | + yield y |
| 16 | + |
| 17 | + async def coro(): |
| 18 | + out = [] |
| 19 | + async for val in generator(1, 2): |
| 20 | + out.append(val) |
| 21 | + return out |
| 22 | + |
| 23 | + res = asyncio.run(coro()) |
| 24 | + assert res == [1, 2] |
| 25 | + |
| 26 | + |
| 27 | +def test_decorate_async_generator_with_error(): |
| 28 | + @logger.catch(reraise=False) |
| 29 | + async def generator(x, y): |
| 30 | + yield x |
| 31 | + yield y |
| 32 | + raise ValueError |
| 33 | + |
| 34 | + async def coro(): |
| 35 | + out = [] |
| 36 | + async for val in generator(1, 2): |
| 37 | + out.append(val) |
| 38 | + return out |
| 39 | + |
| 40 | + res = asyncio.run(coro()) |
| 41 | + assert res == [1, 2] |
| 42 | + |
| 43 | +def test_decorate_async_generator_with_error_reraised(): |
| 44 | + @logger.catch(reraise=True) |
| 45 | + async def generator(x, y): |
| 46 | + yield x |
| 47 | + yield y |
| 48 | + raise ValueError |
| 49 | + |
| 50 | + async def coro(): |
| 51 | + out = [] |
| 52 | + try: |
| 53 | + async for val in generator(1, 2): |
| 54 | + out.append(val) |
| 55 | + except ValueError: |
| 56 | + pass |
| 57 | + else: |
| 58 | + raise AssertionError("ValueError not raised") |
| 59 | + return out |
| 60 | + |
| 61 | + res = asyncio.run(coro()) |
| 62 | + assert res == [1, 2] |
| 63 | + |
| 64 | + |
| 65 | +def test_decorate_async_generator_then_async_send(): |
| 66 | + @logger.catch |
| 67 | + async def generator(x, y): |
| 68 | + yield x |
| 69 | + yield y |
| 70 | + |
| 71 | + async def coro(): |
| 72 | + gen = generator(1, 2) |
| 73 | + await gen.asend(None) |
| 74 | + await gen.asend(None) |
| 75 | + try: |
| 76 | + await gen.asend(None) |
| 77 | + except StopAsyncIteration: |
| 78 | + pass |
| 79 | + else: |
| 80 | + raise AssertionError("StopAsyncIteration not raised") |
| 81 | + |
| 82 | + asyncio.run(coro()) |
| 83 | + |
| 84 | + |
| 85 | +def test_decorate_async_generator_then_async_throw(): |
| 86 | + @logger.catch |
| 87 | + async def generator(x, y): |
| 88 | + yield x |
| 89 | + yield y |
| 90 | + |
| 91 | + async def coro(): |
| 92 | + gen = generator(1, 2) |
| 93 | + await gen.asend(None) |
| 94 | + try: |
| 95 | + await gen.athrow(ValueError) |
| 96 | + except ValueError: |
| 97 | + pass |
| 98 | + else: |
| 99 | + raise AssertionError("ValueError not raised") |
| 100 | + |
| 101 | + asyncio.run(coro()) |
| 102 | + |
| 103 | + |
| 104 | +test_decorate_async_generator() |
| 105 | +test_decorate_async_generator_with_error() |
| 106 | +test_decorate_async_generator_with_error_reraised() |
| 107 | +test_decorate_async_generator_then_async_send() |
| 108 | +test_decorate_async_generator_then_async_throw() |
| 109 | + |
| 110 | +logger.add(sys.stderr, format="{message}") |
| 111 | +logger.info("Done") |
0 commit comments