Example #1
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
Example #2
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'
Example #3
0
def test_scoped():
    provider = ServiceProvider()
    provider.register_scoped(1, lambda: ServiceProvider())
    assert_value_scoped(provider, 1)