diff options
author | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2016-11-28 17:32:40 +0000 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2016-11-28 17:37:31 +0000 |
commit | 0cf884dbabcc039c0771456c55bcf086110ddb54 (patch) | |
tree | 4585c42dcadb8532f6060c077da440e28b57818c /tests | |
parent | 18a7c637844b8f321716cef0549e46b376396ceb (diff) | |
download | buildstream-0cf884dbabcc039c0771456c55bcf086110ddb54.tar.gz |
Added basic yaml tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/yaml/__init__.py | 0 | ||||
-rw-r--r-- | tests/yaml/data/basics.yaml | 26 | ||||
-rw-r--r-- | tests/yaml/data/composite.yaml | 9 | ||||
-rw-r--r-- | tests/yaml/yaml.py | 189 |
4 files changed, 224 insertions, 0 deletions
diff --git a/tests/yaml/__init__.py b/tests/yaml/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/yaml/__init__.py diff --git a/tests/yaml/data/basics.yaml b/tests/yaml/data/basics.yaml new file mode 100644 index 000000000..e57d2163a --- /dev/null +++ b/tests/yaml/data/basics.yaml @@ -0,0 +1,26 @@ +kind: pony +description: The vehicle of choice for rainbow travel +moods: +- happy +- sad +children: +- name: dopey + mood: silly +- name: grumpy + mood: grumpy +- name: happy + mood: happy +- name: doc + mood: happy +- name: sneezy + mood: curious +- name: bashful + mood: bashful +- name: sleepy + mood: sleepy +extra: + this: that + old: new + + + diff --git a/tests/yaml/data/composite.yaml b/tests/yaml/data/composite.yaml new file mode 100644 index 000000000..10196005c --- /dev/null +++ b/tests/yaml/data/composite.yaml @@ -0,0 +1,9 @@ +kind: horse +description: The horse you ride +children: +- name: extra + mood: funny +extra: + another: + one: one + two: two diff --git a/tests/yaml/yaml.py b/tests/yaml/yaml.py new file mode 100644 index 000000000..12389c499 --- /dev/null +++ b/tests/yaml/yaml.py @@ -0,0 +1,189 @@ +import os +import pytest + +from buildstream import _yaml +from buildstream import LoadError, LoadErrorReason +from buildstream._yaml import CompositePolicy + +DATA_DIR = os.path.join( + os.path.dirname(os.path.realpath(__file__)), + 'data', +) + +@pytest.mark.datafiles(os.path.join(DATA_DIR)) +def test_load_yaml(datafiles): + + filename = os.path.join(datafiles.dirname, + datafiles.basename, + 'basics.yaml') + + loaded = _yaml.load(filename) + assert(loaded.get('kind') == 'pony') + +def assert_provenance(filename, line, col, node, key=None, indices=[]): + provenance = _yaml.node_get_provenance(node, key=key, indices=indices) + + if key: + if indices: + assert(isinstance(provenance, _yaml.ElementProvenance)) + else: + assert(isinstance(provenance, _yaml.MemberProvenance)) + else: + assert(isinstance(provenance, _yaml.DictProvenance)) + + assert(provenance.filename == filename) + assert(provenance.line == line) + assert(provenance.col == col) + + +@pytest.mark.datafiles(os.path.join(DATA_DIR)) +def test_basic_provenance(datafiles): + + filename = os.path.join(datafiles.dirname, + datafiles.basename, + 'basics.yaml') + + loaded = _yaml.load(filename) + assert(loaded.get('kind') == 'pony') + + assert_provenance(filename, 1, 0, loaded) + +@pytest.mark.datafiles(os.path.join(DATA_DIR)) +def test_member_provenance(datafiles): + + filename = os.path.join(datafiles.dirname, + datafiles.basename, + 'basics.yaml') + + loaded = _yaml.load(filename) + assert(loaded.get('kind') == 'pony') + assert_provenance(filename, 2, 13, loaded, 'description') + +@pytest.mark.datafiles(os.path.join(DATA_DIR)) +def test_element_provenance(datafiles): + + filename = os.path.join(datafiles.dirname, + datafiles.basename, + 'basics.yaml') + + loaded = _yaml.load(filename) + assert(loaded.get('kind') == 'pony') + assert_provenance(filename, 5, 2, loaded, 'moods', [ 1 ]) + +@pytest.mark.datafiles(os.path.join(DATA_DIR)) +def test_composited_overwrite_provenance(datafiles): + + filename = os.path.join(datafiles.dirname, + datafiles.basename, + 'basics.yaml') + overlayfile = os.path.join(datafiles.dirname, + datafiles.basename, + 'composite.yaml') + + base = _yaml.load(filename) + assert(base.get('kind') == 'pony') + assert_provenance(filename, 1, 0, base) + + overlay = _yaml.load(overlayfile) + assert(overlay.get('kind') == 'horse') + assert_provenance(overlayfile, 1, 0, overlay) + + _yaml.composite_dict(base, overlay, policy=CompositePolicy.OVERWRITE, typesafe=True) + assert(base.get('kind') == 'horse') + + children = base.get('children') + assert(isinstance(children, list)) + assert(len(children) == 1) + + assert_provenance(filename, 1, 0, base) + + # The entire children member is overwritten with the overlay + assert_provenance(overlayfile, 4, 0, base, 'children') + assert_provenance(overlayfile, 4, 2, base, 'children', [ 0 ]) + + # The child dict itself has the overlay provenance + child = children[0] + assert_provenance(overlayfile, 5, 8, child, 'mood') + + +@pytest.mark.datafiles(os.path.join(DATA_DIR)) +def test_composited_array_append_provenance(datafiles): + + filename = os.path.join(datafiles.dirname, + datafiles.basename, + 'basics.yaml') + overlayfile = os.path.join(datafiles.dirname, + datafiles.basename, + 'composite.yaml') + + base = _yaml.load(filename) + assert(base.get('kind') == 'pony') + assert_provenance(filename, 1, 0, base) + + overlay = _yaml.load(overlayfile) + assert(overlay.get('kind') == 'horse') + assert_provenance(overlayfile, 1, 0, overlay) + + _yaml.composite_dict(base, overlay, policy=CompositePolicy.ARRAY_APPEND, typesafe=True) + assert(base.get('kind') == 'horse') + + children = base.get('children') + assert(isinstance(children, list)) + assert(len(children) == 8) + + assert_provenance(filename, 1, 0, base) + assert_provenance(filename, 7, 0, base, 'children') + + # The newly added element has the overlay provenance + assert_provenance(overlayfile, 4, 2, base, 'children', [ 7 ]) + + # The child dict itself has the overlay provenance + child = children[7] + assert_provenance(overlayfile, 5, 8, child, 'mood') + + extra = base.get('extra') + assert(isinstance(extra, dict)) + another = extra.get('another') + assert(isinstance(another, dict)) + + assert_provenance(filename, 22, 2, base, 'extra') + assert_provenance(filename, 22, 2, extra) + assert_provenance(filename, 22, 8, extra, 'this') + + assert_provenance(overlayfile, 8, 4, extra, 'another') + assert_provenance(overlayfile, 8, 4, another) + + + + +@pytest.mark.datafiles(os.path.join(DATA_DIR)) +def test_node_get(datafiles): + + filename = os.path.join(datafiles.dirname, + datafiles.basename, + 'basics.yaml') + overlayfile = os.path.join(datafiles.dirname, + datafiles.basename, + 'composite.yaml') + + base = _yaml.load(filename) + assert(base.get('kind') == 'pony') + overlay = _yaml.load(overlayfile) + assert(overlay.get('kind') == 'horse') + _yaml.composite_dict(base, overlay, policy=CompositePolicy.ARRAY_APPEND, typesafe=True) + assert(base.get('kind') == 'horse') + + children = _yaml.node_get(base, list, 'children') + assert(isinstance(children, list)) + assert(len(children) == 8) + + child = _yaml.node_get(base, dict, 'children', indices=[7]) + assert_provenance(overlayfile, 5, 8, child, 'mood') + + extra = _yaml.node_get(base, dict, 'extra') + another = _yaml.node_get(extra, dict, 'another') + + with pytest.raises(LoadError) as exc: + wrong = _yaml.node_get(another, dict, 'five') + + assert (exc.value.reason == LoadErrorReason.INVALID_DATA) |