mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Dev 3.x flat package (#961)
* Move packages * Added changelog * Update examples/echo_bot.py Co-authored-by: Oleg A. <t0rr@mail.ru> * Rename `handler` -> `handlers` * Update __init__.py Co-authored-by: Oleg A. <t0rr@mail.ru>
This commit is contained in:
parent
5e7932ca20
commit
4315ecf1a2
111 changed files with 376 additions and 390 deletions
0
tests/test_handler/__init__.py
Normal file
0
tests/test_handler/__init__.py
Normal file
64
tests/test_handler/test_base.py
Normal file
64
tests/test_handler/test_base.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import asyncio
|
||||
import datetime
|
||||
from functools import wraps
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram import Bot
|
||||
from aiogram.dispatcher.event.handler import HandlerObject
|
||||
from aiogram.handlers import BaseHandler
|
||||
from aiogram.types import Chat, Message, Update
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class MyHandler(BaseHandler):
|
||||
async def handle(self) -> Any:
|
||||
await asyncio.sleep(0.1)
|
||||
return 42
|
||||
|
||||
|
||||
class TestBaseClassBasedHandler:
|
||||
async def test_base_handler(self):
|
||||
event = Update(update_id=42)
|
||||
handler = MyHandler(event=event, key=42)
|
||||
|
||||
assert handler.event == event
|
||||
assert handler.data["key"] == 42
|
||||
assert not hasattr(handler, "filters")
|
||||
assert await handler == 42
|
||||
|
||||
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)
|
||||
assert handler.bot == bot
|
||||
|
||||
async def test_bot_from_data(self):
|
||||
event = Update(update_id=42)
|
||||
bot = Bot("42:TEST")
|
||||
handler = MyHandler(event=event, key=42, bot=bot)
|
||||
|
||||
assert "bot" in handler.data
|
||||
assert handler.bot == bot
|
||||
|
||||
def test_update_from_data(self):
|
||||
event = Message(
|
||||
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)
|
||||
|
||||
assert handler.event == event
|
||||
assert handler.update == update
|
||||
|
||||
async def test_wrapped_handler(self):
|
||||
# wrap the handler on dummy function
|
||||
handler = wraps(MyHandler)(lambda: None)
|
||||
assert HandlerObject(handler).awaitable is True
|
||||
28
tests/test_handler/test_callback_query.py
Normal file
28
tests/test_handler/test_callback_query.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.handlers import CallbackQueryHandler
|
||||
from aiogram.types import CallbackQuery, User
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class TestCallbackQueryHandler:
|
||||
async def test_attributes_aliases(self):
|
||||
event = CallbackQuery(
|
||||
id="chosen",
|
||||
from_user=User(id=42, is_bot=False, first_name="Test"),
|
||||
data="test",
|
||||
chat_instance="test",
|
||||
)
|
||||
|
||||
class MyHandler(CallbackQueryHandler):
|
||||
async def handle(self) -> Any:
|
||||
assert self.event == event
|
||||
assert self.from_user == self.event.from_user
|
||||
assert self.callback_data == self.event.data
|
||||
assert self.message == self.message
|
||||
return True
|
||||
|
||||
assert await MyHandler(event)
|
||||
29
tests/test_handler/test_chat_member.py
Normal file
29
tests/test_handler/test_chat_member.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import datetime
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.handlers import ChatMemberHandler
|
||||
from aiogram.types import Chat, ChatMemberMember, ChatMemberUpdated, User
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class TestChatMemberUpdated:
|
||||
async def test_attributes_aliases(self):
|
||||
event = ChatMemberUpdated(
|
||||
chat=Chat(id=42, type="private"),
|
||||
from_user=User(id=42, is_bot=False, first_name="Test"),
|
||||
date=datetime.datetime.now(),
|
||||
old_chat_member=ChatMemberMember(user=User(id=42, is_bot=False, first_name="Test")),
|
||||
new_chat_member=ChatMemberMember(user=User(id=42, is_bot=False, first_name="Test")),
|
||||
)
|
||||
|
||||
class MyHandler(ChatMemberHandler):
|
||||
async def handle(self) -> Any:
|
||||
assert self.event == event
|
||||
assert self.from_user == self.event.from_user
|
||||
|
||||
return True
|
||||
|
||||
assert await MyHandler(event)
|
||||
26
tests/test_handler/test_chosen_inline_result.py
Normal file
26
tests/test_handler/test_chosen_inline_result.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.handlers import ChosenInlineResultHandler
|
||||
from aiogram.types import ChosenInlineResult, User
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class TestChosenInlineResultHandler:
|
||||
async def test_attributes_aliases(self):
|
||||
event = ChosenInlineResult(
|
||||
result_id="chosen",
|
||||
from_user=User(id=42, is_bot=False, first_name="Test"),
|
||||
query="test",
|
||||
)
|
||||
|
||||
class MyHandler(ChosenInlineResultHandler):
|
||||
async def handle(self) -> Any:
|
||||
assert self.event == event
|
||||
assert self.from_user == self.event.from_user
|
||||
assert self.query == self.event.query
|
||||
return True
|
||||
|
||||
assert await MyHandler(event)
|
||||
21
tests/test_handler/test_error.py
Normal file
21
tests/test_handler/test_error.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.handlers import ErrorHandler
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class TestErrorHandler:
|
||||
async def test_extensions(self):
|
||||
event = KeyError("kaboom")
|
||||
|
||||
class MyHandler(ErrorHandler):
|
||||
async def handle(self) -> Any:
|
||||
assert self.event == event
|
||||
assert self.exception_name == event.__class__.__name__
|
||||
assert self.exception_message == str(event)
|
||||
return True
|
||||
|
||||
assert await MyHandler(event)
|
||||
27
tests/test_handler/test_inline_query.py
Normal file
27
tests/test_handler/test_inline_query.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.handlers import InlineQueryHandler
|
||||
from aiogram.types import InlineQuery, User
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class TestCallbackQueryHandler:
|
||||
async def test_attributes_aliases(self):
|
||||
event = InlineQuery(
|
||||
id="query",
|
||||
from_user=User(id=42, is_bot=False, first_name="Test"),
|
||||
query="query",
|
||||
offset="0",
|
||||
)
|
||||
|
||||
class MyHandler(InlineQueryHandler):
|
||||
async def handle(self) -> Any:
|
||||
assert self.event == event
|
||||
assert self.from_user == self.event.from_user
|
||||
assert self.query == self.event.query
|
||||
return True
|
||||
|
||||
assert await MyHandler(event)
|
||||
65
tests/test_handler/test_message.py
Normal file
65
tests/test_handler/test_message.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import datetime
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.filters import CommandObject
|
||||
from aiogram.handlers import MessageHandler, MessageHandlerCommandMixin
|
||||
from aiogram.types import Chat, Message, User
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class MyHandler(MessageHandler):
|
||||
async def handle(self) -> Any:
|
||||
return self.event.text
|
||||
|
||||
|
||||
class TestClassBasedMessageHandler:
|
||||
async def test_message_handler(self):
|
||||
event = 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"),
|
||||
)
|
||||
handler = MyHandler(event=event)
|
||||
|
||||
assert handler.from_user == event.from_user
|
||||
assert handler.chat == event.chat
|
||||
|
||||
|
||||
class HandlerWithCommand(MessageHandlerCommandMixin, MessageHandler):
|
||||
async def handle(self) -> Any:
|
||||
return self.command
|
||||
|
||||
|
||||
class TestBaseMessageHandlerCommandMixin:
|
||||
def test_command_accessible(self):
|
||||
handler = HandlerWithCommand(
|
||||
Message(
|
||||
message_id=42,
|
||||
date=datetime.datetime.now(),
|
||||
text="/test args",
|
||||
chat=Chat(id=42, type="private"),
|
||||
from_user=User(id=42, is_bot=False, first_name="Test"),
|
||||
),
|
||||
command=CommandObject(prefix="/", command="command", args="args"),
|
||||
)
|
||||
|
||||
assert isinstance(handler.command, CommandObject)
|
||||
assert handler.command.command == "command"
|
||||
|
||||
def test_command_not_presented(self):
|
||||
handler = HandlerWithCommand(
|
||||
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"),
|
||||
)
|
||||
)
|
||||
|
||||
assert handler.command is None
|
||||
33
tests/test_handler/test_poll.py
Normal file
33
tests/test_handler/test_poll.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.handlers import PollHandler
|
||||
from aiogram.types import Poll, PollOption
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class TestShippingQueryHandler:
|
||||
async def test_attributes_aliases(self):
|
||||
event = Poll(
|
||||
id="query",
|
||||
question="Q?",
|
||||
options=[PollOption(text="A1", voter_count=1)],
|
||||
is_closed=True,
|
||||
is_anonymous=False,
|
||||
type="quiz",
|
||||
allows_multiple_answers=False,
|
||||
total_voter_count=0,
|
||||
correct_option_id=0,
|
||||
)
|
||||
|
||||
class MyHandler(PollHandler):
|
||||
async def handle(self) -> Any:
|
||||
assert self.event == event
|
||||
assert self.question == self.event.question
|
||||
assert self.options == self.event.options
|
||||
|
||||
return True
|
||||
|
||||
assert await MyHandler(event)
|
||||
27
tests/test_handler/test_pre_checkout_query.py
Normal file
27
tests/test_handler/test_pre_checkout_query.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.handlers import PreCheckoutQueryHandler
|
||||
from aiogram.types import PreCheckoutQuery, User
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class TestPreCheckoutQueryHandler:
|
||||
async def test_attributes_aliases(self):
|
||||
event = PreCheckoutQuery(
|
||||
id="query",
|
||||
from_user=User(id=42, is_bot=False, first_name="Test"),
|
||||
currency="BTC",
|
||||
total_amount=7,
|
||||
invoice_payload="payload",
|
||||
)
|
||||
|
||||
class MyHandler(PreCheckoutQueryHandler):
|
||||
async def handle(self) -> Any:
|
||||
assert self.event == event
|
||||
assert self.from_user == self.event.from_user
|
||||
return True
|
||||
|
||||
assert await MyHandler(event)
|
||||
33
tests/test_handler/test_shipping_query.py
Normal file
33
tests/test_handler/test_shipping_query.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.handlers import ShippingQueryHandler
|
||||
from aiogram.types import ShippingAddress, ShippingQuery, User
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class TestShippingQueryHandler:
|
||||
async def test_attributes_aliases(self):
|
||||
event = ShippingQuery(
|
||||
id="query",
|
||||
from_user=User(id=42, is_bot=False, first_name="Test"),
|
||||
invoice_payload="payload",
|
||||
shipping_address=ShippingAddress(
|
||||
country_code="country_code",
|
||||
state="state",
|
||||
city="city",
|
||||
street_line1="street_line1",
|
||||
street_line2="street_line2",
|
||||
post_code="post_code",
|
||||
),
|
||||
)
|
||||
|
||||
class MyHandler(ShippingQueryHandler):
|
||||
async def handle(self) -> Any:
|
||||
assert self.event == event
|
||||
assert self.from_user == self.event.from_user
|
||||
return True
|
||||
|
||||
assert await MyHandler(event)
|
||||
Loading…
Add table
Add a link
Reference in a new issue