refactoring for better testability
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
from bonobo import Graph
|
||||
from bonobo import Graph, NaiveStrategy
|
||||
from bonobo.core.contexts import ExecutionContext
|
||||
from bonobo.util.lifecycle import with_context
|
||||
|
||||
|
||||
def generate_integers():
|
||||
@ -10,6 +11,16 @@ def square(i: int) -> int:
|
||||
return i**2
|
||||
|
||||
|
||||
@with_context
|
||||
def push_result(ctx, i: int):
|
||||
if not hasattr(ctx.parent, 'results'):
|
||||
ctx.parent.results = []
|
||||
ctx.parent.results.append(i)
|
||||
|
||||
|
||||
chain = (generate_integers, square, push_result)
|
||||
|
||||
|
||||
def test_empty_execution_context():
|
||||
graph = Graph()
|
||||
|
||||
@ -20,15 +31,29 @@ def test_empty_execution_context():
|
||||
assert not ctx.running
|
||||
|
||||
|
||||
def test_execution():
|
||||
graph = Graph()
|
||||
graph.add_chain(*chain)
|
||||
|
||||
strategy = NaiveStrategy()
|
||||
ctx = strategy.execute(graph)
|
||||
|
||||
assert ctx.results == [1, 4, 9, 16, 25, 36, 49, 64, 81]
|
||||
|
||||
|
||||
def test_simple_execution_context():
|
||||
graph = Graph()
|
||||
graph.add_chain(generate_integers, square)
|
||||
graph.add_chain(*chain)
|
||||
|
||||
ctx = ExecutionContext(graph)
|
||||
assert len(ctx.components) == 2
|
||||
assert len(ctx.components) == len(chain)
|
||||
assert not len(ctx.plugins)
|
||||
|
||||
assert ctx[0].component is generate_integers
|
||||
assert ctx[1].component is square
|
||||
for i, component in enumerate(chain):
|
||||
assert ctx[i].component is component
|
||||
|
||||
assert not ctx.running
|
||||
|
||||
ctx.impulse()
|
||||
|
||||
assert ctx.running
|
||||
|
||||
Reference in New Issue
Block a user