Passed
Push — master ( 2ef001...6551ad )
by Jan
03:14 queued 11s
created

test_client   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 316
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 44
eloc 236
dl 0
loc 316
rs 8.8798
c 0
b 0
f 0

30 Functions

Rating   Name   Duplication   Size   Complexity  
A get_client() 0 3 1
A get_client_with_option_show_not_selected_rules() 0 3 1
A get_client_with_option_show_not_selected_rules_and_show_fail_rules() 0 8 1
A test_client() 0 6 1
A get_client_with_option_show_fail_rules() 0 3 1
A test_search_rules_id() 0 5 1
A test_search_rules_with_regex() 0 5 1
A test_get_questions() 0 13 1
A test_find_does_not_exist_rule() 0 6 2
A get_client_tree() 0 3 1
A get_client_on_web_browser() 0 3 1
A test_search_rules_id_on_web_browser() 0 5 1
A test_find_not_selected_rule() 0 6 2
A test_get_questions_not_selected() 0 13 1
A test_get_questions_not_selected_and_show_fail_rules() 0 14 1
A test_prepare_graph() 0 10 1
A load_tested_file() 0 11 5
A test_prepare_tree() 0 10 1
A test_search_not_selected_rule() 0 5 2
A test_search_non_existent_rule() 0 5 2
A test_prepare_graph_with_not_selected_rule() 0 4 1
A test_if_not_installed_PyInquirer() 0 10 2
A try_expection_for_prepare_graph() 0 5 2
A test_prepare_graph_with_non_existent_rule() 0 4 1
A test_if_not_installed_PyInquirer_with_option_show_not_selected_rules() 0 11 2
A test_if_not_installed_PyInquirer_with_option_show_fail_rules() 0 10 2
A test_get_wanted_rules() 0 16 1
A test_if_not_installed_PyInquirer_with_option_show_not_selected_rules_and_show_fail_rules() 0 12 2
A test_get_wanted_not_selected_rules() 0 14 1
A test_get_questions_with_option_show_fail_rules() 0 13 2

How to fix   Complexity   

Complexity

Complex classes like test_client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import graph.client
2
import pytest
3
import tests.any_test_help
4
import re
5
import json
6
import mock
7
import sys
8
9
10
def get_client(src, rule):
11
    return graph.client.client(
12
        ["--off-web-browser", tests.any_test_help.get_src(src), rule])
13
14
15
def get_client_on_web_browser(src, rule):
16
    return graph.client.client(
17
        [tests.any_test_help.get_src(src), rule])
18
19
20
def get_client_tree(src, rule):
21
    return graph.client.client(
22
        ["--tree", "--off-web-browser", tests.any_test_help.get_src(src), rule])
23
24
25
def get_client_with_option_show_fail_rules(src, rule):
26
    return graph.client.client(
27
        ["--show-fail-rules", "--off-web-browser", tests.any_test_help.get_src(src), rule])
28
29
30
def get_client_with_option_show_not_selected_rules(src, rule):
31
    return graph.client.client(
32
        ["--show-not-selected-rules", "--off-web-browser", tests.any_test_help.get_src(src), rule])
33
34
35
def get_client_with_option_show_not_selected_rules_and_show_fail_rules(
36
        src,
37
        rule):
38
    return graph.client.client(["--show-not-selected-rules",
39
                                "--show-fail-rules",
40
                                "--off-web-browser",
41
                                tests.any_test_help.get_src(src),
42
                                rule])
43
44
45
def test_client():
46
    src = 'test_data/ssg-fedora-ds-arf.xml'
47
    rule = 'rule'
48
    client = get_client(src, rule)
49
    assert client.source_filename == tests.any_test_help.get_src(src)
50
    assert client.rule_name == rule
51
52
53
def test_search_rules_id():
54
    src = 'test_data/ssg-fedora-ds-arf.xml'
55
    part_of_id_rule = 'xccdf_org.ssgproject.'
56
    client = get_client(src, part_of_id_rule)
57
    assert len(client.search_rules_id()) == 184
58
59
60
def test_search_rules_id_on_web_browser():
61
    src = 'test_data/ssg-fedora-ds-arf.xml'
62
    part_of_id_rule = 'xccdf_org.ssgproject.'
63
    client = get_client_on_web_browser(src, part_of_id_rule)
64
    assert len(client.search_rules_id()) == 184
65
66
67
def test_find_does_not_exist_rule():
68
    rule = 'random_rule_which_doest_exist'
69
    src = 'test_data/ssg-fedora-ds-arf.xml'
70
    client = get_client(src, rule)
71
    with pytest.raises(Exception, match="err- 404 rule not found!"):
72
        assert client.search_rules_id()
73
74
75
def test_find_not_selected_rule():
76
    rule = 'xccdf_org.ssgproject.content_rule_ntpd_specify_remote_server'
77
    src = 'test_data/ssg-fedora-ds-arf.xml'
78
    client = get_client(src, rule)
79
    with pytest.raises(Exception, match=rule):
80
        assert client.search_rules_id()
81
82
83
def test_search_rules_with_regex():
84
    src = 'test_data/ssg-fedora-ds-arf.xml'
85
    regex = r'_package_\w+_removed'
86
    client = get_client(src, regex)
87
    assert len(client.search_rules_id()) == 2
88
89
90
def test_get_questions():
91
    src = 'test_data/ssg-fedora-ds-arf.xml'
92
    regex = r'_package_\w+_removed'
93
    client = get_client(src, regex)
94
    from PyInquirer import Separator
95
96
    out = client.get_questions(
97
        Separator('= The rules ID ='),
98
        Separator('= The not selected rules ID ='))
99
    rule1 = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
100
    rule2 = 'xccdf_org.ssgproject.content_rule_package_sendmail_removed'
101
    assert out[0]['choices'][1]['name'] == rule1
102
    assert out[0]['choices'][2]['name'] == rule2
103
104
105
def test_get_questions_not_selected():
106
    src = 'test_data/ssg-fedora-ds-arf.xml'
107
    regex = r'_package_\w+_removed'
108
    client = get_client_with_option_show_not_selected_rules(src, regex)
109
    from PyInquirer import Separator
110
111
    out = client.get_questions(
112
        Separator('= The rules ID ='),
113
        Separator('= The not selected rules ID ='))
114
    rule1 = 'xccdf_org.ssgproject.content_rule_package_setroubleshoot_removed'
115
    rule2 = 'xccdf_org.ssgproject.content_rule_package_mcstrans_removed'
116
    assert out[0]['choices'][-2]['name'] == rule1
117
    assert out[0]['choices'][-1]['name'] == rule2
118
119
120
def test_get_questions_not_selected_and_show_fail_rules():
121
    src = 'test_data/ssg-fedora-ds-arf.xml'
122
    regex = r'_package_\w+_removed'
123
    client = get_client_with_option_show_not_selected_rules_and_show_fail_rules(
124
        src, regex)
125
    from PyInquirer import Separator
126
127
    out = client.get_questions(
128
        Separator('= The rules ID ='),
129
        Separator('= The not selected rules ID ='))
130
    rule1 = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
131
    rule2 = 'xccdf_org.ssgproject.content_rule_package_mcstrans_removed'
132
    assert out[0]['choices'][1]['name'] == rule1
133
    assert out[0]['choices'][-1]['name'] == rule2
134
135
136
def test_get_questions_with_option_show_fail_rules():
137
    src = 'test_data/ssg-fedora-ds-arf.xml'
138
    regex = r'_package_\w+_removed'
139
    client = get_client_with_option_show_fail_rules(src, regex)
140
    from PyInquirer import Separator
141
142
    out = client.get_questions(
143
        Separator('= The rules ID ='),
144
        Separator('= The not selected rules ID ='))
145
    rule1 = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
146
    assert out[0]['choices'][1]['name'] == rule1
147
    with pytest.raises(Exception, match="list index out of range"):
148
        assert out[0]['choices'][2]['name'] is None
149
150
151
def test_get_wanted_not_selected_rules():
152
    src = 'test_data/ssg-fedora-ds-arf.xml'
153
    regex = r'_package_\w+_removed'
154
    client = get_client(src, regex)
155
156
    out = [
157
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_nis_removed'},
158
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_ntpdate_removed'},
159
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_telnetd_removed'},
160
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_gdm_removed'},
161
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_setroubleshoot_removed'},
162
        {'id_rule': 'xccdf_org.ssgproject.content_rule_package_mcstrans_removed'}]
163
164
    assert out == client._get_wanted_not_selected_rules()
165
166
167
def test_get_wanted_rules():
168
    src = 'test_data/ssg-fedora-ds-arf.xml'
169
    regex = r'_package_\w+_removed'
170
    client = get_client(src, regex)
171
172
    out = [
173
        {'href': '#oval0',
174
         'id_def': 'oval:ssg-package_abrt_removed:def:1',
175
         'id_rule': 'xccdf_org.ssgproject.content_rule_package_abrt_removed',
176
         'result': 'fail'},
177
        {'href': '#oval0',
178
         'id_def': 'oval:ssg-package_sendmail_removed:def:1',
179
         'id_rule': 'xccdf_org.ssgproject.content_rule_package_sendmail_removed',
180
         'result': 'pass'}]
181
182
    assert out == client._get_wanted_rules()
183
184
185
def test_search_non_existent_rule():
186
    src = 'test_data/ssg-fedora-ds-arf.xml'
187
    non_existent_rule = 'non-existent_rule'
188
    with pytest.raises(Exception, match="err- 404 rule not found!"):
189
        assert get_client(src, non_existent_rule).search_rules_id()
190
191
192
def test_search_not_selected_rule():
193
    src = 'test_data/ssg-fedora-ds-arf.xml'
194
    non_existent_rule = 'xccdf_org.ssgproject.content_rule_package_nis_removed'
195
    with pytest.raises(Exception, match=non_existent_rule):
196
        assert get_client(src, non_existent_rule).search_rules_id()
197
198
199
def test_prepare_graph():
200
    src = 'test_data/ssg-fedora-ds-arf.xml'
201
    rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
202
    client = get_client(src, rule)
203
    rules = {'rules': [rule]}
204
    client.prepare_data(rules)
205
    result = load_tested_file('../graph_html_interpreter/data.js')
206
    referenc_result = load_tested_file(
207
        'test_data/referenc_result_data_graph.js')
208
    assert result == referenc_result
209
210
211
def test_prepare_tree():
212
    src = 'test_data/ssg-fedora-ds-arf.xml'
213
    rule = 'xccdf_org.ssgproject.content_rule_package_abrt_removed'
214
    client = get_client_tree(src, rule)
215
    rules = {'rules': [rule]}
216
    client.prepare_data(rules)
217
    result = load_tested_file('../tree_html_interpreter/data.js')
218
    referenc_result = load_tested_file(
219
        'test_data/referenc_result_data_tree.js')
220
    assert result == referenc_result
221
222
223
def load_tested_file(src):
224
    with open(tests.any_test_help.get_src(src), 'r') as f:
225
        data = f.readlines()
226
    out = []
227
    edge = False
228
    for row in data:
229
        if row == '    "edges": [\n':
230
            edge = True
231
        if not edge:
232
            out.append(row)
233
    return out
234
235
236
def try_expection_for_prepare_graph(src, rule, err):
237
    client = get_client(src, rule)
238
    rules = {'rules': [rule]}
239
    with pytest.raises(Exception, match=err):
240
        assert client.prepare_data(rules)
241
242
243
def test_prepare_graph_with_non_existent_rule():
244
    src = 'test_data/ssg-fedora-ds-arf.xml'
245
    rule = 'non-existent_rule'
246
    try_expection_for_prepare_graph(src, rule, '404')
247
248
249
def test_prepare_graph_with_not_selected_rule():
250
    src = 'test_data/ssg-fedora-ds-arf.xml'
251
    rule = 'xccdf_org.ssgproject.content_rule_package_nis_removed'
252
    try_expection_for_prepare_graph(src, rule, 'not selected')
253
254
255
def test_if_not_installed_PyInquirer(capsys):
256
    with mock.patch.dict(sys.modules, {'PyInquirer': None}):
257
        src = 'test_data/ssg-fedora-ds-arf.xml'
258
        regex = r'_package_\w+_removed'
259
        client = get_client(src, regex)
260
        out = client.run_gui_and_return_answers()
261
        assert out is None
262
        captured = capsys.readouterr()
263
        assert captured.out == (
264
            '== The Rule IDs ==\n'
265
            'xccdf_org.ssgproject.content_rule_package_abrt_removed\\b\n'
266
            'xccdf_org.ssgproject.content_rule_package_sendmail_removed\\b\n')
267
268
269
def test_if_not_installed_PyInquirer_with_option_show_fail_rules(capsys):
270
    with mock.patch.dict(sys.modules, {'PyInquirer': None}):
271
        src = 'test_data/ssg-fedora-ds-arf.xml'
272
        regex = r'_package_\w+_removed'
273
        client = get_client_with_option_show_fail_rules(src, regex)
274
        out = client.run_gui_and_return_answers()
275
        assert out is None
276
        captured = capsys.readouterr()
277
        assert captured.out == (
278
            '== The Rule IDs ==\n'
279
            'xccdf_org.ssgproject.content_rule_package_abrt_removed\\b\n')
280
281
282
def test_if_not_installed_PyInquirer_with_option_show_not_selected_rules(
283
        capsys):
284
    with mock.patch.dict(sys.modules, {'PyInquirer': None}):
285
        src = 'test_data/ssg-fedora-ds-arf.xml'
286
        regex = r'_package_\w+_removed'
287
        client = get_client_with_option_show_not_selected_rules(src, regex)
288
        out = client.run_gui_and_return_answers()
289
        assert out is None
290
        captured = capsys.readouterr()
291
        assert captured.out == (
292
            '== The Rule IDs ==\n'
293
            'xccdf_org.ssgproject.content_rule_package_abrt_removed\\b\n'
294
            'xccdf_org.ssgproject.content_rule_package_sendmail_removed\\b\n'
295
            '== The not selected rule IDs ==\n'
296
            'xccdf_org.ssgproject.content_rule_package_nis_removed(Not selected)\n'
297
            'xccdf_org.ssgproject.content_rule_package_ntpdate_removed(Not selected)\n'
298
            'xccdf_org.ssgproject.content_rule_package_telnetd_removed(Not selected)\n'
299
            'xccdf_org.ssgproject.content_rule_package_gdm_removed(Not selected)\n'
300
            'xccdf_org.ssgproject.content_rule_package_setroubleshoot_removed(Not selected)\n'
301
            'xccdf_org.ssgproject.content_rule_package_mcstrans_removed(Not selected)\n')
302
303
304
def test_if_not_installed_PyInquirer_with_option_show_not_selected_rules_and_show_fail_rules(
305
        capsys):
306
    with mock.patch.dict(sys.modules, {'PyInquirer': None}):
307
        src = 'test_data/ssg-fedora-ds-arf.xml'
308
        regex = r'_package_\w+_removed'
309
        client = get_client_with_option_show_not_selected_rules_and_show_fail_rules(
310
            src, regex)
311
        out = client.run_gui_and_return_answers()
312
        assert out is None
313
        captured = capsys.readouterr()
314
        assert captured.out == (
315
            '== The Rule IDs ==\n'
316
            'xccdf_org.ssgproject.content_rule_package_abrt_removed\\b\n'
317
            '== The not selected rule IDs ==\n'
318
            'xccdf_org.ssgproject.content_rule_package_nis_removed(Not selected)\n'
319
            'xccdf_org.ssgproject.content_rule_package_ntpdate_removed(Not selected)\n'
320
            'xccdf_org.ssgproject.content_rule_package_telnetd_removed(Not selected)\n'
321
            'xccdf_org.ssgproject.content_rule_package_gdm_removed(Not selected)\n'
322
            'xccdf_org.ssgproject.content_rule_package_setroubleshoot_removed(Not selected)\n'
323
            'xccdf_org.ssgproject.content_rule_package_mcstrans_removed(Not selected)\n')
324