aiogram/tests/test_dispatcher/test_handler/test_message.py

44 lines
1.2 KiB
Python
Raw Permalink Normal View History

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
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):
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(
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):
handler = HandlerWithCommand(MessageFactory())
2019-12-04 18:04:29 +02:00
assert handler.command is None