def __init__( self, basic_service: Autowired(BasicService), another_basic_service: Autowired(BasicService), ): self.basic_service = basic_service self.another_basic_service = another_basic_service
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
def __init__( self, simple_service: Autowired(SimpleService), dependable_service: Autowired(DependableService), ): self.simple_service = simple_service self.dependable_service = dependable_service
def __init__( self, even_counter: Autowired("counter"), odd_counter: Autowired("counter", namespace="odd"), ): self.even_counter = even_counter self.odd_counter = odd_counter
def __init__( self, client1: Autowired(SingletonClient), client2: Autowired(SingletonClient), ): self.client1 = client1 self.client2 = client2
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
def __init__( self, sum_service: Autowired(AbstractService), multiply_service: Autowired("multiply"), ): self.sum_service = sum_service self.multiply_service = multiply_service
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")
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
def __init__( self, function: Callable, initial_state: Autowired(State, namespace="python-chain"), ): self.initial_state = deepcopy(initial_state) self.function = function
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
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}")
def __init__(self, simple_service: Autowired(SimpleService)): self.simple_service = simple_service
def __init__( self, client: Autowired(ExternalClient), ): self.client = client
def f(foo: Autowired("foo")): ...
def bar(foo: Autowired(List["foo"])): assert foo is not None assert len(foo) == 1
""" 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():
def run(self, dep: Autowired(RealCar)): dep.print() # MockedDep dep.print.assert_called()
def __init__( self, lazy_service: Autowired(LazyService, lazy=True), ): self.lazy_service = lazy_service print("finished injecting")
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
def __init__( self, app: Autowired(Application), ): self.app = app
def a(x: Autowired(Optional["anything"])): assert x is None
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
def __init__(self, dep: Autowired(Dep)): self.dep = dep
def run(self, dep: Autowired(Car)): dep.forward()
def f(foo: Autowired("foo")): assert foo.is_ok()