Passed
Pull Request — master (#4216)
by Matěj
02:33 queued 10s
created

test_utils.test_subset_dict()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 0
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
1
import pytest
2
3
import ssg.utils
4
5
6
def test_is_applicable():
7
    assert ssg.utils.is_applicable('all', 'rhel7')
8
    assert ssg.utils.is_applicable('multi_platform_all', 'rhel7')
9
    assert ssg.utils.is_applicable('rhel7', 'rhel7')
10
    assert ssg.utils.is_applicable('multi_platform_rhel', 'rhel7')
11
    assert ssg.utils.is_applicable('Red Hat Enterprise Linux 7', 'rhel7')
12
13
    assert ssg.utils.is_applicable('all', 'rhosp13')
14
    assert ssg.utils.is_applicable('multi_platform_rhosp', 'rhosp13')
15
    assert ssg.utils.is_applicable('rhosp13', 'rhosp13')
16
    assert ssg.utils.is_applicable('Red Hat OpenStack Platform 13', 'rhosp13')
17
    assert not ssg.utils.is_applicable('rhel7', 'rhosp13')
18
19
    assert not ssg.utils.is_applicable('rhosp13', 'rhel7')
20
    assert not ssg.utils.is_applicable('fedora,multi_platform_ubuntu', 'rhel7')
21
    assert not ssg.utils.is_applicable('ol7', 'rhel7')
22
    assert not ssg.utils.is_applicable('fedora,debian8', 'rhel7')
23
24
25
def test_is_applicable_for_product():
26
    assert ssg.utils.is_applicable_for_product("multi_platform_all", "rhel7")
27
    assert ssg.utils.is_applicable_for_product("multi_platform_rhel", "rhel7")
28
    assert ssg.utils.is_applicable_for_product("multi_platform_rhel,multi_platform_ol", "rhel7")
29
    assert ssg.utils.is_applicable_for_product("Red Hat Enterprise Linux 7", "rhel7")
30
    assert not ssg.utils.is_applicable_for_product("Red Hat Enterprise Linux 7", "rhel6")
31
    assert not ssg.utils.is_applicable_for_product("multi_platform_ol", "rhel7")
32
33
34
def test_map_name():
35
    mn = ssg.utils.map_name
36
37
    assert mn('multi_platform_rhel') == 'Red Hat Enterprise Linux'
38
    assert mn('rhel') == 'Red Hat Enterprise Linux'
39
    assert mn('rhel7') == 'Red Hat Enterprise Linux'
40
    assert mn('rhosp') == 'Red Hat OpenStack Platform'
41
    assert mn('rhosp13') == 'Red Hat OpenStack Platform'
42
43
    with pytest.raises(RuntimeError):
44
        mn('not-a-platform')
45
46
    with pytest.raises(RuntimeError):
47
        mn('multi_platform_all')
48
49
50
def test_parse_name():
51
    pn = ssg.utils.parse_name
52
53
    n, v = pn("rhel7")
54
    assert n == "rhel"
55
    assert v == "7"
56
57
    n, v = pn("rhel")
58
    assert n == "rhel"
59
    assert not v
60
61
    n, v = pn("rhosp13")
62
    assert n == "rhosp"
63
    assert v == "13"
64
65
66
def test_merge_dicts():
67
    left = {1: 2}
68
    right = {"red fish": "blue fish"}
69
    merged_expected = {1: 2, "red fish": "blue fish"}
70
    merged_actual = ssg.utils.merge_dicts(left, right)
71
72
    assert merged_actual == merged_expected
73
    assert 1 in left
74
    assert "red fish" not in left
75
    assert "red fish" in right
76
    assert 1 not in right
77
78
79
def test_required_key():
80
    rk = ssg.utils.required_key
81
    _dict = {'something': 'some_value',
82
             'something_else': 'other_value'}
83
84
    assert rk(_dict, 'something') == 'some_value'
85
    assert rk(_dict, 'something_else') == 'other_value'
86
87
    with pytest.raises(ValueError):
88
        rk(_dict, 'not-in-dict')
89
90
91
def test_subset_dict():
92
    _dict = {1: 2, "red fish": "blue fish"}
93
94
    _keys = [1]
95
    assert ssg.utils.subset_dict(_dict, _keys) == {1: 2}
96
    assert _keys == [1]
97
    assert _dict == {1: 2, "red fish": "blue fish"}
98
99
    assert ssg.utils.subset_dict(_dict, ["red fish"]) == {"red fish": "blue fish"}
100
    assert ssg.utils.subset_dict(_dict, [1, "red fish"]) == _dict
101
    assert ssg.utils.subset_dict(_dict, []) == dict()
102