Ejemplo n.º 1
0
def test_repr_str(container: DependencyContainer):
    container.register_provider(DummyProvider({'name': 'Antidote'}))
    container.update_singletons({'test': 1})

    assert 'test' in repr(container)
    assert repr(container.providers[DummyProvider]) in repr(container)
    assert str(container.providers[DummyProvider]) in str(container)
Ejemplo n.º 2
0
def test_singleton(container: DependencyContainer):
    container.register_provider(DummyFactoryProvider({
        Service: lambda: Service(),
        AnotherService: lambda: AnotherService(),
    }))

    service = container.get(Service)
    assert service is container.get(Service)

    container.providers[DummyFactoryProvider].singleton = False
    another_service = container.get(AnotherService)
    assert another_service is not container.get(AnotherService)

    singletons = {k: v.instance for k, v in container.singletons.items()}
    assert {Service: service, DependencyContainer: container} == singletons
Ejemplo n.º 3
0
def test_dependency_cycle_error(container: DependencyContainer):
    container.register_provider(DummyFactoryProvider({
        Service: lambda: Service(container.get(AnotherService)),
        AnotherService: lambda: AnotherService(container.get(YetAnotherService)),
        YetAnotherService: lambda: YetAnotherService(container.get(Service)),
    }))

    with pytest.raises(DependencyCycleError):
        container.get(Service)

    with pytest.raises(DependencyCycleError):
        container.get(AnotherService)

    with pytest.raises(DependencyCycleError):
        container.get(YetAnotherService)
Ejemplo n.º 4
0
def test_getitem(container: DependencyContainer):
    container.register_provider(DummyFactoryProvider({
        Service: lambda: Service(),
        ServiceWithNonMetDependency: lambda: ServiceWithNonMetDependency(),
    }))
    container.register_provider(DummyProvider({'name': 'Antidote'}))

    with pytest.raises(DependencyNotFoundError):
        container.get(object)

    with pytest.raises(DependencyInstantiationError):
        container.get(ServiceWithNonMetDependency)

    assert isinstance(container.get(Service), Service)
    assert isinstance(container.provide(Service), DependencyInstance)
    assert 'Antidote' == container.get('name')
    assert 'Antidote' == container.provide('name').instance
Ejemplo n.º 5
0
def test_bound_dependency_types_conflict():
    class CustomDependency:
        pass

    class DummyProvider1(DependencyProvider):
        bound_dependency_types = (CustomDependency,)

        def provide(self, dependency: Any) -> DependencyInstance:
            return DependencyInstance(self)

    class DummyProvider2(DependencyProvider):
        bound_dependency_types = (CustomDependency,)

        def provide(self, dependency: Any) -> DependencyInstance:
            return DependencyInstance(self)

    container = DependencyContainer()
    container.register_provider(DummyProvider1(container))

    with pytest.raises(RuntimeError):
        container.register_provider(DummyProvider2(container))
Ejemplo n.º 6
0
def test_context_missing():
    container = DependencyContainer()
    container.update_singletons({'test': 1})
    container.register_provider(DummyProvider({'name': 'Antidote'}))
    s = Service()
    container.update_singletons({Service: s})

    proxy_container = ProxyContainer(container, missing=['name'])

    assert s is proxy_container.get(Service)
    assert 1 == proxy_container.get('test')

    with pytest.raises(DependencyNotFoundError):
        proxy_container.get('name')

    proxy_container = ProxyContainer(container, missing=['test'], include=[Service])

    assert s is proxy_container.get(Service)
    assert 'Antidote' == proxy_container.get('name')

    with pytest.raises(DependencyNotFoundError):
        proxy_container.get('test')
Ejemplo n.º 7
0
def test_bound_dependency_types():
    class CustomDependency:
        pass

    class DummyProvider1(DependencyProvider):
        bound_dependency_types = (CustomDependency,)

        def provide(self, dependency: Any) -> DependencyInstance:
            return DependencyInstance(self)

    class DummyProvider2(DependencyProvider):
        def provide(self, dependency: Any) -> DependencyInstance:
            raise Exception()

    container = DependencyContainer()
    container.register_provider(DummyProvider2(container))
    container.register_provider(DummyProvider1(container))
    assert isinstance(container.get(CustomDependency()), DummyProvider1)

    container = DependencyContainer()
    container.register_provider(DummyProvider1(container))
    container.register_provider(DummyProvider2(container))
    assert isinstance(container.get(CustomDependency()), DummyProvider1)
Ejemplo n.º 8
0
def provider():
    container = DependencyContainer()
    provider = FactoryProvider(container=container)
    container.register_provider(provider)
    return provider
Ejemplo n.º 9
0
def test_register_provider(container: DependencyContainer):
    provider = DummyProvider()
    container.register_provider(provider)

    # Can't check directly if the core is the same, as a proxy is used
    assert provider is container.providers[DummyProvider]
Ejemplo n.º 10
0
def test_invalid_provider(container: DependencyContainer):
    with pytest.raises(TypeError):
        container.register_provider(object())
Ejemplo n.º 11
0
def container():
    c = DependencyContainer()
    c.register_provider(FactoryProvider(container=c))
    c.register_provider(IndirectProvider(container=c))

    return c
Ejemplo n.º 12
0
def container():
    c = DependencyContainer()
    c.register_provider(LazyCallProvider(container=c))
    c.register_provider(FactoryProvider(container=c))

    return c