コード例 #1
0
ファイル: test_container.py プロジェクト: keelerm84/antidote
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)
コード例 #2
0
ファイル: test_container.py プロジェクト: keelerm84/antidote
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
コード例 #3
0
ファイル: test_container.py プロジェクト: keelerm84/antidote
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)
コード例 #4
0
ファイル: test_container.py プロジェクト: keelerm84/antidote
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
コード例 #5
0
ファイル: test_container.py プロジェクト: keelerm84/antidote
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))
コード例 #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')
コード例 #7
0
ファイル: test_container.py プロジェクト: keelerm84/antidote
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)
コード例 #8
0
def provider():
    container = DependencyContainer()
    provider = FactoryProvider(container=container)
    container.register_provider(provider)
    return provider
コード例 #9
0
ファイル: test_container.py プロジェクト: keelerm84/antidote
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]
コード例 #10
0
ファイル: test_container.py プロジェクト: keelerm84/antidote
def test_invalid_provider(container: DependencyContainer):
    with pytest.raises(TypeError):
        container.register_provider(object())
コード例 #11
0
def container():
    c = DependencyContainer()
    c.register_provider(FactoryProvider(container=c))
    c.register_provider(IndirectProvider(container=c))

    return c
コード例 #12
0
ファイル: test_conf.py プロジェクト: keelerm84/antidote
def container():
    c = DependencyContainer()
    c.register_provider(LazyCallProvider(container=c))
    c.register_provider(FactoryProvider(container=c))

    return c