source: rtems-central/rtemsqual/tests/test_items_item.py @ 05246b3

Last change on this file since 05246b3 was 05246b3, checked in by Sebastian Huber <sebastian.huber@…>, on May 11, 2020 at 7:00:49 AM

items: Add and use Link.role

  • Property mode set to 100644
File size: 5.0 KB
Line 
1# SPDX-License-Identifier: BSD-2-Clause
2""" Unit tests for the rtemsqual.items module. """
3
4# Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de)
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26
27import os
28import pytest
29
30from rtemsqual.items import Item, ItemCache, Link
31
32
33class EmptyCache(ItemCache):
34    def __init__(self):
35        return
36
37
38def test_to_abs_uid():
39    item = Item(EmptyCache(), "/x/y", {})
40    assert item.to_abs_uid(".") == "/x/y"
41    assert item.to_abs_uid("z") == "/x/z"
42    assert item.to_abs_uid("/z") == "/z"
43    assert item.to_abs_uid("../z") == "/z"
44    assert item.to_abs_uid("../../z") == "/z"
45
46
47def test_uid():
48    item = Item(EmptyCache(), "x", {})
49    assert item.uid == "x"
50
51
52def test_contains():
53    data = {}
54    data["x"] = "y"
55    item = Item(EmptyCache(), "z", data)
56    assert "x" in item
57    assert "a" not in item
58
59
60def test_get_key_path():
61    data = {}
62    data["a"] = {"b": "c", "d": [1, 2, 3]}
63    data["x"] = "y"
64    item = Item(EmptyCache(), "z", data)
65    assert item.get_by_key_path("x") == "y"
66    assert item.get_by_key_path("a/d[2]") == 3
67    assert item.get_by_key_path("a/b/../d[0]") == 1
68    assert item.get_by_key_path("/a/b/../d[0]") == 1
69    assert item.get_by_key_path("../d[0]", "a/b") == 1
70    with pytest.raises(KeyError):
71        assert item.get_by_key_path("y")
72    with pytest.raises(ValueError):
73        assert item.get_by_key_path("[")
74    with pytest.raises(ValueError):
75        assert item.get_by_key_path("x[y]")
76
77
78def test_getitem():
79    data = {}
80    data["x"] = "y"
81    item = Item(EmptyCache(), "z", data)
82    assert item["x"] == "y"
83
84
85def test_get():
86    data = {}
87    data["x"] = "y"
88    item = Item(EmptyCache(), "z", data)
89    assert item.get("x", "z") == "y"
90    assert item.get("z", "a") == "a"
91
92
93def test_children():
94    child = Item(EmptyCache(), "c", {})
95    parent = Item(EmptyCache(), "p", {})
96    parent.add_link_to_child(Link(child, {"a": "b", "role": "c"}))
97    children = [item for item in parent.children()]
98    assert len(children) == 1
99    assert children[0] == child
100    links = [link for link in parent.links_to_children()]
101    assert len(links) == 1
102    assert links[0].item == child
103    assert links[0]["a"] == "b"
104    assert links[0].role == "c"
105
106
107def _is_enabled(enabled, enabled_by):
108    item = Item(EmptyCache(), "i", {"enabled-by": enabled_by})
109    return item.is_enabled(enabled)
110
111
112def test_is_enabled():
113    assert _is_enabled([], None)
114    assert _is_enabled([], [])
115    assert not _is_enabled([], ["A"])
116    assert _is_enabled(["A"], "A")
117    assert not _is_enabled(["B"], "A")
118    assert _is_enabled(["A"], ["A"])
119    assert not _is_enabled(["B"], ["A"])
120    assert _is_enabled(["A"], ["A", "B"])
121    assert _is_enabled(["B"], ["A", "B"])
122    assert not _is_enabled(["C"], ["A", "B"])
123    assert not _is_enabled(["A"], {"not": "A"})
124    assert _is_enabled(["B"], {"not": "A"})
125    assert not _is_enabled(["A"], {"and": ["A", "B"]})
126    assert _is_enabled(["A", "B"], {"and": ["A", "B"]})
127    assert _is_enabled(["A", "B", "C"], {"and": ["A", "B"]})
128    assert _is_enabled(["A", "B"], {"and": ["A", "B", {"not": "C"}]})
129    assert not _is_enabled(["A", "B", "C"], {"and": ["A", "B", {"not": "C"}]})
130    assert not _is_enabled(["A"], {"and": "A", "x": "y"})
131    assert not _is_enabled(["A"], {"x": "A"})
132    assert _is_enabled([], {"not": {"and": ["A", {"not": "A"}]}})
133
134
135def test_save_and_load(tmpdir):
136    item_file = os.path.join(tmpdir, "i.yml")
137    item = Item(EmptyCache(), "i", {"k": "v"})
138    item.file = item_file
139    assert item.file == item_file
140    item.save()
141    with open(item_file, "r") as src:
142        assert src.read() == "k: v\n"
143    assert item.file == item_file
144
145    item2 = Item(EmptyCache(), "i2", {})
146    item2.file = item_file
147    with pytest.raises(KeyError):
148        item2["k"]
149    item2.load()
150    assert item2["k"] == "v"
151    assert item.file == item_file
Note: See TracBrowser for help on using the repository browser.