refactor(handler): remove require func

refactor requirements feature
This commit is contained in:
mpa 2020-05-31 09:09:56 +04:00
parent de962d043d
commit e060678b40
No known key found for this signature in database
GPG key ID: BCCFBFCCC9B754A8
4 changed files with 69 additions and 82 deletions

View file

@ -18,7 +18,7 @@ async def callback2(foo: int, bar: int, baz: int):
return locals()
async def callback3(foo: int, **kwargs):
async def callback3(foo: int, **data):
return locals()
@ -62,8 +62,8 @@ class TestCallableMixin:
def test_init_decorated(self):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
def wrapper(*args, **data):
return func(*args, **data)
return wrapper
@ -85,7 +85,7 @@ class TestCallableMixin:
assert obj2.callback == callback2
@pytest.mark.parametrize(
"callback,kwargs,result",
"callback,data,result",
[
pytest.param(
callback1, {"foo": 42, "spam": True, "baz": "fuz"}, {"foo": 42, "baz": "fuz"}
@ -108,9 +108,9 @@ class TestCallableMixin:
),
],
)
def test_prepare_kwargs(self, callback, kwargs, result):
def test_prepare_data(self, callback, data, result):
obj = CallableMixin(callback)
assert obj._prepare_kwargs(kwargs) == result
assert obj._prepare_kwargs(data) == result
@pytest.mark.asyncio
async def test_sync_call(self):
@ -127,8 +127,8 @@ class TestCallableMixin:
assert result == {"foo": 42, "bar": "test", "baz": "fuz"}
async def simple_handler(*args, **kwargs):
return args, kwargs
async def simple_handler(*args, **data):
return args, data
class TestHandlerObject:

View file

@ -1,17 +1,37 @@
# todo
from aiogram.dispatcher.requirement import require, CallableRequirement
from aiogram.dispatcher.requirement import Requirement, get_reqs_from_class, get_reqs_from_callable
tick_data = {"ticks": 0}
req1 = Requirement(lambda: 1)
req2 = Requirement(lambda: 1)
async def callback(
o,
x=req1,
y=req2
):
...
def test_require():
x = require(lambda: "str", use_cache=True, cache_key=0)
assert isinstance(x, CallableRequirement)
x = Requirement(lambda: "str", use_cache=True, cache_key=0)
assert isinstance(x, Requirement)
assert callable(x) & callable(x.callable)
assert x.cache_key == 0
assert x.use_cache
class TestCallableRequirementCache:
def test_cache(self):
...
class TestReqUtils:
def test_get_reqs_from_callable(self):
assert set(get_reqs_from_callable(callback).values()) == {req1, req2}
assert set(get_reqs_from_callable(callback).keys()) == {"x", "y"}
def test_get_reqs_from_class(self):
class Class:
x = req1
y = req2
assert set(get_reqs_from_class(Class)) == {"x", "y"}