Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_auto_fill_when_not_done(
self, mocker, annotation_handler_builder_mock
):
# Setup
argument_handler_builder = mocker.Mock(
stub=arguments.ArgumentAnnotationHandlerBuilder
)
method_handler_builder = annotation_handler_builder_mock
uri_definition_builder = mocker.Mock(spec=commands.URIDefinitionBuilder)
builder = commands.RequestDefinitionBuilder(
"method",
uri_definition_builder,
argument_handler_builder,
method_handler_builder,
)
# Setup success condition
argument_handler_builder.is_done.return_value = False
argument_handler_builder.missing_arguments = ["arg1"]
uri_definition_builder.remaining_variables = ["arg1"]
# Verify
def test_add_annotation_with_name(self, mocker, argument_mock, args):
builder = arguments.ArgumentAnnotationHandlerBuilder(None, args, False)
builder.listener = mocker.stub()
builder.add_annotation(argument_mock, name=args[-1])
builder.listener.assert_called_with(argument_mock)
assert args[-1] not in builder.missing_arguments
def test_from_func(self):
def func(_):
pass
handler = arguments.ArgumentAnnotationHandlerBuilder.from_func(func)
another_handler = arguments.ArgumentAnnotationHandlerBuilder.from_func(
func
)
assert handler is another_handler
def test_add_named_annotation_without_name(
self, mocker, named_argument_mock, args
):
builder = arguments.ArgumentAnnotationHandlerBuilder(None, args, False)
named_argument_mock.name = None
builder.listener = mocker.stub()
builder.add_annotation(named_argument_mock)
builder.listener.assert_called_with(named_argument_mock)
assert named_argument_mock.name == args[0]
assert args[0] not in builder.missing_arguments
def test_add_annotation_with_no_missing_arguments(self, argument_mock):
def dummy():
pass
builder = arguments.ArgumentAnnotationHandlerBuilder(dummy, [], False)
with pytest.raises(arguments.ExhaustedArguments):
builder.add_annotation(argument_mock)
def test_auto_fill_when_not_done(
self, mocker, annotation_handler_builder_mock
):
# Setup
argument_handler_builder = mocker.Mock(
stub=arguments.ArgumentAnnotationHandlerBuilder
)
method_handler_builder = annotation_handler_builder_mock
uri_definition_builder = mocker.Mock(spec=commands.URIDefinitionBuilder)
builder = commands.RequestDefinitionBuilder(
"method",
uri_definition_builder,
argument_handler_builder,
method_handler_builder,
)
# Setup success condition
argument_handler_builder.is_done.return_value = False
argument_handler_builder.missing_arguments = ["arg1"]
uri_definition_builder.remaining_variables = ["arg1"]
# Verify
def _set_init_handler(namespace):
try:
init = namespace["__init__"]
except KeyError:
pass
else:
builder = arguments.ArgumentAnnotationHandlerBuilder.from_func(init)
handler = builder.build()
@functools.wraps(init)
def new_init(self, *args, **kwargs):
init(self, *args, **kwargs)
call_args = utils.get_call_args(init, self, *args, **kwargs)
f = functools.partial(
handler.handle_call_args, call_args=call_args
)
hook = hooks_.RequestAuditor(f)
self.session.inject(hook)
namespace["__init__"] = new_init
def __call__(self, obj):
if inspect.isfunction(obj):
handler = arguments.ArgumentAnnotationHandlerBuilder.from_func(obj)
self._helper(handler)
return obj
else:
return super(args, self).__call__(obj)
def __call__(
self, func, request_definition_builder_factory=RequestDefinitionBuilder
):
spec = utils.get_arg_spec(func)
arg_handler = arguments.ArgumentAnnotationHandlerBuilder(
func, spec.args
)
builder = request_definition_builder_factory(
self._method,
URIDefinitionBuilder(self._uri),
arg_handler,
decorators.MethodAnnotationHandlerBuilder(),
)
# Need to add the annotations after constructing the request
# definition builder so it has a chance to attach its listener.
arg_handler.set_annotations(spec.annotations)
# Use return value type hint as expected return type
if spec.return_annotation is not None:
builder = returns.schema(spec.return_annotation)(builder)
def __call__(self, obj):
if inspect.isfunction(obj):
ArgumentAnnotationHandlerBuilder.from_func(obj).add_annotation(self)
return obj
else:
return super(FuncDecoratorMixin, self).__call__(obj)