Ejemplo n.º 1
0
 def __init__(
     self,
     basic_service: Autowired(BasicService),
     another_basic_service: Autowired(BasicService),
 ):
     self.basic_service = basic_service
     self.another_basic_service = another_basic_service
Ejemplo n.º 2
0
    def test__inject__with_class_dependency_and_explicit_parameters(
            self, mocker: MockFixture):
        # given
        mocked_inject = mocker.patch(
            "injectable.autowiring.autowired_type.inject")
        mocked_inject_multiple = mocker.patch(
            "injectable.autowiring.autowired_type.inject_multiple")
        expected = {
            "namespace": "dummy",
            "group": "dummy",
            "exclude_groups": ["dummy"],
            "lazy": True,
            "optional": False,
        }
        autowired = Autowired(
            TestAutowiredType,
            namespace=expected["namespace"],
            group=expected["group"],
            exclude_groups=expected["exclude_groups"],
            lazy=expected["lazy"],
        )

        # when
        autowired.inject()

        # then
        assert mocked_inject.call_count == 1
        assert mocked_inject_multiple.call_count == 0
        args, kwargs = mocked_inject.call_args
        assert args[0] is TestAutowiredType
        assert kwargs == expected
Ejemplo n.º 3
0
 def __init__(
         self,
         simple_service: Autowired(SimpleService),
         dependable_service: Autowired(DependableService),
 ):
     self.simple_service = simple_service
     self.dependable_service = dependable_service
Ejemplo n.º 4
0
 def __init__(
         self,
         even_counter: Autowired("counter"),
         odd_counter: Autowired("counter", namespace="odd"),
 ):
     self.even_counter = even_counter
     self.odd_counter = odd_counter
Ejemplo n.º 5
0
 def __init__(
         self,
         client1: Autowired(SingletonClient),
         client2: Autowired(SingletonClient),
 ):
     self.client1 = client1
     self.client2 = client2
Ejemplo n.º 6
0
 def __init__(
         self,
         some_service: Autowired(Optional["foo"]),
         bunch_of_services: Autowired(Optional[List["bar"]]),
 ):
     self.some_service = some_service
     self.bunch_of_services = bunch_of_services
 def __init__(
     self,
     abstract_service_1: Autowired(AbstractService),
     abstract_service_2: Autowired("multiply"),
 ):
     self.abstract_service_1 = abstract_service_1
     self.abstract_service_2 = abstract_service_2
Ejemplo n.º 8
0
 def __init__(
         self,
         sum_service: Autowired(AbstractService),
         multiply_service: Autowired("multiply"),
 ):
     self.sum_service = sum_service
     self.multiply_service = multiply_service
Ejemplo n.º 9
0
 def __init__(
     self, service_a: Autowired(ServiceA, lazy=True), service_b: Autowired(ServiceB)
 ):
     # ServiceB::__init__ called
     print("example init started")
     # example init started
     self.service_a = service_a
     self.service_b = service_b
     print("example init finished")
Ejemplo n.º 10
0
 def __init__(
     self,
     default_measuring_service: Autowired(Optional["MEASURING_SERVICE"]),
     intl_measuring_service: Autowired("MEASURING_SERVICE",
                                       namespace="INTL"),
     us_measuring_service: Autowired("MEASURING_SERVICE", namespace="US"),
 ):
     self.default_measuring_service = default_measuring_service
     self.intl_measuring_service = intl_measuring_service
     self.us_measuring_service = us_measuring_service
Ejemplo n.º 11
0
 def __init__(
         self,
         function: Callable,
         initial_state: Autowired(State, namespace="python-chain"),
 ):
     self.initial_state = deepcopy(initial_state)
     self.function = function
Ejemplo n.º 12
0
 def __init__(
     self,
     function: Callable,
     initial_state: Autowired(State, namespace="python-chain"),
 ):
     super().__init__(function, initial_state=initial_state)
     update_wrapper(self, function)
    def test__inject__with_list_class_dependency(self, mocker: MockFixture):
        # given
        mocked_inject = mocker.patch("injectable.autowiring.autowired_type.inject")
        mocked_inject_multiple = mocker.patch(
            "injectable.autowiring.autowired_type.inject_multiple"
        )
        autowired = Autowired(list[TestAutowiredTypePy39])

        # when
        autowired.inject()

        # then
        assert mocked_inject.call_count == 0
        assert mocked_inject_multiple.call_count == 1
        args, kwargs = mocked_inject_multiple.call_args
        assert args[0] == TestAutowiredTypePy39
    def test__inject__with_list_qualifier_dependency(self, mocker: MockFixture):
        # given
        mocked_inject = mocker.patch("injectable.autowiring.autowired_type.inject")
        mocked_inject_multiple = mocker.patch(
            "injectable.autowiring.autowired_type.inject_multiple"
        )
        expected = "Expected"
        autowired = Autowired(list[expected])

        # when
        autowired.inject()

        # then
        assert mocked_inject.call_count == 0
        assert mocked_inject_multiple.call_count == 1
        args, kwargs = mocked_inject_multiple.call_args
        assert args[0] == expected
Ejemplo n.º 15
0
def client_factory(configuration_service: Autowired(ConfigurationService)):
    if configuration_service.client_type == 1:
        return ClientOne(configuration_service.client_endpoint)
    elif configuration_service.client_type == 2:
        return ClientTwo(configuration_service.client_endpoint)
    raise RuntimeError(f"Unknown client_type: {configuration_service.client_type}")
Ejemplo n.º 16
0
 def __init__(self, simple_service: Autowired(SimpleService)):
     self.simple_service = simple_service
Ejemplo n.º 17
0
 def __init__(
         self,
         client: Autowired(ExternalClient),
 ):
     self.client = client
Ejemplo n.º 18
0
def f(foo: Autowired("foo")):
    ...
Ejemplo n.º 19
0
def bar(foo: Autowired(List["foo"])):
    assert foo is not None
    assert len(foo) == 1
Ejemplo n.º 20
0
"""
Test the fix for the issue 8:
Autowired(List[...]) does not work with qualifiers
https://github.com/allrod5/injectable/issues/8

Injectable 3.1.3 attempts to use the argument of typing.List as a qualifier though
string arguments to subscriptable typing types are encapsulated in a typing.ForwardRef
object so the argument of the ForwardRef should be used as qualifier actually and not
the ForwardRef object itself.

This issue was fixed in injectable 3.1.4.
"""
from typing import List

from injectable import injectable, autowired, Autowired, load_injection_container
from injectable.testing import reset_injection_container


@injectable(qualifier="foo")
class Foo:
    pass


@autowired
def bar(foo: Autowired(List["foo"])):
    assert foo is not None
    assert len(foo) == 1


def test_issue_8_fix():
Ejemplo n.º 21
0
 def run(self, dep: Autowired(RealCar)):
     dep.print()
     # MockedDep
     dep.print.assert_called()
Ejemplo n.º 22
0
 def __init__(
         self,
         lazy_service: Autowired(LazyService, lazy=True),
 ):
     self.lazy_service = lazy_service
     print("finished injecting")
Ejemplo n.º 23
0
 def __init__(self, configuration: Autowired(Configurator)):
     # thanks to autowired it is not necessary to add the configuration argument
     # when calling _configure Bicycle()
     self.configuration = configuration
Ejemplo n.º 24
0
 def __init__(
         self,
         app: Autowired(Application),
 ):
     self.app = app
Ejemplo n.º 25
0
def a(x: Autowired(Optional["anything"])):
    assert x is None
Ejemplo n.º 26
0
 def __init__(self, service_a: Autowired("A")):
     self.service_a = service_a
     self.message = "ServiceB.message"
 def __init__(self, service_a: Autowired("A"), service_b: Autowired("B")):
     self.service_a = service_a
     self.service_b = service_b
Ejemplo n.º 28
0
 def __init__(self, dep: Autowired(Dep)):
     self.dep = dep
Ejemplo n.º 29
0
 def run(self, dep: Autowired(Car)):
         dep.forward()
Ejemplo n.º 30
0
def f(foo: Autowired("foo")):
    assert foo.is_ok()