Passed
Push — rhel8-release-exp ( 442f34...52bf4e )
by
unknown
09:37
created

tests.unit_tests.test_json_transitions   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 146
dl 0
loc 250
rs 10
c 0
b 0
f 0
wmc 5

5 Functions

Rating   Name   Duplication   Size   Complexity  
A test_rearrange_references() 0 47 1
A test_remove_empty_values() 0 17 1
A test_rearrange_identifiers() 0 45 1
A test_is_not_empty() 0 16 1
B test_remove_not_selected_rules() 0 105 1
1
# Copyright 2022, Red Hat, Inc.
2
# SPDX-License-Identifier: LGPL-2.1-or-later
3
4
5
import pytest
6
7
from openscap_report.scap_results_parser.data_structures.json_transformation import (
8
    is_not_empty, rearrange_identifiers, rearrange_references,
9
    remove_empty_values, remove_not_selected_rules)
10
11
12
@pytest.mark.unit_test
13
@pytest.mark.parametrize(
14
    "dictionary_json, result",
15
    [
16
        (
17
            {
18
                "rules": {
19
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
20
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
21
                        "identifiers": [
22
                            {
23
                                "text": "CCE-90841-8",
24
                                "system": "random-link.com",
25
                            }
26
                        ],
27
                        "references": [
28
                            {
29
                                "text": "11",
30
                                "href": "idk-link.com",
31
                            }
32
                        ],
33
                    }
34
                }
35
            },
36
            {
37
                "rules": {
38
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
39
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
40
                        "identifiers": [
41
                            {
42
                                "text": "CCE-90841-8",
43
                                "system": "random-link.com",
44
                            }
45
                        ],
46
                        "references": [
47
                            "11",
48
                        ],
49
                    }
50
                },
51
                "references": {"11": "idk-link.com"},
52
            },
53
        )
54
    ],
55
)
56
def test_rearrange_references(dictionary_json, result):
57
    rearrange_references(dictionary_json)
58
    assert dictionary_json == result
59
60
61
@pytest.mark.unit_test
62
@pytest.mark.parametrize(
63
    "dictionary_json, result",
64
    [
65
        (
66
            {
67
                "rules": {
68
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
69
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
70
                        "identifiers": [
71
                            {
72
                                "text": "CCE-90841-8",
73
                                "system": "random-link.com",
74
                            }
75
                        ],
76
                        "references": [
77
                            {
78
                                "text": "11",
79
                                "href": "idk-link.com",
80
                            }
81
                        ],
82
                    }
83
                }
84
            },
85
            {
86
                "rules": {
87
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
88
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
89
                        "identifiers": ["CCE-90841-8"],
90
                        "references": [
91
                            {
92
                                "text": "11",
93
                                "href": "idk-link.com",
94
                            }
95
                        ],
96
                    }
97
                },
98
                "identifiers": {"CCE-90841-8": "random-link.com"},
99
            },
100
        )
101
    ],
102
)
103
def test_rearrange_identifiers(dictionary_json, result):
104
    rearrange_identifiers(dictionary_json)
105
    assert dictionary_json == result
106
107
108
@pytest.mark.unit_test
109
@pytest.mark.parametrize(
110
    "val, result",
111
    [
112
        (None, False),
113
        ([], False),
114
        ({}, False),
115
        (1.3, True),
116
        (0.0, True),
117
        (-1.3, True),
118
        (["a", "b"], True),
119
        ({"a": "b", "c": "d"}, True),
120
    ],
121
)
122
def test_is_not_empty(val, result):
123
    assert is_not_empty(val) == result
124
125
126
@pytest.mark.unit_test
127
@pytest.mark.parametrize(
128
    "dictionary_json, ids_of_selected_rules, result",
129
    [
130
        (
131
            {
132
                "rules": {
133
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
134
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
135
                        "result": "pass",
136
                    },
137
                    "rule_a": {
138
                        "rule_id": "rule_a",
139
                        "result": "notselected",
140
                    },
141
                }
142
            },
143
            ["xccdf_org.ssgproject.content_rule_rpm_verify_hashes"],
144
            {
145
                "rules": {
146
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
147
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
148
                        "result": "pass",
149
                    },
150
                }
151
            },
152
        ),
153
        (
154
            {
155
                "rules": {
156
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
157
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
158
                        "result": "pass",
159
                    },
160
                    "rule_a": {
161
                        "rule_id": "rule_a",
162
                        "result": "notselected",
163
                    },
164
                }
165
            },
166
            [],
167
            {
168
                "rules": {
169
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
170
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
171
                        "result": "pass",
172
                    },
173
                }
174
            },
175
        ),
176
        (
177
            {
178
                "rules": {
179
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
180
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
181
                        "result": "pass",
182
                    },
183
                    "rule_a": {
184
                        "rule_id": "rule_a",
185
                        "result": "fail",
186
                    },
187
                }
188
            },
189
            ["xccdf_org.ssgproject.content_rule_rpm_verify_hashes"],
190
            {
191
                "rules": {
192
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
193
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
194
                        "result": "pass",
195
                    },
196
                }
197
            },
198
        ),
199
        (
200
            {
201
                "rules": {
202
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
203
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
204
                        "result": "pass",
205
                    },
206
                    "rule_a": {
207
                        "rule_id": "rule_a",
208
                        "result": "fail",
209
                    },
210
                }
211
            },
212
            ["xccdf_org.ssgproject.content_rule_rpm_verify_hashes", "rule_a"],
213
            {
214
                "rules": {
215
                    "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
216
                        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
217
                        "result": "pass",
218
                    },
219
                    "rule_a": {
220
                        "rule_id": "rule_a",
221
                        "result": "fail",
222
                    },
223
                }
224
            },
225
        ),
226
    ],
227
)
228
def test_remove_not_selected_rules(dictionary_json, ids_of_selected_rules, result):
229
    remove_not_selected_rules(dictionary_json, ids_of_selected_rules)
230
    assert dictionary_json == result
231
232
233
@pytest.mark.unit_test
234
@pytest.mark.parametrize(
235
    "dictionary, result",
236
    [
237
        ({"a": "", "b": {"c": ""}, "x": "x"}, {"x": "x"}),
238
        ({"a": "a", "b": "", "x": "x"}, {"a": "a", "x": "x"}),
239
        ({"a": [], "x": "x"}, {"x": "x"}),
240
        ({"a": {}, "x": "x"}, {"x": "x"}),
241
        ({"a": 3.14, "b": {}, "x": "x"}, {"a": 3.14, "x": "x"}),
242
        (
243
            {"a": "", "b": {"c": {"z": "y"}, "m": "o"}, "x": "x"},
244
            {"b": {"c": {"z": "y"}, "m": "o"}, "x": "x"},
245
        ),
246
    ],
247
)
248
def test_remove_empty_values(dictionary, result):
249
    assert remove_empty_values(dictionary) == result
250