예제 #1
0
def test_argument_ioc_at_scoped():
    root_provider = ServiceProvider()
    with root_provider.scope() as scoped_provider:

        def singleton_func(ioc):
            # when you register in scoped_provider,
            # value should singleton base on scoped_provider.
            assert ioc is scoped_provider
            return 'singleton'

        def scoped_func(ioc):
            assert ioc is scoped_provider
            return 'scoped'

        def transient_func(ioc):
            assert ioc is scoped_provider
            return 'transient'

        scoped_provider.register_singleton(1, singleton_func)
        scoped_provider.register_scoped(2, scoped_func)
        scoped_provider.register_transient(3, transient_func)

        assert scoped_provider[1] == 'singleton'
        assert scoped_provider[2] == 'scoped'
        assert scoped_provider[3] == 'transient'
예제 #2
0
def assert_value_transient(provider: ServiceProvider, key):
    scoped_1 = provider.scope()
    scoped_2 = provider.scope()
    scoped_1_1 = scoped_1.scope()

    for l, r in itertools.combinations_with_replacement(
        [provider, scoped_1, scoped_2, scoped_1_1], 2):
        assert l.get(key) is not r.get(key)
예제 #3
0
def test_symbols_values_ref():
    from anyioc.symbols import Symbols

    provider = ServiceProvider()

    assert provider[Symbols.provider] is provider
    assert provider[Symbols.provider_root] is provider
    assert provider[Symbols.cache] is provider[Symbols.cache]

    with provider.scope() as scoped_provider:
        assert scoped_provider[Symbols.provider] is scoped_provider
        assert scoped_provider[Symbols.provider_root] is provider
        assert scoped_provider[Symbols.cache] is scoped_provider[Symbols.cache]
        assert scoped_provider[Symbols.cache] is not provider[Symbols.cache]
예제 #4
0
def test_options_auto_enter():
    provider = ServiceProvider(auto_enter=True)

    class ContextManager:
        value = 0

        def __enter__(self):
            self.value = 1
            return self

        def __exit__(self, exc_type, exc_val, exc_tb):
            self.value = 2

    provider.register_scoped('mgr', ContextManager)
    with provider.scope() as scoped_provider:
        mgr = scoped_provider['mgr']
        assert mgr.value == 1
    assert mgr.value == 2
예제 #5
0
def test_get_many_from_multilevel():
    provider = ServiceProvider()
    provider.register_transient('a', lambda ioc: 10)
    provider.register_transient('a', lambda ioc: 11)

    provider2 = provider.scope()
    provider2.register_transient('a', lambda ioc: 20)
    provider2.register_transient('a', lambda ioc: 21)

    provider3 = provider2.scope()
    provider3.register_transient('a', lambda ioc: 30)
    provider3.register_transient('a', lambda ioc: 31)

    provider4 = provider3.scope()
    provider4.register_transient('a', lambda ioc: 40)
    provider4.register_transient('a', lambda ioc: 41)

    assert [31, 30, 21, 20, 11, 10] == provider3.get_many('a')
예제 #6
0
def test_symbols_types():
    from anyioc.symbols import Symbols
    from anyioc.ioc import IServiceProvider
    from anyioc.ioc_resolver import IServiceInfoResolver

    provider = ServiceProvider()

    assert isinstance(provider[Symbols.provider], IServiceProvider)
    assert isinstance(provider[Symbols.provider_root], IServiceProvider)
    assert isinstance(provider[Symbols.cache], dict)
    assert isinstance(provider[Symbols.missing_resolver], IServiceInfoResolver)

    with provider.scope() as scoped_provider:
        assert isinstance(scoped_provider[Symbols.provider], IServiceProvider)
        assert isinstance(scoped_provider[Symbols.provider_root],
                          IServiceProvider)
        assert isinstance(scoped_provider[Symbols.cache], dict)
        assert isinstance(scoped_provider[Symbols.missing_resolver],
                          IServiceInfoResolver)
예제 #7
0
def test_predefined_keys():
    map_to_self_keys = (
        # str
        'ioc', 'provider', 'service_provider',
        # type
        ServiceProvider, IServiceProvider,
        # symbol
        Symbols.provider
    )

    provider = ServiceProvider()
    for k in map_to_self_keys:
        assert provider is provider[k]

    with provider.scope() as scoped:
        for k in map_to_self_keys:
            assert scoped is scoped[k]

        with scoped.scope() as deep_scoped:
            for k in map_to_self_keys:
                assert deep_scoped is deep_scoped[k]
예제 #8
0
def test_argument_ioc_at_root():
    root_provider = ServiceProvider()
    with root_provider.scope() as scoped_provider:

        def singleton_func(ioc):
            assert ioc is root_provider
            return 'singleton'

        def scoped_func(ioc):
            assert ioc is scoped_provider
            return 'scoped'

        def transient_func(ioc):
            assert ioc is scoped_provider
            return 'transient'

        root_provider.register_singleton(1, singleton_func)
        root_provider.register_scoped(2, scoped_func)
        root_provider.register_transient(3, transient_func)

        assert scoped_provider[1] == 'singleton'
        assert scoped_provider[2] == 'scoped'
        assert scoped_provider[3] == 'transient'
예제 #9
0
def test_types():
    # since scoped is scoped[ServiceProvider]
    provider = ServiceProvider()
    with provider.scope() as scoped:
        assert isinstance(scoped, ServiceProvider)