Beispiel #1
0
def test_is_supported_in_page_position_argument_resolver(
        argument_type, expected_is_supported):
    def func(arg1: argument_type):  # pragma: no cover
        return arg1

    method = ComponentMethod(func)
    argument = method.get_argument('arg1')
    resolver = PagePositionArgumentResolver()

    # Act
    is_supported = resolver.is_supported(argument)

    # Assert
    assert is_supported == expected_is_supported, argument.type_
Beispiel #2
0
def test_resolve_argument_with_order_by_without_order_by_annotation():
    @winter.core.component_method
    def method(arg1: PagePosition):  # pragma: no cover
        return arg1

    argument = method.get_argument('arg1')
    resolver = PagePositionArgumentResolver()
    request = Mock(spec=DRFRequest)
    request.query_params = QueryDict('order_by=id')

    # Act
    page_position = resolver.resolve_argument(argument, request, {})

    # Assert
    assert page_position == PagePosition()
def test_page_position_argument_inspector_with_allowed_order_by_fields(
        default_sort, default_in_parameter):
    class SimpleController:
        @winter.route_get('')
        @winter.web.pagination.order_by(['id'], default_sort=default_sort)
        def method(self, arg1: PagePosition):  # pragma: no cover
            pass

    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 test_page_position_argument_inspector(argument_type,
                                          must_return_parameters):
    class SimpleController:
        @winter.route_get('')
        def method(self, arg1: argument_type):  # pragma: no cover
            pass

    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
Beispiel #5
0
def test_resolve_argument_ok_in_page_position_argument_resolver(
        query_string, expected_page_position):
    @winter.web.pagination.order_by(['name', 'id', 'email', 'x'])
    def method(page_position: PagePosition):  # pragma: no cover
        return page_position

    argument = method.get_argument('page_position')

    resolver = PagePositionArgumentResolver(allow_any_order_by_field=True)

    request = Mock(spec=DRFRequest)
    request.query_params = QueryDict(query_string)

    # Act
    page_position = resolver.resolve_argument(argument, request, {})

    # Assert
    assert page_position == expected_page_position
Beispiel #6
0
def test_resolve_argument_fails_in_page_position_argument_resolver(
        query_string, exception_type, message):
    @winter.web.pagination.order_by(['id'])
    def method(arg1: int):  # pragma: no cover
        return arg1

    argument = method.get_argument('arg1')

    request = Mock(spec=DRFRequest)
    request.query_params = QueryDict(query_string)
    resolver = PagePositionArgumentResolver()

    # Assert
    with pytest.raises(exception_type) as exception_info:
        # Act
        resolver.resolve_argument(argument, request, {})

    assert exception_info.value.args[0] == message