From 84d767fdfc14163090b3bab4661f9b4341fcfb72 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Fri, 28 Jul 2023 22:16:52 +0300 Subject: [PATCH] Fixed tests --- aiogram/dispatcher/dispatcher.py | 2 +- aiogram/utils/chat_action.py | 22 +++++++++++----------- tests/conftest.py | 9 ++------- tests/test_handler/test_base.py | 14 ++++++++------ tests/test_utils/test_chat_action.py | 5 ++--- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 6b64b180..eb2d55f2 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -18,7 +18,7 @@ from ..fsm.strategy import FSMStrategy from ..methods import GetUpdates, TelegramMethod from ..methods.base import TelegramType from ..types import Update, User -from ..types.base import UNSET_TYPE, UNSET +from ..types.base import UNSET, UNSET_TYPE from ..types.update import UpdateTypeLookupError from ..utils.backoff import Backoff, BackoffConfig from .event.bases import UNHANDLED, SkipHandler diff --git a/aiogram/utils/chat_action.py b/aiogram/utils/chat_action.py index 41e648f1..d4778443 100644 --- a/aiogram/utils/chat_action.py +++ b/aiogram/utils/chat_action.py @@ -129,7 +129,7 @@ class ChatActionSender: def typing( cls, chat_id: Union[int, str], - bot: Optional[Bot] = None, + bot: Bot, interval: float = DEFAULT_INTERVAL, initial_sleep: float = DEFAULT_INITIAL_SLEEP, ) -> "ChatActionSender": @@ -146,7 +146,7 @@ class ChatActionSender: def upload_photo( cls, chat_id: Union[int, str], - bot: Optional[Bot] = None, + bot: Bot, interval: float = DEFAULT_INTERVAL, initial_sleep: float = DEFAULT_INITIAL_SLEEP, ) -> "ChatActionSender": @@ -163,7 +163,7 @@ class ChatActionSender: def record_video( cls, chat_id: Union[int, str], - bot: Optional[Bot] = None, + bot: Bot, interval: float = DEFAULT_INTERVAL, initial_sleep: float = DEFAULT_INITIAL_SLEEP, ) -> "ChatActionSender": @@ -180,7 +180,7 @@ class ChatActionSender: def upload_video( cls, chat_id: Union[int, str], - bot: Optional[Bot] = None, + bot: Bot, interval: float = DEFAULT_INTERVAL, initial_sleep: float = DEFAULT_INITIAL_SLEEP, ) -> "ChatActionSender": @@ -197,7 +197,7 @@ class ChatActionSender: def record_voice( cls, chat_id: Union[int, str], - bot: Optional[Bot] = None, + bot: Bot, interval: float = DEFAULT_INTERVAL, initial_sleep: float = DEFAULT_INITIAL_SLEEP, ) -> "ChatActionSender": @@ -214,7 +214,7 @@ class ChatActionSender: def upload_voice( cls, chat_id: Union[int, str], - bot: Optional[Bot] = None, + bot: Bot, interval: float = DEFAULT_INTERVAL, initial_sleep: float = DEFAULT_INITIAL_SLEEP, ) -> "ChatActionSender": @@ -231,7 +231,7 @@ class ChatActionSender: def upload_document( cls, chat_id: Union[int, str], - bot: Optional[Bot] = None, + bot: Bot, interval: float = DEFAULT_INTERVAL, initial_sleep: float = DEFAULT_INITIAL_SLEEP, ) -> "ChatActionSender": @@ -248,7 +248,7 @@ class ChatActionSender: def choose_sticker( cls, chat_id: Union[int, str], - bot: Optional[Bot] = None, + bot: Bot, interval: float = DEFAULT_INTERVAL, initial_sleep: float = DEFAULT_INITIAL_SLEEP, ) -> "ChatActionSender": @@ -265,7 +265,7 @@ class ChatActionSender: def find_location( cls, chat_id: Union[int, str], - bot: Optional[Bot] = None, + bot: Bot, interval: float = DEFAULT_INTERVAL, initial_sleep: float = DEFAULT_INITIAL_SLEEP, ) -> "ChatActionSender": @@ -282,7 +282,7 @@ class ChatActionSender: def record_video_note( cls, chat_id: Union[int, str], - bot: Optional[Bot] = None, + bot: Bot, interval: float = DEFAULT_INTERVAL, initial_sleep: float = DEFAULT_INITIAL_SLEEP, ) -> "ChatActionSender": @@ -299,7 +299,7 @@ class ChatActionSender: def upload_video_note( cls, chat_id: Union[int, str], - bot: Optional[Bot] = None, + bot: Bot, interval: float = DEFAULT_INTERVAL, initial_sleep: float = DEFAULT_INITIAL_SLEEP, ) -> "ChatActionSender": diff --git a/tests/conftest.py b/tests/conftest.py index b7b7774a..1c72f386 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,7 @@ import pytest from _pytest.config import UsageError from redis.asyncio.connection import parse_url as parse_redis_url -from aiogram import Bot, Dispatcher +from aiogram import Dispatcher from aiogram.fsm.storage.memory import ( DisabledEventIsolation, MemoryStorage, @@ -109,12 +109,7 @@ async def disabled_isolation(): @pytest.fixture() def bot(): - bot = MockedBot() - token = Bot.set_current(bot) - try: - yield bot - finally: - Bot.reset_current(token) + return MockedBot() @pytest.fixture() diff --git a/tests/test_handler/test_base.py b/tests/test_handler/test_base.py index f79a6a6b..5f3b1225 100644 --- a/tests/test_handler/test_base.py +++ b/tests/test_handler/test_base.py @@ -29,15 +29,17 @@ class TestBaseClassBasedHandler: async def test_bot_from_context(self): event = Update(update_id=42) - handler = MyHandler(event=event, key=42) bot = Bot("42:TEST") - - with pytest.raises(LookupError): - handler.bot - - Bot.set_current(bot) + handler = MyHandler(event=event, key=42, bot=bot) assert handler.bot == bot + async def test_bot_from_context_missing(self): + event = Update(update_id=42) + handler = MyHandler(event=event, key=42) + + with pytest.raises(RuntimeError): + handler.bot + async def test_bot_from_data(self): event = Update(update_id=42) bot = Bot("42:TEST") diff --git a/tests/test_utils/test_chat_action.py b/tests/test_utils/test_chat_action.py index 517b8e90..84cb8abb 100644 --- a/tests/test_utils/test_chat_action.py +++ b/tests/test_utils/test_chat_action.py @@ -36,10 +36,9 @@ class TestChatActionSender: "upload_video_note", ], ) - @pytest.mark.parametrize("pass_bot", [True, False]) - async def test_factory(self, action: str, bot: MockedBot, pass_bot: bool): + async def test_factory(self, action: str, bot: MockedBot): sender_factory = getattr(ChatActionSender, action) - sender = sender_factory(chat_id=42, bot=bot if pass_bot else None) + sender = sender_factory(chat_id=42, bot=bot) assert isinstance(sender, ChatActionSender) assert sender.action == action assert sender.chat_id == 42