コード例 #1
0
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'
コード例 #2
0
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'
コード例 #3
0
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')
コード例 #4
0
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)
コード例 #5
0
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'
コード例 #6
0
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'
コード例 #7
0
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'
コード例 #8
0
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')
コード例 #9
0
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'
コード例 #10
0
def test_tree_coerce_dict():
    d, s = {}, 'test'
    assert ConfigTree._coerce(d, s) == (d, s)
    d, s = {'key': 'val'}, 'test'
    assert ConfigTree._coerce(d, s) == (d, s)
    d = {
        'key1': {
            'sub_key1': ['val', 'val', 'val'],
            'sub_key2': 'val'
        },
        'key2': 'val'
    }
    s = 'test'
    assert ConfigTree._coerce(d, s) == (d, s)
コード例 #11
0
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()
コード例 #12
0
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'
コード例 #13
0
ファイル: utils.py プロジェクト: niklomax/daedalus
def base_plugins():
    config = {
        'required': {
            'data': {
                'controller':
                'vivarium_population_spenser.testing.mock_artifact.MockArtifactManager',
                'builder_interface':
                'vivarium.framework.artifact.ArtifactInterface'
            }
        }
    }
    return ConfigTree(config)
コード例 #14
0
def test_tree_coerce_yaml(tmpdir):
    d = '''\
     key1:
         sub_key1:
             - val
             - val
             - val
         sub_key2: [val, val]
     key2: val'''
    r = {
        'key1': {
            'sub_key1': ['val', 'val', 'val'],
            'sub_key2': ['val', 'val']
        },
        'key2': 'val'
    }
    s = 'test'
    p = tmpdir.join('model_spec.yaml')
    with p.open('w') as f:
        f.write(d)
    assert ConfigTree._coerce(str(p), s) == (r, s)
    assert ConfigTree._coerce(str(p), None) == (r, str(p))
コード例 #15
0
def test_tree_coerce_str():
    d = ''''''
    s = 'test'
    assert ConfigTree._coerce(d, s) == (None, s)
    d = '''\
    key: val'''
    assert ConfigTree._coerce(d, s) == ({'key': 'val'}, s)
    d = '''\
    key1:
        sub_key1:
            - val
            - val
            - val
        sub_key2: val
    key2: val'''
    r = {
        'key1': {
            'sub_key1': ['val', 'val', 'val'],
            'sub_key2': 'val'
        },
        'key2': 'val'
    }
    assert ConfigTree._coerce(d, s) == (r, s)
    d = '''\
        key1:
            sub_key1: [val, val, val]
            sub_key2: val
        key2: val'''
    r = {
        'key1': {
            'sub_key1': ['val', 'val', 'val'],
            'sub_key2': 'val'
        },
        'key2': 'val'
    }
    assert ConfigTree._coerce(d, s) == (r, s)
コード例 #16
0
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'
    }]
コード例 #17
0
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'
コード例 #18
0
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'
コード例 #19
0
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'
コード例 #20
0
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
コード例 #21
0
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'
コード例 #22
0
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
コード例 #23
0
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
                    }
                }
            }})
コード例 #24
0
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')
コード例 #25
0
def test_to_dict_dict():
    test_dict = {'configuration': {'time': {'start': {'year': 2000}}}}
    config = ConfigTree(test_dict)
    assert config.to_dict() == test_dict
コード例 #26
0
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()
コード例 #27
0
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'
コード例 #28
0
def test_get_missing():
    d = ConfigTree()
    d.test_key = 'test_value'

    # Missing keys should be empty containers
    assert len(d.missing_key) == 0
コード例 #29
0
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)
コード例 #30
0
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'