Ejemplo n.º 1
0
def test_replace_source_not_found():
    source_a = DictConfigSource({})
    source_b = DictConfigSource({})
    config = Config()
    config.add_source(source_a)
    with pytest.raises(ValueError):
        config.replace_source(source_b, source_a)
Ejemplo n.º 2
0
def test_set_sources():
    old_sources = [DictConfigSource({})]
    new_sources = [DictConfigSource({}), DictConfigSource({})]
    config = Config(sources=old_sources)
    config.set_sources(new_sources)
    assert len(config.config_sources) == 2
    assert config.config_sources[0] is new_sources[0]
    assert config.config_sources[1] is new_sources[1]
Ejemplo n.º 3
0
def test_replace_source():
    sources = [DictConfigSource({}), DictConfigSource({})]
    config = Config(sources=sources)

    assert config.config_sources == sources
    assert config.config_sources[0] is sources[0]
    assert config.config_sources[1] is sources[1]

    replacement_source = DictConfigSource({})
    config.replace_source(sources[0], replacement_source)

    assert config.config_sources == [replacement_source, sources[1]]
    assert config.config_sources[0] is replacement_source
    assert config.config_sources[1] is sources[1]
Ejemplo n.º 4
0
 def read(self):
     if self.dict_config is not None:
         self.add_source(DictConfigSource(self.dict_config))
     self.add_source(EnvironmentConfigSource(prefix=self.prefix))
     if self.config_path is not None:
         self.add_source(IniFileConfigSource(self.config_path))
     return super().read()
Ejemplo n.º 5
0
def test_multiple_group_keys_with_section_decorators():
    @section('a')
    class Child1(Config):
        k1 = key(cast=str)

    @section('b')
    class Child2(Config):
        k2 = key(cast=str)

    class ParentConfig(Config):
        c1 = group_key(Child1)
        c2 = group_key(Child2)

    c1 = Child1()
    c2 = Child2()

    assert c1._section_name == 'a'
    assert c2._section_name == 'b'

    p = ParentConfig()
    p.add_source(DictConfigSource({'a': {'k1': 'v1'}, 'b': {'k2': 'v2'}}))
    assert p.c1._section_name == 'a'
    assert p.c2._section_name == 'b'

    assert 'v1' == p.c1.k1
    assert 'v2' == p.c2.k2
Ejemplo n.º 6
0
def test_get_key():
    class SampleConfig(Config):
        prop = key('s', 'prop1')

    config = SampleConfig()
    config.add_source(DictConfigSource({'s': {'PROP1': 'propval'}}))
    v = config.get_key('s', 'prop1')
    assert v == 'propval'
Ejemplo n.º 7
0
def test_section_decorator():
    @section('my_section')
    class SampleConfig(Config):
        prop1 = key(key_name='prop1')

    c = SampleConfig()
    c.add_source(DictConfigSource({'my_section': dict(PROP1='abc')}))
    assert 'abc' == c.prop1
Ejemplo n.º 8
0
def test_compose_configs():
    config = ParentConfig()
    config.add_source(DictConfigSource({
        'child': {'PROP1': '1'},
        'parent': {'PROP1': '2'},
    }))

    # Check that we are actually trying to read the grandchild which has a missing key
    with pytest.raises(KeyError):
        config.read()
    config.add_source(DictConfigSource({
        'grandchild': {'PROP1': '3'}
    }))
    config.read()
    assert '2' == config.prop1
    assert isinstance(config.child_config, ChildConfig)
    assert '1' == config.child_config.prop1
    assert isinstance(config.child_config.grandchild_config, GrandchildConfig)
    assert '3' == config.child_config.grandchild_config.prop1
Ejemplo n.º 9
0
def test_cast_with_default():
    @section('s')
    class SampleConfig(Config):
        nullable_key = key(cast=str, required=False, default=None)
        bool_key = key(cast=bool, required=False, default=False)

    s = SampleConfig()
    s.add_source(DictConfigSource({}))
    assert s.nullable_key is None
    assert s.bool_key is False
Ejemplo n.º 10
0
def test_section_decorator_precedence():
    @section('decorator')
    class SampleConfig(Config):
        decorator_section = key(cast=str)
        key_specific_section = key(section_name='key_specific', cast=str)

    c = SampleConfig(sources=[DictConfigSource({
        'decorator': dict(decorator_section='a'),
        'key_specific': dict(key_specific_section='b')
    })])
    c.read()
Ejemplo n.º 11
0
def test_add_source():
    c = Config()
    with pytest.raises(TypeError):
        bad_type: ConfigSource = 3
        c.add_source(bad_type)

    assert len(c.config_sources) == 0
    new_source = DictConfigSource({})
    c.add_source(new_source)
    assert len(c.config_sources) == 1
    assert c.config_sources[-1] is new_source
Ejemplo n.º 12
0
def test_least_verbose_config():
    @section('X')
    class SampleConfig(Config):
        prop1 = key()
        prop2 = key(cast=int)

    c = SampleConfig()
    c.add_source(DictConfigSource({'X': dict(PROP1='abc', PROP2='44')}))

    assert 'abc' == c.prop1
    assert 44 == c.prop2
Ejemplo n.º 13
0
def test_key_name_inference():
    class SampleConfig(Config):
        prop1 = key(section_name='s')
        prop2 = key(section_name='s')

    c = SampleConfig()

    c.add_source(DictConfigSource({'s': dict(
        PROP1='abc',
        PROP2='def',
    )}))
    assert 'abc' == c.prop1
    assert 'def' == c.prop2
Ejemplo n.º 14
0
def test_read(config_dict, expect_error):
    class SampleConfig(Config):
        prop1 = key('s', 'prop1')

    config = SampleConfig()
    config.add_source(DictConfigSource({'s': config_dict}))

    if expect_error:
        with pytest.raises(KeyError):
            config.read()
    else:
        config.read()

    if 'PROP1' in config_dict:
        assert config_dict['PROP1'] == config.prop1
Ejemplo n.º 15
0
def test_get_key(prop_val, args, expected_value_or_error):
    class SampleConfig(Config):
        prop = key('s', 'prop1', **args)

    config = SampleConfig()
    if prop_val is None:
        source_dict = {}
    else:
        source_dict = {'s': {'PROP1': prop_val}}
    config.add_source(DictConfigSource(source_dict))

    if inspect.isclass(expected_value_or_error) and issubclass(expected_value_or_error, BaseException):
        with pytest.raises(expected_value_or_error):
            _ = config.prop
    else:
        v = config.prop
        assert expected_value_or_error == v
Ejemplo n.º 16
0
def test_subclass_config():
    class SampleConfig(Config):
        prop1 = key('s', 'prop1', cast=float)
        prop2 = key('s', 'prop2', cast=int)
        prop3 = key('s', 'prop3', cast=str)
        prop4 = key('s', 'prop4')

    config_source = DictConfigSource({
        's': {
            'PROP1': '3.5',
            'PROP2': '2',
            'PROP3': 'hello',
            'PROP4': 'abc'
        }
    })
    config = SampleConfig()
    config.add_source(config_source)

    assert 3.5 == config.prop1
    assert 2 == config.prop2
    assert 'hello' == config.prop3
    assert 'abc' == config.prop4
Ejemplo n.º 17
0
def test_init_with_sources():
    c = Config(sources=[
        DictConfigSource({}),
        DictConfigSource({})
    ])
    assert 2 == len(c.config_sources)
Ejemplo n.º 18
0
def test_replace_source_bad_type():
    source = DictConfigSource({})
    config = Config(sources=[source])
    with pytest.raises(TypeError):
        bad_type: ConfigSource = 3
        config.replace_source(source, bad_type)
Ejemplo n.º 19
0
def test_dict_config_source():
    source = DictConfigSource({'s': dict(A='1', b='2')})
    do_assertions(source, "<DictConfigSource>")
Ejemplo n.º 20
0
def test_construct_config_with_provider():
    sources = [DictConfigSource({})]
    provider = ConfigProvider(sources)
    config = ParentConfig(sources=sources, provider=provider)
    assert config.provider is provider
    assert config.config_sources == sources
Ejemplo n.º 21
0
def test_construct_config_without_provider():
    sources = [DictConfigSource({})]
    config = ParentConfig(sources=sources)
    assert isinstance(config.provider, ConfigProvider)
    assert config.provider.config_sources == sources