wip: filesystem as a service.
This commit is contained in:
@ -1,15 +1,16 @@
|
||||
import pytest
|
||||
|
||||
from bonobo import Bag, CsvReader, CsvWriter
|
||||
from bonobo import Bag, CsvReader, CsvWriter, open_fs
|
||||
from bonobo.constants import BEGIN, END
|
||||
from bonobo.execution.node import NodeExecutionContext
|
||||
from bonobo.util.testing import CapturingNodeExecutionContext
|
||||
|
||||
|
||||
def test_write_csv_to_file(tmpdir):
|
||||
file = tmpdir.join('output.json')
|
||||
writer = CsvWriter(path=str(file))
|
||||
context = NodeExecutionContext(writer, None)
|
||||
fs, filename = open_fs(tmpdir), 'output.csv'
|
||||
|
||||
writer = CsvWriter(path=filename)
|
||||
context = NodeExecutionContext(writer, services={'fs': fs})
|
||||
|
||||
context.recv(BEGIN, Bag({'foo': 'bar'}), Bag({'foo': 'baz', 'ignore': 'this'}), END)
|
||||
|
||||
@ -18,19 +19,19 @@ def test_write_csv_to_file(tmpdir):
|
||||
context.step()
|
||||
context.stop()
|
||||
|
||||
assert file.read() == 'foo\nbar\nbaz\n'
|
||||
assert fs.open(filename).read() == 'foo\nbar\nbaz\n'
|
||||
|
||||
with pytest.raises(AttributeError):
|
||||
getattr(context, 'file')
|
||||
|
||||
|
||||
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')
|
||||
fs, filename = open_fs(tmpdir), 'input.csv'
|
||||
fs.open(filename, 'w').write('a,b,c\na foo,b foo,c foo\na bar,b bar,c bar')
|
||||
|
||||
reader = CsvReader(path=str(file), delimiter=',')
|
||||
reader = CsvReader(path=filename, delimiter=',')
|
||||
|
||||
context = CapturingNodeExecutionContext(reader, None)
|
||||
context = CapturingNodeExecutionContext(reader, services={'fs': fs})
|
||||
|
||||
context.start()
|
||||
context.recv(BEGIN, Bag(), END)
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
|
||||
from bonobo import Bag, FileReader, FileWriter
|
||||
from bonobo import Bag, FileReader, FileWriter, open_fs
|
||||
from bonobo.constants import BEGIN, END
|
||||
from bonobo.execution.node import NodeExecutionContext
|
||||
from bonobo.util.testing import CapturingNodeExecutionContext
|
||||
@ -14,10 +14,10 @@ from bonobo.util.testing import CapturingNodeExecutionContext
|
||||
]
|
||||
)
|
||||
def test_file_writer_in_context(tmpdir, lines, output):
|
||||
file = tmpdir.join('output.txt')
|
||||
fs, filename = open_fs(tmpdir), 'output.txt'
|
||||
|
||||
writer = FileWriter(path=str(file))
|
||||
context = NodeExecutionContext(writer, None)
|
||||
writer = FileWriter(path=filename)
|
||||
context = NodeExecutionContext(writer, services={'fs': fs})
|
||||
|
||||
context.start()
|
||||
context.recv(BEGIN, *map(Bag, lines), END)
|
||||
@ -25,25 +25,27 @@ def test_file_writer_in_context(tmpdir, lines, output):
|
||||
context.step()
|
||||
context.stop()
|
||||
|
||||
assert file.read() == output
|
||||
assert fs.open(filename).read() == output
|
||||
|
||||
|
||||
def test_file_writer_out_of_context(tmpdir):
|
||||
file = tmpdir.join('output.txt')
|
||||
writer = FileWriter(path=str(file))
|
||||
fs, filename = open_fs(tmpdir), 'output.txt'
|
||||
|
||||
with writer.open() as fp:
|
||||
writer = FileWriter(path=filename)
|
||||
|
||||
with writer.open(fs) as fp:
|
||||
fp.write('Yosh!')
|
||||
|
||||
assert file.read() == 'Yosh!'
|
||||
assert fs.open(filename).read() == 'Yosh!'
|
||||
|
||||
|
||||
def test_file_reader_in_context(tmpdir):
|
||||
file = tmpdir.join('input.txt')
|
||||
file.write('Hello\nWorld\n')
|
||||
fs, filename = open_fs(tmpdir), 'input.txt'
|
||||
|
||||
reader = FileReader(path=str(file))
|
||||
context = CapturingNodeExecutionContext(reader, None)
|
||||
fs.open(filename, 'w').write('Hello\nWorld\n')
|
||||
|
||||
reader = FileReader(path=filename)
|
||||
context = CapturingNodeExecutionContext(reader, services={'fs': fs})
|
||||
|
||||
context.start()
|
||||
context.recv(BEGIN, Bag(), END)
|
||||
|
||||
@ -1,22 +1,23 @@
|
||||
import pytest
|
||||
|
||||
from bonobo import Bag, JsonReader, JsonWriter
|
||||
from bonobo import Bag, JsonReader, JsonWriter, open_fs
|
||||
from bonobo.constants import BEGIN, END
|
||||
from bonobo.execution.node import NodeExecutionContext
|
||||
from bonobo.util.testing import CapturingNodeExecutionContext
|
||||
|
||||
|
||||
def test_write_json_to_file(tmpdir):
|
||||
file = tmpdir.join('output.json')
|
||||
writer = JsonWriter(path=str(file))
|
||||
context = NodeExecutionContext(writer, None)
|
||||
fs, filename = open_fs(tmpdir), 'output.json'
|
||||
|
||||
writer = JsonWriter(path=filename)
|
||||
context = NodeExecutionContext(writer, services={'fs': fs})
|
||||
|
||||
context.start()
|
||||
context.recv(BEGIN, Bag({'foo': 'bar'}), END)
|
||||
context.step()
|
||||
context.stop()
|
||||
|
||||
assert file.read() == '[\n{"foo": "bar"}\n]'
|
||||
assert fs.open(filename).read() == '[\n{"foo": "bar"}\n]'
|
||||
|
||||
with pytest.raises(AttributeError):
|
||||
getattr(context, 'file')
|
||||
@ -26,11 +27,11 @@ def test_write_json_to_file(tmpdir):
|
||||
|
||||
|
||||
def test_read_json_from_file(tmpdir):
|
||||
file = tmpdir.join('input.json')
|
||||
file.write('[{"x": "foo"},{"x": "bar"}]')
|
||||
reader = JsonReader(path=str(file))
|
||||
fs, filename = open_fs(tmpdir), 'input.json'
|
||||
fs.open(filename, 'w').write('[{"x": "foo"},{"x": "bar"}]')
|
||||
reader = JsonReader(path=filename)
|
||||
|
||||
context = CapturingNodeExecutionContext(reader, None)
|
||||
context = CapturingNodeExecutionContext(reader, services={'fs': fs})
|
||||
|
||||
context.start()
|
||||
context.recv(BEGIN, Bag(), END)
|
||||
|
||||
Reference in New Issue
Block a user