From de962d043dc5598b40de4db41e5de708ecbbc8b4 Mon Sep 17 00:00:00 2001 From: mpa Date: Thu, 28 May 2020 05:34:31 +0400 Subject: [PATCH] fix(tests): use relevant BaseHandler.__init__ signature fix breaking tests, change signature of BaseHandler --- aiogram/dispatcher/handler/base.py | 16 +++++++++++----- tests/test_dispatcher/test_handler/test_base.py | 8 ++++---- .../test_handler/test_callback_query.py | 2 +- .../test_dispatcher/test_handler/test_message.py | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/aiogram/dispatcher/handler/base.py b/aiogram/dispatcher/handler/base.py index 3b47b456..162e8f10 100644 --- a/aiogram/dispatcher/handler/base.py +++ b/aiogram/dispatcher/handler/base.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import TYPE_CHECKING, Any, Dict, Generic, TypeVar, cast +from typing import TYPE_CHECKING, Any, Dict, Generic, Optional, TypeVar, cast from aiogram import Bot from aiogram.api.types import Update @@ -18,12 +18,18 @@ class BaseHandler(BaseHandlerMixin[T], ABC): Base class for all class-based handlers """ - def __init__(self, event: T, requirements_data: Dict[str, Any], data: Dict[str, Any]) -> None: + def __init__( + self, + event: T, + requirements_data: Optional[Dict[str, Any]] = None, + data: Optional[Dict[str, Any]] = None, + ) -> None: self.event: T = event - self.data: Dict[str, Any] = data + self.data: Dict[str, Any] = data or {} - for req_attr, req in requirements_data.items(): - setattr(self, req_attr, req) + if requirements_data: + for req_attr, req in requirements_data.items(): + setattr(self, req_attr, req) @property def bot(self) -> Bot: diff --git a/tests/test_dispatcher/test_handler/test_base.py b/tests/test_dispatcher/test_handler/test_base.py index 21063b62..b90adb64 100644 --- a/tests/test_dispatcher/test_handler/test_base.py +++ b/tests/test_dispatcher/test_handler/test_base.py @@ -19,7 +19,7 @@ class TestBaseClassBasedHandler: @pytest.mark.asyncio async def test_base_handler(self): event = Update(update_id=42) - handler = MyHandler(event=event, key=42) + handler = MyHandler(event=event, data=dict(key=42)) assert handler.event == event assert handler.data["key"] == 42 @@ -29,7 +29,7 @@ class TestBaseClassBasedHandler: @pytest.mark.asyncio async def test_bot_from_context(self): event = Update(update_id=42) - handler = MyHandler(event=event, key=42) + handler = MyHandler(event=event, data=dict(key=42)) bot = Bot("42:TEST") with pytest.raises(LookupError): @@ -42,7 +42,7 @@ class TestBaseClassBasedHandler: async def test_bot_from_data(self): event = Update(update_id=42) bot = Bot("42:TEST") - handler = MyHandler(event=event, key=42, bot=bot) + handler = MyHandler(event=event, data=dict(key=42, bot=bot)) assert "bot" in handler.data assert handler.bot == bot @@ -52,7 +52,7 @@ class TestBaseClassBasedHandler: message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now() ) update = Update(update_id=42, message=event) - handler = MyHandler(event=event, update=update) + handler = MyHandler(event=event, data=dict(update=update)) assert handler.event == event assert handler.update == update diff --git a/tests/test_dispatcher/test_handler/test_callback_query.py b/tests/test_dispatcher/test_handler/test_callback_query.py index c63381de..2f089875 100644 --- a/tests/test_dispatcher/test_handler/test_callback_query.py +++ b/tests/test_dispatcher/test_handler/test_callback_query.py @@ -24,4 +24,4 @@ class TestCallbackQueryHandler: assert self.message == self.message return True - assert await MyHandler(event) + assert await MyHandler(event, {}, {}) diff --git a/tests/test_dispatcher/test_handler/test_message.py b/tests/test_dispatcher/test_handler/test_message.py index d4cc4818..4320ed07 100644 --- a/tests/test_dispatcher/test_handler/test_message.py +++ b/tests/test_dispatcher/test_handler/test_message.py @@ -44,7 +44,7 @@ class TestBaseMessageHandlerCommandMixin: chat=Chat(id=42, type="private"), from_user=User(id=42, is_bot=False, first_name="Test"), ), - command=CommandObject(prefix="/", command="command", args="args"), + data=dict(command=CommandObject(prefix="/", command="command", args="args")), ) assert isinstance(handler.command, CommandObject)