New bag implementation improves a lot how bonobo works, even if this is highly backward incompatible (sorry, that's needed, and better sooner than later). * New implementation uses the same approach as python's namedtuple, by dynamically creating the python type's code. This has drawbacks, as it feels like not the right way, but also a lot of benefits that cannot be achieved using a regular approach, especially the constructor parameter order, hardcoded. * Memory usage is now much more efficient. The "keys" memory space will be used only once per "io type", being spent in the underlying type definition instead of in the actual instances. * Transformations now needs to use tuples as output, which will be bound to its "output type". The output type can be infered from the tuple length, or explicitely set by the user using either `context.set_output_type(...)` or `context.set_output_fields(...)` (to build a bag type from a list of field names). Jupyter/Graphviz integration is more tight, allowing to easily display graphs in a notebook, or displaying the live transformation status in an html table instead of a simple <div>. For now, context processors were hacked to stay working as before but the current API is not satisfactory, and should be replaced. This new big change being unreasonable without some time to work on it properly, it is postponed for next versions (0.7, 0.8, ...). Maybe the best idea is to have some kind of "local services", that would use the same dependency injection mechanism as the execution-wide services. Services are now passed by keywoerd arguments only, to avoid confusion with data-arguments.
139 lines
3.4 KiB
Python
139 lines
3.4 KiB
Python
import pprint
|
|
|
|
import pytest
|
|
|
|
from bonobo.config.configurables import Configurable
|
|
from bonobo.config.options import Option
|
|
from bonobo.util.inspect import inspect_node
|
|
|
|
|
|
class NoOptConfigurable(Configurable):
|
|
pass
|
|
|
|
|
|
class MyConfigurable(Configurable):
|
|
required_str = Option(str)
|
|
default_str = Option(str, default='foo')
|
|
integer = Option(int, required=False)
|
|
|
|
|
|
class MyHarderConfigurable(MyConfigurable):
|
|
also_required = Option(bool, required=True)
|
|
|
|
|
|
class MyBetterConfigurable(MyConfigurable):
|
|
required_str = Option(str, required=False, default='kaboom')
|
|
|
|
|
|
class MyConfigurableUsingPositionalOptions(MyConfigurable):
|
|
first = Option(str, required=True, positional=True)
|
|
second = Option(str, required=True, positional=True)
|
|
third = Option(str, required=False, positional=True)
|
|
|
|
|
|
def test_missing_required_option_error():
|
|
with inspect_node(MyConfigurable()) as ni:
|
|
assert ni.partial
|
|
|
|
with pytest.raises(TypeError) as exc:
|
|
MyConfigurable(_final=True)
|
|
assert exc.match('missing 1 required option:')
|
|
|
|
|
|
def test_missing_required_options_error():
|
|
with inspect_node(MyHarderConfigurable()) as ni:
|
|
assert ni.partial
|
|
|
|
with pytest.raises(TypeError) as exc:
|
|
MyHarderConfigurable(_final=True)
|
|
assert exc.match('missing 2 required options:')
|
|
|
|
|
|
def test_extraneous_option_error():
|
|
with pytest.raises(TypeError) as exc:
|
|
MyConfigurable(required_str='foo', hello='world')
|
|
assert exc.match('got 1 unexpected option:')
|
|
|
|
|
|
def test_extraneous_options_error():
|
|
with pytest.raises(TypeError) as exc:
|
|
MyConfigurable(required_str='foo', hello='world', acme='corp')
|
|
assert exc.match('got 2 unexpected options:')
|
|
|
|
|
|
def test_defaults():
|
|
o = MyConfigurable(required_str='hello')
|
|
|
|
with inspect_node(o) as ni:
|
|
assert not ni.partial
|
|
|
|
assert o.required_str == 'hello'
|
|
assert o.default_str == 'foo'
|
|
assert o.integer is None
|
|
|
|
|
|
def test_str_type_factory():
|
|
o = MyConfigurable(required_str=42)
|
|
|
|
with inspect_node(o) as ni:
|
|
assert not ni.partial
|
|
|
|
assert o.required_str == '42'
|
|
assert o.default_str == 'foo'
|
|
assert o.integer is None
|
|
|
|
|
|
def test_int_type_factory():
|
|
o = MyConfigurable(required_str='yo', default_str='bar', integer='42')
|
|
|
|
with inspect_node(o) as ni:
|
|
assert not ni.partial
|
|
|
|
assert o.required_str == 'yo'
|
|
assert o.default_str == 'bar'
|
|
assert o.integer == 42
|
|
|
|
|
|
def test_bool_type_factory():
|
|
o = MyHarderConfigurable(required_str='yes', also_required='True')
|
|
|
|
with inspect_node(o) as ni:
|
|
assert not ni.partial
|
|
|
|
assert o.required_str == 'yes'
|
|
assert o.default_str == 'foo'
|
|
assert o.integer is None
|
|
assert o.also_required is True
|
|
|
|
|
|
def test_option_resolution_order():
|
|
o = MyBetterConfigurable()
|
|
|
|
with inspect_node(o) as ni:
|
|
assert not ni.partial
|
|
|
|
assert o.required_str == 'kaboom'
|
|
assert o.default_str == 'foo'
|
|
assert o.integer is None
|
|
|
|
|
|
def test_option_positional():
|
|
o = MyConfigurableUsingPositionalOptions('1', '2', '3', required_str='hello')
|
|
|
|
with inspect_node(o) as ni:
|
|
assert not ni.partial
|
|
|
|
assert o.first == '1'
|
|
assert o.second == '2'
|
|
assert o.third == '3'
|
|
assert o.required_str == 'hello'
|
|
assert o.default_str == 'foo'
|
|
assert o.integer is None
|
|
|
|
|
|
def test_no_opt_configurable():
|
|
o = NoOptConfigurable()
|
|
|
|
with inspect_node(o) as ni:
|
|
assert not ni.partial
|