def test_implementation_permanent_singleton(service: ServiceProvider, singleton: bool, permanent: bool): scope = Scope.singleton() if singleton else None choice = 'a' def implementation(): return dict(a=A, b=B)[choice] service.register(A, scope=scope) service.register(B, scope=scope) indirect = IndirectProvider() impl = indirect.register_implementation(Interface, implementation, permanent=permanent) instance = world.test.maybe_provide_from(indirect, impl).unwrapped assert isinstance(instance, A) assert (instance is world.get(A)) is singleton assert world.test.maybe_provide_from( indirect, impl).is_singleton() is (singleton and permanent) choice = 'b' assert implementation() == B assert world.test.maybe_provide_from( indirect, impl).is_singleton() is (singleton and permanent) instance = world.test.maybe_provide_from(indirect, impl).unwrapped if permanent: assert isinstance(instance, A) assert (instance is world.get(A)) is singleton else: assert isinstance(instance, B) assert (instance is world.get(B)) is singleton
def test_register(singleton: bool): with world.test.empty(): provider = ServiceProvider() provider.register(A, scope=Scope.singleton() if singleton else None) assert world.test.maybe_provide_from(provider, A).is_singleton() is singleton assert isinstance( world.test.maybe_provide_from(provider, A).unwrapped, A)
def test_lazy(provider: FactoryProvider): world.test.singleton('A', build) factory_id = provider.register(A, factory_dependency='A', scope=Scope.singleton()) assert isinstance(world.get(factory_id), A) # singleton assert world.get(factory_id) is world.get(factory_id)
def test_factory_singleton(): with world.test.empty(): @world.test.factory(Service) def build(): return Service() assert world.get(Service) is world.get(Service) with world.test.empty(): @world.test.factory(Service, singleton=True) def build2(): return Service() assert world.get(Service) is world.get(Service) with world.test.empty(): @world.test.factory(Service, scope=Scope.singleton()) def build3(): return Service() assert world.get(Service) is world.get(Service)
@pytest.fixture(autouse=True) def empty_world(): with world.test.empty(): yield def test_new(): s = world.scopes.new(name="test") assert isinstance(s, Scope) assert s.name == "test" @pytest.mark.parametrize('expectation,name', [ (pytest.raises(TypeError, match=".*name.*str.*"), object()), (pytest.raises(ValueError, match=".*reserved.*"), Scope.singleton().name), (pytest.raises(ValueError, match=".*reserved.*"), Scope.sentinel().name), (pytest.raises(ValueError, match=".*empty.*"), "") ]) def test_invalid_new_scope_name(expectation, name): with expectation: world.scopes.new(name=name) def test_no_duplicate_scope(): world.scopes.new(name='dummy') with pytest.raises(ValueError, match=".*already exists.*"): world.scopes.new(name='dummy') def test_reset():
from contextlib import contextmanager import pytest from antidote import Scope from antidote.utils import validated_scope dummy_scope = Scope('dummy') @contextmanager def does_not_raise(): yield @pytest.mark.parametrize('expectation, kwargs', [ pytest.param(pytest.raises(TypeError, match='.*scope.*'), dict(scope=object(), default=None), id='scope=object'), pytest.param(pytest.raises(TypeError, match='.*default.*'), dict(scope=None, default=object()), id='default=object'), pytest.param(pytest.raises(TypeError, match='.*singleton.*'), dict(scope=Scope.sentinel(), singleton=object(), default=None), id='singleton=object'), pytest.param(pytest.raises(TypeError, match='.*both.*'), dict(scope=None, singleton=False, default=None), id='singleton & scope'), ]) def test_invalid_validated_scope(expectation, kwargs):
from antidote import Scope, world from antidote._providers.service import Parameterized, ServiceProvider from antidote.exceptions import (DependencyNotFoundError, DuplicateDependencyError, FrozenWorldError) @pytest.fixture def provider(): with world.test.empty(): world.provider(ServiceProvider) yield world.get(ServiceProvider) @pytest.fixture(params=[ pytest.param(None, id='scope=None'), pytest.param(Scope.singleton(), id='scope=singleton') ]) def scope(request): return request.param class KeepInit: def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs class A(KeepInit): pass
def __init__(self, value, singleton=False): self.value = value self.scope = Scope.singleton() if singleton else None
def f4(dependency: Hashable) -> Optional[DependencyValue]: if dependency == 'test': return DependencyValue(object(), scope=Scope.singleton())
def test_singleton(provider: FactoryProvider, singleton: bool, factory_params: dict): scope = Scope.singleton() if singleton else None factory_id = provider.register(A, scope=scope, **factory_params) assert isinstance(world.get(factory_id), A) assert (world.get(factory_id) is world.get(factory_id)) == singleton
def test_simple(provider: FactoryProvider): factory_id = provider.register(A, factory=build, scope=Scope.singleton()) assert isinstance(world.get(factory_id), A) # singleton assert world.get(factory_id) is world.get(factory_id)