def test_implicit_from_services_only_when_annotation_is_none(): def handler(dog): ... binders = get_binders(Route(b"/", handler), {"dog": Dog("Snoopy")}) assert isinstance(binders[0], ServiceBinder) def handler(dog: str): ... binders = get_binders(Route(b"/", handler), {"dog": Dog("Snoopy")}) assert isinstance(binders[0], QueryBinder)
def test_request_binding(): def handler(request): assert request binders = get_binders(Route(b'/', handler), {}) assert isinstance(binders[0], RequestBinder)
def test_identity_binder_by_param_name_user(): async def handler(user): ... binders = get_binders(Route(b'/', handler), {}) assert isinstance(binders[0], IdentityBinder)
def test_request_binding(): def handler(request): ... binders = get_binders(Route(b"/", handler), Services()) assert isinstance(binders[0], RequestBinder)
def test_parameters_get_binders_list_types_default_from_query_required(): def handler(a: List[str]): ... binders = get_binders(Route(b"/", handler), Services()) assert all(isinstance(binder, QueryBinder) for binder in binders) assert all(binder.required for binder in binders)
def test_parameters_get_binders_list_types_default_from_query_required(): def handler(a: List[str]): pass binders = get_binders(Route(b'/', handler), {}) assert all(isinstance(binder, FromQuery) for binder in binders) assert all(binder.required for binder in binders)
def test_identity_binder_by_param_type(annotation_type): async def handler(param): ... handler.__annotations__["param"] = annotation_type binders = get_binders(Route(b"/", handler), Services()) assert isinstance(binders[0], IdentityBinder)
def test_services_binding(): app_services = {} def handler(services): assert services is app_services binders = get_binders(Route(b'/', handler), app_services) assert isinstance(binders[0], ExactBinder)
def test_from_header_specific_name(): def handler(a: FromHeader(str, 'example')): pass binders = get_binders(Route(b'/', handler), {}) assert isinstance(binders[0], FromHeader) assert binders[0].expected_type is str assert binders[0].name == 'example'
def test_identity_binder_by_param_type(annotation_type): async def handler(param): ... handler.__annotations__['param'] = annotation_type binders = get_binders(Route(b'/', handler), {}) assert isinstance(binders[0], IdentityBinder)
def test_from_query_specific_name(): def handler(a: FromQuery(str, 'example')): ... binders = get_binders(Route(b'/', handler), {}) assert isinstance(binders[0], FromQuery) assert binders[0].expected_type is str assert binders[0].name == 'example'
def test_parameters_get_binders_list_types_default_from_query_optional(): def handler(a: Optional[List[str]]): ... binders = get_binders(Route(b"/", handler), Services()) assert all(isinstance(binder, QueryBinder) for binder in binders) assert all(binder.required is False for binder in binders) assert binders[0].parameter_name == "a" assert binders[0].expected_type == List[str]
def test_parameters_get_binders_from_route(): def handler(a, b, c): pass binders = get_binders(Route(b'/:a/:b/:c', handler), {}) assert all(isinstance(binder, FromRoute) for binder in binders) assert binders[0].name == 'a' assert binders[1].name == 'b' assert binders[2].name == 'c'
def test_parameters_get_binders_list_types_default_from_query_optional(): def handler(a: Optional[List[str]]): pass binders = get_binders(Route(b'/', handler), {}) assert all(isinstance(binder, FromQuery) for binder in binders) assert all(binder.required is False for binder in binders) assert binders[0].name == 'a' assert binders[0].expected_type == List[str]
def test_parameters_get_binders_from_route(): def handler(a, b, c): ... binders = get_binders(Route(b"/:a/:b/:c", handler), Services()) assert all(isinstance(binder, RouteBinder) for binder in binders) assert binders[0].parameter_name == "a" assert binders[1].parameter_name == "b" assert binders[2].parameter_name == "c"
def test_parameters_get_binders_default_query(): def handler(a, b, c): pass binders = get_binders(Route(b'/', handler), {}) assert all(isinstance(binder, FromQuery) for binder in binders) assert binders[0].name == 'a' assert binders[1].name == 'b' assert binders[2].name == 'c'
def test_parameters_get_binders_default_query(): def handler(a, b, c): ... binders = get_binders(Route(b"/", handler), Services()) assert all(isinstance(binder, QueryBinder) for binder in binders) assert binders[0].parameter_name == "a" assert binders[1].parameter_name == "b" assert binders[2].parameter_name == "c"
def test_parameters_get_binders_from_body_optional(): def handler(a: Optional[Cat]): ... binders = get_binders(Route(b"/", handler), Services()) assert len(binders) == 1 binder = binders[0] assert isinstance(binder, JSONBinder) assert binder.expected_type is Cat assert binder.required is False
def test_from_query_unspecified_type(): def handler(a: FromQuery): ... binders = get_binders(Route(b"/", handler), Services()) binder = binders[0] assert isinstance(binder, QueryBinder) assert binder.expected_type is List[str] assert binder.required is True assert binder.parameter_name == "a"
def test_from_query_optional_list_type(): def handler(a: FromQuery[Optional[List[str]]]): ... binders = get_binders(Route(b"/", handler), Services()) binder = binders[0] assert isinstance(binder, QueryBinder) assert binder.expected_type is List[str] assert binder.required is False assert binder.parameter_name == "a"
def test_parameters_get_binders_from_body_optional(): def handler(a: Optional[Cat]): pass binders = get_binders(Route(b'/', handler), {}) assert len(binders) == 1 binder = binders[0] assert isinstance(binder, FromJson) assert binder.expected_type is Cat assert binder.required is False
def test_parameters_get_binders_from_body(): def handler(a: Cat): ... binders = get_binders(Route(b'/', handler), {}) assert len(binders) == 1 binder = binders[0] assert isinstance(binder, FromJson) assert binder.expected_type is Cat assert binder.required is True
def test_from_query_optional_type_with_union(): def handler(a: FromQuery[Union[None, str]]): ... binders = get_binders(Route(b"/", handler), Services()) binder = binders[0] assert isinstance(binder, QueryBinder) assert binder.expected_type is str assert binder.required is False assert binder.parameter_name == "a"
def test_parameters_get_binders_from_body(): def handler(a: Cat): ... binders = get_binders(Route(b"/", handler), Services()) assert len(binders) == 1 binder = binders[0] assert isinstance(binder, JsonBinder) assert binder.expected_type is Cat assert binder.required is True
def test_parameters_get_binders_from_services_by_name(): def handler(a, b, c): ... binders = get_binders( Route(b"/", handler), {"a": object(), "b": object(), "c": object()} ) assert all(isinstance(binder, ServiceBinder) for binder in binders) assert binders[0].expected_type == "a" assert binders[1].expected_type == "b" assert binders[2].expected_type == "c"
def test_from_header_specific_name(): class FromExampleHeader(FromHeader[str]): name = "example" def handler(a: FromExampleHeader): ... binders = get_binders(Route(b"/", handler), Services()) assert isinstance(binders[0], HeaderBinder) assert binders[0].expected_type is str assert binders[0].parameter_name == "example"
def test_from_header_accept_language_example(): class AcceptLanguageHeader(FromHeader[str]): name = "accept-language" def handler(a: AcceptLanguageHeader): ... binders = get_binders(Route(b"/", handler), Services()) assert isinstance(binders[0], HeaderBinder) assert binders[0].expected_type is str assert binders[0].parameter_name == "accept-language"
def test_parameters_get_binders_from_services_by_type(): def handler(a: str, b: int, c: Cat): ... binders = get_binders( Route(b"/", handler), {str: object(), int: object(), Cat: object()} ) assert all(isinstance(binder, ServiceBinder) for binder in binders) assert binders[0].expected_type is str assert binders[1].expected_type is int assert binders[2].expected_type is Cat
def test_parameters_get_binders_sequence_types_default_from_query(): def handler(a: Sequence[str], b: Sequence[int], c: Sequence[bool]): pass binders = get_binders(Route(b'/', handler), {}) assert all(isinstance(binder, FromQuery) for binder in binders) assert binders[0].name == 'a' assert binders[0].expected_type == Sequence[str] assert binders[1].name == 'b' assert binders[1].expected_type == Sequence[int] assert binders[2].name == 'c' assert binders[2].expected_type == Sequence[bool]
def test_parameters_get_binders_simple_types_default_from_query(): def handler(a: str, b: int, c: bool): pass binders = get_binders(Route(b'/', handler), {}) assert all(isinstance(binder, FromQuery) for binder in binders) assert binders[0].name == 'a' assert binders[0].expected_type == str assert binders[1].name == 'b' assert binders[1].expected_type == int assert binders[2].name == 'c' assert binders[2].expected_type == bool