def test_dictionary_style_access(): d = ConfigTree() d.test_key = 'test_value' d['test_key2'] = 'test_value2' assert d['test_key'] == 'test_value' assert d.test_key2 == 'test_value2'
def test_load_yaml_string(): d = ConfigTree() d._loads(TEST_YAML_ONE, source='inline_test') assert d.test_section.test_key == 'test_value' assert d.test_section.test_key2 == 'test_value2' assert d.test_section2.test_key == 'test_value3'
def test_outer_layer_set(): d = ConfigTree(layers=['inner', 'outer']) d.set_with_metadata('test_key', 'test_value', 'inner') d.set_with_metadata('test_key', 'test_value2', 'outer') d.test_key = 'test_value3' assert d.test_key == 'test_value3'
def test_exception_on_source_for_missing_key(): d = ConfigTree(layers=['inner', 'outer']) d.read_dict({'test_key': 'test_value'}, layer='inner', source='initial_load') with pytest.raises(KeyError) as excinfo: source = d.metadata('missing_key') assert 'missing_key' in str(excinfo.value)
def test_outer_layer_set(): d = ConfigTree(layers=['inner', 'outer']) d._set_with_metadata('test_key', 'test_value', 'inner', source=None) d._set_with_metadata('test_key', 'test_value3', layer=None, source=None) assert d.test_key == 'test_value3' assert d['test_key'] == 'test_value3' d = ConfigTree(layers=['inner', 'outer']) d._set_with_metadata('test_key', 'test_value', 'inner', source=None) d.test_key = 'test_value3' assert d.test_key == 'test_value3' assert d['test_key'] == 'test_value3' d = ConfigTree(layers=['inner', 'outer']) d._set_with_metadata('test_key', 'test_value', 'inner', source=None) d['test_key'] = 'test_value3' assert d.test_key == 'test_value3' assert d['test_key'] == 'test_value3'
def test_reset_layer(): d = ConfigTree(layers=['a', 'b', 'c']) d.set_with_metadata('test_key', 'test_value', 'a') d.set_with_metadata('test_key', 'test_value2', 'b') assert d.test_key == 'test_value2' d.reset_layer('b') assert d.test_key == 'test_value' d.set_with_metadata('test_key', 'test_value3', 'b')
def test_load_yaml_file(tmpdir): tmp_file = tmpdir.join('test_file.yaml') tmp_file.write(TEST_YAML_ONE) d = ConfigTree() d._load(str(tmp_file)) assert d.test_section.test_key == 'test_value' assert d.test_section.test_key2 == 'test_value2' assert d.test_section2.test_key == 'test_value3'
def test_read_dict(): d = ConfigTree(layers=['inner', 'outer']) d.read_dict({ 'test_key': 'test_value', 'test_key2': 'test_value2' }, layer='inner') d.read_dict({'test_key': 'test_value3'}, layer='outer') assert d.test_key == 'test_value3' assert d.test_key2 == 'test_value2'
def test_drop_layer(): d = ConfigTree(layers=['a', 'b', 'c']) d.set_with_metadata('test_key', 'test_value', 'a') d.set_with_metadata('test_key', 'test_value2', 'b') d.set_with_metadata('test_key', 'test_value3', 'c') assert d.test_key == 'test_value3' d.drop_layer('c') assert d.test_key == 'test_value2' with pytest.raises(ValueError): d.drop_layer('c')
def test_single_layer(): d = ConfigTree() d.test_key = 'test_value' d.test_key2 = 'test_value2' assert d.test_key == 'test_value' assert d.test_key2 == 'test_value2' d.test_key2 = 'test_value3' assert d.test_key2 == 'test_value3' assert d.test_key == 'test_value'
def test_dictionary_style_access(): d = ConfigTree() d.update({'test_key': 'test_value', 'test_key2': 'test_value2'}) assert d['test_key'] == 'test_value' assert d['test_key2'] == 'test_value2' with pytest.raises(DuplicatedConfigurationError): d['test_key2'] = 'test_value3' assert d['test_key2'] == 'test_value2' assert d['test_key'] == 'test_value'
def test_single_layer(): d = ConfigTree() d.update({'test_key': 'test_value', 'test_key2': 'test_value2'}) assert d.test_key == 'test_value' assert d.test_key2 == 'test_value2' with pytest.raises(DuplicatedConfigurationError): d.test_key2 = 'test_value3' assert d.test_key2 == 'test_value2' assert d.test_key == 'test_value'
def base_plugins(): config = { 'required': { 'data': { 'controller': 'vivarium_population_spenser.testing.mock_artifact.MockArtifactManager', 'builder_interface': 'vivarium.framework.artifact.ArtifactInterface' } } } return ConfigTree(config)
def test_multiple_layer_get(): d = ConfigTree(layers=['first', 'second', 'third']) d.set_with_metadata('test_key', 'test_value', 'first') d.set_with_metadata('test_key', 'test_value2', 'second') d.set_with_metadata('test_key', 'test_value3', 'third') d.set_with_metadata('test_key2', 'test_value4', 'first') d.set_with_metadata('test_key2', 'test_value5', 'second') d.set_with_metadata('test_key3', 'test_value6', 'first') assert d.test_key == 'test_value3' assert d.test_key2 == 'test_value5' assert d.test_key3 == 'test_value6'
def test_source_metadata(): d = ConfigTree(layers=['inner', 'outer']) d.update({'test_key': 'test_value'}, layer='inner', source='initial_load') d.update({'test_key': 'test_value2'}, layer='outer', source='update') assert d.metadata('test_key') == [{ 'layer': 'inner', 'source': 'initial_load', 'value': 'test_value' }, { 'layer': 'outer', 'source': 'update', 'value': 'test_value2' }]
def test_repr_display(): expected_repr = '''\ Key1: override_2: value_ov_2 source: ov2_src override_1: value_ov_1 source: ov1_src base: value_base source: base_src''' # codifies the notion that repr() displays values from most to least overridden # regardless of initialization order layers = ['base', 'override_1', 'override_2'] cfg = ConfigTree(layers=layers) cfg.update({'Key1': 'value_ov_2'}, layer='override_2', source='ov2_src') cfg.update({'Key1': 'value_ov_1'}, layer='override_1', source='ov1_src') cfg.update({'Key1': 'value_base'}, layer='base', source='base_src') assert repr(cfg) == textwrap.dedent(expected_repr) cfg = ConfigTree(layers=layers) cfg.update({'Key1': 'value_base'}, layer='base', source='base_src') cfg.update({'Key1': 'value_ov_1'}, layer='override_1', source='ov1_src') cfg.update({'Key1': 'value_ov_2'}, layer='override_2', source='ov2_src') assert repr(cfg) == textwrap.dedent(expected_repr)
def _get_default_specification(): default_config_layers = [ 'base', 'component_configs', 'model_override', 'override' ] default_metadata = {'layer': 'base', 'source': 'vivarium_defaults'} model_specification = ConfigTree(layers=default_config_layers) model_specification.update(DEFAULT_PLUGINS, **default_metadata) model_specification.update({'components': None}) user_config_path = os.path.expanduser('~/vivarium.yaml') if os.path.exists(user_config_path): model_specification.configuration.update(user_config_path, layer='component_configs') return model_specification
def test_reset_layer_with_preserved_keys(): d = ConfigTree(layers=['a', 'b', 'c']) d.set_with_metadata('test_key', 'test_value', 'a') d.set_with_metadata('test_key2', 'test_value2', 'a') d.set_with_metadata('test_key3', 'test_value3', 'a') d.set_with_metadata('test_key4', 'test_value4', 'a') d.set_with_metadata('test_key', 'test_value5', 'b') d.set_with_metadata('test_key2', 'test_value6', 'b') d.set_with_metadata('test_key3', 'test_value7', 'b') d.set_with_metadata('test_key4', 'test_value8', 'b') d.reset_layer('b', preserve_keys=['test_key2', 'test_key3']) assert d.test_key == 'test_value' assert d.test_key2 == 'test_value6' assert d.test_key3 == 'test_value7' assert d.test_key4 == 'test_value4'
def test_unused_keys(): d = ConfigTree( {'test_key': { 'test_key2': 'test_value', 'test_key3': 'test_value2' }}) assert d.unused_keys() == {'test_key.test_key2', 'test_key.test_key3'} _ = d.test_key.test_key2 assert d.unused_keys() == {'test_key.test_key3'} _ = d.test_key.test_key3 assert not d.unused_keys()
def test_freeze(): config = ConfigTree( data={'configuration': { 'time': { 'start': { 'year': 2000 } } }}) config.freeze() with pytest.raises(ConfigurationError): config.update( data={'configuration': { 'time': { 'end': { 'year': 2001 } } }})
def test_read_dict_nested(): d = ConfigTree(layers=['inner', 'outer']) d.read_dict( { 'test_container': { 'test_key': 'test_value', 'test_key2': 'test_value2' } }, layer='inner') d.read_dict({'test_container': {'test_key': 'test_value3'}}, layer='inner') assert d.test_container.test_key == 'test_value3' assert d.test_container.test_key2 == 'test_value2' d.read_dict({'test_container': { 'test_key2': 'test_value4' }}, layer='outer') assert d.test_container.test_key2 == 'test_value4'
def test_update_dict_nested(): d = ConfigTree(layers=['inner', 'outer']) d.update( { 'test_container': { 'test_key': 'test_value', 'test_key2': 'test_value2' } }, layer='inner') with pytest.raises(DuplicatedConfigurationError): d.update({'test_container': { 'test_key': 'test_value3' }}, layer='inner') assert d.test_container.test_key == 'test_value' assert d.test_container.test_key2 == 'test_value2' d.update({'test_container': {'test_key2': 'test_value4'}}, layer='outer') assert d.test_container.test_key2 == 'test_value4'
def test_retrieval_behavior(): layer_inner = 'inner' layer_middle = 'middle' layer_outer = 'outer' default_cfg_value = 'value_a' layer_list = [layer_inner, layer_middle, layer_outer] # update the ConfigTree layers in different order and verify that has no effect on # the values retrieved ("outer" is retrieved when no layer is specified regardless of # the initialization order for scenario in [layer_list, reversed(layer_list)]: cfg = ConfigTree(layers=layer_list) for layer in scenario: cfg.update({default_cfg_value: layer}, layer=layer) assert cfg.get_from_layer(default_cfg_value) == layer_outer assert cfg.get_from_layer(default_cfg_value, layer=layer_outer) == layer_outer assert cfg.get_from_layer(default_cfg_value, layer=layer_middle) == layer_middle assert cfg.get_from_layer(default_cfg_value, layer=layer_inner) == layer_inner
def test_reset_layer_with_preserved_keys_at_depth(): d = ConfigTree(layers=['a', 'b', 'c']) d.read_dict( { 'test_key': { 'test_key2': 'test_value', 'test_key3': { 'test_key4': 'test_value2' } }, 'test_key5': { 'test_key6': 'test_value3', 'test_key7': 'test_value4' } }, layer='a') d.read_dict( { 'test_key': { 'test_key2': 'test_value5', 'test_key3': { 'test_key4': 'test_value6' } }, 'test_key5': { 'test_key6': 'test_value7', 'test_key7': 'test_value8' } }, layer='b') d.reset_layer( 'b', preserve_keys=['test_key.test_key3', 'test_key.test_key5.test_key6']) d.test_key.test_key2 == 'test_value' d.test_key.test_key3.test_key4 == 'test_value6' d.test_key5.test_key6 == 'test_value7' d.test_key5.test_key7 == 'test_value4'
def empty_tree(layers): return ConfigTree(layers=layers)
def test_set_missing_key(): d = ConfigTree() with pytest.raises(ConfigurationKeyError): d.missing_key = 'test_value' with pytest.raises(ConfigurationKeyError): d['missing_key'] = 'test_value'
def test_exception_on_source_for_missing_key(): d = ConfigTree(layers=['inner', 'outer']) d.update({'test_key': 'test_value'}, layer='inner', source='initial_load') with pytest.raises(ConfigurationKeyError): d.metadata('missing_key')
def test_get_missing(): d = ConfigTree() d.test_key = 'test_value' # Missing keys should be empty containers assert len(d.missing_key) == 0
def test_to_dict_dict(): test_dict = {'configuration': {'time': {'start': {'year': 2000}}}} config = ConfigTree(test_dict) assert config.to_dict() == test_dict
def test_to_dict_yaml(test_spec): config = ConfigTree(str(test_spec)) with test_spec.open() as f: yaml_config = yaml.full_load(f) assert yaml_config == config.to_dict()