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)
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]
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]
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()
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
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'
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
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
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
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()
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
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
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
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
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
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
def test_init_with_sources(): c = Config(sources=[ DictConfigSource({}), DictConfigSource({}) ]) assert 2 == len(c.config_sources)
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)
def test_dict_config_source(): source = DictConfigSource({'s': dict(A='1', b='2')}) do_assertions(source, "<DictConfigSource>")
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
def test_construct_config_without_provider(): sources = [DictConfigSource({})] config = ParentConfig(sources=sources) assert isinstance(config.provider, ConfigProvider) assert config.provider.config_sources == sources