First implementation of services and basic injection. Not working with CLI for now.

This commit is contained in:
Romain Dorgueil
2017-04-25 22:04:21 +02:00
parent 18abb39206
commit efcd4361cc
41 changed files with 538 additions and 324 deletions

View File

@ -1,7 +1,6 @@
from operator import attrgetter
from bonobo import contextual, ContextProcessor
from bonobo.context.processors import get_context_processors
from bonobo.config.processors import ContextProcessor, contextual, resolve_processors
@contextual
@ -46,7 +45,7 @@ class CP3(CP2):
def get_all_processors_names(cls):
return list(map(attrgetter('__name__'), get_context_processors(cls)))
return list(map(attrgetter('__name__'), resolve_processors(cls)))
def test_inheritance_and_ordering():

View File

@ -1,24 +0,0 @@
from bonobo import inject, service
class MyFoo():
pass
def test_service_is_singleton():
@service
def foo():
return MyFoo()
assert foo() is foo()
@inject(foo)
def bar(myfoo):
assert myfoo is foo()
bar()
foo2 = foo.define()
assert type(foo()) == type(foo2())
assert foo2() is not foo()

View File

@ -2,7 +2,7 @@ import pytest
from bonobo import Bag, CsvReader, CsvWriter
from bonobo.constants import BEGIN, END
from bonobo.context.execution import NodeExecutionContext
from bonobo.execution import NodeExecutionContext
from bonobo.util.testing import CapturingNodeExecutionContext

View File

@ -1,15 +1,15 @@
import pytest
from bonobo import FileWriter, Bag, FileReader
from bonobo.context.execution import NodeExecutionContext
from bonobo.util.testing import CapturingNodeExecutionContext
from bonobo import Bag, FileReader, FileWriter
from bonobo.constants import BEGIN, END
from bonobo.execution import NodeExecutionContext
from bonobo.util.testing import CapturingNodeExecutionContext
@pytest.mark.parametrize(
'lines,output',
[
(('ACME', ), 'ACME'), # one line...
(('ACME',), 'ACME'), # one line...
(('Foo', 'Bar', 'Baz'), 'Foo\nBar\nBaz'), # more than one line...
]
)

View File

@ -1,8 +1,8 @@
import pytest
from bonobo import Bag, JsonWriter, JsonReader
from bonobo import Bag, JsonReader, JsonWriter
from bonobo.constants import BEGIN, END
from bonobo.context.execution import NodeExecutionContext
from bonobo.execution import NodeExecutionContext
from bonobo.util.testing import CapturingNodeExecutionContext

View File

@ -1,11 +1,12 @@
import pytest
import bonobo as bb
import bonobo.basics
@pytest.mark.timeout(2)
def test_run_graph_noop():
graph = bb.Graph(bb.noop)
graph = bb.Graph(bonobo.basics.noop)
assert len(graph) == 1
result = bb.run(graph, strategy='threadpool')

View File

@ -1,6 +1,8 @@
import pytest
from bonobo import Configurable, Option
from bonobo.config.configurables import Configurable
from bonobo.config.options import Option
from bonobo.config.services import Container, Service, validate_service_name
class MyConfigurable(Configurable):
@ -17,6 +19,26 @@ class MyBetterConfigurable(MyConfigurable):
required_str = Option(str, required=False, default='kaboom')
class PrinterInterface():
def print(self, *args):
raise NotImplementedError()
class ConcretePrinter(PrinterInterface):
def __init__(self, prefix):
self.prefix = prefix
def print(self, *args):
return ';'.join((self.prefix, *args))
class MyServiceDependantConfigurable(Configurable):
printer = Service(PrinterInterface, )
def __call__(self, printer: PrinterInterface, *args):
return printer.print(*args)
def test_missing_required_option_error():
with pytest.raises(TypeError) as exc:
MyConfigurable()
@ -75,3 +97,37 @@ def test_option_resolution_order():
assert o.required_str == 'kaboom'
assert o.default_str == 'foo'
assert o.integer == None
def test_service_name_validator():
assert validate_service_name('foo') == 'foo'
assert validate_service_name('foo.bar') == 'foo.bar'
assert validate_service_name('Foo') == 'Foo'
assert validate_service_name('Foo.Bar') == 'Foo.Bar'
assert validate_service_name('Foo.a0') == 'Foo.a0'
with pytest.raises(ValueError):
validate_service_name('foo.0')
with pytest.raises(ValueError):
validate_service_name('0.foo')
SERVICES = Container(
printer0=ConcretePrinter(prefix='0'),
printer1=ConcretePrinter(prefix='1'),
)
def test_service_dependency():
o = MyServiceDependantConfigurable(printer='printer0')
assert o(SERVICES.get('printer0'), 'foo', 'bar') == '0;foo;bar'
assert o(SERVICES.get('printer1'), 'bar', 'baz') == '1;bar;baz'
assert o(*SERVICES.args_for(o), 'foo', 'bar') == '0;foo;bar'
def test_service_dependency_unavailable():
o = MyServiceDependantConfigurable(printer='printer2')
with pytest.raises(KeyError):
SERVICES.args_for(o)

View File

@ -1,6 +1,7 @@
from bonobo import Graph, NaiveStrategy, Bag, contextual
from bonobo import Graph, NaiveStrategy, Bag
from bonobo.config.processors import contextual
from bonobo.constants import BEGIN, END
from bonobo.context.execution import GraphExecutionContext
from bonobo.execution import GraphExecutionContext
def generate_integers():