Files
bonobo/tests/io/test_json.py
2016-12-27 13:31:38 +01:00

36 lines
946 B
Python

import pytest
from bonobo import Bag, JsonFileWriter
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 = JsonFileWriter(str(file))
context = ComponentExecutionContext(json_writer, None)
context.initialize()
context.recv(BEGIN, Bag({'foo': 'bar'}), END)
context.step()
context.finalize()
assert file.read() == '''[
{"foo": "bar"}
]'''
with pytest.raises(AttributeError):
getattr(context, 'fp')
with pytest.raises(AttributeError):
getattr(context, 'first')
def test_write_json_without_initializer_should_not_work(tmpdir):
file = tmpdir.join('output.json')
json_writer = JsonFileWriter(str(file))
context = ComponentExecutionContext(json_writer, None)
with pytest.raises(AttributeError):
json_writer(context, {'foo': 'bar'})