2019-12-03 00:03:15 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2019-12-03 18:32:21 +02:00
|
|
|
from aiogram.dispatcher.filters import CommandObject
|
|
|
|
|
from aiogram.dispatcher.handler.message import MessageHandler, MessageHandlerCommandMixin
|
2020-07-03 17:08:37 +05:00
|
|
|
from tests.factories.message import MessageFactory
|
2019-12-03 00:03:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyHandler(MessageHandler):
|
|
|
|
|
async def handle(self) -> Any:
|
|
|
|
|
return self.event.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestClassBasedMessageHandler:
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_message_handler(self):
|
2020-07-03 17:08:55 +05:00
|
|
|
event = MessageFactory()
|
2019-12-03 00:03:15 +02:00
|
|
|
handler = MyHandler(event=event)
|
|
|
|
|
|
|
|
|
|
assert handler.from_user == event.from_user
|
|
|
|
|
assert handler.chat == event.chat
|
2019-12-03 18:32:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class HandlerWithCommand(MessageHandlerCommandMixin, MessageHandler):
|
|
|
|
|
async def handle(self) -> Any:
|
|
|
|
|
return self.command
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestBaseMessageHandlerCommandMixin:
|
|
|
|
|
def test_command_accessible(self):
|
|
|
|
|
handler = HandlerWithCommand(
|
2020-07-03 17:08:55 +05:00
|
|
|
MessageFactory(text="/test args"),
|
2019-12-03 18:32:21 +02:00
|
|
|
command=CommandObject(prefix="/", command="command", args="args"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert isinstance(handler.command, CommandObject)
|
|
|
|
|
assert handler.command.command == "command"
|
2019-12-04 18:04:29 +02:00
|
|
|
|
|
|
|
|
def test_command_not_presented(self):
|
2020-07-03 17:08:55 +05:00
|
|
|
handler = HandlerWithCommand(MessageFactory())
|
2019-12-04 18:04:29 +02:00
|
|
|
|
|
|
|
|
assert handler.command is None
|