def test_props_extra(regular_container):
    # Send an extra prop, not one that overrides an injected prop

    injector = Injector(regular_container)
    target: Target9 = injector(Target9, flag=88)
    result: int = target()
    assert 88 == result
def test_props_unannotated_typed(regular_container):
    def target(container: ServiceContainer):
        return container

    injector = Injector(regular_container)
    result: int = injector(target, container=88)
    assert 88 == result
def test_one_parameter_field_type(regular_container):
    def target(view: View):
        return view

    injector = Injector(regular_container)
    result: View = injector(target)
    assert result.name == 'View'
def test_no_parameters(regular_container):
    def target():
        return 99

    injector = Injector(regular_container)
    result: int = injector(target)
    assert result == 99
def test_props_override(regular_container):
    # Send a prop that overrides an injected prop

    injector = Injector(regular_container)
    target: Target10 = injector(Target10, container=88)
    result = target()
    assert 88 == result
def test_get_then_attr(regular_container):
    """ Pipeline: Get, Attr """

    injector = Injector(regular_container)
    target: Target11 = injector(Target11)
    result: str = target()
    assert result == 'View'
def test_two_parameters_unannotated(regular_container):
    def target(container: ServiceContainer, view: View):
        return view

    injector = Injector(regular_container)
    result: View = injector(target)
    assert result.name == 'View'
def test_one_parameter_container(regular_container):
    def target(container: ServiceContainer):
        view = container.get(View)
        return view

    injector = Injector(regular_container)
    result: View = injector(target)
    assert result.name == 'View'
def test_system_props_unannotated_untyped(regular_container):
    def target(children):
        return children

    injector = Injector(regular_container)
    system_props = dict(children=77)
    result: int = injector(target, system_props=system_props)
    assert 77 == result
def test_optional_unannotated(regular_container):
    # TODO: Need Optional[str] = None default value
    def target(container: Optional[ServiceContainer]):
        if container is None:
            return None
        view = container.get(View)
        return view

    injector = Injector(regular_container)
    result: View = injector(target)
    assert result.name == 'View'
def test_one_parameter_annotated(french_container):
    def target(
        french_view: Annotated[
            FrenchView,
            Get(View),
        ]
    ):
        return french_view

    injector = Injector(french_container)
    result: FrenchView = injector(target)
    assert result.name == 'French View'
def test_default_value_unannotated(regular_container):
    class Foo:
        pass

    f = Foo()

    def target(some_foo: Foo = f) -> Foo:
        return some_foo

    injector = Injector(regular_container)
    result: Foo = injector(target)
    assert result is f
def test_two_parameters_annotated(french_container):
    def target(
        container: ServiceContainer,
        french_customer: Annotated[
            FrenchView,
            Get(View),
        ],
    ):
        return french_customer

    injector = Injector(french_container)
    result: Customer = injector(target)
    assert result.name == 'French View'
def test_optional_annotated(french_container):
    def target(
        french_view: Optional[
            Annotated[
                FrenchView,
                Get(View),
            ]
        ],
    ):
        return french_view

    injector = Injector(french_container)
    result: FrenchView = injector(target)
    assert result.name == 'French View'
def test_get_then_attr(regular_container):
    """ Pipeline: Get, Attr """

    def target(
        view_name: Annotated[
            str,
            Get(View),
            Attr('name'),
        ],
    ):
        return view_name

    injector = Injector(regular_container)
    result: View = injector(target)
    assert result == 'View'
def test_context_then_attr(regular_container):
    """ Pipeline: Context, Attr """

    def target(
        customer_name: Annotated[
            str,
            Context(),
            Attr('name'),
        ],
    ):
        return customer_name

    injector = Injector(regular_container)
    result: View = injector(target)
    assert result == 'Customer'
def test_default_value_annotated(regular_container):
    class Foo:
        pass

    def target(
        view_name: Annotated[
            str,
            Get(Foo),
            Attr('name'),
        ] = 'View Name'
    ):
        return view_name

    injector = Injector(regular_container)
    result: str = injector(target)
    assert result == 'View Name'
예제 #18
0
 def view_factory(container: ServiceContainer):
     injector = Injector(container)
     view_instance = injector(target)
     return view_instance
def test_two_parameters_annotated(french_container):
    injector = Injector(french_container)
    target: Target6 = injector(Target6)
    result: FrenchView = target()
    assert result.name == 'French View'
def test_two_parameters_unannotated(regular_container):
    injector = Injector(regular_container)
    target: Target5 = injector(Target5)
    result: View = target()
    assert result.name == 'View'
def test_one_parameter_field_type(regular_container):
    injector = Injector(regular_container)
    target: Target3 = injector(Target3)
    result: View = target()
    assert result.name == 'View'
def test_no_parameters(regular_container):
    injector = Injector(regular_container)
    target: Target1 = injector(Target1)
    result: int = target()
    assert result == 99
def test_construction(regular_container):
    injector = Injector(regular_container)
    assert injector
def test_optional_annotated(french_container):
    injector = Injector(french_container)
    target: Target8 = injector(Target8)
    result: FrenchView = target()
    assert result.name == 'French View'
def test_optional_unannotated(regular_container):
    injector = Injector(regular_container)
    target: Target7 = injector(Target7)
    result: Optional[View] = target()
    if result is not None:
        assert result.name == 'View'