Fix outer_middleware resolution (#637) (#640)

* Fix outer_middleware resolution (#637)

* Reformat files

* Reorder routers when resolve middlewares

Co-authored-by: Alex Root Junior <jroot.junior@gmail.com>
This commit is contained in:
evgfilim1 2021-07-29 01:55:16 +05:00 committed by GitHub
parent d06241d945
commit 0ac7ebe99f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 10 deletions

View file

@ -2,6 +2,7 @@ import asyncio
import datetime
import time
import warnings
from collections import Counter
from typing import Any
import pytest
@ -494,6 +495,50 @@ class TestDispatcher:
assert result["event_router"] == router1
assert result["test"] == "PASS"
@pytest.mark.asyncio
async def test_nested_router_middleware_resolution(self, bot: MockedBot):
counter = Counter()
def mw(type_: str, inject_data: dict):
async def middleware(h, event, data):
counter[type_] += 1
data.update(inject_data)
return await h(event, data)
return middleware
async def handler(event, foo, bar, baz, fizz, buzz):
counter["child.handler"] += 1
root = Dispatcher()
child = Router()
root.message.outer_middleware(mw("root.outer_middleware", {"foo": True}))
root.message.middleware(mw("root.middleware", {"bar": None}))
child.message.outer_middleware(mw("child.outer_middleware", {"fizz": 42}))
child.message.middleware(mw("child.middleware", {"buzz": -42}))
child.message.register(handler)
root.include_router(child)
await root.feed_update(
bot=bot,
update=Update(
update_id=42,
message=Message(
message_id=42,
date=datetime.datetime.fromtimestamp(0),
chat=Chat(id=-42, type="group"),
),
),
baz=...,
)
assert counter["root.outer_middleware"] == 2
assert counter["root.middleware"] == 1
assert counter["child.outer_middleware"] == 1
assert counter["child.middleware"] == 1
assert counter["child.handler"] == 1
@pytest.mark.asyncio
async def test_process_update_call_request(self, bot: MockedBot):
dispatcher = Dispatcher()