refactoring contexts, moved json writer to a class as it make more sense.
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
from bonobo import Graph, NaiveStrategy
|
||||
from bonobo import Graph, NaiveStrategy, Bag
|
||||
from bonobo.core.contexts import ExecutionContext
|
||||
from bonobo.util.lifecycle import with_context
|
||||
from bonobo.util.tokens import BEGIN, END
|
||||
|
||||
|
||||
def generate_integers():
|
||||
@ -28,7 +29,7 @@ def test_empty_execution_context():
|
||||
assert not len(ctx.components)
|
||||
assert not len(ctx.plugins)
|
||||
|
||||
assert not ctx.running
|
||||
assert not ctx.alive
|
||||
|
||||
|
||||
def test_execution():
|
||||
@ -52,8 +53,8 @@ def test_simple_execution_context():
|
||||
for i, component in enumerate(chain):
|
||||
assert ctx[i].component is component
|
||||
|
||||
assert not ctx.running
|
||||
assert not ctx.alive
|
||||
|
||||
ctx.impulse()
|
||||
ctx.recv(BEGIN, Bag(), END)
|
||||
|
||||
assert ctx.running
|
||||
assert ctx.alive
|
||||
|
||||
@ -1,21 +1,19 @@
|
||||
import pytest
|
||||
|
||||
from bonobo import to_json
|
||||
from bonobo.util.lifecycle import get_initializer, get_finalizer
|
||||
|
||||
|
||||
class ContextMock:
|
||||
pass
|
||||
from bonobo import to_json, Bag
|
||||
from bonobo.core.contexts import ComponentExecutionContext
|
||||
from bonobo.util.tokens import BEGIN, END
|
||||
|
||||
|
||||
def test_write_json_to_file(tmpdir):
|
||||
file = tmpdir.join('output.json')
|
||||
json_writer = to_json(str(file))
|
||||
context = ContextMock()
|
||||
context = ComponentExecutionContext(json_writer, None)
|
||||
|
||||
get_initializer(json_writer)(context)
|
||||
json_writer(context, {'foo': 'bar'})
|
||||
get_finalizer(json_writer)(context)
|
||||
context.initialize()
|
||||
context.recv(BEGIN, Bag({'foo': 'bar'}), END)
|
||||
context.step()
|
||||
context.finalize()
|
||||
|
||||
assert file.read() == '''[
|
||||
{"foo": "bar"}
|
||||
@ -32,6 +30,6 @@ def test_write_json_without_initializer_should_not_work(tmpdir):
|
||||
file = tmpdir.join('output.json')
|
||||
json_writer = to_json(str(file))
|
||||
|
||||
context = ContextMock()
|
||||
context = ComponentExecutionContext(json_writer, None)
|
||||
with pytest.raises(AttributeError):
|
||||
json_writer(context, {'foo': 'bar'})
|
||||
|
||||
Reference in New Issue
Block a user