|
| 1 | +from typing import Any, Callable, Coroutine |
| 2 | + |
| 3 | +from taskiq.abc.broker import AsyncBroker |
| 4 | +from taskiq.decor import AsyncTaskiqDecoratedTask |
| 5 | +from taskiq.message import BrokerMessage |
| 6 | + |
| 7 | + |
| 8 | +class _TestBroker(AsyncBroker): |
| 9 | + """Broker for testing purpose.""" |
| 10 | + |
| 11 | + async def kick(self, message: BrokerMessage) -> None: |
| 12 | + """ |
| 13 | + This method is used to send messages. |
| 14 | +
|
| 15 | + But in this case it just throws messages away. |
| 16 | +
|
| 17 | + :param message: message to lost. |
| 18 | + """ |
| 19 | + |
| 20 | + async def listen( |
| 21 | + self, |
| 22 | + callback: Callable[[BrokerMessage], Coroutine[Any, Any, None]], |
| 23 | + ) -> None: |
| 24 | + """ |
| 25 | + This method is not implemented. |
| 26 | +
|
| 27 | + :param callback: callback that is never called. |
| 28 | + """ |
| 29 | + |
| 30 | + |
| 31 | +def test_decorator_success() -> None: |
| 32 | + """Test that decoration without parameters works.""" |
| 33 | + tbrok = _TestBroker() |
| 34 | + |
| 35 | + @tbrok.task |
| 36 | + async def test_func() -> None: |
| 37 | + """Some test function.""" |
| 38 | + |
| 39 | + assert isinstance(test_func, AsyncTaskiqDecoratedTask) |
| 40 | + |
| 41 | + |
| 42 | +def test_decorator_with_name_success() -> None: |
| 43 | + """Test that task_name is successfully set.""" |
| 44 | + tbrok = _TestBroker() |
| 45 | + |
| 46 | + @tbrok.task(task_name="my_task") |
| 47 | + async def test_func() -> None: |
| 48 | + """Some test function.""" |
| 49 | + |
| 50 | + assert isinstance(test_func, AsyncTaskiqDecoratedTask) |
| 51 | + assert test_func.task_name == "my_task" |
| 52 | + |
| 53 | + |
| 54 | +def test_decorator_with_labels_success() -> None: |
| 55 | + """Tests that labels are assigned for task as is.""" |
| 56 | + tbrok = _TestBroker() |
| 57 | + |
| 58 | + @tbrok.task(label1=1, label2=2) |
| 59 | + async def test_func() -> None: |
| 60 | + """Some test function.""" |
| 61 | + |
| 62 | + assert isinstance(test_func, AsyncTaskiqDecoratedTask) |
| 63 | + assert test_func.labels == { |
| 64 | + "label1": 1, |
| 65 | + "label2": 2, |
| 66 | + } |
0 commit comments