| Total Complexity | 55 |
| Total Lines | 410 |
| Duplicated Lines | 19.02 % |
| Changes | 0 | ||
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:
Complex classes like tests.protocols.gmpv7.test_create_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 GmpCreateScheduleTestCase(unittest.TestCase): |
||
| 28 | |||
| 29 | def setUp(self): |
||
| 30 | self.connection = MockConnection() |
||
| 31 | self.gmp = Gmp(self.connection) |
||
| 32 | |||
| 33 | def test_create_schedule(self): |
||
| 34 | self.gmp.create_schedule( |
||
| 35 | name='foo', |
||
| 36 | ) |
||
| 37 | |||
| 38 | self.connection.send.has_been_called_with( |
||
| 39 | '<create_schedule>' |
||
| 40 | '<name>foo</name>' |
||
| 41 | '</create_schedule>' |
||
| 42 | ) |
||
| 43 | |||
| 44 | def test_create_schedule_missing_name(self): |
||
| 45 | with self.assertRaises(RequiredArgument): |
||
| 46 | self.gmp.create_schedule( |
||
| 47 | name=None, |
||
| 48 | ) |
||
| 49 | |||
| 50 | with self.assertRaises(RequiredArgument): |
||
| 51 | self.gmp.create_schedule( |
||
| 52 | name='', |
||
| 53 | ) |
||
| 54 | |||
| 55 | def test_create_schedule_with_comment(self): |
||
| 56 | self.gmp.create_schedule( |
||
| 57 | name='foo', |
||
| 58 | comment='bar' |
||
| 59 | ) |
||
| 60 | |||
| 61 | self.connection.send.has_been_called_with( |
||
| 62 | '<create_schedule>' |
||
| 63 | '<name>foo</name>' |
||
| 64 | '<comment>bar</comment>' |
||
| 65 | '</create_schedule>' |
||
| 66 | ) |
||
| 67 | |||
| 68 | def test_create_schedule_with_first_time_missing_minute(self): |
||
| 69 | with self.assertRaises(RequiredArgument): |
||
| 70 | self.gmp.create_schedule( |
||
| 71 | name='foo', |
||
| 72 | first_time_hour=10, |
||
| 73 | first_time_day_of_month=1, |
||
| 74 | first_time_month=1, |
||
| 75 | first_time_year=2020, |
||
| 76 | ) |
||
| 77 | |||
| 78 | def test_create_schedule_with_first_time_invalid_minute(self): |
||
| 79 | with self.assertRaises(InvalidArgument): |
||
| 80 | self.gmp.create_schedule( |
||
| 81 | name='foo', |
||
| 82 | first_time_minute='', |
||
| 83 | first_time_hour=1, |
||
| 84 | first_time_day_of_month=1, |
||
| 85 | first_time_month=1, |
||
| 86 | first_time_year=2020, |
||
| 87 | ) |
||
| 88 | with self.assertRaises(InvalidArgument): |
||
| 89 | self.gmp.create_schedule( |
||
| 90 | name='foo', |
||
| 91 | first_time_minute=-1, |
||
| 92 | first_time_hour=1, |
||
| 93 | first_time_day_of_month=1, |
||
| 94 | first_time_month=1, |
||
| 95 | first_time_year=2020, |
||
| 96 | ) |
||
| 97 | |||
| 98 | def test_create_schedule_with_first_time_missing_hour(self): |
||
| 99 | with self.assertRaises(RequiredArgument): |
||
| 100 | self.gmp.create_schedule( |
||
| 101 | name='foo', |
||
| 102 | first_time_minute=10, |
||
| 103 | first_time_day_of_month=1, |
||
| 104 | first_time_month=1, |
||
| 105 | first_time_year=2020, |
||
| 106 | ) |
||
| 107 | |||
| 108 | def test_create_schedule_with_first_time_invalid_hour(self): |
||
| 109 | with self.assertRaises(InvalidArgument): |
||
| 110 | self.gmp.create_schedule( |
||
| 111 | name='foo', |
||
| 112 | first_time_minute=10, |
||
| 113 | first_time_hour='', |
||
| 114 | first_time_day_of_month=1, |
||
| 115 | first_time_month=1, |
||
| 116 | first_time_year=2020, |
||
| 117 | ) |
||
| 118 | |||
| 119 | with self.assertRaises(InvalidArgument): |
||
| 120 | self.gmp.create_schedule( |
||
| 121 | name='foo', |
||
| 122 | first_time_minute=10, |
||
| 123 | first_time_hour=-1, |
||
| 124 | first_time_day_of_month=1, |
||
| 125 | first_time_month=1, |
||
| 126 | first_time_year=2020, |
||
| 127 | ) |
||
| 128 | |||
| 129 | def test_create_schedule_with_first_time_missing_day_of_month(self): |
||
| 130 | with self.assertRaises(RequiredArgument): |
||
| 131 | self.gmp.create_schedule( |
||
| 132 | name='foo', |
||
| 133 | first_time_minute=0, |
||
| 134 | first_time_hour=0, |
||
| 135 | first_time_month=1, |
||
| 136 | first_time_year=2020, |
||
| 137 | ) |
||
| 138 | |||
| 139 | View Code Duplication | def test_create_schedule_with_first_time_invalid_day_of_month(self): |
|
|
|
|||
| 140 | with self.assertRaises(InvalidArgument): |
||
| 141 | self.gmp.create_schedule( |
||
| 142 | name='foo', |
||
| 143 | first_time_minute=0, |
||
| 144 | first_time_hour=0, |
||
| 145 | first_time_day_of_month='', |
||
| 146 | first_time_month=1, |
||
| 147 | first_time_year=2020, |
||
| 148 | ) |
||
| 149 | |||
| 150 | with self.assertRaises(InvalidArgument): |
||
| 151 | self.gmp.create_schedule( |
||
| 152 | name='foo', |
||
| 153 | first_time_minute=0, |
||
| 154 | first_time_hour=0, |
||
| 155 | first_time_day_of_month=0, |
||
| 156 | first_time_month=1, |
||
| 157 | first_time_year=2020, |
||
| 158 | ) |
||
| 159 | |||
| 160 | with self.assertRaises(InvalidArgument): |
||
| 161 | self.gmp.create_schedule( |
||
| 162 | name='foo', |
||
| 163 | first_time_minute=0, |
||
| 164 | first_time_hour=0, |
||
| 165 | first_time_day_of_month=-1, |
||
| 166 | first_time_month=1, |
||
| 167 | first_time_year=2020, |
||
| 168 | ) |
||
| 169 | |||
| 170 | with self.assertRaises(InvalidArgument): |
||
| 171 | self.gmp.create_schedule( |
||
| 172 | name='foo', |
||
| 173 | first_time_minute=0, |
||
| 174 | first_time_hour=0, |
||
| 175 | first_time_day_of_month=32, |
||
| 176 | first_time_month=1, |
||
| 177 | first_time_year=2020, |
||
| 178 | ) |
||
| 179 | |||
| 180 | def test_create_schedule_with_first_time_missing_month(self): |
||
| 181 | with self.assertRaises(RequiredArgument): |
||
| 182 | self.gmp.create_schedule( |
||
| 183 | name='foo', |
||
| 184 | first_time_minute=0, |
||
| 185 | first_time_hour=0, |
||
| 186 | first_time_day_of_month=1, |
||
| 187 | first_time_year=2020, |
||
| 188 | ) |
||
| 189 | |||
| 190 | View Code Duplication | def test_create_schedule_with_first_time_invalid_month(self): |
|
| 191 | with self.assertRaises(InvalidArgument): |
||
| 192 | self.gmp.create_schedule( |
||
| 193 | name='foo', |
||
| 194 | first_time_minute=0, |
||
| 195 | first_time_hour=0, |
||
| 196 | first_time_day_of_month=1, |
||
| 197 | first_time_month='', |
||
| 198 | first_time_year=2020, |
||
| 199 | ) |
||
| 200 | |||
| 201 | with self.assertRaises(InvalidArgument): |
||
| 202 | self.gmp.create_schedule( |
||
| 203 | name='foo', |
||
| 204 | first_time_minute=0, |
||
| 205 | first_time_hour=0, |
||
| 206 | first_time_day_of_month=1, |
||
| 207 | first_time_month=0, |
||
| 208 | first_time_year=2020, |
||
| 209 | ) |
||
| 210 | |||
| 211 | with self.assertRaises(InvalidArgument): |
||
| 212 | self.gmp.create_schedule( |
||
| 213 | name='foo', |
||
| 214 | first_time_minute=0, |
||
| 215 | first_time_hour=0, |
||
| 216 | first_time_day_of_month=1, |
||
| 217 | first_time_month=-1, |
||
| 218 | first_time_year=2020, |
||
| 219 | ) |
||
| 220 | |||
| 221 | with self.assertRaises(InvalidArgument): |
||
| 222 | self.gmp.create_schedule( |
||
| 223 | name='foo', |
||
| 224 | first_time_minute=0, |
||
| 225 | first_time_hour=0, |
||
| 226 | first_time_day_of_month=1, |
||
| 227 | first_time_month=13, |
||
| 228 | first_time_year=2020, |
||
| 229 | ) |
||
| 230 | |||
| 231 | def test_create_schedule_with_first_time_missing_year(self): |
||
| 232 | with self.assertRaises(RequiredArgument): |
||
| 233 | self.gmp.create_schedule( |
||
| 234 | name='foo', |
||
| 235 | first_time_minute=0, |
||
| 236 | first_time_hour=0, |
||
| 237 | first_time_day_of_month=1, |
||
| 238 | first_time_month=12, |
||
| 239 | ) |
||
| 240 | |||
| 241 | def test_create_schedule_with_first_time_invalid_year(self): |
||
| 242 | with self.assertRaises(InvalidArgument): |
||
| 243 | self.gmp.create_schedule( |
||
| 244 | name='foo', |
||
| 245 | first_time_minute=0, |
||
| 246 | first_time_hour=0, |
||
| 247 | first_time_day_of_month=1, |
||
| 248 | first_time_month=1, |
||
| 249 | first_time_year=1, |
||
| 250 | ) |
||
| 251 | |||
| 252 | with self.assertRaises(InvalidArgument): |
||
| 253 | self.gmp.create_schedule( |
||
| 254 | name='foo', |
||
| 255 | first_time_minute=0, |
||
| 256 | first_time_hour=0, |
||
| 257 | first_time_day_of_month=1, |
||
| 258 | first_time_month=0, |
||
| 259 | first_time_year=2020, |
||
| 260 | ) |
||
| 261 | |||
| 262 | |||
| 263 | def test_create_schedule_with_first_time(self): |
||
| 264 | self.gmp.create_schedule( |
||
| 265 | name='foo', |
||
| 266 | first_time_minute=0, |
||
| 267 | first_time_hour=0, |
||
| 268 | first_time_day_of_month=1, |
||
| 269 | first_time_month=1, |
||
| 270 | first_time_year=2020, |
||
| 271 | ) |
||
| 272 | |||
| 273 | self.connection.send.has_been_called_with( |
||
| 274 | '<create_schedule>' |
||
| 275 | '<name>foo</name>' |
||
| 276 | '<first_time>' |
||
| 277 | '<minute>0</minute>' |
||
| 278 | '<hour>0</hour>' |
||
| 279 | '<day_of_month>1</day_of_month>' |
||
| 280 | '<month>1</month>' |
||
| 281 | '<year>2020</year>' |
||
| 282 | '</first_time>' |
||
| 283 | '</create_schedule>' |
||
| 284 | ) |
||
| 285 | |||
| 286 | |||
| 287 | def test_create_schedule_invalid_duration(self): |
||
| 288 | with self.assertRaises(InvalidArgument): |
||
| 289 | self.gmp.create_schedule( |
||
| 290 | name='foo', |
||
| 291 | duration='bar', |
||
| 292 | duration_unit='day', |
||
| 293 | ) |
||
| 294 | |||
| 295 | with self.assertRaises(InvalidArgument): |
||
| 296 | self.gmp.create_schedule( |
||
| 297 | name='foo', |
||
| 298 | duration=0, |
||
| 299 | duration_unit='day', |
||
| 300 | ) |
||
| 301 | |||
| 302 | with self.assertRaises(InvalidArgument): |
||
| 303 | self.gmp.create_schedule( |
||
| 304 | name='foo', |
||
| 305 | duration=-1, |
||
| 306 | duration_unit='day', |
||
| 307 | ) |
||
| 308 | |||
| 309 | def test_create_schedule_with_duration_missing_unit(self): |
||
| 310 | with self.assertRaises(RequiredArgument): |
||
| 311 | self.gmp.create_schedule( |
||
| 312 | name='foo', |
||
| 313 | duration=1, |
||
| 314 | ) |
||
| 315 | |||
| 316 | def test_create_schedule_with_duration_invalid_unit(self): |
||
| 317 | with self.assertRaises(InvalidArgument): |
||
| 318 | self.gmp.create_schedule( |
||
| 319 | name='foo', |
||
| 320 | duration=1, |
||
| 321 | duration_unit='foo' |
||
| 322 | ) |
||
| 323 | |||
| 324 | def test_create_schedule_with_duration(self): |
||
| 325 | self.gmp.create_schedule( |
||
| 326 | name='foo', |
||
| 327 | duration=1, |
||
| 328 | duration_unit='day', |
||
| 329 | ) |
||
| 330 | |||
| 331 | self.connection.send.has_been_called_with( |
||
| 332 | '<create_schedule>' |
||
| 333 | '<name>foo</name>' |
||
| 334 | '<duration>1' |
||
| 335 | '<unit>day</unit>' |
||
| 336 | '</duration>' |
||
| 337 | '</create_schedule>' |
||
| 338 | ) |
||
| 339 | |||
| 340 | def test_create_schedule_with_period_missing_unit(self): |
||
| 341 | with self.assertRaises(RequiredArgument): |
||
| 342 | self.gmp.create_schedule( |
||
| 343 | name='foo', |
||
| 344 | period=1, |
||
| 345 | ) |
||
| 346 | |||
| 347 | def test_create_schedule_with_period_invalid_unit(self): |
||
| 348 | with self.assertRaises(InvalidArgument): |
||
| 349 | self.gmp.create_schedule( |
||
| 350 | name='foo', |
||
| 351 | period=1, |
||
| 352 | period_unit='foo' |
||
| 353 | ) |
||
| 354 | |||
| 355 | def test_create_schedule_invalid_period(self): |
||
| 356 | with self.assertRaises(InvalidArgument): |
||
| 357 | self.gmp.create_schedule( |
||
| 358 | name='foo', |
||
| 359 | period='foo', |
||
| 360 | period_unit='day' |
||
| 361 | ) |
||
| 362 | |||
| 363 | with self.assertRaises(InvalidArgument): |
||
| 364 | self.gmp.create_schedule( |
||
| 365 | name='foo', |
||
| 366 | period=0, |
||
| 367 | period_unit='day' |
||
| 368 | ) |
||
| 369 | |||
| 370 | with self.assertRaises(InvalidArgument): |
||
| 371 | self.gmp.create_schedule( |
||
| 372 | name='foo', |
||
| 373 | period=-1, |
||
| 374 | period_unit='day' |
||
| 375 | ) |
||
| 376 | |||
| 377 | |||
| 378 | def test_create_schedule_with_period(self): |
||
| 379 | self.gmp.create_schedule( |
||
| 380 | name='foo', |
||
| 381 | period=1, |
||
| 382 | period_unit='day', |
||
| 383 | ) |
||
| 384 | |||
| 385 | self.connection.send.has_been_called_with( |
||
| 386 | '<create_schedule>' |
||
| 387 | '<name>foo</name>' |
||
| 388 | '<period>1' |
||
| 389 | '<unit>day</unit>' |
||
| 390 | '</period>' |
||
| 391 | '</create_schedule>' |
||
| 392 | ) |
||
| 393 | |||
| 394 | def test_create_schedule_with_timezone(self): |
||
| 395 | self.gmp.create_schedule( |
||
| 396 | name='foo', |
||
| 397 | timezone='foo', |
||
| 398 | ) |
||
| 399 | |||
| 400 | self.connection.send.has_been_called_with( |
||
| 401 | '<create_schedule>' |
||
| 402 | '<name>foo</name>' |
||
| 403 | '<timezone>foo</timezone>' |
||
| 404 | '</create_schedule>' |
||
| 405 | ) |
||
| 406 | |||
| 407 | |||
| 408 | if __name__ == '__main__': |
||
| 409 | unittest.main() |
||
| 410 |