Big refactoring, way simpler management of transformations. Early branch for upcoming version 0.2.
This commit is contained in:
@ -1,21 +1,22 @@
|
||||
import pytest
|
||||
|
||||
from bonobo import Bag, CsvReader, CsvWriter
|
||||
from bonobo.core.contexts import ComponentExecutionContext
|
||||
from bonobo.util.testing import CapturingComponentExecutionContext
|
||||
from bonobo.context.execution import NodeExecutionContext
|
||||
from bonobo.util.testing import CapturingNodeExecutionContext
|
||||
from bonobo.util.tokens import BEGIN, END
|
||||
|
||||
|
||||
def test_write_csv_to_file(tmpdir):
|
||||
file = tmpdir.join('output.json')
|
||||
writer = CsvWriter(str(file))
|
||||
context = ComponentExecutionContext(writer, None)
|
||||
writer = CsvWriter(path=str(file))
|
||||
context = NodeExecutionContext(writer, None)
|
||||
|
||||
context.initialize()
|
||||
context.recv(BEGIN, Bag({'foo': 'bar'}), Bag({'foo': 'baz', 'ignore': 'this'}), END)
|
||||
|
||||
context.start()
|
||||
context.step()
|
||||
context.step()
|
||||
context.finalize()
|
||||
context.stop()
|
||||
|
||||
assert file.read() == 'foo\nbar\nbaz\n'
|
||||
|
||||
@ -23,27 +24,18 @@ def test_write_csv_to_file(tmpdir):
|
||||
getattr(context, 'file')
|
||||
|
||||
|
||||
def test_write_json_without_initializer_should_not_work(tmpdir):
|
||||
file = tmpdir.join('output.json')
|
||||
writer = CsvWriter(str(file))
|
||||
|
||||
context = ComponentExecutionContext(writer, None)
|
||||
with pytest.raises(AttributeError):
|
||||
writer(context, {'foo': 'bar'})
|
||||
|
||||
|
||||
def test_read_csv_from_file(tmpdir):
|
||||
file = tmpdir.join('input.csv')
|
||||
file.write('a,b,c\na foo,b foo,c foo\na bar,b bar,c bar')
|
||||
|
||||
reader = CsvReader(str(file), delimiter=',')
|
||||
reader = CsvReader(path=str(file), delimiter=',')
|
||||
|
||||
context = CapturingComponentExecutionContext(reader, None)
|
||||
context = CapturingNodeExecutionContext(reader, None)
|
||||
|
||||
context.initialize()
|
||||
context.start()
|
||||
context.recv(BEGIN, Bag(), END)
|
||||
context.step()
|
||||
context.finalize()
|
||||
context.stop()
|
||||
|
||||
assert len(context.send.mock_calls) == 2
|
||||
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
import pytest
|
||||
|
||||
from bonobo import FileWriter, Bag, FileReader
|
||||
from bonobo.core.contexts import ComponentExecutionContext
|
||||
from bonobo.util.testing import CapturingComponentExecutionContext
|
||||
from bonobo.context.execution import NodeExecutionContext
|
||||
from bonobo.util.testing import CapturingNodeExecutionContext
|
||||
from bonobo.util.tokens import BEGIN, END
|
||||
|
||||
|
||||
@ -16,27 +16,24 @@ from bonobo.util.tokens import BEGIN, END
|
||||
def test_file_writer_in_context(tmpdir, lines, output):
|
||||
file = tmpdir.join('output.txt')
|
||||
|
||||
writer = FileWriter(str(file))
|
||||
context = ComponentExecutionContext(writer, None)
|
||||
writer = FileWriter(path=str(file))
|
||||
context = NodeExecutionContext(writer, None)
|
||||
|
||||
context.initialize()
|
||||
context.start()
|
||||
context.recv(BEGIN, *map(Bag, lines), END)
|
||||
for i in range(len(lines)):
|
||||
context.step()
|
||||
context.finalize()
|
||||
context.stop()
|
||||
|
||||
assert file.read() == output
|
||||
|
||||
with pytest.raises(AttributeError):
|
||||
getattr(context, 'file')
|
||||
|
||||
|
||||
def test_file_writer_out_of_context(tmpdir):
|
||||
file = tmpdir.join('output.txt')
|
||||
writer = FileWriter(str(file))
|
||||
fp = writer.open()
|
||||
fp.write('Yosh!')
|
||||
writer.close(fp)
|
||||
writer = FileWriter(path=str(file))
|
||||
|
||||
with writer.open() as fp:
|
||||
fp.write('Yosh!')
|
||||
|
||||
assert file.read() == 'Yosh!'
|
||||
|
||||
@ -45,13 +42,13 @@ def test_file_reader_in_context(tmpdir):
|
||||
file = tmpdir.join('input.txt')
|
||||
file.write('Hello\nWorld\n')
|
||||
|
||||
reader = FileReader(str(file))
|
||||
context = CapturingComponentExecutionContext(reader, None)
|
||||
reader = FileReader(path=str(file))
|
||||
context = CapturingNodeExecutionContext(reader, None)
|
||||
|
||||
context.initialize()
|
||||
context.start()
|
||||
context.recv(BEGIN, Bag(), END)
|
||||
context.step()
|
||||
context.finalize()
|
||||
context.stop()
|
||||
|
||||
assert len(context.send.mock_calls) == 2
|
||||
|
||||
|
||||
@ -1,20 +1,21 @@
|
||||
import pytest
|
||||
|
||||
from bonobo import Bag, JsonWriter, JsonReader
|
||||
from bonobo.core.contexts import ComponentExecutionContext
|
||||
from bonobo.util.testing import CapturingComponentExecutionContext
|
||||
from bonobo.context.execution import NodeExecutionContext
|
||||
from bonobo.util.objects import ValueHolder
|
||||
from bonobo.util.testing import CapturingNodeExecutionContext
|
||||
from bonobo.util.tokens import BEGIN, END
|
||||
|
||||
|
||||
def test_write_json_to_file(tmpdir):
|
||||
file = tmpdir.join('output.json')
|
||||
writer = JsonWriter(str(file))
|
||||
context = ComponentExecutionContext(writer, None)
|
||||
writer = JsonWriter(path=str(file))
|
||||
context = NodeExecutionContext(writer, None)
|
||||
|
||||
context.initialize()
|
||||
context.start()
|
||||
context.recv(BEGIN, Bag({'foo': 'bar'}), END)
|
||||
context.step()
|
||||
context.finalize()
|
||||
context.stop()
|
||||
|
||||
assert file.read() == '[\n{"foo": "bar"}\n]'
|
||||
|
||||
@ -25,26 +26,17 @@ def test_write_json_to_file(tmpdir):
|
||||
getattr(context, 'first')
|
||||
|
||||
|
||||
def test_write_json_without_initializer_should_not_work(tmpdir):
|
||||
file = tmpdir.join('output.json')
|
||||
writer = JsonWriter(str(file))
|
||||
|
||||
context = ComponentExecutionContext(writer, None)
|
||||
with pytest.raises(AttributeError):
|
||||
writer(context, {'foo': 'bar'})
|
||||
|
||||
|
||||
def test_read_json_from_file(tmpdir):
|
||||
file = tmpdir.join('input.json')
|
||||
file.write('[{"x": "foo"},{"x": "bar"}]')
|
||||
reader = JsonReader(str(file))
|
||||
reader = JsonReader(path=str(file))
|
||||
|
||||
context = CapturingComponentExecutionContext(reader, None)
|
||||
context = CapturingNodeExecutionContext(reader, None)
|
||||
|
||||
context.initialize()
|
||||
context.start()
|
||||
context.recv(BEGIN, Bag(), END)
|
||||
context.step()
|
||||
context.finalize()
|
||||
context.stop()
|
||||
|
||||
assert len(context.send.mock_calls) == 2
|
||||
|
||||
|
||||
Reference in New Issue
Block a user