Пример #1
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'
Пример #2
0
def test_parameters_count():
    provider = ServiceProvider()
    # 0 args
    provider.register_singleton(1, lambda: 101)
    # 1 args
    provider.register_singleton(2, lambda x: 102)
    # 2 args
    with raises(TypeError):
        provider.register_singleton(3, lambda x, y: 103)

    assert provider[1] == 101
    assert provider[2] == 102
Пример #3
0
def test_singleton():
    provider = ServiceProvider()
    provider.register_singleton(1, lambda: ServiceProvider())
    assert_value_singleton(provider, 1)