def test_is_supported(argument_name, expected_is_supported): @winter.route_get('{?' + argument_name + '}') def method(query_param: int): return query_param resolver = QueryParameterArgumentResolver() argument = method.get_argument('query_param') is_supported = resolver.is_supported(argument) second_is_supported = resolver.is_supported(argument) assert is_supported == second_is_supported == expected_is_supported
def test_query_parameter_resolver_with_raises_argument_not_supported(): @winter.route_get() def method(invalid_query_param: int): return invalid_query_param resolver = QueryParameterArgumentResolver() request = get_request('invalid_query_param=1') argument = method.get_argument('invalid_query_param') with pytest.raises(ArgumentNotSupported) as exception: resolver.resolve_argument(argument, request, {}) assert str(exception.value ) == 'Unable to resolve argument invalid_query_param: int'
def test_query_parameter_resolver(argument_name, query_string, expected_value): @winter.route_get('{?without_default,optional,with_default,array*}') def method( without_default: int, optional: Optional[str], array: List[int], with_default: str = 'default', ): return without_default, optional, array, with_default resolver = QueryParameterArgumentResolver() argument = method.get_argument(argument_name) request = get_request(query_string) assert resolver.resolve_argument(argument, request, {}) == expected_value
def test_query_parameter_resolver_with_raises_parse_error( query_string, expected_exception_message): @winter.route_get('{?query_param}') def method(query_param: int): return query_param resolver = QueryParameterArgumentResolver() argument = method.get_argument('query_param') request = get_request(query_string) with pytest.raises(decoder.JSONDecodeException) as exception: resolver.resolve_argument(argument, request, {}) assert str(exception.value) == expected_exception_message
def test_orphan_map_query_parameter_fails(): @winter.route_get('{?x,y}') @winter.map_query_parameter('x', to='x_param') @winter.map_query_parameter('y', to='y_param') def method(x_param: int, y_param: int = 1): return 0 resolver = QueryParameterArgumentResolver() argument = method.get_argument('x_param') request = get_request('?x=1') with pytest.raises(decoder.JSONDecodeException) as exception: resolver.resolve_argument(argument, request, {}) assert str(exception.value) == 'Missing required query parameter "x"'