| Total Complexity | 51 |
| Total Lines | 230 |
| Duplicated Lines | 58.26 % |
| 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.test_errors 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) 2019 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 ( |
||
| 22 | InvalidArgument, |
||
| 23 | InvalidArgumentType, |
||
| 24 | RequiredArgument, |
||
| 25 | GvmError, |
||
| 26 | GvmServerError, |
||
| 27 | GvmResponseError, |
||
| 28 | ) |
||
| 29 | |||
| 30 | |||
| 31 | View Code Duplication | class InvalidArgumentTestCase(unittest.TestCase): |
|
|
|
|||
| 32 | def test_raise_with_message(self): |
||
| 33 | with self.assertRaisesRegex(InvalidArgument, '^foo bar$'): |
||
| 34 | raise InvalidArgument('foo bar') |
||
| 35 | |||
| 36 | def test_message_precedence(self): |
||
| 37 | with self.assertRaisesRegex(InvalidArgument, '^foo bar$') as cm: |
||
| 38 | raise InvalidArgument('foo bar', argument='foo', function='bar') |
||
| 39 | |||
| 40 | ex = cm.exception |
||
| 41 | self.assertEqual(ex.argument, 'foo') |
||
| 42 | self.assertEqual(ex.function, 'bar') |
||
| 43 | |||
| 44 | self.assertEqual(str(ex), 'foo bar') |
||
| 45 | |||
| 46 | def test_raise_with_argument(self): |
||
| 47 | with self.assertRaises(InvalidArgument) as cm: |
||
| 48 | raise InvalidArgument(argument='foo') |
||
| 49 | |||
| 50 | ex = cm.exception |
||
| 51 | self.assertEqual(ex.argument, 'foo') |
||
| 52 | self.assertIsNone(ex.function) |
||
| 53 | |||
| 54 | def test_raise_with_function(self): |
||
| 55 | with self.assertRaises(InvalidArgument) as cm: |
||
| 56 | raise InvalidArgument(function='foo') |
||
| 57 | |||
| 58 | ex = cm.exception |
||
| 59 | self.assertEqual(ex.function, 'foo') |
||
| 60 | self.assertIsNone(ex.argument) |
||
| 61 | |||
| 62 | def test_raise_with_argument_and_function(self): |
||
| 63 | with self.assertRaises(InvalidArgument) as cm: |
||
| 64 | raise InvalidArgument(argument='foo', function='bar') |
||
| 65 | |||
| 66 | ex = cm.exception |
||
| 67 | self.assertEqual(ex.argument, 'foo') |
||
| 68 | self.assertEqual(ex.function, 'bar') |
||
| 69 | |||
| 70 | def test_string_conversion(self): |
||
| 71 | with self.assertRaises(InvalidArgument) as cm: |
||
| 72 | raise InvalidArgument('foo bar', argument='foo') |
||
| 73 | |||
| 74 | ex = cm.exception |
||
| 75 | self.assertEqual(str(ex), 'foo bar') |
||
| 76 | |||
| 77 | with self.assertRaises(InvalidArgument) as cm: |
||
| 78 | raise InvalidArgument(argument='foo') |
||
| 79 | |||
| 80 | ex = cm.exception |
||
| 81 | self.assertEqual(str(ex), 'Invalid argument foo') |
||
| 82 | |||
| 83 | with self.assertRaises(InvalidArgument) as cm: |
||
| 84 | raise InvalidArgument(function='foo') |
||
| 85 | |||
| 86 | ex = cm.exception |
||
| 87 | self.assertEqual(str(ex), 'Invalid argument for foo') |
||
| 88 | |||
| 89 | with self.assertRaises(InvalidArgument) as cm: |
||
| 90 | raise InvalidArgument(argument='foo', function='bar') |
||
| 91 | |||
| 92 | ex = cm.exception |
||
| 93 | self.assertEqual(str(ex), 'Invalid argument foo for bar') |
||
| 94 | |||
| 95 | def test_is_gvm_error(self): |
||
| 96 | with self.assertRaises(GvmError): |
||
| 97 | raise InvalidArgument('foo bar') |
||
| 98 | |||
| 99 | |||
| 100 | View Code Duplication | class RequiredArgumentTestCase(unittest.TestCase): |
|
| 101 | def test_raise_with_message(self): |
||
| 102 | with self.assertRaisesRegex(RequiredArgument, '^foo bar$'): |
||
| 103 | raise RequiredArgument('foo bar') |
||
| 104 | |||
| 105 | def test_message_precedence(self): |
||
| 106 | with self.assertRaisesRegex(RequiredArgument, '^foo bar$') as cm: |
||
| 107 | raise RequiredArgument('foo bar', argument='foo', function='bar') |
||
| 108 | |||
| 109 | ex = cm.exception |
||
| 110 | self.assertEqual(ex.argument, 'foo') |
||
| 111 | self.assertEqual(ex.function, 'bar') |
||
| 112 | |||
| 113 | self.assertEqual(str(ex), 'foo bar') |
||
| 114 | |||
| 115 | def test_raise_with_argument(self): |
||
| 116 | with self.assertRaises(RequiredArgument) as cm: |
||
| 117 | raise RequiredArgument(argument='foo') |
||
| 118 | |||
| 119 | ex = cm.exception |
||
| 120 | self.assertEqual(ex.argument, 'foo') |
||
| 121 | self.assertIsNone(ex.function) |
||
| 122 | |||
| 123 | def test_raise_with_function(self): |
||
| 124 | with self.assertRaises(RequiredArgument) as cm: |
||
| 125 | raise RequiredArgument(function='foo') |
||
| 126 | |||
| 127 | ex = cm.exception |
||
| 128 | self.assertEqual(ex.function, 'foo') |
||
| 129 | self.assertIsNone(ex.argument) |
||
| 130 | |||
| 131 | def test_raise_with_argument_and_function(self): |
||
| 132 | with self.assertRaises(RequiredArgument) as cm: |
||
| 133 | raise RequiredArgument(argument='foo', function='bar') |
||
| 134 | |||
| 135 | ex = cm.exception |
||
| 136 | self.assertEqual(ex.argument, 'foo') |
||
| 137 | self.assertEqual(ex.function, 'bar') |
||
| 138 | |||
| 139 | def test_string_conversion(self): |
||
| 140 | with self.assertRaises(RequiredArgument) as cm: |
||
| 141 | raise RequiredArgument('foo bar') |
||
| 142 | |||
| 143 | ex = cm.exception |
||
| 144 | self.assertEqual(str(ex), 'foo bar') |
||
| 145 | |||
| 146 | with self.assertRaises(RequiredArgument) as cm: |
||
| 147 | raise RequiredArgument(argument='foo') |
||
| 148 | |||
| 149 | ex = cm.exception |
||
| 150 | self.assertEqual(str(ex), 'Required argument foo') |
||
| 151 | |||
| 152 | with self.assertRaises(RequiredArgument) as cm: |
||
| 153 | raise RequiredArgument(function='foo') |
||
| 154 | |||
| 155 | ex = cm.exception |
||
| 156 | self.assertEqual(str(ex), 'Required argument missing for foo') |
||
| 157 | |||
| 158 | with self.assertRaises(RequiredArgument) as cm: |
||
| 159 | raise RequiredArgument(argument='foo', function='bar') |
||
| 160 | |||
| 161 | ex = cm.exception |
||
| 162 | self.assertEqual(str(ex), 'bar requires a foo argument') |
||
| 163 | |||
| 164 | def test_is_gvm_error(self): |
||
| 165 | with self.assertRaises(GvmError): |
||
| 166 | raise RequiredArgument('foo bar') |
||
| 167 | |||
| 168 | |||
| 169 | class GvmServerErrorTestCase(unittest.TestCase): |
||
| 170 | def test_raise_with_message_and_status(self): |
||
| 171 | with self.assertRaisesRegex( |
||
| 172 | GvmServerError, '^GvmServerError: foo - bar$' |
||
| 173 | ): |
||
| 174 | raise GvmServerError('foo', 'bar') |
||
| 175 | |||
| 176 | def test_is_gvm_error(self): |
||
| 177 | with self.assertRaises(GvmError): |
||
| 178 | raise GvmServerError('foo', 'bar') |
||
| 179 | |||
| 180 | |||
| 181 | class GvmResponseErrorTestCase(unittest.TestCase): |
||
| 182 | def test_raise_with_message_and_status(self): |
||
| 183 | with self.assertRaisesRegex( |
||
| 184 | GvmResponseError, '^GvmResponseError: foo - bar$' |
||
| 185 | ): |
||
| 186 | raise GvmResponseError('foo', 'bar') |
||
| 187 | |||
| 188 | def test_is_gvm_error(self): |
||
| 189 | with self.assertRaises(GvmError): |
||
| 190 | raise GvmResponseError('foo', 'bar') |
||
| 191 | |||
| 192 | |||
| 193 | class InvalidArgumentTypeTestCase(unittest.TestCase): |
||
| 194 | def test_raise_with_argument_and_arg_type(self): |
||
| 195 | with self.assertRaisesRegex( |
||
| 196 | InvalidArgumentType, '^The argument foo must be of type bar.$' |
||
| 197 | ): |
||
| 198 | raise InvalidArgumentType('foo', 'bar') |
||
| 199 | |||
| 200 | def test_raise_with_function(self): |
||
| 201 | with self.assertRaisesRegex( |
||
| 202 | InvalidArgumentType, |
||
| 203 | '^In baz the argument foo must be of type bar.$', |
||
| 204 | ): |
||
| 205 | raise InvalidArgumentType('foo', 'bar', function='baz') |
||
| 206 | |||
| 207 | def test_string_conversion(self): |
||
| 208 | with self.assertRaises(InvalidArgumentType) as cm: |
||
| 209 | raise InvalidArgumentType('foo', 'bar') |
||
| 210 | |||
| 211 | ex = cm.exception |
||
| 212 | self.assertEqual(str(ex), 'The argument foo must be of type bar.') |
||
| 213 | self.assertIsNone(ex.function) |
||
| 214 | |||
| 215 | with self.assertRaises(InvalidArgumentType) as cm: |
||
| 216 | raise InvalidArgumentType('foo', 'bar', function='baz') |
||
| 217 | |||
| 218 | ex = cm.exception |
||
| 219 | self.assertEqual( |
||
| 220 | str(ex), 'In baz the argument foo must be of type bar.' |
||
| 221 | ) |
||
| 222 | |||
| 223 | def test_is_gvm_error(self): |
||
| 224 | with self.assertRaises(GvmError): |
||
| 225 | raise InvalidArgumentType('foo', 'bar') |
||
| 226 | |||
| 227 | |||
| 228 | if __name__ == '__main__': |
||
| 229 | unittest.main() |
||
| 230 |