mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-08 17:13:56 +00:00
Add filters and class based handler for errors
This commit is contained in:
parent
9e673998f0
commit
0fbd2819f9
9 changed files with 167 additions and 1 deletions
51
tests/test_dispatcher/test_filters/test_exception.py
Normal file
51
tests/test_dispatcher/test_filters/test_exception.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import re
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.dispatcher.filters import ExceptionMessageFilter, ExceptionTypeFilter
|
||||
|
||||
|
||||
class TestExceptionMessageFilter:
|
||||
@pytest.mark.parametrize("value", ["value", re.compile("value")])
|
||||
def test_converter(self, value):
|
||||
obj = ExceptionMessageFilter(match=value)
|
||||
assert isinstance(obj.match, re.Pattern)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_match(self):
|
||||
obj = ExceptionMessageFilter(match="KABOOM")
|
||||
|
||||
result = await obj(Exception())
|
||||
assert not result
|
||||
|
||||
result = await obj(Exception("KABOOM"))
|
||||
assert isinstance(result, dict)
|
||||
assert "match_exception" in result
|
||||
|
||||
|
||||
class MyException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class MyAnotherException(MyException):
|
||||
pass
|
||||
|
||||
|
||||
class TestExceptionTypeFilter:
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"exception,value",
|
||||
[
|
||||
[Exception(), False],
|
||||
[ValueError(), False],
|
||||
[TypeError(), False],
|
||||
[MyException(), True],
|
||||
[MyAnotherException(), True],
|
||||
],
|
||||
)
|
||||
async def test_check(self, exception: Exception, value: bool):
|
||||
obj = ExceptionTypeFilter(exception=MyException)
|
||||
|
||||
result = await obj(exception)
|
||||
|
||||
assert result == value
|
||||
Loading…
Add table
Add a link
Reference in a new issue