mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-10 17:53:17 +00:00
Add prototype of class-based handlers
This commit is contained in:
parent
2a731f7ce2
commit
b82a1a6fb0
11 changed files with 178 additions and 8 deletions
47
tests/test_dispatcher/test_handler/test_base.py
Normal file
47
tests/test_dispatcher/test_handler/test_base.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import asyncio
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram import Bot
|
||||
from aiogram.api.types import Update
|
||||
from aiogram.dispatcher.handler.base import BaseHandler
|
||||
|
||||
|
||||
class MyHandler(BaseHandler):
|
||||
async def handle(self) -> Any:
|
||||
await asyncio.sleep(0.1)
|
||||
return 42
|
||||
|
||||
|
||||
class TestBaseClassBasedHandler:
|
||||
@pytest.mark.asyncio
|
||||
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 hasattr(handler, "bot")
|
||||
assert not hasattr(handler, "filters")
|
||||
assert await handler == 42
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bot_mixin_from_context(self):
|
||||
event = Update(update_id=42)
|
||||
handler = MyHandler(event=event, key=42)
|
||||
bot = Bot("42:TEST")
|
||||
|
||||
assert handler.bot is None
|
||||
|
||||
Bot.set_current(bot)
|
||||
assert handler.bot == bot
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bot_mixin_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
|
||||
Loading…
Add table
Add a link
Reference in a new issue