def test_is_iterable(base_config): cfg = Configuration(base_config) cfg_keys = [k for k in cfg] assert len(cfg_keys) == 5 assert len(cfg.keys()) == 5 assert len(cfg.values()) == 5 assert len(cfg.items()) == 5
def test_apply_nested_dict(base_config): cfg = Configuration(base_config) cfg.apply({"foo": "baz", "logging": {"level": 6}}) assert cfg['foo'] == 'baz' assert cfg['logging']['level'] == 6 assert cfg['logging']['format'] == 'some format'
def test_apply_object(base_config): cfg = Configuration(base_config) ConfigOverrides = namedtuple('ConfigOverrides', ('foo', 'is_enabled')) cfg.apply_object(ConfigOverrides(foo='blah', is_enabled=True)) assert cfg['foo'] == 'blah' assert cfg['is_enabled'] is True
def test_apply_flat_dict_prefix_stripped_missing_keys(base_config): cfg = Configuration(base_config) flat_overrides = {'figcan_logging_level': 6, 'figcan_logging_shmevel': 9} cfg.apply_flat(flat_overrides, prefix='figcan_') assert 'shmevel' not in cfg['logging'] assert 'logging_shmevel' not in cfg
def test_apply_object_to_subkey(base_config): cfg = Configuration(base_config) ConfigOverrides = namedtuple('ConfigOverrides', ('level', 'format')) cfg.apply_object(ConfigOverrides(level=4, format='this is a format'), apply_on=('logging', )) assert cfg['logging']['level'] == 4 assert cfg['logging']['format'] == 'this is a format'
def test_apply_flat_dict_custom_nesting_separator(base_config): cfg = Configuration(base_config) flat_overrides = { 'is_enabled': True, 'is.enabled': False, 'logging.level': 6, 'logging.handlers_handler1': 'something else' } cfg.apply_flat(flat_overrides, namespace_separator='.') assert cfg['is_enabled'] is True assert cfg['logging']['level'] == 6 assert cfg['logging']['handlers']['handler1'] == 'some config'
def test_apply_object_from_argparse(base_config): parser = argparse.ArgumentParser() parser.add_argument('--level', type=int) parser.add_argument('--format', type=str) parser.add_argument('--other-flag', action='store_true') args = parser.parse_args( ['--level', '3', '--format', 'some great format', '--other-flag']) cfg = Configuration(base_config) cfg.apply_object(args, apply_on=('logging', )) assert cfg['logging']['level'] == 3 assert cfg['logging']['format'] == 'some great format'
def test_apply_flat_dict(base_config): cfg = Configuration(base_config) flat_overrides = { 'foo': 'baz', 'is_enabled': True, 'logging_level': 6, 'logging_handlers_handler1': 'something else' } cfg.apply_flat(flat_overrides) assert cfg['foo'] == 'baz' assert cfg['is_enabled'] is True assert cfg['logging']['level'] == 6 assert cfg['logging']['handlers']['handler1'] == 'something else'
def test_throw_if_read_nonexisting_items(base_config): cfg = Configuration(base_config) with pytest.raises(KeyError): assert cfg['is_fababled'] is False with pytest.raises(TypeError): assert cfg['items']['item_foo'] == 3
def test_throw_on_write(base_config): cfg = Configuration(base_config) with pytest.raises(TypeError): cfg['is_enabled'] = True with pytest.raises(TypeError): cfg['new_key'] = 'new value'
def test_apply_flat_dict_prefix_stripped(base_config): cfg = Configuration(base_config) flat_overrides = { 'foo': 'baz', 'figcan_is_enabled': True, 'figcan_logging_level': 6, 'figcan_logging_handlers_handler1': 'something else', 'logging_level': 4 } cfg.apply_flat(flat_overrides, prefix='figcan_') assert cfg['foo'] == 'bar' assert cfg['is_enabled'] is True assert cfg['logging']['handlers']['handler1'] == 'something else' assert cfg['logging']['level'] == 6
def test_apply_nokey_flexible_config_added(base_config): base_config['logging']['handlers'] = Extensible( base_config['logging']['handlers']) cfg = Configuration(base_config) cfg.apply({ "foo": "baz", "logging": { "handlers": { "handler3": 'new handler' } } }) assert cfg['foo'] == 'baz' assert cfg['logging']['level'] == 5 assert cfg['logging']['handlers']['handler1'] == 'some config' assert cfg['logging']['handlers']['handler3'] == 'new handler'
def test_can_read_existing_items(base_config): cfg = Configuration(base_config) assert cfg['is_enabled'] is False assert cfg['items'][2] == 3 assert cfg['logging']['handlers']['handler1'] == 'some config'
def test_can_read_nonexisting_key_with_default(base_config): cfg = Configuration(base_config) assert cfg.get('is_fababled', 'foo') == 'foo'
def test_apply_nokey_ignored(base_config): cfg = Configuration(base_config) cfg.apply({"foos": "baz"}, raise_on_unknown_key=False) assert 'foos' not in cfg
def test_apply_nokey_raises(base_config): cfg = Configuration(base_config) with pytest.raises(KeyError): cfg.apply({"foos": "baz"})
def test_apply_nested_dict_base_not_modified(base_config): cfg = Configuration(base_config) cfg.apply({"foo": "baz", "logging": {"level": 6}}) assert base_config['foo'] == 'bar'
def test_can_check_for_items_in(base_config): cfg = Configuration(base_config) assert 'items' in cfg assert 'format' in cfg['logging']
def test_has_len(base_config): cfg = Configuration(base_config) assert len(cfg) == 5