Example #1
0
def test_takes_dependency(provider: FactoryProvider):
    provider.register_factory(factory=lambda cls: cls(),
                              dependency=Service,
                              takes_dependency=True)

    assert isinstance(provider.provide(Service).instance, Service)
    assert provider.provide(AnotherService) is None
Example #2
0
def test_profile_instance(container: DependencyContainer,
                          interface_provider: IndirectProvider,
                          factory_provider: FactoryProvider):
    factory_provider.register_class(ServiceA)
    factory_provider.register_class(ServiceB, singleton=False)
    interface_provider.register(IService, ServiceA, Profile.A)
    interface_provider.register(IService, ServiceB, Profile.B)
    current_profile = Profile.A

    def get_profile():
        return current_profile

    factory_provider.register_factory(Profile,
                                      singleton=False,
                                      factory=get_profile)

    service_a = container.get(ServiceA)
    service_b = container.get(ServiceB)

    assert service_a is interface_provider.provide(IService).instance
    assert interface_provider.provide(IService).singleton is False

    current_profile = Profile.B
    assert service_b is not interface_provider.provide(IService).instance
    assert isinstance(interface_provider.provide(IService).instance, ServiceB)
    assert interface_provider.provide(IService).singleton is False
Example #3
0
def test_duplicate_error(provider: FactoryProvider):
    provider.register_class(Service)

    with pytest.raises(DuplicateDependencyError):
        provider.register_class(Service)

    with pytest.raises(DuplicateDependencyError):
        provider.register_factory(factory=lambda: Service(),
                                  dependency=Service)

    with pytest.raises(DuplicateDependencyError):
        provider.register_providable_factory(factory_dependency='dummy',
                                             dependency=Service)
Example #4
0
def test_build(provider: FactoryProvider):
    provider.register_class(Service)

    s = provider.provide(Build(Service, val=object)).instance
    assert isinstance(s, Service)
    assert dict(val=object) == s.kwargs

    provider.register_factory(AnotherService,
                              factory=AnotherService,
                              takes_dependency=True)

    s = provider.provide(Build(AnotherService, val=object)).instance
    assert isinstance(s, AnotherService)
    assert (AnotherService, ) == s.args
    assert dict(val=object) == s.kwargs
Example #5
0
def test_non_singleton_factory(provider: FactoryProvider):
    def factory_builder():
        def factory(o=object()):
            return o

        return factory

    provider.register_factory('factory',
                              factory=factory_builder,
                              singleton=False)
    provider.register_providable_factory('service',
                                         factory_dependency='factory')

    service = provider.provide('service').instance
    assert provider.provide('service').instance is not service
Example #6
0
def test_invalid_type(provider: FactoryProvider, kwargs):
    with pytest.raises(TypeError):
        provider.register_factory(**kwargs)