Example #1
0
def test_builder_register_inject_by_can_be_str_anno():
    provider = ServiceProvider()
    provider.register_value(int, 10)
    provider.register_value(str, '2')

    @provider.builder.transient('wtf', inject_by='anno')
    def func(f1: int, f2: str):
        return f'{f1}{f2}'

    assert '102' == provider['wtf']
Example #2
0
def test_builder_register_inject_by_can_be_str_name():
    provider = ServiceProvider()
    provider.register_value('f1', 1)
    provider.register_value('f2', 2)

    @provider.builder.transient('wtf', inject_by='name')
    def func(f1, f2):
        return f1 + f2

    assert 3 == provider['wtf']
Example #3
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_type_resolver_with_cache():
    class CLASS:
        def __init__(self, name):
            self.name = name

    provider = ServiceProvider()
    provider.register_value('name', 'some-name')
    with raises(ServiceNotFoundError):
        _ = provider[CLASS]
    tsir = TypesServiceInfoResolver()
    tsir.inject_by = inject_by_name
    provider[Symbols.missing_resolver].append(tsir)
    assert provider[CLASS].name == 'some-name'
    assert provider[CLASS] is not provider[CLASS]
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
def test_chain_resolver():
    class CLASS:
        def __init__(self, name):
            self.name = name

    provider = ServiceProvider()
    provider.register_value('name', 'some-name')
    tsir = TypesServiceInfoResolver()
    tsir.inject_by = inject_by_name
    provider[Symbols.missing_resolver].append(ImportServiceInfoResolver() +
                                              tsir)
    import sys
    assert provider['sys'] is sys
    assert provider[CLASS].name == 'some-name'
    with raises(ServiceNotFoundError):
        _ = provider['unknown-some-wtf-module']
Example #7
0
def test_register_bind():
    provider = ServiceProvider()
    provider.register_value('k', 'value')
    provider.register_bind('b', 'k')
    assert provider['b'] == 'value'
Example #8
0
def test_resolve_value():
    provider = ServiceProvider()
    provider.register_value('k', 'value')
    assert provider['k'] == 'value'