def test_get_dict_different_types(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT3_1), config_from_dict(DICT3_2), # a2 is ignored here config_from_dict(DICT3_3), ) assert cfg.get_dict("a2") == { 'b2.c1': 10, 'b2.c2': 'YWJjZGVmZ2g=', 'b2.c3': 'abcdefgh', 'g2': 10, 'w2': 123, 'w3': 'abc' } assert cfg.a2.as_dict() == { 'b2.c1': 10, 'b2.c2': 'YWJjZGVmZ2g=', 'b2.c3': 'abcdefgh', 'g2': 10, 'w2': 123, 'w3': 'abc' } assert dict(cfg.a2) == { 'b2.c1': 10, 'b2.c2': 'YWJjZGVmZ2g=', 'b2.c3': 'abcdefgh', 'g2': 10, 'w2': 123, 'w3': 'abc' } with raises(TypeError): # the first configuration overrides the type assert cfg.get_dict("z1") is Exception assert cfg.z1 == 100
def test_get_dict(): # type: ignore cfg = ConfigurationSet(config_from_dict(DICT2_1), config_from_dict(DICT2_2), config_from_env(prefix=PREFIX)) assert cfg.get_dict("a2") == { 'b1.c1': 'f', 'b1.c2': False, 'b1.c3': None, 'b2.c1': 10, 'b2.c2': 'YWJjZGVmZ2g=', 'b2.c3': 'abcdefgh' } assert cfg.a2.as_dict() == { 'b1.c1': 'f', 'b1.c2': False, 'b1.c3': None, 'b2.c1': 10, 'b2.c2': 'YWJjZGVmZ2g=', 'b2.c3': 'abcdefgh' } assert dict(cfg.a2) == { 'b1.c1': 'f', 'b1.c2': False, 'b1.c3': None, 'b2.c1': 10, 'b2.c2': 'YWJjZGVmZ2g=', 'b2.c3': 'abcdefgh' } with raises(KeyError): assert cfg.get_dict("a3") is Exception assert set( cfg.a2.values()) == {'f', False, None, 10, 'YWJjZGVmZ2g=', 'abcdefgh'} assert dict(cfg.a2) == dict(cfg.a2.items())
def test_get_dict_different_types(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT3_1, lowercase_keys=True), config_from_dict(DICT3_2, lowercase_keys=True), # a2 is ignored here config_from_dict(DICT3_3, lowercase_keys=True), ) assert cfg.get_dict("a2") == { "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", "g2": 10, "w2": 123, "w3": "abc", } assert cfg.a2.as_dict() == { "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", "g2": 10, "w2": 123, "w3": "abc", } assert dict(cfg.a2) == { "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", "g2": 10, "w2": 123, "w3": "abc", } with pytest.raises(TypeError): # the first configuration overrides the type assert cfg.get_dict("z1") is Exception assert cfg.z1 == 100
def test_configset_len(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT, lowercase_keys=False), config_from_dict(PROTECTED, lowercase_keys=False), ) assert len(cfg) == 8 with cfg.dotted_iter(): assert len(cfg) == len(DICT) + len(PROTECTED)
def test_configset_copy(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT, lowercase_keys=True), config_from_dict(PROTECTED, lowercase_keys=True), ) assert len(cfg) == 17 cfg2 = cfg.copy() assert cfg == cfg2
def test_configset_clear(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT, lowercase_keys=False), config_from_dict(PROTECTED, lowercase_keys=False), ) assert len(cfg) == 17 cfg.clear() assert len(cfg) == 0
def test_configset_list(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT, lowercase_keys=False), config_from_dict(PROTECTED, lowercase_keys=False), ) assert sorted(cfg) == sorted(["A1", "a1", "a2"] + list(PROTECTED.keys())) with cfg.dotted_iter(): assert sorted(cfg) == sorted( list(DICT.keys()) + list(PROTECTED.keys())) assert list(cfg) == list(reversed(cfg))[::-1]
def test_get(): # type: ignore cfg = ConfigurationSet(config_from_dict(DICT2_1), config_from_dict(DICT2_2), config_from_env(prefix=PREFIX)) assert cfg.get("a2.b2") == config_from_dict({ 'c1': 10, 'c2': 'YWJjZGVmZ2g=', 'c3': 'abcdefgh' }) assert cfg.get("a2.b5", "1") == "1"
def test_get(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert cfg.get("a2.b2") == config_from_dict( {"c1": 10, "c2": "YWJjZGVmZ2g=", "c3": "abcdefgh"} ) assert cfg.get("a2.b5", "1") == "1"
def test_issue_49(): # type: ignore d = {"path": {"to": {"value-a": "A", "value-b": "B"}}} base_cfg = config_from_dict(d) d = {"path": {"to": {"value-a": "C"}}} cfg = config_from_dict(d) cfg_set = ConfigurationSet(cfg, base_cfg) path_config = cfg_set.get("path") assert path_config == {"to.value-a": "C", "to.value-b": "B"}
def test_configset_setdefault(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT, lowercase_keys=False), config_from_dict(PROTECTED, lowercase_keys=False), ) # no changes assert cfg.setdefault("a2.b1.c1") == "f" assert len(cfg) == 8 with cfg.dotted_iter(): assert len(cfg) == 17 assert sorted(cfg) == sorted( list(DICT.keys()) + list(PROTECTED.keys())) # add key assert cfg.setdefault("a2.b1.c7") is None assert len(cfg) == 8 with cfg.dotted_iter(): assert len(cfg) == 18 # add key with default assert cfg.setdefault("a2.b1.c8", "some value") == "some value" assert len(cfg) == 8 with cfg.dotted_iter(): assert len(cfg) == 19 assert cfg["a2.b1.c8"] == "some value"
def test_same_as_configuration(): # type: ignore cfg = config_from_dict(DICT2_1, lowercase_keys=True) cfgset = ConfigurationSet(config_from_dict(DICT2_1, lowercase_keys=True)) assert cfg.get_dict("a2") == cfgset.get_dict("a2") assert cfg.a2.as_dict() == cfgset.a2.as_dict() assert dict(cfg.a2) == dict(cfgset.a2) assert dict(cfg.a2) == dict(cfg.a2.items()) assert dict(cfgset.a2) == dict(cfgset.a2.items()) assert cfg.as_dict() == cfgset.as_dict() assert dict(cfg) == dict(cfgset)
def test_configset_delitem(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT, lowercase_keys=True), config_from_dict(PROTECTED, lowercase_keys=True), ) with cfg.dotted_iter(): assert len(cfg) == 17 del cfg["a1.b1"] with cfg.dotted_iter(): assert len(cfg) == 14 with raises(KeyError): del cfg["z"]
def test_dict_methods_keys_values(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert sorted(cfg.keys()) == [ "a1", "a2", ] assert dict(zip(cfg.keys(), cfg.values())) == { "a1": { "b1.c1": "1", "b1.c2": "2", "b1.c3": "3", "b2.c1": "a", "b2.c2": "True", "b2.c3": "1.1", }, "a2": { "b1.c1": "f", "b1.c2": False, "b1.c3": None, "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", }, } with cfg.dotted_iter(): assert sorted(cfg.keys()) == [ "a1.b1.c1", "a1.b1.c2", "a1.b1.c3", "a1.b2.c1", "a1.b2.c2", "a1.b2.c3", "a2.b1.c1", "a2.b1.c2", "a2.b1.c3", "a2.b2.c1", "a2.b2.c2", "a2.b2.c3", ] assert dict(zip(cfg.keys(), cfg.values())) == cfg.as_dict()
def test_repr_and_str(): # type: ignore import sys path = os.path.join(os.path.dirname(__file__), "python_config.py") cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), config_from_python(path, prefix="CONFIG", lowercase_keys=True), ) joined_dicts = dict((k, str(v)) for k, v in DICT1.items()) joined_dicts.update(DICT2_1) joined_dicts.update(DICT2_2) joined_dicts["sys.version"] = sys.hexversion assert hex(id(cfg)) in repr(cfg) assert ( str(cfg) == "{'a1.b1.c1': '1', 'a1.b1.c2': '2', 'a1.b1.c3': '3', 'a1.b2.c1': 'a', 'a1.b2.c2': 'True', " "'a1.b2.c3': '1.1', 'a2.b1.c1': 'f', 'a2.b1.c2': False, 'a2.b1.c3': None, 'a2.b2.c1': 10, " "'a2.b2.c2': 'YWJjZGVmZ2g=', 'a2.b2.c3': 'abcdefgh', 'sys.version': " + str(sys.hexversion) + "}" )
def load_config(): global _conf home = expanduser('~') config_folder = f'{home}/.config/holmes' config_file = f'{config_folder}/config.yaml' if exists(config_file): with open(config_file) as input: _conf = ConfigurationSet(config_from_env('HOLMES'), config_from_yaml(input)) else: DEFAULT_CONFIG = { 'storage': 'file', 'files': { 'frames': f'{home}/Library/ApplicationSupport/watson/frames', 'state': f'{home}/Library/ApplicationSupport/watson/state', } } _conf = ConfigurationSet(config_from_dict(DEFAULT_CONFIG))
def test_configset_in(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT, lowercase_keys=False), config_from_dict(PROTECTED, lowercase_keys=False), ) assert "x" not in cfg assert "a1" in cfg assert "a1.b2" in cfg assert "a1.b2.c3" in cfg
def test_dict_methods_items(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert dict(cfg.items()) == { "a1": { "b1.c1": "1", "b1.c2": "2", "b1.c3": "3", "b2.c1": "a", "b2.c2": "True", "b2.c3": "1.1", }, "a2": { "b1.c1": "f", "b1.c2": False, "b1.c3": None, "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", }, } with cfg.dotted_iter(): assert dict(cfg.items()) == dict( [ ("a2.b2.c2", "YWJjZGVmZ2g="), ("a1.b2.c2", "True"), ("a1.b2.c1", "a"), ("a1.b1.c2", "2"), ("a2.b2.c3", "abcdefgh"), ("a2.b1.c1", "f"), ("a1.b1.c3", "3"), ("a2.b1.c2", False), ("a2.b1.c3", None), ("a1.b1.c1", "1"), ("a2.b2.c1", 10), ("a1.b2.c3", "1.1"), ] )
def test_merging_values(): # type: ignore DICT5_1 = {"a5.b1.c2": 3} DICT5_2 = {"a5.b1.c1": 1, "a5.b1.c2": 2} cfg = ConfigurationSet( config_from_dict(DICT5_1), config_from_dict(DICT5_2), ) assert cfg["a5.b1"] == {"c1": 1, "c2": 3} assert cfg.a5.b1 == {"c1": 1, "c2": 3}
def test_configset_setitem(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT, lowercase_keys=False), config_from_dict(PROTECTED, lowercase_keys=False), ) with cfg.dotted_iter(): assert len(cfg) == 17 assert cfg["a1.b2.c1"] == "a" cfg["a1.b2.c1"] = 89 with cfg.dotted_iter(): assert len(cfg) == 17 assert cfg["a1.b2.c1"] == 89 cfg["a1.b2.c4"] = True with cfg.dotted_iter(): assert len(cfg) == 18 assert cfg["a1.b2.c1"] == 89 assert cfg["a1.b2.c4"] is True cfg["a3"] = {"b1": 10, "b2": "test"} with cfg.dotted_iter(): assert len(cfg) == 20 assert cfg["a3.b1"] == 10 assert cfg["a3.b2"] == "test"
def test_configset_update(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT, lowercase_keys=True), config_from_dict(PROTECTED, lowercase_keys=True), ) with cfg.dotted_iter(): assert len(cfg) == 17 assert cfg["a1.b2.c1"] == "a" cfg.update(NESTED) with cfg.dotted_iter(): assert len(cfg) == 17 assert cfg["a1.b2.c1"] == "a0" cfg.update({"important_password_2": "abc"}) with cfg.dotted_iter(): assert len(cfg) == 18
def test_load_env(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) # from env assert cfg["a1.b1.c1"] == "1" assert cfg["a1.b1"].get_int("c1") == 1 assert cfg["a1.b1"].as_dict() == {"c1": "1", "c2": "2", "c3": "3"} assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": "True", "c3": "1.1"} # from dict assert cfg["a2.b1.c1"] == "f" assert cfg["a2.b2"].as_dict() == {"c1": 10, "c2": "YWJjZGVmZ2g=", "c3": "abcdefgh"}
def test_fails(): # type: ignore cfg = ConfigurationSet(config_from_dict(DICT2_1), config_from_dict(DICT2_2), config_from_env(prefix=PREFIX)) with raises(KeyError, message="a1.b2.c3.d4"): assert cfg["a1.b2.c3.d4"] is Exception with raises(KeyError, message="'c4'"): assert cfg.a1.b2.c4 is Exception with raises(ValueError, message="Expected a valid True or False expression."): assert cfg["a1.b2"].get_bool("c3") is Exception
def test_configset_pop(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT, lowercase_keys=True), config_from_dict(PROTECTED, lowercase_keys=True), ) with cfg.dotted_iter(): assert len(cfg) == 17 assert cfg.pop("a2.b1.c1") == "f" assert cfg.pop("a2.b1.c1", "something") == "something" with raises(KeyError): cfg.pop("a2.b1.c1") with cfg.dotted_iter(): assert len(cfg) == 16
def test_dict_methods_items(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert dict(cfg.items()) == dict( [ ("a2.b2.c2", "YWJjZGVmZ2g="), ("a1.b2.c2", "True"), ("a1.b2.c1", "a"), ("a1.b1.c2", "2"), ("a2.b2.c3", "abcdefgh"), ("a2.b1.c1", "f"), ("a1.b1.c3", "3"), ("a2.b1.c2", False), ("a2.b1.c3", None), ("a1.b1.c1", "1"), ("a2.b2.c1", 10), ("a1.b2.c3", "1.1"), ] )
def test_repr(): # type: ignore import sys path = os.path.join(os.path.dirname(__file__), 'python_config.py') cfg = ConfigurationSet(config_from_dict(DICT2_1), config_from_dict(DICT2_2), config_from_env(prefix=PREFIX), config_from_python(path, prefix='CONFIG')) joined_dicts = dict((k, str(v)) for k, v in DICT1.items()) joined_dicts.update(DICT2_1) joined_dicts.update(DICT2_2) joined_dicts['sys.version'] = sys.hexversion assert str(dict( (k.lower(), v) for k, v in joined_dicts.items())) in repr(cfg)
def test_get_dict(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert cfg.get_dict("a2") == { "b1.c1": "f", "b1.c2": False, "b1.c3": None, "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", } assert cfg.a2.as_dict() == { "b1.c1": "f", "b1.c2": False, "b1.c3": None, "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", } assert dict(cfg.a2) == { "b1.c1": "f", "b1.c2": False, "b1.c3": None, "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", } with raises(KeyError): assert cfg.get_dict("a3") is Exception assert set( cfg.a2.values()) == {"f", False, None, 10, "YWJjZGVmZ2g=", "abcdefgh"} assert dict(cfg.a2) == dict(cfg.a2.items())
def test_fails(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) with pytest.raises(KeyError, match="a1.b2.c3.d4"): assert cfg["a1.b2.c3.d4"] is Exception with pytest.raises(KeyError, match="c4"): assert cfg.a1.b2.c4 is Exception with pytest.raises(ValueError, match="Expected a valid True or False expression."): assert cfg["a1.b2"].get_bool("c3") is Exception
def test_load_env(): # type: ignore cfg = ConfigurationSet(config_from_dict(DICT2_1), config_from_dict(DICT2_2), config_from_env(prefix=PREFIX)) # from env assert cfg["a1.b1.c1"] == '1' assert cfg["a1.b1"].get_int('c1') == 1 assert cfg["a1.b1"].as_dict() == {"c1": '1', "c2": '2', "c3": '3'} assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": 'True', "c3": '1.1'} # from dict assert cfg["a2.b1.c1"] == 'f' assert cfg["a2.b2"].as_dict() == { 'c1': 10, 'c2': 'YWJjZGVmZ2g=', 'c3': 'abcdefgh' }
def test_repr(): # type: ignore import sys path = os.path.join(os.path.dirname(__file__), "python_config.py") cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), config_from_python(path, prefix="CONFIG", lowercase_keys=True), ) joined_dicts = dict((k, str(v)) for k, v in DICT1.items()) joined_dicts.update(DICT2_1) joined_dicts.update(DICT2_2) joined_dicts["sys.version"] = sys.hexversion assert str(dict( (k.lower(), v) for k, v in joined_dicts.items())) in repr(cfg)