Improved test coverage

This commit is contained in:
Syrus Akbary 2017-08-01 23:39:27 -07:00
parent eff882e5a5
commit 7e901f83ae
3 changed files with 70 additions and 42 deletions

View File

@ -1,39 +0,0 @@
from ..pyutils.compat import signature
from functools import wraps, partial
def resolver_from_annotations(func):
from ..types import Context, ResolveInfo
func_signature = signature(func)
_context_var = None
_info_var = None
for key, parameter in func_signature.parameters.items():
param_type = parameter.annotation
if param_type is Context:
_context_var = key
elif param_type is ResolveInfo:
_info_var = key
continue
# We generate different functions as it will be faster
# than calculating the args on the fly when executing
# the function resolver.
if _context_var and _info_var:
def inner(root, args, context, info):
return func(root, **dict(args, **{_info_var: info, _context_var: context}))
elif _context_var:
def inner(root, args, context, info):
return func(root, **dict(args, **{_context_var: context}))
elif _info_var:
def inner(root, args, context, info):
return func(root, **dict(args, **{_info_var: info}))
else:
def inner(root, args, context, info):
return func(root, **args)
if isinstance(func, partial):
return inner
return wraps(func)(inner)

View File

@ -0,0 +1,65 @@
import pytest
from .. import deprecated
from ..deprecated import deprecated as deprecated_decorator, warn_deprecation
def test_warn_deprecation(mocker):
mocker.patch.object(deprecated.warnings, 'warn')
warn_deprecation("OH!")
deprecated.warnings.warn.assert_called_with('OH!', stacklevel=2, category=DeprecationWarning)
def test_deprecated_decorator(mocker):
mocker.patch.object(deprecated, 'warn_deprecation')
@deprecated_decorator
def my_func():
return True
result = my_func()
assert result
deprecated.warn_deprecation.assert_called_with("Call to deprecated function my_func.")
def test_deprecated_class(mocker):
mocker.patch.object(deprecated, 'warn_deprecation')
@deprecated_decorator
class X:
pass
result = X()
assert result
deprecated.warn_deprecation.assert_called_with("Call to deprecated class X.")
def test_deprecated_decorator_text(mocker):
mocker.patch.object(deprecated, 'warn_deprecation')
@deprecated_decorator("Deprecation text")
def my_func():
return True
result = my_func()
assert result
deprecated.warn_deprecation.assert_called_with("Call to deprecated function my_func (Deprecation text).")
def test_deprecated_class_text(mocker):
mocker.patch.object(deprecated, 'warn_deprecation')
@deprecated_decorator("Deprecation text")
class X:
pass
result = X()
assert result
deprecated.warn_deprecation.assert_called_with("Call to deprecated class X (Deprecation text).")
def test_deprecated_other_object(mocker):
mocker.patch.object(deprecated, 'warn_deprecation')
with pytest.raises(TypeError) as exc_info:
deprecated_decorator({})

View File

@ -4,10 +4,12 @@ from .. import deprecated
def test_resolve_only_args(mocker): def test_resolve_only_args(mocker):
mocker.patch.object(deprecated, 'warn_deprecation') mocker.patch.object(deprecated, 'warn_deprecation')
def resolver(*args, **kwargs): def resolver(root, **args):
return kwargs return root, args
my_data = {'one': 1, 'two': 2} my_data = {'one': 1, 'two': 2}
wrapped = resolve_only_args(resolver) wrapped_resolver = resolve_only_args(resolver)
assert deprecated.warn_deprecation.called assert deprecated.warn_deprecation.called
result = wrapped_resolver(1, 2, a=3)
assert result == (1, {'a': 3})