def test_page_position_argument_inspector(argument_type, must_return_parameters): class SimpleController: @winter.route_get('') def method(arg1: argument_type): return arg1 route = get_route(SimpleController.method) resolver = PagePositionArgumentResolver() inspector = PagePositionArgumentsInspector(resolver) if must_return_parameters: expected_parameters = [ inspector.limit_parameter, inspector.offset_parameter, ] else: expected_parameters = [] # Act parameters = inspector.inspect_parameters(route) # Assert assert parameters == expected_parameters
def test_page_position_argument_inspector_with_allowed_order_by_fields( default_sort, default_in_parameter): class SimpleController: @winter.route_get('') @winter.pagination.order_by(['id'], default_sort=default_sort) def method(self, arg1: PagePosition): return arg1 route = get_route(SimpleController.method) resolver = PagePositionArgumentResolver() inspector = PagePositionArgumentsInspector(resolver) order_by_parameter = openapi.Parameter( name=resolver.order_by_name, description='Comma separated order by fields. Allowed fields: id.', required=False, in_=openapi.IN_QUERY, type=openapi.TYPE_ARRAY, items={'type': openapi.TYPE_STRING}, default=default_in_parameter, ) expected_parameters = [ inspector.limit_parameter, inspector.offset_parameter, order_by_parameter, ] # Act parameters = inspector.inspect_parameters(route) # Assert assert parameters == expected_parameters
def auto_schema(): view = View() View.post.method = Controller.post route = get_route(Controller.post) try: yield SwaggerAutoSchema(view, 'path', route.http_method, 'components', 'request', {}) finally: del View.post.method
def test_query_parameter_inspector(): inspector = QueryParametersInspector() route = get_route(ControllerForQueryParameter.simple_method) # Act parameters = inspector.inspect_parameters(route) # Assert assert len(parameters) == 2, parameters parameter_by_name = {parameter.name: parameter for parameter in parameters} valid_parameter = parameter_by_name['valid_query_param'] assert valid_parameter.type == openapi.TYPE_INTEGER assert valid_parameter.description == '' invalid_parameter = parameter_by_name['mapped_query_param'] assert invalid_parameter.type == openapi.TYPE_STRING assert invalid_parameter.description == '(Note: parameter type can be wrong)'
def test_path_parameter_inspector(): inspector = PathParametersInspector() route = get_route(ControllerForTestingInspectors.simple_method) # Act parameters = inspector.inspect_parameters(route) # Assert assert len(parameters) == 2 parameter_by_name = {parameter.name: parameter for parameter in parameters} valid_parameter = parameter_by_name['valid_param'] assert valid_parameter.type == openapi.TYPE_INTEGER assert valid_parameter.description == 'Valid doc' invalid_parameter = parameter_by_name['invalid_param'] assert invalid_parameter.type == openapi.TYPE_STRING assert invalid_parameter.description == 'Invalid doc (Note: parameter type can be wrong)' assert 'not_in_method' not in parameter_by_name
def test_get_operation_with_serializer(): view = View() route = get_route(Controller.post_with_serializer) View.post.route = route reference_resolver = openapi.ReferenceResolver('definitions', 'parameters', force_init=True) auto_schema = SwaggerAutoSchema(view, 'path', route.http_method, reference_resolver, 'request', {}) operation = auto_schema.get_operation(['test_app', 'post']) schema_ref = openapi.SchemaRef( resolver=reference_resolver, schema_name='User', ) parameters = [ openapi.Parameter( name='data', in_=openapi.IN_BODY, required=True, schema=schema_ref, ), ] responses = openapi.Responses({ '200': openapi.Response( description='', schema=openapi.Schema( type=openapi.TYPE_OBJECT, properties={ 'name': { 'type': 'string', }, }, ), ), }) assert operation == openapi.Operation( operation_id='Controller.post_with_serializer', responses=responses, consumes=['application/json'], produces=['application/json'], tags=['test_app'], parameters=parameters, ) del View.post.route
def test_get_operation_without_body(): view = View() route = get_route(Controller.get) View.get.route = route reference_resolver = openapi.ReferenceResolver('definitions', 'parameters', force_init=True) auto_schema = SwaggerAutoSchema(view, 'path', route.http_method, reference_resolver, 'request', {}) operation = auto_schema.get_operation(['test_app', 'post']) parameters = [] responses = openapi.Responses({ '200': openapi.Response(description=''), }) assert operation == openapi.Operation( operation_id='Controller.get', responses=responses, consumes=['application/json'], produces=['application/json'], tags=['test_app'], parameters=parameters, ) del View.get.route
def test_get_operation(): view = View() route = get_route(Controller.post) View.post.route = route auto_schema = SwaggerAutoSchema(view, 'path', route.http_method, 'components', 'request', {}) operation = auto_schema.get_operation(['test_app', 'post']) parameters = [ openapi.Parameter( name='data', in_=openapi.IN_BODY, required=True, schema=openapi.Schema( title='UserDTO', type=openapi.TYPE_OBJECT, properties={ 'name': { 'type': openapi.TYPE_STRING, }, }, ), ), openapi.Parameter( name='path_param', in_=openapi.IN_PATH, description='', required=True, type=openapi.TYPE_INTEGER, ), openapi.Parameter( name='query_param', in_=openapi.IN_QUERY, description='', required=True, type=openapi.TYPE_INTEGER, ), ] responses = openapi.Responses({ '200': openapi.Response( description='', schema=openapi.Schema( type=openapi.TYPE_OBJECT, properties={ 'name': { 'type': 'string', }, }, ), ), }) assert operation == openapi.Operation( operation_id='Controller.post', responses=responses, consumes=['application/json; charset=utf-8'], produces=['application/json; charset=utf-8'], description='This is post method', tags=['test_app'], parameters=parameters, )