Skip to content

Commit

Permalink
Merge pull request #79 from kalaspuff/feature/call-decorated-invoker-…
Browse files Browse the repository at this point in the history
…methods-directly

Invoker methods can now be called directly without the need to mock the invoker decorator function
  • Loading branch information
kalaspuff authored Jun 8, 2017
2 parents 3ca1b18 + 728af78 commit c5d64c3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 35 deletions.
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 3 additions & 29 deletions tests/test_mock_decorator.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions tomodachi/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion tomodachi/invoker/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .base import Invoker, FUNCTION_ATTRIBUTE # noqa
from .base import Invoker, FUNCTION_ATTRIBUTE, START_ATTRIBUTE # noqa
7 changes: 6 additions & 1 deletion tomodachi/invoker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import functools

FUNCTION_ATTRIBUTE = 'TOMODACHI_INVOKER'
START_ATTRIBUTE = 'TOMODACHI_INVOKER_START'


class Invoker(object):
Expand All @@ -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]
Expand Down

0 comments on commit c5d64c3

Please # to comment.