Esempio n. 1
0
def test_get_many():
    provider = ServiceProvider()
    provider.register_transient('a', lambda ioc: 1)
    provider.register_transient('a', lambda ioc: 2)
    provider.register_transient('a', lambda ioc: 3)

    assert [3, 2, 1] == provider.get_many('a')
Esempio n. 2
0
def test_error_message():
    provider = ServiceProvider()
    provider.register_transient('a', lambda ioc: ioc['b'])
    provider.register_transient('b', lambda ioc: ioc['c'])
    provider.register_transient('c', lambda ioc: ioc['d'])

    with raises(
            ServiceNotFoundError,
            match="unknown service: 'd'; resolve chain: 'a'->'b'->'c'->'d'"):
        provider['a']
Esempio n. 3
0
def test_symbol_caller_frame_from_deep():
    provider = ServiceProvider()

    def get_name(ioc):
        fr = ioc[Symbols.caller_frame]
        mo = inspect.getmodule(fr.frame)
        return mo.__name__

    provider.register_transient('name', get_name)
    assert provider['name'] == 'test_symbols'
Esempio n. 4
0
def test_resolve_group():
    provider = ServiceProvider()
    provider.register_transient('str', lambda: 'name')
    provider.register_transient('int', lambda: 1)
    provider.register_value('float', 1.1)
    group_keys = ['str', 'int']
    provider.register_group('any', group_keys)
    assert provider['any'] == ('name', 1)
    # always transient:
    assert provider['any'] is not provider['any']
    # allow to add later
    group_keys.append('float')
    assert provider['any'] == ('name', 1, 1.1)
def test_():
    from anyioc.ioc_resolver import TypingServiceInfoResolver
    from typing import Tuple, List

    class TestClass:
        pass

    provider = ServiceProvider()
    provider.register_transient(TestClass, TestClass)
    provider.register_value(int, 15)
    provider[Symbols.missing_resolver].append(TypingServiceInfoResolver())

    ret = provider[Tuple[TestClass, int]]
    assert isinstance(ret, tuple)
    assert isinstance(ret[0], TestClass)
    assert ret[1] == 15
Esempio n. 6
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')
Esempio n. 7
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'
Esempio n. 8
0
def test_transient():
    provider = ServiceProvider()
    provider.register_transient(1, lambda: ServiceProvider())
    assert_value_transient(provider, 1)