Example #1
0
def test_partial():
    context = Context()

    foo = Resource('foo', __name__)
    bar = Resource('bar', __name__)
    foobar = Resource('foobar', __name__)

    @foo.implement
    @impl.plain
    def foo_impl():
        return 'foo'

    @bar.implement
    @impl.plain
    def bar_impl():
        return 'bar'

    @foobar.implement
    @impl.autowired('foo', foo)
    @impl.autowired('bar', bar)
    @impl.partial
    def foobar_impl(foo_count, bar_count, *, foo, bar):
        return (foo * foo_count) + (bar * bar_count)

    with context.resolve(foobar) as f:
        assert 'foobarbar' == f(1, 2)
        assert 'foofoobarbarbar' == f(2, 3)
Example #2
0
def test_shared_autowire():
    counter = Resource('counter', __name__)
    double = Resource('double', __name__)

    @double.implement
    @impl.autowired('counter', counter)
    @impl.plain
    def double_count(counter):
        return counter * 2

    context = Context()

    count = 0

    @context.provide(counter)
    @shared
    @impl.implementation
    @contextlib.contextmanager
    def increase_and_get_count(resource, context):
        nonlocal count
        count += 1
        yield count

    with context.resolve(double) as value:
        assert 2 == value
        with context.resolve(double) as value:
            assert 2 == value
    with context.resolve(counter) as value:
        assert 2 == value
Example #3
0
def test_not_provided():
    empty = Resource('empty', __name__)

    context = Context()

    with pytest.raises(ResourceNotProvidedError):
        with context.resolve(empty):
            pass
Example #4
0
def test_context():
    @resource.create
    @impl.autowired('context', context_resource)
    @impl.plain
    def get_context(context):
        return context

    context = Context()

    with context.resolve(get_context) as resolved:
        assert context == resolved
Example #5
0
def test_plain():
    hello = Resource('hello', __name__)

    @hello.implement
    @impl.plain
    def get_resource():
        return 'Hello'

    context = Context()

    with context.resolve(hello) as value:
        assert 'Hello' == value
Example #6
0
def test_implement():
    context = Context()
    resource = Resource('test', __name__)

    @resource.implement
    @impl.implementation
    @contextlib.contextmanager
    def resource_impl(resource, context):
        yield 'Test'

    with context.resolve(resource) as value:
        assert 'Test' == value
Example #7
0
def test_resource():
    resource = Resource('resource', __name__)

    @resource.implement
    @impl.implementation
    @contextlib.contextmanager
    def resource_impl(resource, context):
        yield 'Hello'

    context = Context()

    with context.resolve(resource) as value:
        assert 'Hello' == value
Example #8
0
def test_hello():
    hello = Resource('hello', __name__)
    name = Resource('name', __name__)

    @hello.implement
    @impl.implementation
    @contextlib.contextmanager
    def hello_impl(resource, context):
        yield 'Hello'

    @name.implement
    @impl.implementation
    @contextlib.contextmanager
    def name_impl(resource, context):
        yield 'John'

    context = Context()

    @context.partial(hello, name=name)
    def say(hello, by, name='World'):
        return '{hello}, {name}! by {by}'.format(
            hello=hello,
            name=name,
            by=by,
        )

    assert "Hello, John! by Rose" == say('Rose')
    assert "Hello, John! by Amy" == say('Amy')
Example #9
0
def test_create():
    context = Context()

    @resource.create
    @impl.plain
    def foo():
        return 'foo'

    @resource.create
    @impl.autowired(foo)
    @impl.plain
    def bar(foo):
        return 'bar-' + foo

    with context.resolve(foo) as value:
        assert 'foo' == value

    with context.resolve(bar) as value:
        assert 'bar-foo' == value
Example #10
0
def test_plain():
    context = Context()
    foo = Resource('foo', __name__)
    bar = Resource('bar', __name__)

    @foo.implement
    @impl.plain
    def foo_impl():
        return 'foo'

    @bar.implement
    @impl.autowired('foo', foo)
    @impl.plain
    def bar_impl(foo):
        return 'bar-' + foo

    with context.resolve(foo) as value:
        assert 'foo' == value

    with context.resolve(bar) as value:
        assert 'bar-foo' == value
Example #11
0
def test_autowire_multiple():
    @resource.create
    @impl.plain
    def foo():
        return 'foo'

    @resource.create
    @impl.plain
    def bar():
        return 'bar'

    @resource.create
    @impl.autowired('foo', foo)
    @impl.autowired('bar', bar)
    @impl.plain
    def baz(foo, bar):
        return '{}-{}-baz'.format(foo, bar)

    context = Context()
    with context.resolve(baz) as value:
        assert 'foo-bar-baz' == value
Example #12
0
def test_autowire():
    config_dir = Resource('config_dir', __name__)
    db_config = Resource('db_config', __name__)

    @db_config.implement
    @impl.autowired('config_dir', config_dir)
    @impl.contextmanager
    def db_config_path(config_dir):
        yield os.path.join(config_dir, 'database.yml')

    dev_context = Context()
    prod_context = Context()

    @dev_context.provide(config_dir)
    @impl.plain
    def get_dev_config_dir():
        return './config/'

    @prod_context.provide(config_dir)
    @impl.plain
    def get_prod_config_dir():
        return '/etc/autowire/'

    with dev_context.resolve(db_config) as dev_db_config:
        assert os.path.join(get_dev_config_dir(), 'database.yml') == \
            dev_db_config

    with prod_context.resolve(db_config) as prod_db_config:
        assert os.path.join(get_prod_config_dir(), 'database.yml') == \
            prod_db_config
Example #13
0
def test_shared():
    counter = Resource('counter', __name__)

    context = Context()

    count = 0

    @context.provide(counter)
    @shared
    @impl.implementation
    @contextlib.contextmanager
    def increase_and_get_count(resource, context):
        nonlocal count
        count += 1
        yield count

    with context.resolve(counter) as value:
        assert 1 == value
        with context.resolve(counter) as value:
            assert 1 == value
    with context.resolve(counter) as value:
        assert 2 == value
Example #14
0
def test_globally_shared_failure():
    number = Resource('number', __name__)

    context = Context()

    @resource.create
    @shared(globally=True)
    @impl.autowired(number)
    @impl.plain
    def double(number):
        return number * 2

    child = Context(context)

    @child.provide(number)
    @impl.plain
    def get_one(resource, context):
        return 1

    with pytest.raises(ResourceNotProvidedError):
        with child.resolve(double):
            pass
Example #15
0
def test_contextual():
    context = Context()
    foo = Resource('foo', __name__)
    bar = Resource('bar', __name__)

    @foo.implement
    @impl.contextual
    @contextlib.contextmanager
    def foo_impl():
        yield 'foo'

    @bar.implement
    @impl.autowired('foo', foo)
    @impl.contextmanager
    def bar_impl(foo):
        yield 'bar-' + foo

    with context.resolve(foo) as value:
        assert 'foo' == value

    with context.resolve(bar) as value:
        assert 'bar-foo' == value
Example #16
0
def test_provide():
    env = Resource('env', __name__)

    parent = Context()
    child = Context(parent)

    @impl.plain
    def get_env():
        return 'development'

    child.provide(env, get_env)

    with pytest.raises(ResourceNotProvidedError):
        with parent.resolve(env):
            pass

    with child.resolve(env) as value:
        assert 'development' == value
Example #17
0
def test_scope():
    level = Resource('level', __name__)

    parent = Context()
    child = Context(parent)

    @parent.provide(level)
    @impl.plain
    def parent_level():
        return 1

    @child.provide(level)
    @impl.plain
    def child_level():
        return 2

    with parent.resolve(level) as parent_value, \
            child.resolve(level) as child_value:
        assert 1 == parent_value
        assert 2 == child_value
Example #18
0
def test_globally_shared():
    number = Resource('number', __name__)

    context = Context()

    counter = 0

    @number.implement
    @shared(globally=True)
    @impl.implementation
    @contextlib.contextmanager
    def get_next_number(resource, context):
        nonlocal counter
        counter += 1
        yield counter

    child = Context(context)

    with child.resolve(number) as value1, context.resolve(number) as value2:
        assert 1 == value1
        assert 1 == value2
Example #19
0
def test_partial():
    name = Resource('name', __name__)
    env = Resource('env', __name__)

    context = Context()

    @impl.plain
    def get_env():
        return 'development'

    @impl.plain
    def get_name():
        return 'name'

    context.provide(name, get_name)
    context.provide(env, get_env)

    @context.partial(name, env=env)
    def runnable(name, phrase, env):
        return "{}, {}-{}!".format(phrase, name, env)

    assert 'Hello, name-development!' == runnable("Hello")