[stdlib] Fix I/O related nodes (especially json), there were bad bugs with ioformat.
This commit is contained in:
@ -1,9 +1,22 @@
|
||||
import pytest
|
||||
|
||||
from bonobo import Bag, FileReader, FileWriter, open_fs
|
||||
from bonobo import Bag, FileReader, FileWriter
|
||||
from bonobo.constants import BEGIN, END
|
||||
from bonobo.execution.node import NodeExecutionContext
|
||||
from bonobo.util.testing import CapturingNodeExecutionContext
|
||||
from bonobo.util.testing import CapturingNodeExecutionContext, FilesystemTester
|
||||
|
||||
txt_tester = FilesystemTester('txt')
|
||||
txt_tester.input_data = 'Hello\nWorld\n'
|
||||
|
||||
|
||||
def test_file_writer_contextless(tmpdir):
|
||||
fs, filename, services = txt_tester.get_services_for_writer(tmpdir)
|
||||
|
||||
with FileWriter(path=filename).open(fs) as fp:
|
||||
fp.write('Yosh!')
|
||||
|
||||
with fs.open(filename) as fp:
|
||||
assert fp.read() == 'Yosh!'
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@ -14,46 +27,23 @@ from bonobo.util.testing import CapturingNodeExecutionContext
|
||||
]
|
||||
)
|
||||
def test_file_writer_in_context(tmpdir, lines, output):
|
||||
fs, filename = open_fs(tmpdir), 'output.txt'
|
||||
fs, filename, services = txt_tester.get_services_for_writer(tmpdir)
|
||||
|
||||
writer = FileWriter(path=filename)
|
||||
context = NodeExecutionContext(writer, services={'fs': fs})
|
||||
|
||||
context.start()
|
||||
context.write(BEGIN, *map(Bag, lines), END)
|
||||
for _ in range(len(lines)):
|
||||
context.step()
|
||||
context.stop()
|
||||
with NodeExecutionContext(FileWriter(path=filename), services=services) as context:
|
||||
context.write(BEGIN, *map(Bag, lines), END)
|
||||
for _ in range(len(lines)):
|
||||
context.step()
|
||||
|
||||
with fs.open(filename) as fp:
|
||||
assert fp.read() == output
|
||||
|
||||
|
||||
def test_file_writer_out_of_context(tmpdir):
|
||||
fs, filename = open_fs(tmpdir), 'output.txt'
|
||||
def test_file_reader(tmpdir):
|
||||
fs, filename, services = txt_tester.get_services_for_reader(tmpdir)
|
||||
|
||||
writer = FileWriter(path=filename)
|
||||
|
||||
with writer.open(fs) as fp:
|
||||
fp.write('Yosh!')
|
||||
|
||||
with fs.open(filename) as fp:
|
||||
assert fp.read() == 'Yosh!'
|
||||
|
||||
|
||||
def test_file_reader_in_context(tmpdir):
|
||||
fs, filename = open_fs(tmpdir), 'input.txt'
|
||||
|
||||
with fs.open(filename, 'w') as fp:
|
||||
fp.write('Hello\nWorld\n')
|
||||
|
||||
reader = FileReader(path=filename)
|
||||
context = CapturingNodeExecutionContext(reader, services={'fs': fs})
|
||||
|
||||
context.start()
|
||||
context.write(BEGIN, Bag(), END)
|
||||
context.step()
|
||||
context.stop()
|
||||
with CapturingNodeExecutionContext(FileReader(path=filename), services=services) as context:
|
||||
context.write(BEGIN, Bag(), END)
|
||||
context.step()
|
||||
|
||||
assert len(context.send.mock_calls) == 2
|
||||
|
||||
|
||||
Reference in New Issue
Block a user