Removed the use of the context instance (Bot.get_current) from all placements that were used previously. (#1230)

* Removed the use of the context instance (Bot.get_current) from all placements that were used previously.

* Fixed tests

* Added changelog

* Change category
This commit is contained in:
Alex Root Junior 2023-07-28 22:23:32 +03:00 committed by GitHub
parent 479e302cba
commit 2ecf9cefd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 45 additions and 49 deletions

View file

@ -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()

View file

@ -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")

View file

@ -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