Passed
Pull Request — main (#130)
by Jan
06:04
created

tests.unit_tests.test_json_transitions   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 110
dl 0
loc 174
rs 10
c 0
b 0
f 0
wmc 4

4 Functions

Rating   Name   Duplication   Size   Complexity  
A test_rearrange_identifiers() 0 33 1
A test_is_not_empty() 0 13 1
B test_remove_not_selected_rules() 0 81 1
A test_rearrange_references() 0 30 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 (
0 ignored issues
show
Unused Code introduced by
Unused remove_empty_values imported from openscap_report.scap_results_parser.data_structures.json_transformation
Loading history...
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("dictionary_json, result", [
14
    ({"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
15
			"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
16
			"identifiers": [{
17
				"text":"CCE-90841-8",
18
                "system": "random-link.com",
19
            }],
20
			"references": [{
21
				"text":"11",
22
                "href": "idk-link.com",
23
            }],
24
		}
25
    }},
26
    {"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
27
			"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
28
			"identifiers": [{
29
				"text":"CCE-90841-8",
30
                "system": "random-link.com",
31
            }],
32
			"references": [
33
				"11",
34
			],
35
		}
36
    },
37
    "references": {"11": "idk-link.com"}})
38
])
39
def test_rearrange_references(dictionary_json, result):
40
    rearrange_references(dictionary_json)
41
    assert dictionary_json == result
42
43
44
@pytest.mark.unit_test
45
@pytest.mark.parametrize("dictionary_json, result", [
46
    ({"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
47
			"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
48
			"identifiers": [{
49
				"text":"CCE-90841-8",
50
                "system": "random-link.com",
51
            }],
52
			"references": [{
53
				"text":"11",
54
                "href": "idk-link.com",
55
            }],
56
		}
57
    }},
58
    {"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
59
			"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
60
			"identifiers": [
61
				"CCE-90841-8"
62
			],
63
            "references": [{
64
				"text":"11",
65
                "href": "idk-link.com",
66
            }],
67
		}
68
    },
69
    "identifiers": {"CCE-90841-8": "random-link.com"}})
70
])
71
def test_rearrange_identifiers(dictionary_json, result):
72
    rearrange_identifiers(dictionary_json)
73
    print(dictionary_json)
74
    print("aaaa")
75
    print(result)
76
    assert dictionary_json == result
77
78
79
@pytest.mark.unit_test
80
@pytest.mark.parametrize("val, result", [
81
    (None, False),
82
    ([], False),
83
    ({}, False),
84
    (1.3, True),
85
    (0.0, True),
86
    (-1.3, True),
87
    (["a", "b"], True),
88
    ({"a":"b", "c":"d"}, True),
89
])
90
def test_is_not_empty(val, result):
91
    assert is_not_empty(val) == result
92
93
@pytest.mark.unit_test
94
@pytest.mark.parametrize("dictionary_json, ids_of_selected_rules, result", [
95
    ({"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
96
			        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
97
                    "result": "pass",
98
		        },
99
                "rule_a": {
100
			        "rule_id": "rule_a",
101
                    "result": "notselected",
102
		        }
103
        }
104
    },
105
    ["xccdf_org.ssgproject.content_rule_rpm_verify_hashes"],
106
    {"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
107
			        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
108
                    "result": "pass",
109
		        },
110
        }
111
    }),
112
113
    ({"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
114
			        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
115
                    "result": "pass",
116
		        },
117
                "rule_a": {
118
			        "rule_id": "rule_a",
119
                    "result": "notselected",
120
		        }
121
        }
122
    },
123
    [],
124
    {"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
125
			        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
126
                    "result": "pass",
127
		        },
128
        }
129
    }),
130
131
    ({"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
132
			        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
133
                    "result": "pass",
134
		        },
135
                "rule_a": {
136
			        "rule_id": "rule_a",
137
                    "result": "fail",
138
		        }
139
        }
140
    },
141
    ["xccdf_org.ssgproject.content_rule_rpm_verify_hashes"],
142
    {"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
143
			        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
144
                    "result": "pass",
145
		        },
146
        }
147
    }),
148
149
    ({"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
150
			        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
151
                    "result": "pass",
152
		        },
153
                "rule_a": {
154
			        "rule_id": "rule_a",
155
                    "result": "fail",
156
		        }
157
        }
158
    },
159
    ["xccdf_org.ssgproject.content_rule_rpm_verify_hashes", "rule_a"],
160
    {"rules": { "xccdf_org.ssgproject.content_rule_rpm_verify_hashes": {
161
			        "rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes",
162
                    "result": "pass",
163
		        },
164
                "rule_a": {
165
			        "rule_id": "rule_a",
166
                    "result": "fail",
167
		        }
168
        }
169
    }),
170
])
171
def test_remove_not_selected_rules(dictionary_json, ids_of_selected_rules, result):
172
    remove_not_selected_rules(dictionary_json, ids_of_selected_rules)
173
    assert dictionary_json == result
174