tests.unit_tests.test_json_transitions   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 149
dl 0
loc 255
rs 10
c 0
b 0
f 0
wmc 5

5 Functions

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