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_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_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 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_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_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_loader():
    update = Updater(namespace={"floor": math.floor})
    load = Loader(update=update)
    result = load(data_dir)
    assert result == {
        "a.x": 1,
        "a.y": 2,
        "b.x": 10,
        "b.y": 20,
        "subsystem.a.x": 101,
        "subsystem.a.y": 102,
        "subsystem.b.x": 110,
        "subsystem.b.y": 120,
        "c.x": 10,
        "c.y": 20,
        "c.z": 30,
    }

    walk = Walker(env="y")
    load = Loader(walk=walk, update=update)
    result = load(data_dir)
    assert result == {
        "a.x": 1,
        "a.y": 2,
        "b.x": 10,
        "b.y": 20,
        "subsystem.a.x": 101,
        "subsystem.a.y": 102,
        "subsystem.b.x": 110,
        "subsystem.b.y": 120,
        "a.b.x": 1,
        "a.b.y": 2,
        "a.b.z": 3,
        "a.b.c": "x = 1, y = 2",
        "a.b.l": [4, 5, 6],
        "z": 3,
        "path": os.path.join(data_dir, "somepath"),
        "here": os.path.join(data_dir, "env-y.yaml"),
        "c.x": 10,
        "c.y": 20,
        "c.z": 30,
    }