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_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 #3
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 #4
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)
Example #5
0
def test_unknown_dependency(provider: FactoryProvider, dependency):
    assert provider.provide(dependency) is None