Exemple #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
Exemple #2
0
def test_singleton(provider: FactoryProvider):
    provider.register_class(Service, singleton=True)
    provider.register_class(AnotherService, singleton=False)

    provide = provider.provide
    assert provide(Service).singleton is True
    assert provide(AnotherService).singleton is False
Exemple #3
0
def test_interface(container: DependencyContainer,
                   interface_provider: IndirectProvider,
                   factory_provider: FactoryProvider):
    factory_provider.register_class(Service)
    interface_provider.register(IService, Service)

    expected = container.get(Service)
    assert interface_provider.provide(IService).instance is expected
    assert interface_provider.provide(IService).singleton is True
Exemple #4
0
def test_not_singleton_interface(container: DependencyContainer,
                                 interface_provider: IndirectProvider,
                                 factory_provider: FactoryProvider):
    factory_provider.register_class(Service, singleton=False)
    interface_provider.register(IService, Service)

    service = container.get(Service)
    assert interface_provider.provide(IService).instance is not service
    assert isinstance(interface_provider.provide(IService).instance, Service)
    assert interface_provider.provide(IService).singleton is False
Exemple #5
0
def test_undefined_context(container: DependencyContainer,
                           interface_provider: IndirectProvider,
                           factory_provider: FactoryProvider):
    factory_provider.register_class(ServiceA)
    factory_provider.register_class(ServiceB)
    interface_provider.register(IService, ServiceA, Profile.A)
    container.update_singletons({Profile: Profile.B})

    with pytest.raises(UndefinedContextError):
        interface_provider.provide(IService)
Exemple #6
0
def test_singleton_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)

    service_a = container.get(ServiceA)

    container.update_singletons({Profile: Profile.A})
    assert service_a is interface_provider.provide(IService).instance
    assert interface_provider.provide(IService).singleton is True
Exemple #7
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)
Exemple #8
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
Exemple #9
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
Exemple #10
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
Exemple #11
0
def test_simple(provider: FactoryProvider):
    provider.register_class(Service)

    dependency = provider.provide(Service)
    assert isinstance(dependency.instance, Service)
    assert repr(Service) in repr(provider)
Exemple #12
0
def provider():
    container = DependencyContainer()
    provider = FactoryProvider(container=container)
    container.register_provider(provider)
    return provider
Exemple #13
0
def test_unknown_dependency(provider: FactoryProvider, dependency):
    assert provider.provide(dependency) is None
Exemple #14
0
def test_invalid_type(provider: FactoryProvider, kwargs):
    with pytest.raises(TypeError):
        provider.register_factory(**kwargs)
Exemple #15
0
def factory_provider(container):
    provider = FactoryProvider(container=container)
    container.register_provider(provider)
    return provider