source: rtems-central/rtemsqual/tests/test_items_item.py @ 833470c

Last change on this file since 833470c was 833470c, checked in by Sebastian Huber <sebastian.huber@…>, on May 6, 2020 at 10:36:53 AM

items: Add Item.get()

  • 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"}))
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
105
106def _is_enabled(enabled, enabled_by):
107    item = Item(EmptyCache(), "i", {"enabled-by": enabled_by})
108    return item.is_enabled(enabled)
109
110
111def test_is_enabled():
112    assert _is_enabled([], None)
113    assert _is_enabled([], [])
114    assert not _is_enabled([], ["A"])
115    assert _is_enabled(["A"], "A")
116    assert not _is_enabled(["B"], "A")
117    assert _is_enabled(["A"], ["A"])
118    assert not _is_enabled(["B"], ["A"])
119    assert _is_enabled(["A"], ["A", "B"])
120    assert _is_enabled(["B"], ["A", "B"])
121    assert not _is_enabled(["C"], ["A", "B"])
122    assert not _is_enabled(["A"], {"not": "A"})
123    assert _is_enabled(["B"], {"not": "A"})
124    assert not _is_enabled(["A"], {"and": ["A", "B"]})
125    assert _is_enabled(["A", "B"], {"and": ["A", "B"]})
126    assert _is_enabled(["A", "B", "C"], {"and": ["A", "B"]})
127    assert _is_enabled(["A", "B"], {"and": ["A", "B", {"not": "C"}]})
128    assert not _is_enabled(["A", "B", "C"], {"and": ["A", "B", {"not": "C"}]})
129    assert not _is_enabled(["A"], {"and": "A", "x": "y"})
130    assert not _is_enabled(["A"], {"x": "A"})
131    assert _is_enabled([], {"not": {"and": ["A", {"not": "A"}]}})
132
133
134def test_save_and_load(tmpdir):
135    item_file = os.path.join(tmpdir, "i.yml")
136    item = Item(EmptyCache(), "i", {"k": "v"})
137    item.file = item_file
138    assert item.file == item_file
139    item.save()
140    with open(item_file, "r") as src:
141        assert src.read() == "k: v\n"
142    assert item.file == item_file
143
144    item2 = Item(EmptyCache(), "i2", {})
145    item2.file = item_file
146    with pytest.raises(KeyError):
147        item2["k"]
148    item2.load()
149    assert item2["k"] == "v"
150    assert item.file == item_file
Note: See TracBrowser for help on using the repository browser.