mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-08 17:13:56 +00:00
Partially cover dispatcher
This commit is contained in:
parent
fa42dcdce2
commit
55496ab9ca
2 changed files with 61 additions and 2 deletions
|
|
@ -37,11 +37,11 @@ class Dispatcher(Router):
|
|||
update_id = update.update_id + 1
|
||||
|
||||
async def polling(self, bot: Bot):
|
||||
self.emit_startup(bot=bot)
|
||||
await self.emit_startup(bot=bot)
|
||||
try:
|
||||
async for update in self.listen_updates(bot):
|
||||
async for result in self.feed_update(bot, update):
|
||||
if isinstance(result, TelegramMethod):
|
||||
await result
|
||||
finally:
|
||||
self.emit_shutdown(bot=bot)
|
||||
await self.emit_shutdown(bot=bot)
|
||||
|
|
|
|||
59
tests/test_dispatcher/test_dispatcher.py
Normal file
59
tests/test_dispatcher/test_dispatcher.py
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram import Bot
|
||||
from aiogram.api.types import Message, Update, Chat, User
|
||||
from aiogram.dispatcher.dispatcher import Dispatcher
|
||||
from aiogram.dispatcher.router import Router
|
||||
|
||||
|
||||
class TestDispatcher:
|
||||
def test_parent_router(self):
|
||||
dp = Dispatcher()
|
||||
with pytest.raises(RuntimeError):
|
||||
dp.parent_router = Router()
|
||||
assert dp.parent_router is None
|
||||
dp._parent_router = Router()
|
||||
assert dp.parent_router is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_feed_update(self):
|
||||
dp = Dispatcher()
|
||||
bot = Bot("TOKEN")
|
||||
|
||||
@dp.message_handler()
|
||||
async def my_handler(message: Message, **kwargs):
|
||||
assert "bot" in kwargs
|
||||
assert isinstance(kwargs["bot"], Bot)
|
||||
assert kwargs["bot"] == bot
|
||||
return message.text
|
||||
|
||||
results_count = 0
|
||||
async for result in dp.feed_update(
|
||||
bot=bot,
|
||||
update=Update(
|
||||
update_id=42,
|
||||
message=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"),
|
||||
),
|
||||
),
|
||||
):
|
||||
results_count += 1
|
||||
assert result == "test"
|
||||
|
||||
assert results_count == 1
|
||||
|
||||
@pytest.mark.skip
|
||||
@pytest.mark.asyncio
|
||||
async def test_listen_updates(self):
|
||||
pass
|
||||
|
||||
@pytest.mark.skip
|
||||
@pytest.mark.asyncio
|
||||
async def test_polling(self):
|
||||
pass
|
||||
Loading…
Add table
Add a link
Reference in a new issue