コード例 #1
0
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)
コード例 #2
0
def test_request_binding():
    def handler(request):
        assert request

    binders = get_binders(Route(b'/', handler), {})

    assert isinstance(binders[0], RequestBinder)
コード例 #3
0
def test_identity_binder_by_param_name_user():
    async def handler(user):
        ...

    binders = get_binders(Route(b'/', handler), {})

    assert isinstance(binders[0], IdentityBinder)
コード例 #4
0
def test_request_binding():
    def handler(request):
        ...

    binders = get_binders(Route(b"/", handler), Services())

    assert isinstance(binders[0], RequestBinder)
コード例 #5
0
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)
コード例 #6
0
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)
コード例 #7
0
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)
コード例 #8
0
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)
コード例 #9
0
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'
コード例 #10
0
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)
コード例 #11
0
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'
コード例 #12
0
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]
コード例 #13
0
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'
コード例 #14
0
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]
コード例 #15
0
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"
コード例 #16
0
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'
コード例 #17
0
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"
コード例 #18
0
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
コード例 #19
0
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"
コード例 #20
0
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"
コード例 #21
0
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
コード例 #22
0
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
コード例 #23
0
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"
コード例 #24
0
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
コード例 #25
0
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"
コード例 #26
0
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"
コード例 #27
0
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"
コード例 #28
0
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
コード例 #29
0
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]
コード例 #30
0
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