From 728af78796f1e3fc1cba43c3562661ff7c311391 Mon Sep 17 00:00:00 2001 From: Carl Oscar Aaro Date: Thu, 8 Jun 2017 23:59:00 +0200 Subject: [PATCH] Invoker methods can now be called directly without the need to mock the invoker decorator function --- requirements.txt | 2 -- tests/test_mock_decorator.py | 32 +++----------------------------- tomodachi/container.py | 8 ++++++-- tomodachi/invoker/__init__.py | 2 +- tomodachi/invoker/base.py | 7 ++++++- 5 files changed, 16 insertions(+), 35 deletions(-) diff --git a/requirements.txt b/requirements.txt index 8fec044c8..11de7dcf3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,10 +11,8 @@ coverage==4.4.1 docutils==0.13.1 execnet==1.4.1 jmespath==0.9.3 -mock==2.0.0 multidict==2.1.6 packaging==16.8 -pbr==3.0.1 pep8==1.7.0 py==1.4.34 pyparsing==2.2.0 diff --git a/tests/test_mock_decorator.py b/tests/test_mock_decorator.py index ee7ae3f20..49ad2e775 100644 --- a/tests/test_mock_decorator.py +++ b/tests/test_mock_decorator.py @@ -1,36 +1,10 @@ import asyncio -import functools -import importlib -import mock +import services.mock_decorator_service -def mock_decorator(*args, **kwargs): - def decorator(f): - @functools.wraps(f) - def decorated_function(*args, **kwargs): - return f(*args, **kwargs) - return decorated_function - return decorator - - -def test_without_mocked_function(monkeypatch, capsys): - import services.mock_decorator_service - importlib.reload(services.mock_decorator_service) - +def test_without_mocked_invoker_function(): service = services.mock_decorator_service.MockDecoratorService() loop = asyncio.get_event_loop() loop.run_until_complete(service.test()) - assert service.function_tested is False - - -def test_mocked_function(monkeypatch, capsys): - with mock.patch('tomodachi.transport.aws_sns_sqs.aws_sns_sqs', mock_decorator): - import services.mock_decorator_service - importlib.reload(services.mock_decorator_service) - - service = services.mock_decorator_service.MockDecoratorService() - - loop = asyncio.get_event_loop() - loop.run_until_complete(service.test()) - assert service.function_tested is True + assert service.function_tested is True diff --git a/tomodachi/container.py b/tomodachi/container.py index 0ae55d1da..402ae09f9 100644 --- a/tomodachi/container.py +++ b/tomodachi/container.py @@ -7,7 +7,7 @@ import types import uuid from tomodachi import CLASS_ATTRIBUTE -from tomodachi.invoker import FUNCTION_ATTRIBUTE +from tomodachi.invoker import FUNCTION_ATTRIBUTE, START_ATTRIBUTE from tomodachi.config import merge_dicts @@ -102,7 +102,11 @@ def invoker_function_sorter(m): return i return -1 - invoker_functions = [name for name, fn in inspect.getmembers(cls) if inspect.isfunction(fn) and getattr(fn, FUNCTION_ATTRIBUTE, None)] + invoker_functions = [] + for name, fn in inspect.getmembers(cls): + if inspect.isfunction(fn) and getattr(fn, FUNCTION_ATTRIBUTE, None): + setattr(fn, START_ATTRIBUTE, True) + invoker_functions.append(name) invoker_functions.sort(key=invoker_function_sorter) if invoker_functions: invoker_tasks = invoker_tasks | set([asyncio.ensure_future(getattr(instance, name)()) for name in invoker_functions]) diff --git a/tomodachi/invoker/__init__.py b/tomodachi/invoker/__init__.py index 27b70e60b..520a0e42d 100644 --- a/tomodachi/invoker/__init__.py +++ b/tomodachi/invoker/__init__.py @@ -1 +1 @@ -from .base import Invoker, FUNCTION_ATTRIBUTE # noqa +from .base import Invoker, FUNCTION_ATTRIBUTE, START_ATTRIBUTE # noqa diff --git a/tomodachi/invoker/base.py b/tomodachi/invoker/base.py index c9689ff4d..22e09736f 100644 --- a/tomodachi/invoker/base.py +++ b/tomodachi/invoker/base.py @@ -2,6 +2,7 @@ import functools FUNCTION_ATTRIBUTE = 'TOMODACHI_INVOKER' +START_ATTRIBUTE = 'TOMODACHI_INVOKER_START' class Invoker(object): @@ -12,7 +13,11 @@ def decorator(cls, cls_func): def _wrapper(*args, **kwargs): def wrapper(func): @functools.wraps(func) - async def _decorator(obj): + async def _decorator(obj, *a, **kw): + if not getattr(_decorator, START_ATTRIBUTE, None): + return await func(obj, *a, **kw) + + setattr(_decorator, START_ATTRIBUTE, False) if not cls.context.get(obj, None): cls.context[obj] = {i: getattr(obj, i) for i in dir(obj) if not callable(i) and not i.startswith("__") and not isinstance(getattr(obj, i), types.MethodType)} context = cls.context[obj]