[core] Still refactoring the core behaviour of bags, starting to be much simpler.
This commit is contained in:
committed by
Romain Dorgueil
parent
f18889830b
commit
9a54f7b4aa
@ -28,9 +28,7 @@ def test_write_csv_to_file_kwargs(tmpdir, add_kwargs):
|
||||
fs, filename, services = csv_tester.get_services_for_writer(tmpdir)
|
||||
|
||||
with NodeExecutionContext(CsvWriter(filename, **add_kwargs), services=services) as context:
|
||||
context.write(BEGIN, Bag(**{'foo': 'bar'}), Bag(**{'foo': 'baz', 'ignore': 'this'}), END)
|
||||
context.step()
|
||||
context.step()
|
||||
context.write_sync({'foo': 'bar'}, {'foo': 'baz', 'ignore': 'this'})
|
||||
|
||||
with fs.open(filename) as fp:
|
||||
assert fp.read() == 'foo\nbar\nbaz\n'
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import pytest
|
||||
|
||||
from bonobo import Bag, JsonReader, JsonWriter, settings
|
||||
from bonobo.constants import BEGIN, END
|
||||
from bonobo import JsonReader, JsonWriter, settings
|
||||
from bonobo.execution.node import NodeExecutionContext
|
||||
from bonobo.util.testing import FilesystemTester
|
||||
|
||||
@ -29,8 +28,7 @@ def test_write_json_kwargs(tmpdir, add_kwargs):
|
||||
fs, filename, services = json_tester.get_services_for_writer(tmpdir)
|
||||
|
||||
with NodeExecutionContext(JsonWriter(filename, **add_kwargs), services=services) as context:
|
||||
context.write(BEGIN, Bag(**{'foo': 'bar'}), END)
|
||||
context.step()
|
||||
context.write_sync({'foo': 'bar'})
|
||||
|
||||
with fs.open(filename) as fp:
|
||||
assert fp.read() == '[{"foo": "bar"}]'
|
||||
|
||||
@ -14,7 +14,7 @@ def test_write_pickled_dict_to_file(tmpdir):
|
||||
fs, filename, services = pickle_tester.get_services_for_writer(tmpdir)
|
||||
|
||||
with NodeExecutionContext(PickleWriter(filename), services=services) as context:
|
||||
context.write_sync(Bag({'foo': 'bar'}), Bag({'foo': 'baz', 'ignore': 'this'}))
|
||||
context.write_sync(Bag(({'foo': 'bar'}, {})), Bag(({'foo': 'baz', 'ignore': 'this'}, {})))
|
||||
|
||||
with fs.open(filename, 'rb') as fp:
|
||||
assert pickle.loads(fp.read()) == {'foo': 'bar'}
|
||||
@ -27,7 +27,7 @@ def test_read_pickled_list_from_file(tmpdir):
|
||||
fs, filename, services = pickle_tester.get_services_for_reader(tmpdir)
|
||||
|
||||
with BufferingNodeExecutionContext(PickleReader(filename), services=services) as context:
|
||||
context.write_sync(Bag())
|
||||
context.write_sync(())
|
||||
output = context.get_buffer()
|
||||
|
||||
assert len(output) == 2
|
||||
|
||||
66
tests/nodes/factory.py
Normal file
66
tests/nodes/factory.py
Normal file
@ -0,0 +1,66 @@
|
||||
from unittest import TestCase
|
||||
|
||||
import pytest
|
||||
|
||||
from bonobo import Bag
|
||||
from bonobo.nodes.factory import Factory
|
||||
from bonobo.util.testing import BufferingNodeExecutionContext
|
||||
|
||||
|
||||
@pytest.mark.filterwarnings('ignore:Factory')
|
||||
class FactoryTypeTest(TestCase):
|
||||
def execute_node(self, node, *rows):
|
||||
with BufferingNodeExecutionContext(node) as context:
|
||||
context.write_sync(*map(Bag, rows))
|
||||
return context.get_buffer()
|
||||
|
||||
def test_args_as_str(self):
|
||||
f = Factory()
|
||||
f[0].as_str().upper()
|
||||
|
||||
output = self.execute_node(f, 'foo', 'bar', 'baz')
|
||||
|
||||
assert len(output) == 3
|
||||
assert output[0] == 'FOO'
|
||||
assert output[1] == 'BAR'
|
||||
assert output[2] == 'BAZ'
|
||||
|
||||
def test_kwargs_as_str(self):
|
||||
f = Factory()
|
||||
f['foo'].as_str().upper()
|
||||
|
||||
output = self.execute_node(f, {'foo': 'bar'}, {'foo': 'baz'})
|
||||
assert len(output) == 2
|
||||
assert output[0] == {'foo': 'BAR'}
|
||||
assert output[1] == {'foo': 'BAZ'}
|
||||
|
||||
|
||||
"""
|
||||
draft below.
|
||||
|
||||
if __name__ == '__main__':
|
||||
f = Factory()
|
||||
|
||||
f[0].dict().map_keys({'foo': 'F00'})
|
||||
|
||||
print('operations:', f.operations)
|
||||
print(f({'foo': 'bisou'}, foo='blah'))
|
||||
|
||||
specs:
|
||||
|
||||
- rename keys of an input dict (in args, or kwargs) using a translation map.
|
||||
|
||||
|
||||
f = Factory()
|
||||
|
||||
f[0]
|
||||
f['xxx'] =
|
||||
|
||||
f[0].dict().get('foo.bar').move_to('foo.baz').apply(str.upper)
|
||||
f[0].get('foo.*').items().map(str.lower)
|
||||
|
||||
f['foo'].keys_map({
|
||||
'a': 'b'
|
||||
})
|
||||
|
||||
"""
|
||||
@ -1,8 +1,10 @@
|
||||
import pickle
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from bonobo import Bag
|
||||
from bonobo.constants import INHERIT_INPUT
|
||||
from bonobo.constants import INHERIT_INPUT, BEGIN
|
||||
from bonobo.structs import Token
|
||||
|
||||
args = (
|
||||
@ -31,6 +33,32 @@ def test_basic():
|
||||
my_callable2.assert_called_once_with(*args, **kwargs)
|
||||
|
||||
|
||||
def test_constructor_empty():
|
||||
a, b = Bag(), Bag()
|
||||
assert a == b
|
||||
assert a.args is ()
|
||||
assert a.kwargs == {}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('arg_in', 'arg_out'), (
|
||||
((), ()),
|
||||
({}, ()),
|
||||
(('a', 'b', 'c'), None),
|
||||
))
|
||||
def test_constructor_shorthand(arg_in, arg_out):
|
||||
if arg_out is None:
|
||||
arg_out = arg_in
|
||||
assert Bag(arg_in) == arg_out
|
||||
|
||||
|
||||
def test_constructor_kwargs_only():
|
||||
assert Bag(foo='bar') == {'foo': 'bar'}
|
||||
|
||||
|
||||
def test_constructor_identity():
|
||||
assert Bag(BEGIN) is BEGIN
|
||||
|
||||
|
||||
def test_inherit():
|
||||
bag = Bag('a', a=1)
|
||||
bag2 = Bag.inherit('b', b=2, _parent=bag)
|
||||
|
||||
30
tests/util/test_collections.py
Normal file
30
tests/util/test_collections.py
Normal file
@ -0,0 +1,30 @@
|
||||
from bonobo.util import sortedlist, ensure_tuple
|
||||
from bonobo.util.collections import tuplize
|
||||
|
||||
|
||||
def test_sortedlist():
|
||||
l = sortedlist()
|
||||
l.insort(2)
|
||||
l.insort(1)
|
||||
l.insort(3)
|
||||
l.insort(2)
|
||||
assert l == [1, 2, 2, 3]
|
||||
|
||||
|
||||
def test_ensure_tuple():
|
||||
assert ensure_tuple('a') == ('a', )
|
||||
assert ensure_tuple(('a', )) == ('a', )
|
||||
assert ensure_tuple(()) is ()
|
||||
|
||||
|
||||
def test_tuplize():
|
||||
tuplized_lambda = tuplize(lambda: [1, 2, 3])
|
||||
assert tuplized_lambda() == (1, 2, 3)
|
||||
|
||||
@tuplize
|
||||
def some_generator():
|
||||
yield 'c'
|
||||
yield 'b'
|
||||
yield 'a'
|
||||
|
||||
assert some_generator() == ('c', 'b', 'a')
|
||||
@ -1,22 +0,0 @@
|
||||
import types
|
||||
|
||||
from bonobo.util.iterators import force_iterator
|
||||
|
||||
|
||||
def test_force_iterator_with_string():
|
||||
assert force_iterator('foo') == ['foo']
|
||||
|
||||
|
||||
def test_force_iterator_with_none():
|
||||
assert force_iterator(None) == []
|
||||
|
||||
|
||||
def test_force_iterator_with_generator():
|
||||
def generator():
|
||||
yield 'aaa'
|
||||
yield 'bbb'
|
||||
yield 'ccc'
|
||||
|
||||
iterator = force_iterator(generator())
|
||||
assert isinstance(iterator, types.GeneratorType)
|
||||
assert list(iterator) == ['aaa', 'bbb', 'ccc']
|
||||
18
tests/util/test_resolvers.py
Normal file
18
tests/util/test_resolvers.py
Normal file
@ -0,0 +1,18 @@
|
||||
import bonobo
|
||||
from bonobo.util.resolvers import _parse_option, _resolve_options, _resolve_transformations
|
||||
|
||||
|
||||
def test_parse_option():
|
||||
assert _parse_option('foo=bar') == ('foo', 'bar')
|
||||
assert _parse_option('foo="bar"') == ('foo', 'bar')
|
||||
assert _parse_option('sep=";"') == ('sep', ';')
|
||||
assert _parse_option('foo') == ('foo', True)
|
||||
|
||||
|
||||
def test_resolve_options():
|
||||
assert _resolve_options(('foo=bar', 'bar="baz"')) == {'foo': 'bar', 'bar': 'baz'}
|
||||
assert _resolve_options() == {}
|
||||
|
||||
|
||||
def test_resolve_transformations():
|
||||
assert _resolve_transformations(('PrettyPrinter', )) == (bonobo.PrettyPrinter, )
|
||||
Reference in New Issue
Block a user