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
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
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
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
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)
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
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)
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
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
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
def test_simple(provider: FactoryProvider): provider.register_class(Service) dependency = provider.provide(Service) assert isinstance(dependency.instance, Service) assert repr(Service) in repr(provider)
def provider(): container = DependencyContainer() provider = FactoryProvider(container=container) container.register_provider(provider) return provider
def test_unknown_dependency(provider: FactoryProvider, dependency): assert provider.provide(dependency) is None
def test_invalid_type(provider: FactoryProvider, kwargs): with pytest.raises(TypeError): provider.register_factory(**kwargs)
def factory_provider(container): provider = FactoryProvider(container=container) container.register_provider(provider) return provider