Skip to content

Commit

Permalink
OPT test return values of the hooks inside the hooks unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels Lensink committed Feb 1, 2016
1 parent 1fb813b commit 2fb93b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
16 changes: 7 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,23 @@ clean: pyclean docsclean
venv:
@virtualenv -p python2.7 venv
@$(PIP) install -U "pip>=7.0" -q

requirements: venv
@$(PIP) install -U "pip>=7.0" -q
@$(PIP) install -r $(DEPS)

test: requirements pyclean
test: venv pyclean
$(TOX) -e $(TOX_PY_LIST)

test/%: requirements pyclean
test/%: venv pyclean
$(TOX) -e $(TOX_PY_LIST) -- $*

lint: requirements
lint: venv
@$(TOX) -e lint
@$(TOX) -e isort-check

isort: requirements
isort: venv
@$(TOX) -e isort-fix

docs: requirements docsclean
docs: venv docsclean
@$(TOX) -e docs

docker:
Expand All @@ -61,11 +59,11 @@ docker:
docker/%:
$(DOCKER_COMPOSE) run --rm --service-ports app make $*

setup.py: requirements
setup.py: venv
@$(PYTHON) setup_gen.py
@$(PYTHON) setup.py check --restructuredtext

publish: setup.py
@$(PYTHON) setup.py sdist upload

build: clean requirements tox setup.py
build: clean venv tox setup.py
36 changes: 29 additions & 7 deletions tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

from tests.conftest import go
from time_execution import configure, time_execution
from time_execution.backends.base import BaseMetricsBackend


class AssertBackend(BaseMetricsBackend):

def __init__(self, callback):
self.callback = callback

def write(self, name, **data):
return self.callback(name, **data)


class TestTimeExecution(unittest.TestCase):
Expand All @@ -17,24 +27,38 @@ def test_args(**kwargs):
def test_metadata(*args, **kwargs):
return dict(test_key='test value')

configure(hooks=[test_args, test_metadata])
def asserts(name, **data):
self.assertEqual(data['test_key'], 'test value')

configure(
backends=[AssertBackend(asserts)],
hooks=[test_args, test_metadata]
)

go()

def test_hook_exception(self):

message = 'exception message'

def exception_hook(exception, **kwargs):
self.assertTrue(exception)
self.assertIsInstance(exception, TimeExecutionException)
return dict(exception_message=str(exception))

configure(hooks=[exception_hook])
def asserts(name, **data):
self.assertEqual(data['exception_message'], message)

configure(
backends=[AssertBackend(asserts)],
hooks=[exception_hook]
)

class TimeExecutionException(Exception):
message = 'default'
pass

@time_execution
def go():
raise TimeExecutionException('test exception')
raise TimeExecutionException(message)

with self.assertRaises(TimeExecutionException):
go()
Expand All @@ -44,7 +68,6 @@ def test_hook_func_args(self):

def hook(response, exception, metric, func_args, func_kwargs):
self.assertEqual(func_args[0], param)
return metric

configure(hooks=[hook])

Expand All @@ -59,7 +82,6 @@ def test_hook_func_kwargs(self):

def hook(response, exception, metric, func_args, func_kwargs):
self.assertEqual(func_kwargs['param1'], param)
return metric

configure(hooks=[hook])

Expand Down

0 comments on commit 2fb93b9

Please sign in to comment.