aiogram/tests/test_dispatcher/test_middlewares/test_lifetimecontroller.py
Andrey Tikhonov c43ff9b6f9
Patch 2 (#977)
* Fix filtering skip_patterns

* cache skip actions

* fix item' is not defined, add tests
2022-08-14 18:29:35 +03:00

53 lines
1.7 KiB
Python

from unittest.mock import AsyncMock
import pytest
from aiogram.dispatcher.middlewares import LifetimeControllerMiddleware
pytestmark = pytest.mark.asyncio
async def test_no_skip():
class Middleware(LifetimeControllerMiddleware):
pre_process = AsyncMock()
post_process = AsyncMock()
m = Middleware()
await m.trigger("pre_process_update_xxx", [1, 2, 3])
m.pre_process.assert_called_once_with(1, 3, 2)
m.post_process.assert_not_called()
await m.trigger("post_process_update_xxx", [1, 2, 3])
m.pre_process.reset_mock()
m.pre_process.assert_not_called()
m.post_process.assert_called_once_with(1, 3, 2)
async def test_skip_prefix():
class Middleware(LifetimeControllerMiddleware):
skip_patterns = ["update"]
pre_process = AsyncMock()
post_process = AsyncMock()
m = Middleware()
await m.trigger("pre_process_update_xxx", [1, 2, 3])
m.pre_process.assert_called_once_with(1, 3, 2)
m.post_process.assert_not_called()
await m.trigger("post_process_update_xxx", [1, 2, 3])
m.pre_process.reset_mock()
m.pre_process.assert_not_called()
m.post_process.assert_called_once_with(1, 3, 2)
async def test_skip():
class Middleware(LifetimeControllerMiddleware):
skip_patterns = ["update_xxx"]
pre_process = AsyncMock()
post_process = AsyncMock()
m = Middleware()
await m.trigger("pre_process_update_xxx", [1, 2, 3])
m.pre_process.assert_not_called()
m.post_process.assert_not_called()
await m.trigger("post_process_update_xxx", [1, 2, 3])
m.pre_process.reset_mock()
m.pre_process.assert_not_called()
m.post_process.assert_not_called()