From 3a38125619fa473eb0568969ccb85a9cb3279791 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Mon, 25 Feb 2019 12:34:26 +0300 Subject: [PATCH] Check bot instance on Dispatcher __init__ --- aiogram/dispatcher/dispatcher.py | 5 ++++- tests/test_dispatcher.py | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/test_dispatcher.py diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 035ec1f5..d1fc72ba 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -35,6 +35,9 @@ class Dispatcher(DataMixin, ContextInstanceMixin): throttling_rate_limit=DEFAULT_RATE_LIMIT, no_throttle_error=False, filters_factory=None): + if not isinstance(bot, Bot): + raise TypeError(f"Argument 'bot' must be an instance of Bot, not '{type(bot).__name__}'") + if loop is None: loop = bot.loop if storage is None: @@ -276,7 +279,7 @@ class Dispatcher(DataMixin, ContextInstanceMixin): :return: """ - if self._polling: + if hasattr(self, '_polling') and self._polling: log.info('Stop polling...') self._polling = False diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py new file mode 100644 index 00000000..3dec03e7 --- /dev/null +++ b/tests/test_dispatcher.py @@ -0,0 +1,35 @@ +from aiogram import Dispatcher, Bot + +import pytest + +pytestmark = pytest.mark.asyncio + + +@pytest.yield_fixture() +async def bot(event_loop): + """ Bot fixture """ + _bot = Bot(token='123456789:AABBCCDDEEFFaabbccddeeff-1234567890', + loop=event_loop) + yield _bot + await _bot.close() + + +class TestDispatcherInit: + async def test_successful_init(self, bot): + """ + Success __init__ case + + :param bot: bot instance + :type bot: Bot + """ + dp = Dispatcher(bot=bot) + assert isinstance(dp, Dispatcher) + + @pytest.mark.parametrize("bot_instance", [None, Bot, 123, 'abc']) + async def test_wrong_bot_instance(self, bot_instance): + """ + User provides wrong data to 'bot' argument. + :return: TypeError with reason + """ + with pytest.raises(TypeError): + _ = Dispatcher(bot=bot_instance)