def test_get_value(): td = Tree() assert td.get("x.y", 1) == 1 assert "x.y" not in td assert td.setdefault("x.y", 2) == 2 assert "x.y" in td
def test_loader_set_default_tree(): tree = Tree({"foo": "bar"}) load = Loader(tree=tree) assert id(load.tree) == id(tree) tree = Tree() load = Loader(tree=tree) assert id(load.tree) == id(tree)
def test_resolver_proxy(): tree = Tree({"foo": Promise(lambda: 42), "bar": "baz"}) tree = ResolverProxy(tree, "/test/source.yaml") assert tree["foo"] == 42 assert tree["bar"] == "baz" assert tree["__file__"] == "/test/source.yaml" assert tree["__dir__"] == "/test" assert set(tree.keys()) == set(["foo", "bar"]) with pytest.raises(KeyError): tree["baz"]
def test_read_write(): td = Tree() td["1"] = 1 td["a.2"] = 2 td["a.b.3"] = 3 td["a"]["b.4"] = 4 td["a"]["b"]["5"] = 5 td["a.b"]["6"] = 6 assert td["1"] == 1 assert td["a.2"] == 2 assert td["a.b.3"] == 3 assert td["a.b.4"] == 4 assert td["a.b.5"] == 5 assert td["a.b.6"] == 6 assert td["a"]["2"] == 2 assert td["a"]["b.3"] == 3 assert td["a"]["b.4"] == 4 assert td["a"]["b.5"] == 5 assert td["a"]["b.6"] == 6 assert td["a"]["b"]["3"] == 3 assert td["a"]["b"]["4"] == 4 assert td["a"]["b"]["5"] == 5 assert td["a"]["b"]["6"] == 6 assert td["a.b"]["3"] == 3 assert td["a.b"]["4"] == 4 assert td["a.b"]["5"] == 5 assert td["a.b"]["6"] == 6
def test_update_action_branch(): tree = Tree({"x.a": 1, "x.b": 2}) action = UpdateAction(tree, "x.c", 3, "/test/source.yaml") assert action.branch == action.tree["x"] action = UpdateAction(tree, "y", 3, "/test/source.yaml") assert action.branch == action.tree
def test_updater_set_default(): tree = Tree({"foo": "bar"}) update = Updater() update(tree, "foo?", "baz", "/test/source.yaml") assert tree == {"foo": "bar"} update(tree, "bar?", "baz", "/test/source.yaml") assert tree == {"foo": "bar", "bar": "baz"}
def test_updater_format_value(): tree = Tree({"x.a": 1, "x.b": 2}) update = Updater() update(tree, "x.c", "$>> {self[x.a]} {branch[b]}", "/test/source.yaml") assert isinstance(tree["x.c"], Promise) assert tree["x.c"]() == "1 2" tree["x.a"] = "foo" tree["x.b"] = "bar" assert tree["x.c"]() == "foo bar"
def td(): return Tree({ "1": 1, "a.2": 2, "a.b.3": 3, "a.b.4": 4, "a.b.5": 5, "a.b.6": 6 })
def test_get_branch(): td = Tree() bx = td.branch("x") assert "x" not in td # Empty branch not in tree bx["1"] = 1 assert "x" in td assert "x.1" in td bxy = bx.branch("y") assert "x.y" not in td assert "y" not in bx bxy["2"] = 2 assert "x.y.2" in td assert "x.y" in td assert "y.2" in bx assert "y" in bx
def test_updater_printf_value(): tree = Tree({"x.a": 1, "x.b": 2}) update = Updater() update(tree, "x.c", "%>> %(x.a)s %(x.b)r", "/test/source.yaml") assert isinstance(tree["x.c"], Promise) assert tree["x.c"]() == "1 2" tree["x.a"] = "foo" tree["x.b"] = "bar" assert tree["x.c"]() == "foo 'bar'"
def test_updater_eval_value(): tree = Tree({"x.a": 5.0, "x.b": 2}) update = Updater(namespace={"floor": math.floor}) update(tree, "x.c", '>>> floor(self["x.a"] / branch["b"])', "/test/source.yaml") assert isinstance(tree["x.c"], Promise) assert tree["x.c"]() == 2.0 tree["x.a"] = 10.0 tree["x.b"] = 3 assert tree["x.c"]() == 3.0
def test_updater_required_valeue(): tree = Tree() update = Updater() update(tree, "foo", "!!!", "/test/source.yaml") update(tree, "bar", "!!! Update me", "/test/source.yaml") assert isinstance(tree["foo"], Required) assert repr(tree["foo"]) == "Undefined required key <foo>" assert isinstance(tree["bar"], Required) assert repr(tree["bar"]) == "Undefined required key <bar>: Update me"
def test_postprocessor_check_required(): tree = Tree({ "foo": Required("foo", ""), "bar": Required("bar", "Update me") }) postprocess = PostProcessor() with pytest.raises(ProcessingError) as info: postprocess(tree) assert info.value.args == (tree["bar"], tree["foo"])
def test_update_action_update(): tree = Tree() action = UpdateAction(tree, "foo", "bar", "/test/source.yaml") action.update = lambda a: a.tree.setdefault(a.key, a.value) action() assert tree == {"foo": "bar"} action.value = "baz" action() assert tree == {"foo": "bar"}
def test_override_branch(): td = Tree({"x.y.1": 1}) td["x"] = 1 assert "x.y.1" not in td assert "x.y" not in td assert td["x"] == 1 td["x.y.1"] = 1 assert "x.y.1" in td assert "x.y" in td assert td["x"] == {"y.1": 1}
def test_updater_call_method(): tree = Tree({"foo": []}) update = Updater() update(tree, "foo#append", 1, "/test/source.yaml") assert tree["foo"] == [1] update(tree, "foo#append", ">>> 2", "/test/source.yaml") assert isinstance(tree["foo"], Promise) assert tree["foo"](), [1, 2] update(tree, "foo#append", 3, "/test/source.yaml") assert isinstance(tree["foo"], Promise) assert tree["foo"](), [1, 2, 3]
def test_pop_key_error(): td = Tree() with pytest.raises(KeyError): td.pop("x")
def test_postprocessor_resolve_promise(): tree = Tree({"foo": Promise(lambda: 42), "bar": "baz"}) postprocess = PostProcessor() postprocess(tree) assert tree == {"foo": 42, "bar": "baz"}
def test_update_action_default_update(): tree = Tree() UpdateAction(tree, "foo", "bar", "/test/source.yaml")() assert tree == {"foo": "bar"}
def test_delete_key_error(): td = Tree() with pytest.raises(KeyError): del td["x"]
def test_rarefy(): rd = rarefy(Tree({"a.b.c": 1, "x.y.z": 1})) assert rd == {"a": {"b": {"c": 1}}, "x": {"y": {"z": 1}}} rd = rarefy({"a.b.c": {"x.y.z": 1}}) assert rd == {"a": {"b": {"c": {"x": {"y": {"z": 1}}}}}}
def test_update_action_promise(): action = UpdateAction(Tree(), "foo", "bar", "/test/source.yaml") promise = action.promise(lambda: int(None)) with pytest.raises(TypeError) as info: promise() assert info.value.args[-1] == action
def test_update_action_repr(): action = UpdateAction(Tree(), "foo", "bar", "/test/source.yaml") assert repr(action) == "<tree['foo'] = 'bar' from '/test/source.yaml'>"
def test_tree_key_error(): td = Tree() with pytest.raises(KeyError): td["x"]
def test_branch_key_error(): td = Tree({"a.y": 1}) with pytest.raises(KeyError): td["a"]["x"]
from os import linesep from configtree import formatter from configtree.tree import Tree t = Tree({ "a.x": 1, "a.y": 'Testing "json"', "a.z": "Testing 'shell'", "list": ['Testing "json"', "Testing 'shell'"], "none": None, "bool": True, }) def test_json(): result = formatter.to_json(t, indent=4, sort=True) result = [line.rstrip() for line in result.split(linesep)] assert result == [ "{", ' "a.x": 1,', ' "a.y": "Testing \\"json\\"",', ' "a.z": "Testing \'shell\'",', ' "bool": true,', ' "list": [', ' "Testing \\"json\\"",', " \"Testing 'shell'\"", " ],", ' "none": null', "}", ]
def test_repr(): td = Tree() td["x.y"] = 1 assert repr(td) == "Tree({'x.y': 1})" assert repr(td["x"]) == "BranchProxy('x'): {'y': 1}"