def test_unit__marshmallow_output_processor__ok__missing_data(self): processor = MarshmallowOutputProcessor() processor.schema = MySchema() tested_data = { 'last_name': 'Turing', } with pytest.raises(OutputValidationException): processor.process(tested_data)
def test_unit__marshmallow_output_processor__ok__process_success(self): processor = MarshmallowOutputProcessor() processor.schema = MySchema() tested_data = { 'first_name': 'Alan', 'last_name': 'Turing', } data = processor.process(tested_data) assert data == tested_data
def test_unit__output_data_wrapping__fail__error_response(self): context = MyContext() processor = MarshmallowOutputProcessor() processor.schema = MySchema() wrapper = OutputControllerWrapper(context, processor) @wrapper.get_wrapper def func(foo): return 'wrong result format' result = func(42) # see MyProcessor#process assert isinstance(result, dict) assert 'http_code' in result assert result['http_code'] == HTTPStatus.INTERNAL_SERVER_ERROR assert 'original_error' in result assert result['original_error'].error_details == { 'name': ['Missing data for required field.'] }
def output_body( self, schema: typing.Any, processor: ProcessorInterface = None, context: ContextInterface = None, error_http_code: HTTPStatus = HTTPStatus.BAD_REQUEST, default_http_code: HTTPStatus = HTTPStatus.OK, ) -> typing.Callable[[typing.Callable[..., typing.Any]], typing.Any]: processor = processor or MarshmallowOutputProcessor() processor.schema = schema context = context or self._context_getter decoration = OutputBodyControllerWrapper( context=context, processor=processor, error_http_code=error_http_code, default_http_code=default_http_code, ) def decorator(func): self._buffer.output_body = OutputBodyDescription(decoration) return decoration.get_wrapper(func) return decorator
from hapic.processor import MarshmallowOutputProcessor def bob(f): def boby(*args, **kwargs): return f return boby @hapic.with_api_doc_bis() @bottle.route('/hello/<name>') @hapic.input(HelloPathSchema(), MarshmallowPathInputProcessor(), context=BottleContext()) # nopep8 @hapic.output_body(HelloResponseSchema(), MarshmallowOutputProcessor()) @bob def hello(name: str): return "Hello {}!".format(name) @hapic.with_api_doc_bis() @bottle.route('/hello2/<name>') @hapic.input(HelloPathSchema(), MarshmallowPathInputProcessor(), context=BottleContext()) # nopep8 @hapic.input(HelloJsonSchema(), MarshmallowJsonInputProcessor(), context=BottleContext()) # nopep8 @hapic.output_body(HelloResponseSchema()) @bob