various bugfixes and updates
This commit is contained in:
parent
0b889196bb
commit
1c6d386c0f
|
@ -3,7 +3,7 @@
|
||||||
__title__ = 'bison'
|
__title__ = 'bison'
|
||||||
__description__ = 'Python application configuration'
|
__description__ = 'Python application configuration'
|
||||||
__url__ = 'https://github.com/edaniszewski/bison'
|
__url__ = 'https://github.com/edaniszewski/bison'
|
||||||
__version__ = '0.1.0'
|
__version__ = '0.1.1'
|
||||||
__author__ = 'Erick Daniszewski'
|
__author__ = 'Erick Daniszewski'
|
||||||
__author_email__ = 'edaniszewski@gmail.com'
|
__author_email__ = 'edaniszewski@gmail.com'
|
||||||
__license__ = 'MIT'
|
__license__ = 'MIT'
|
||||||
|
|
|
@ -75,6 +75,11 @@ class Bison(object):
|
||||||
# the unified configuration.
|
# the unified configuration.
|
||||||
self._full_config = None
|
self._full_config = None
|
||||||
|
|
||||||
|
def __getitem__(self, item):
|
||||||
|
# Set __getitem__ so the Bison config can be accessed via subscripting,
|
||||||
|
# e.g. config['foo']
|
||||||
|
return self.get(item)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def config(self):
|
def config(self):
|
||||||
"""Get the complete configuration where the default, config,
|
"""Get the complete configuration where the default, config,
|
||||||
|
|
|
@ -119,8 +119,9 @@ class Scheme(object):
|
||||||
# the option does not exist in the config
|
# the option does not exist in the config
|
||||||
else:
|
else:
|
||||||
# if the option is not required, then it is fine to omit.
|
# if the option is not required, then it is fine to omit.
|
||||||
# otherwise, its omission constitutes a validation error.
|
# otherwise, its omission constitutes a validation error unless
|
||||||
if arg.required:
|
# a default value is specified for the option.
|
||||||
|
if arg.required and isinstance(arg.default, NoDefault):
|
||||||
raise errors.SchemeValidationError(
|
raise errors.SchemeValidationError(
|
||||||
'Option "{}" is required, but not found.'.format(arg.name)
|
'Option "{}" is required, but not found.'.format(arg.name)
|
||||||
)
|
)
|
||||||
|
|
|
@ -81,6 +81,24 @@ class TestBison:
|
||||||
value = b.get(key, default=default)
|
value = b.get(key, default=default)
|
||||||
assert value == expected
|
assert value == expected
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'key,expected,config', [
|
||||||
|
('foo', None, None),
|
||||||
|
('foo', None, bison.DotDict()),
|
||||||
|
('foo', None, bison.DotDict({'foo': None})),
|
||||||
|
('foo', 'bar', bison.DotDict({'foo': 'bar'})),
|
||||||
|
('foo.bar', 'baz', bison.DotDict({'foo': {'bar': 'baz'}})),
|
||||||
|
('foo.bar.baz', 1, bison.DotDict({'foo': {'bar': {'baz': 1}}})),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_get_subscriptable(self, key, expected, config):
|
||||||
|
"""Get config values from Bison via subscripting."""
|
||||||
|
b = bison.Bison()
|
||||||
|
b._full_config = config # for the test, set the config manually
|
||||||
|
|
||||||
|
value = b[key]
|
||||||
|
assert value == expected
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
'key,value', [
|
'key,value', [
|
||||||
('foo', 'bar'),
|
('foo', 'bar'),
|
||||||
|
|
|
@ -760,3 +760,41 @@ class TestScheme:
|
||||||
sch = scheme.Scheme()
|
sch = scheme.Scheme()
|
||||||
with pytest.raises(errors.SchemeValidationError):
|
with pytest.raises(errors.SchemeValidationError):
|
||||||
sch.validate(value)
|
sch.validate(value)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'args,value', [
|
||||||
|
(
|
||||||
|
# option does not exist in config, has default, not required
|
||||||
|
(scheme.Option('foo', default='bar'),),
|
||||||
|
{}
|
||||||
|
),
|
||||||
|
(
|
||||||
|
# option does not exist in config, has default, not required
|
||||||
|
(
|
||||||
|
scheme.DictOption('foo', scheme=scheme.Scheme(
|
||||||
|
scheme.Option('bar', default=1),
|
||||||
|
scheme.Option('baz'),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
{'foo': {'baz': 2}}
|
||||||
|
),
|
||||||
|
(
|
||||||
|
# option does not exist in config, has default, not required
|
||||||
|
(
|
||||||
|
scheme.ListOption('foo', member_scheme=scheme.Scheme(
|
||||||
|
scheme.Option('bar', default=1),
|
||||||
|
scheme.Option('baz'),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
{'foo': [{'baz': 2}, {'bar': 3, 'baz': 2}]}
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_validate_has_default(self, args, value):
|
||||||
|
"""Validate a Scheme where a default value is set, and the required field
|
||||||
|
may or may not be set.
|
||||||
|
|
||||||
|
If a default value is provided, it should be assumed to not be required.
|
||||||
|
"""
|
||||||
|
sch = scheme.Scheme(*args)
|
||||||
|
sch.validate(value)
|
||||||
|
|
Loading…
Reference in New Issue