Passed
Pull Request — master (#89)
by
unknown
01:45
created

tests.protocols.gmpv7.test_modify_schedule   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 413
Duplicated Lines 32.45 %

Importance

Changes 0
Metric Value
eloc 276
dl 134
loc 413
rs 5.5199
c 0
b 0
f 0
wmc 56

25 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpModifyScheduleTestCase.test_modify_schedule_with_duration_invalid_unit() 0 6 2
A GmpModifyScheduleTestCase.test_modify_schedule_invalid_period() 0 20 4
A GmpModifyScheduleTestCase.test_modify_schedule_with_period_missing_unit() 0 5 2
A GmpModifyScheduleTestCase.test_modify_schedule_missing_schedule_id() 0 9 3
A GmpModifyScheduleTestCase.test_modify_schedule_invalid_duration() 0 20 4
A GmpModifyScheduleTestCase.test_modify_schedule_with_first_time_missing_month() 0 8 2
A GmpModifyScheduleTestCase.test_modify_schedule() 0 7 1
A GmpModifyScheduleTestCase.setUp() 0 3 1
A GmpModifyScheduleTestCase.test_modify_schedule_with_period() 0 9 1
A GmpModifyScheduleTestCase.test_modify_schedule_with_first_time_missing_hour() 0 8 2
A GmpModifyScheduleTestCase.test_modify_schedule_with_first_time_missing_day_of_month() 0 8 2
A GmpModifyScheduleTestCase.test_modify_schedule_with_name() 0 8 1
A GmpModifyScheduleTestCase.test_modify_schedule_with_first_time_missing_minute() 0 8 2
A GmpModifyScheduleTestCase.test_modify_schedule_with_first_time_invalid_minute() 18 18 3
B GmpModifyScheduleTestCase.test_modify_schedule_with_first_time_invalid_day_of_month() 39 39 5
B GmpModifyScheduleTestCase.test_modify_schedule_with_first_time_invalid_month() 39 39 5
A GmpModifyScheduleTestCase.test_modify_schedule_with_timezone() 0 8 1
A GmpModifyScheduleTestCase.test_modify_schedule_with_first_time_invalid_hour() 19 19 3
A GmpModifyScheduleTestCase.test_modify_schedule_with_duration() 0 9 1
A GmpModifyScheduleTestCase.test_create_schedule_with_first_time() 0 12 1
A GmpModifyScheduleTestCase.test_modify_schedule_with_first_time_missing_year() 0 8 2
A GmpModifyScheduleTestCase.test_modify_schedule_with_first_time_invalid_year() 19 19 3
A GmpModifyScheduleTestCase.test_modify_schedule_with_period_invalid_unit() 0 6 2
A GmpModifyScheduleTestCase.test_modify_schedule_with_duration_missing_unit() 0 5 2
A GmpModifyScheduleTestCase.test_modify_schedule_with_comment() 0 8 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like tests.protocols.gmpv7.test_modify_schedule 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
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import unittest
20
21
from gvm.errors import RequiredArgument, InvalidArgument
22
from gvm.protocols.gmpv7 import Gmp
23
24
from .. import MockConnection
25
26
27
class GmpModifyScheduleTestCase(unittest.TestCase):
28
29
    def setUp(self):
30
        self.connection = MockConnection()
31
        self.gmp = Gmp(self.connection)
32
33
    def test_modify_schedule(self):
34
        self.gmp.modify_schedule(
35
            schedule_id='s1',
36
        )
37
38
        self.connection.send.has_been_called_with(
39
            '<modify_schedule schedule_id="s1"/>'
40
        )
41
42
    def test_modify_schedule_missing_schedule_id(self):
43
        with self.assertRaises(RequiredArgument):
44
            self.gmp.modify_schedule(
45
                schedule_id=None,
46
            )
47
48
        with self.assertRaises(RequiredArgument):
49
            self.gmp.modify_schedule(
50
                schedule_id='',
51
            )
52
53
    def test_modify_schedule_with_comment(self):
54
        self.gmp.modify_schedule(
55
            schedule_id='s1',
56
            comment='foo',
57
        )
58
59
        self.connection.send.has_been_called_with(
60
            '<modify_schedule schedule_id="s1">'
61
            '<comment>foo</comment>'
62
            '</modify_schedule>'
63
        )
64
65
    def test_create_schedule_with_first_time(self):
66
        self.gmp.modify_schedule(
67
            schedule_id='s1',
68
            first_time_minute=0,
69
            first_time_hour=0,
70
            first_time_day_of_month=1,
71
            first_time_month=1,
72
            first_time_year=2020,
73
        )
74
75
        self.connection.send.has_been_called_with(
76
            '<modify_schedule schedule_id="s1">'
77
            '<first_time>'
78
            '<minute>0</minute>'
79
            '<hour>0</hour>'
80
            '<day_of_month>1</day_of_month>'
81
            '<month>1</month>'
82
            '<year>2020</year>'
83
            '</first_time>'
84
            '</modify_schedule>'
85
        )
86
87
    def test_modify_schedule_with_first_time_missing_minute(self):
88
        with self.assertRaises(RequiredArgument):
89
            self.gmp.modify_schedule(
90
                schedule_id='s1',
91
                first_time_hour=10,
92
                first_time_day_of_month=1,
93
                first_time_month=1,
94
                first_time_year=2020,
95
            )
96
97 View Code Duplication
    def test_modify_schedule_with_first_time_invalid_minute(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
98
        with self.assertRaises(InvalidArgument):
99
            self.gmp.modify_schedule(
100
                schedule_id='s1',
101
                first_time_minute='',
102
                first_time_hour=1,
103
                first_time_day_of_month=1,
104
                first_time_month=1,
105
                first_time_year=2020,
106
            )
107
        with self.assertRaises(InvalidArgument):
108
            self.gmp.modify_schedule(
109
                schedule_id='s1',
110
                first_time_minute=-1,
111
                first_time_hour=1,
112
                first_time_day_of_month=1,
113
                first_time_month=1,
114
                first_time_year=2020,
115
            )
116
117
    def test_modify_schedule_with_first_time_missing_hour(self):
118
        with self.assertRaises(RequiredArgument):
119
            self.gmp.modify_schedule(
120
                schedule_id='s1',
121
                first_time_minute=10,
122
                first_time_day_of_month=1,
123
                first_time_month=1,
124
                first_time_year=2020,
125
            )
126
127 View Code Duplication
    def test_modify_schedule_with_first_time_invalid_hour(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
128
        with self.assertRaises(InvalidArgument):
129
            self.gmp.modify_schedule(
130
                schedule_id='s1',
131
                first_time_minute=10,
132
                first_time_hour='',
133
                first_time_day_of_month=1,
134
                first_time_month=1,
135
                first_time_year=2020,
136
            )
137
138
        with self.assertRaises(InvalidArgument):
139
            self.gmp.modify_schedule(
140
                schedule_id='s1',
141
                first_time_minute=10,
142
                first_time_hour=-1,
143
                first_time_day_of_month=1,
144
                first_time_month=1,
145
                first_time_year=2020,
146
            )
147
148
    def test_modify_schedule_with_first_time_missing_day_of_month(self):
149
        with self.assertRaises(RequiredArgument):
150
            self.gmp.modify_schedule(
151
                schedule_id='s1',
152
                first_time_minute=0,
153
                first_time_hour=0,
154
                first_time_month=1,
155
                first_time_year=2020,
156
            )
157
158 View Code Duplication
    def test_modify_schedule_with_first_time_invalid_day_of_month(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
159
        with self.assertRaises(InvalidArgument):
160
            self.gmp.modify_schedule(
161
                schedule_id='s1',
162
                first_time_minute=0,
163
                first_time_hour=0,
164
                first_time_day_of_month='',
165
                first_time_month=1,
166
                first_time_year=2020,
167
            )
168
169
        with self.assertRaises(InvalidArgument):
170
            self.gmp.modify_schedule(
171
                schedule_id='s1',
172
                first_time_minute=0,
173
                first_time_hour=0,
174
                first_time_day_of_month=0,
175
                first_time_month=1,
176
                first_time_year=2020,
177
            )
178
179
        with self.assertRaises(InvalidArgument):
180
            self.gmp.modify_schedule(
181
                schedule_id='s1',
182
                first_time_minute=0,
183
                first_time_hour=0,
184
                first_time_day_of_month=-1,
185
                first_time_month=1,
186
                first_time_year=2020,
187
            )
188
189
        with self.assertRaises(InvalidArgument):
190
            self.gmp.modify_schedule(
191
                schedule_id='s1',
192
                first_time_minute=0,
193
                first_time_hour=0,
194
                first_time_day_of_month=32,
195
                first_time_month=1,
196
                first_time_year=2020,
197
            )
198
199
    def test_modify_schedule_with_first_time_missing_month(self):
200
        with self.assertRaises(RequiredArgument):
201
            self.gmp.modify_schedule(
202
                schedule_id='s1',
203
                first_time_minute=0,
204
                first_time_hour=0,
205
                first_time_day_of_month=1,
206
                first_time_year=2020,
207
            )
208
209 View Code Duplication
    def test_modify_schedule_with_first_time_invalid_month(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
210
        with self.assertRaises(InvalidArgument):
211
            self.gmp.modify_schedule(
212
                schedule_id='s1',
213
                first_time_minute=0,
214
                first_time_hour=0,
215
                first_time_day_of_month=1,
216
                first_time_month='',
217
                first_time_year=2020,
218
            )
219
220
        with self.assertRaises(InvalidArgument):
221
            self.gmp.modify_schedule(
222
                schedule_id='s1',
223
                first_time_minute=0,
224
                first_time_hour=0,
225
                first_time_day_of_month=1,
226
                first_time_month=0,
227
                first_time_year=2020,
228
            )
229
230
        with self.assertRaises(InvalidArgument):
231
            self.gmp.modify_schedule(
232
                schedule_id='s1',
233
                first_time_minute=0,
234
                first_time_hour=0,
235
                first_time_day_of_month=1,
236
                first_time_month=-1,
237
                first_time_year=2020,
238
            )
239
240
        with self.assertRaises(InvalidArgument):
241
            self.gmp.modify_schedule(
242
                schedule_id='s1',
243
                first_time_minute=0,
244
                first_time_hour=0,
245
                first_time_day_of_month=1,
246
                first_time_month=13,
247
                first_time_year=2020,
248
            )
249
250
    def test_modify_schedule_with_first_time_missing_year(self):
251
        with self.assertRaises(RequiredArgument):
252
            self.gmp.modify_schedule(
253
                schedule_id='s1',
254
                first_time_minute=0,
255
                first_time_hour=0,
256
                first_time_day_of_month=1,
257
                first_time_month=12,
258
            )
259
260 View Code Duplication
    def test_modify_schedule_with_first_time_invalid_year(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
261
        with self.assertRaises(InvalidArgument):
262
            self.gmp.modify_schedule(
263
                schedule_id='s1',
264
                first_time_minute=0,
265
                first_time_hour=0,
266
                first_time_day_of_month=1,
267
                first_time_month=1,
268
                first_time_year=1,
269
            )
270
271
        with self.assertRaises(InvalidArgument):
272
            self.gmp.modify_schedule(
273
                schedule_id='s1',
274
                first_time_minute=0,
275
                first_time_hour=0,
276
                first_time_day_of_month=1,
277
                first_time_month=0,
278
                first_time_year=2020,
279
            )
280
281
    def test_modify_schedule_with_name(self):
282
        self.gmp.modify_schedule(
283
            schedule_id='s1',
284
            name='foo',
285
        )
286
287
        self.connection.send.has_been_called_with(
288
            '<modify_schedule schedule_id="s1">'
289
            '<name>foo</name>'
290
            '</modify_schedule>'
291
        )
292
293
    def test_modify_schedule_invalid_duration(self):
294
        with self.assertRaises(InvalidArgument):
295
            self.gmp.modify_schedule(
296
                schedule_id='s1',
297
                duration='bar',
298
                duration_unit='day',
299
            )
300
301
        with self.assertRaises(InvalidArgument):
302
            self.gmp.modify_schedule(
303
                schedule_id='s1',
304
                duration=0,
305
                duration_unit='day',
306
            )
307
308
        with self.assertRaises(InvalidArgument):
309
            self.gmp.modify_schedule(
310
                schedule_id='s1',
311
                duration=-1,
312
                duration_unit='day',
313
            )
314
315
    def test_modify_schedule_with_duration_missing_unit(self):
316
        with self.assertRaises(RequiredArgument):
317
            self.gmp.modify_schedule(
318
                schedule_id='s1',
319
                duration=1,
320
            )
321
322
    def test_modify_schedule_with_duration_invalid_unit(self):
323
        with self.assertRaises(InvalidArgument):
324
            self.gmp.modify_schedule(
325
                schedule_id='s1',
326
                duration=1,
327
                duration_unit='foo'
328
            )
329
330
    def test_modify_schedule_with_duration(self):
331
        self.gmp.modify_schedule(
332
            schedule_id='s1',
333
            duration=1,
334
            duration_unit='day',
335
        )
336
337
        self.connection.send.has_been_called_with(
338
            '<modify_schedule schedule_id="s1">'
339
            '<duration>1'
340
            '<unit>day</unit>'
341
            '</duration>'
342
            '</modify_schedule>'
343
        )
344
345
    def test_modify_schedule_with_period_missing_unit(self):
346
        with self.assertRaises(RequiredArgument):
347
            self.gmp.modify_schedule(
348
                schedule_id='s1',
349
                period=1,
350
            )
351
352
    def test_modify_schedule_with_period_invalid_unit(self):
353
        with self.assertRaises(InvalidArgument):
354
            self.gmp.modify_schedule(
355
                schedule_id='s1',
356
                period=1,
357
                period_unit='foo'
358
            )
359
360
    def test_modify_schedule_invalid_period(self):
361
        with self.assertRaises(InvalidArgument):
362
            self.gmp.modify_schedule(
363
                schedule_id='s1',
364
                period='foo',
365
                period_unit='day'
366
            )
367
368
        with self.assertRaises(InvalidArgument):
369
            self.gmp.modify_schedule(
370
                schedule_id='s1',
371
                period=0,
372
                period_unit='day'
373
            )
374
375
        with self.assertRaises(InvalidArgument):
376
            self.gmp.modify_schedule(
377
                schedule_id='s1',
378
                period=-1,
379
                period_unit='day'
380
            )
381
382
383
    def test_modify_schedule_with_period(self):
384
        self.gmp.modify_schedule(
385
            schedule_id='s1',
386
            period=1,
387
            period_unit='day',
388
        )
389
390
        self.connection.send.has_been_called_with(
391
            '<modify_schedule schedule_id="s1">'
392
            '<period>1'
393
            '<unit>day</unit>'
394
            '</period>'
395
            '</modify_schedule>'
396
        )
397
398
    def test_modify_schedule_with_timezone(self):
399
        self.gmp.modify_schedule(
400
            schedule_id='s1',
401
            timezone='foo',
402
        )
403
404
        self.connection.send.has_been_called_with(
405
            '<modify_schedule schedule_id="s1">'
406
            '<timezone>foo</timezone>'
407
            '</modify_schedule>'
408
        )
409
410
411
if __name__ == '__main__':
412
    unittest.main()
413