source: rtems-central/rtemsqual/tests/test_items_item.py @ 9dad293

Last change on this file since 9dad293 was 9dad293, checked in by Sebastian Huber <sebastian.huber@…>, on 04/28/20 at 09:29:48

items: Use generator functions for item links

This enables an easy access to link attributes.

  • Property mode set to 100644
File size: 4.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, Link
31
32
33def test_to_abs_uid():
34    item = Item("/x/y", {})
35    assert item.to_abs_uid("z") == "/x/z"
36    assert item.to_abs_uid("/z") == "/z"
37    assert item.to_abs_uid("../z") == "/z"
38    assert item.to_abs_uid("../../z") == "/z"
39
40
41def test_uid():
42    item = Item("x", {})
43    assert item.uid == "x"
44
45
46def test_contains():
47    data = {}
48    data["x"] = "y"
49    item = Item("z", data)
50    assert "x" in item
51    assert "a" not in item
52
53
54def test_getitem():
55    data = {}
56    data["x"] = "y"
57    item = Item("z", data)
58    assert item["x"] == "y"
59
60
61def test_children():
62    child = Item("c", {})
63    parent = Item("p", {})
64    parent.add_link_to_child(Link(child, {"a": "b"}))
65    children = [item for item in parent.children()]
66    assert len(children) == 1
67    assert children[0] == child
68    links = [link for link in parent.links_to_children()]
69    assert len(links) == 1
70    assert links[0].item == child
71    assert links[0]["a"] == "b"
72
73
74def _is_enabled(enabled, enabled_by):
75    item = Item("i", {"enabled-by": enabled_by})
76    return item.is_enabled(enabled)
77
78
79def test_is_enabled():
80    assert _is_enabled([], None)
81    assert _is_enabled([], [])
82    assert not _is_enabled([], ["A"])
83    assert _is_enabled(["A"], "A")
84    assert not _is_enabled(["B"], "A")
85    assert _is_enabled(["A"], ["A"])
86    assert not _is_enabled(["B"], ["A"])
87    assert _is_enabled(["A"], ["A", "B"])
88    assert _is_enabled(["B"], ["A", "B"])
89    assert not _is_enabled(["C"], ["A", "B"])
90    assert not _is_enabled(["A"], {"not": "A"})
91    assert _is_enabled(["B"], {"not": "A"})
92    assert not _is_enabled(["A"], {"and": ["A", "B"]})
93    assert _is_enabled(["A", "B"], {"and": ["A", "B"]})
94    assert _is_enabled(["A", "B", "C"], {"and": ["A", "B"]})
95    assert _is_enabled(["A", "B"], {"and": ["A", "B", {"not": "C"}]})
96    assert not _is_enabled(["A", "B", "C"], {"and": ["A", "B", {"not": "C"}]})
97    assert not _is_enabled(["A"], {"and": "A", "x": "y"})
98    assert not _is_enabled(["A"], {"x": "A"})
99    assert _is_enabled([], {"not": {"and": ["A", {"not": "A"}]}})
100
101
102def test_save_and_load(tmpdir):
103    item_file = os.path.join(tmpdir, "i.yml")
104    item = Item("i", {"k": "v"})
105    item.file = item_file
106    assert item.file == item_file
107    item.save()
108    with open(item_file, "r") as src:
109        assert src.read() == "k: v\n"
110    assert item.file == item_file
111
112    item2 = Item("i2", {})
113    item2.file = item_file
114    with pytest.raises(KeyError):
115        item2["k"]
116    item2.load()
117    assert item2["k"] == "v"
118    assert item.file == item_file
Note: See TracBrowser for help on using the repository browser.