test_decorator.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # -*- coding: utf-8 -*-
  2. import typing
  3. from http import HTTPStatus
  4. import marshmallow
  5. from hapic.context import ContextInterface
  6. from hapic.data import HapicData
  7. from hapic.decorator import ControllerWrapper
  8. from hapic.decorator import InputControllerWrapper
  9. from hapic.decorator import OutputControllerWrapper
  10. from hapic.processor import RequestParameters
  11. from hapic.processor import MarshmallowOutputProcessor
  12. from hapic.processor import ProcessValidationError
  13. from hapic.processor import ProcessorInterface
  14. from tests.base import Base
  15. class MyContext(ContextInterface):
  16. def get_request_parameters(self, *args, **kwargs) -> RequestParameters:
  17. return RequestParameters(
  18. path_parameters={'fake': args},
  19. query_parameters={},
  20. body_parameters={},
  21. form_parameters={},
  22. header_parameters={},
  23. )
  24. def get_response(
  25. self,
  26. response: dict,
  27. http_code: int,
  28. ) -> typing.Any:
  29. return {
  30. 'original_response': response,
  31. 'http_code': http_code,
  32. }
  33. def get_validation_error_response(
  34. self,
  35. error: ProcessValidationError,
  36. http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
  37. ) -> typing.Any:
  38. return {
  39. 'original_error': error,
  40. 'http_code': http_code,
  41. }
  42. class MyProcessor(ProcessorInterface):
  43. def process(self, value):
  44. return value + 1
  45. def get_validation_error(
  46. self,
  47. request_context: RequestParameters,
  48. ) -> ProcessValidationError:
  49. return ProcessValidationError(
  50. error_details={
  51. 'original_request_context': request_context,
  52. },
  53. error_message='ERROR',
  54. )
  55. class MyControllerWrapper(ControllerWrapper):
  56. def before_wrapped_func(
  57. self,
  58. func_args: typing.Tuple[typing.Any, ...],
  59. func_kwargs: typing.Dict[str, typing.Any],
  60. ) -> typing.Union[None, typing.Any]:
  61. if func_args and func_args[0] == 666:
  62. return {
  63. 'error_response': 'we are testing'
  64. }
  65. func_kwargs['added_parameter'] = 'a value'
  66. def after_wrapped_function(self, response: typing.Any) -> typing.Any:
  67. return response * 2
  68. class MyInputControllerWrapper(InputControllerWrapper):
  69. def get_processed_data(
  70. self,
  71. request_parameters: RequestParameters,
  72. ) -> typing.Any:
  73. return {'we_are_testing': request_parameters.path_parameters}
  74. def update_hapic_data(
  75. self,
  76. hapic_data: HapicData,
  77. processed_data: typing.Dict[str, typing.Any],
  78. ) -> typing.Any:
  79. hapic_data.query = processed_data
  80. class MySchema(marshmallow.Schema):
  81. name = marshmallow.fields.String(required=True)
  82. class TestControllerWrapper(Base):
  83. def test_unit__base_controller_wrapper__ok__no_behaviour(self):
  84. context = MyContext()
  85. processor = MyProcessor()
  86. wrapper = ControllerWrapper(context, processor)
  87. @wrapper.get_wrapper
  88. def func(foo):
  89. return foo
  90. result = func(42)
  91. assert result == 42
  92. def test_unit__base_controller__ok__replaced_response(self):
  93. context = MyContext()
  94. processor = MyProcessor()
  95. wrapper = MyControllerWrapper(context, processor)
  96. @wrapper.get_wrapper
  97. def func(foo):
  98. return foo
  99. # see MyControllerWrapper#before_wrapped_func
  100. result = func(666)
  101. # result have been replaced by MyControllerWrapper#before_wrapped_func
  102. assert {'error_response': 'we are testing'} == result
  103. def test_unit__controller_wrapper__ok__overload_input(self):
  104. context = MyContext()
  105. processor = MyProcessor()
  106. wrapper = MyControllerWrapper(context, processor)
  107. @wrapper.get_wrapper
  108. def func(foo, added_parameter=None):
  109. # see MyControllerWrapper#before_wrapped_func
  110. assert added_parameter == 'a value'
  111. return foo
  112. result = func(42)
  113. # See MyControllerWrapper#after_wrapped_function
  114. assert result == 84
  115. class TestInputControllerWrapper(Base):
  116. def test_unit__input_data_wrapping__ok__nominal_case(self):
  117. context = MyContext()
  118. processor = MyProcessor()
  119. wrapper = MyInputControllerWrapper(context, processor)
  120. @wrapper.get_wrapper
  121. def func(foo, hapic_data=None):
  122. assert hapic_data
  123. assert isinstance(hapic_data, HapicData)
  124. # see MyControllerWrapper#before_wrapped_func
  125. assert hapic_data.query == {'we_are_testing': {'fake': (42,)}}
  126. return foo
  127. result = func(42)
  128. assert result == 42
  129. class TestOutputControllerWrapper(Base):
  130. def test_unit__output_data_wrapping__ok__nominal_case(self):
  131. context = MyContext()
  132. processor = MyProcessor()
  133. wrapper = OutputControllerWrapper(context, processor)
  134. @wrapper.get_wrapper
  135. def func(foo, hapic_data=None):
  136. # If no use of input wrapper, no hapic_data is given
  137. assert not hapic_data
  138. return foo
  139. result = func(42)
  140. # see MyProcessor#process
  141. assert {
  142. 'http_code': HTTPStatus.OK,
  143. 'original_response': 43,
  144. } == result
  145. def test_unit__output_data_wrapping__fail__error_response(self):
  146. context = MyContext()
  147. processor = MarshmallowOutputProcessor()
  148. processor.schema = MySchema()
  149. wrapper = OutputControllerWrapper(context, processor)
  150. @wrapper.get_wrapper
  151. def func(foo):
  152. return 'wrong result format'
  153. result = func(42)
  154. # see MyProcessor#process
  155. assert isinstance(result, dict)
  156. assert 'http_code' in result
  157. assert result['http_code'] == HTTPStatus.INTERNAL_SERVER_ERROR
  158. assert 'original_error' in result
  159. assert result['original_error'].error_details == {
  160. 'name': ['Missing data for required field.']
  161. }