|
| 1 | +from aiocarrot import Carrot, Consumer |
| 2 | + |
| 3 | +from pydantic import BaseModel |
| 4 | + |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +import asyncio |
| 8 | + |
| 9 | + |
| 10 | +async def test_validation(carrot_client: Carrot) -> None: |
| 11 | + success_counts = 0 |
| 12 | + consumer = Consumer() |
| 13 | + |
| 14 | + class SampleModel(BaseModel): |
| 15 | + number: int |
| 16 | + optional_number: Optional[int] = None |
| 17 | + |
| 18 | + @consumer.message(name='full_model') |
| 19 | + async def full_model_message(model: SampleModel) -> None: |
| 20 | + nonlocal success_counts |
| 21 | + |
| 22 | + if model.number != 10: |
| 23 | + raise ValueError('<number> must be 10') |
| 24 | + |
| 25 | + if model.optional_number != 20: |
| 26 | + raise ValueError('<optional_number> must be 20') |
| 27 | + |
| 28 | + success_counts += 1 |
| 29 | + |
| 30 | + @consumer.message(name='empty_model') |
| 31 | + async def empty_model_message(model: SampleModel) -> None: |
| 32 | + nonlocal success_counts |
| 33 | + |
| 34 | + if model.number != 12: |
| 35 | + raise ValueError('<number> must be 12') |
| 36 | + |
| 37 | + if model.optional_number is not None: |
| 38 | + raise ValueError('<optional_number> must be None') |
| 39 | + |
| 40 | + success_counts += 1 |
| 41 | + |
| 42 | + @consumer.message(name='int_variables') |
| 43 | + async def int_variables_message(number: int, optional_number: Optional[int] = None) -> None: |
| 44 | + nonlocal success_counts |
| 45 | + |
| 46 | + if number != 15: |
| 47 | + raise ValueError('<number> must be 15') |
| 48 | + |
| 49 | + if optional_number is not None and optional_number != 0: |
| 50 | + raise ValueError('<optional_number> must be empty or 0') |
| 51 | + |
| 52 | + success_counts += 1 |
| 53 | + |
| 54 | + @consumer.message(name='boolean_variables') |
| 55 | + async def boolean_variables_message(value: bool) -> None: |
| 56 | + nonlocal success_counts |
| 57 | + |
| 58 | + if value is True: |
| 59 | + raise ValueError('<value> must be False') |
| 60 | + |
| 61 | + success_counts += 1 |
| 62 | + |
| 63 | + carrot_client.setup_consumer(consumer) |
| 64 | + |
| 65 | + await asyncio.sleep(1) |
| 66 | + |
| 67 | + await carrot_client.send('full_model', model=SampleModel(number=10, optional_number=20)) |
| 68 | + await carrot_client.send('empty_model', model=SampleModel(number=12)) |
| 69 | + await carrot_client.send('int_variables', number=15, optional_number=0) |
| 70 | + await carrot_client.send('int_variables', number=15, optional_number=None) |
| 71 | + await carrot_client.send('int_variables', number=15) |
| 72 | + await carrot_client.send('boolean_variables', value=False) |
| 73 | + await carrot_client.send('boolean_variables', value=True) |
| 74 | + await carrot_client.send('boolean_variables', value=None) |
| 75 | + await carrot_client.send('boolean_variables', value=254) |
| 76 | + |
| 77 | + await asyncio.sleep(1) # Wait for tasks completed |
| 78 | + |
| 79 | + assert success_counts == 6 |
0 commit comments