aiogram/tests/test_dispatcher/test_handler.py
2020-11-08 21:54:06 +00:00

74 lines
2 KiB
Python

import functools
import pytest
from aiogram.dispatcher.handler import Handler, _check_spec, _get_spec
def callback1(foo: int, bar: int, baz: int):
return locals()
async def callback2(foo: int, bar: int, baz: int):
return locals()
async def callback3(foo: int, **kwargs):
return locals()
class TestHandlerObj:
@staticmethod
def test_init_decorated():
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@decorator
def callback1(foo, bar, baz):
pass
@decorator
@decorator
def callback2(foo, bar, baz):
pass
obj1 = Handler.HandlerObj(callback1, _get_spec(callback1))
obj2 = Handler.HandlerObj(callback2, _get_spec(callback2))
if set(obj1.spec.args) != {"foo", "bar", "baz"}:
raise AssertionError
if obj1.handler != callback1:
raise AssertionError
if set(obj2.spec.args) != {"foo", "bar", "baz"}:
raise AssertionError
if obj2.handler != callback2:
raise AssertionError
@pytest.mark.parametrize(
"callback,kwargs,result",
[
pytest.param(
callback1,
{"foo": 42, "spam": True, "baz": "fuz"},
{"foo": 42, "baz": "fuz"},
),
pytest.param(
callback2,
{"foo": 42, "spam": True, "baz": "fuz", "bar": "test"},
{"foo": 42, "baz": "fuz", "bar": "test"},
),
pytest.param(
callback3,
{"foo": 42, "spam": True, "baz": "fuz", "bar": "test"},
{"foo": 42, "spam": True, "baz": "fuz", "bar": "test"},
),
],
)
def test__check_spec(self, callback, kwargs, result):
spec = _get_spec(callback)
if _check_spec(spec, kwargs) != result:
raise AssertionError