From 55496ab9ca6039b343af3c4282f6385c91dccf1d Mon Sep 17 00:00:00 2001 From: jrootjunior Date: Tue, 26 Nov 2019 11:41:23 +0200 Subject: [PATCH] Partially cover dispatcher --- aiogram/dispatcher/dispatcher.py | 4 +- tests/test_dispatcher/test_dispatcher.py | 59 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 tests/test_dispatcher/test_dispatcher.py diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 0b9e55e6..06c95155 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -37,11 +37,11 @@ class Dispatcher(Router): update_id = update.update_id + 1 async def polling(self, bot: Bot): - self.emit_startup(bot=bot) + await self.emit_startup(bot=bot) try: async for update in self.listen_updates(bot): async for result in self.feed_update(bot, update): if isinstance(result, TelegramMethod): await result finally: - self.emit_shutdown(bot=bot) + await self.emit_shutdown(bot=bot) diff --git a/tests/test_dispatcher/test_dispatcher.py b/tests/test_dispatcher/test_dispatcher.py new file mode 100644 index 00000000..b4183cc7 --- /dev/null +++ b/tests/test_dispatcher/test_dispatcher.py @@ -0,0 +1,59 @@ +import datetime + +import pytest + +from aiogram import Bot +from aiogram.api.types import Message, Update, Chat, User +from aiogram.dispatcher.dispatcher import Dispatcher +from aiogram.dispatcher.router import Router + + +class TestDispatcher: + def test_parent_router(self): + dp = Dispatcher() + with pytest.raises(RuntimeError): + dp.parent_router = Router() + assert dp.parent_router is None + dp._parent_router = Router() + assert dp.parent_router is None + + @pytest.mark.asyncio + async def test_feed_update(self): + dp = Dispatcher() + bot = Bot("TOKEN") + + @dp.message_handler() + async def my_handler(message: Message, **kwargs): + assert "bot" in kwargs + assert isinstance(kwargs["bot"], Bot) + assert kwargs["bot"] == bot + return message.text + + results_count = 0 + async for result in dp.feed_update( + bot=bot, + update=Update( + update_id=42, + message=Message( + message_id=42, + date=datetime.datetime.now(), + text="test", + chat=Chat(id=42, type="private"), + from_user=User(id=42, is_bot=False, first_name="Test"), + ), + ), + ): + results_count += 1 + assert result == "test" + + assert results_count == 1 + + @pytest.mark.skip + @pytest.mark.asyncio + async def test_listen_updates(self): + pass + + @pytest.mark.skip + @pytest.mark.asyncio + async def test_polling(self): + pass