| Total Complexity | 77 |
| Total Lines | 251 |
| Duplicated Lines | 22.71 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 1 |
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 LocalizedFieldTestCase 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 json |
||
| 15 | class LocalizedFieldTestCase(TestCase): |
||
| 16 | """Tests the :see:LocalizedField class.""" |
||
| 17 | |||
| 18 | @staticmethod |
||
| 19 | def test_init(): |
||
| 20 | """Tests whether the :see:__init__ function |
||
| 21 | correctly handles parameters""" |
||
| 22 | |||
| 23 | field = LocalizedField(blank=True) |
||
| 24 | assert field.required == [] |
||
| 25 | |||
| 26 | field = LocalizedField(blank=False) |
||
| 27 | assert field.required == [settings.LANGUAGE_CODE] |
||
| 28 | |||
| 29 | field = LocalizedField(required=True) |
||
| 30 | assert field.required == [lang_code for lang_code, _ in |
||
| 31 | settings.LANGUAGES] |
||
| 32 | |||
| 33 | field = LocalizedField(required=False) |
||
| 34 | assert field.required == [] |
||
| 35 | |||
| 36 | |||
| 37 | @staticmethod |
||
| 38 | def test_from_db_value(): |
||
| 39 | """Tests whether the :see:from_db_value function |
||
| 40 | produces the expected :see:LocalizedValue.""" |
||
| 41 | |||
| 42 | input_data = get_init_values() |
||
| 43 | localized_value = LocalizedField().from_db_value(input_data) |
||
| 44 | |||
| 45 | for lang_code, _ in settings.LANGUAGES: |
||
| 46 | assert getattr(localized_value, lang_code) == input_data[lang_code] |
||
| 47 | |||
| 48 | @staticmethod |
||
| 49 | def test_from_db_value_none(): |
||
| 50 | """Tests whether the :see:from_db_value function |
||
| 51 | correctly handles None values.""" |
||
| 52 | |||
| 53 | localized_value = LocalizedField().from_db_value(None) |
||
| 54 | |||
| 55 | for lang_code, _ in settings.LANGUAGES: |
||
| 56 | assert localized_value.get(lang_code) is None |
||
| 57 | |||
| 58 | def test_from_db_value_none_return_none(self): |
||
| 59 | """Tests whether the :see:from_db_value function |
||
| 60 | correctly handles None values when LOCALIZED_FIELDS_EXPERIMENTAL |
||
| 61 | is set to True.""" |
||
| 62 | |||
| 63 | with self.settings(LOCALIZED_FIELDS_EXPERIMENTAL=True): |
||
| 64 | localized_value = LocalizedField.from_db_value(None) |
||
| 65 | |||
| 66 | assert localized_value is None |
||
| 67 | |||
| 68 | @staticmethod |
||
| 69 | def test_to_python(): |
||
| 70 | """Tests whether the :see:to_python function |
||
| 71 | produces the expected :see:LocalizedValue.""" |
||
| 72 | |||
| 73 | input_data = get_init_values() |
||
| 74 | localized_value = LocalizedField().to_python(input_data) |
||
| 75 | |||
| 76 | for language, value in input_data.items(): |
||
| 77 | assert localized_value.get(language) == value |
||
| 78 | |||
| 79 | @staticmethod |
||
| 80 | def test_to_python_non_json(): |
||
| 81 | """Tests whether the :see:to_python function |
||
| 82 | properly handles a string that is not JSON.""" |
||
| 83 | |||
| 84 | localized_value = LocalizedField().to_python('my value') |
||
| 85 | assert localized_value.get() == 'my value' |
||
| 86 | |||
| 87 | @staticmethod |
||
| 88 | def test_to_python_none(): |
||
| 89 | """Tests whether the :see:to_python function |
||
| 90 | produces the expected :see:LocalizedValue |
||
| 91 | instance when it is passes None.""" |
||
| 92 | |||
| 93 | localized_value = LocalizedField().to_python(None) |
||
| 94 | assert localized_value |
||
| 95 | |||
| 96 | for lang_code, _ in settings.LANGUAGES: |
||
| 97 | assert localized_value.get(lang_code) is None |
||
| 98 | |||
| 99 | @staticmethod |
||
| 100 | def test_to_python_non_dict(): |
||
| 101 | """Tests whether the :see:to_python function produces |
||
| 102 | the expected :see:LocalizedValue when it is |
||
| 103 | passed a non-dictionary value.""" |
||
| 104 | |||
| 105 | localized_value = LocalizedField().to_python(list()) |
||
| 106 | assert localized_value |
||
| 107 | |||
| 108 | for lang_code, _ in settings.LANGUAGES: |
||
| 109 | assert localized_value.get(lang_code) is None |
||
| 110 | |||
| 111 | @staticmethod |
||
| 112 | def test_to_python_str(): |
||
| 113 | """Tests whether the :see:to_python function produces |
||
| 114 | the expected :see:LocalizedValue when it is |
||
| 115 | passed serialized string value.""" |
||
| 116 | |||
| 117 | serialized_str = json.dumps(get_init_values()) |
||
| 118 | localized_value = LocalizedField().to_python(serialized_str) |
||
| 119 | assert isinstance(localized_value, LocalizedValue) |
||
| 120 | |||
| 121 | for language, value in get_init_values().items(): |
||
| 122 | assert localized_value.get(language) == value |
||
| 123 | assert getattr(localized_value, language) == value |
||
| 124 | |||
| 125 | @staticmethod |
||
| 126 | def test_get_prep_value(): |
||
| 127 | """"Tests whether the :see:get_prep_value function |
||
| 128 | produces the expected dictionary.""" |
||
| 129 | |||
| 130 | input_data = get_init_values() |
||
| 131 | localized_value = LocalizedValue(input_data) |
||
| 132 | |||
| 133 | output_data = LocalizedField().get_prep_value(localized_value) |
||
| 134 | |||
| 135 | for language, value in input_data.items(): |
||
| 136 | assert language in output_data |
||
| 137 | assert output_data.get(language) == value |
||
| 138 | |||
| 139 | @staticmethod |
||
| 140 | def test_get_prep_value_none(): |
||
| 141 | """Tests whether the :see:get_prep_value function |
||
| 142 | produces the expected output when it is passed None.""" |
||
| 143 | |||
| 144 | output_data = LocalizedField().get_prep_value(None) |
||
| 145 | assert not output_data |
||
| 146 | |||
| 147 | @staticmethod |
||
| 148 | def test_get_prep_value_no_localized_value(): |
||
| 149 | """Tests whether the :see:get_prep_value function |
||
| 150 | produces the expected output when it is passed a |
||
| 151 | non-LocalizedValue value.""" |
||
| 152 | |||
| 153 | output_data = LocalizedField().get_prep_value(['huh']) |
||
| 154 | assert not output_data |
||
| 155 | |||
| 156 | def test_get_prep_value_clean(self): |
||
| 157 | """Tests whether the :see:get_prep_value produces |
||
| 158 | None as the output when it is passed an empty, but |
||
| 159 | valid LocalizedValue value but, only when null=True.""" |
||
| 160 | |||
| 161 | localized_value = LocalizedValue() |
||
| 162 | |||
| 163 | with self.assertRaises(IntegrityError): |
||
| 164 | LocalizedField(null=False).get_prep_value(localized_value) |
||
| 165 | |||
| 166 | assert not LocalizedField(null=True).get_prep_value(localized_value) |
||
| 167 | assert not LocalizedField().clean(None) |
||
| 168 | assert not LocalizedField().clean(['huh']) |
||
| 169 | |||
| 170 | @staticmethod |
||
| 171 | def test_formfield(): |
||
| 172 | """Tests whether the :see:formfield function |
||
| 173 | correctly returns a valid form.""" |
||
| 174 | |||
| 175 | assert isinstance( |
||
| 176 | LocalizedField().formfield(), |
||
| 177 | LocalizedFieldForm |
||
| 178 | ) |
||
| 179 | |||
| 180 | # case optional filling |
||
| 181 | field = LocalizedField(blank=True, required=[]) |
||
| 182 | assert not field.formfield().required |
||
| 183 | for field in field.formfield().fields: |
||
| 184 | assert not field.required |
||
| 185 | |||
| 186 | # case required for any language |
||
| 187 | field = LocalizedField(blank=False, required=[]) |
||
| 188 | assert field.formfield().required |
||
| 189 | for field in field.formfield().fields: |
||
| 190 | assert not field.required |
||
| 191 | |||
| 192 | # case required for specific languages |
||
| 193 | required_langs = ['ro', 'nl'] |
||
| 194 | field = LocalizedField(blank=False, required=required_langs) |
||
| 195 | assert field.formfield().required |
||
| 196 | for field in field.formfield().fields: |
||
| 197 | if field.label in required_langs: |
||
| 198 | assert field.required |
||
| 199 | else: |
||
| 200 | assert not field.required |
||
| 201 | |||
| 202 | # case required for all languages |
||
| 203 | field = LocalizedField(blank=False, required=True) |
||
| 204 | assert field.formfield().required |
||
| 205 | for field in field.formfield().fields: |
||
| 206 | assert field.required |
||
| 207 | |||
| 208 | View Code Duplication | def test_required_all(self): |
|
| 209 | """Tests whether passing required=True properly validates |
||
| 210 | that all languages are filled in.""" |
||
| 211 | |||
| 212 | model = get_fake_model(dict( |
||
| 213 | title=LocalizedField(required=True) |
||
| 214 | )) |
||
| 215 | |||
| 216 | with self.assertRaises(IntegrityError): |
||
| 217 | model.objects.create(title=dict(ro='romanian', nl='dutch')) |
||
| 218 | |||
| 219 | with self.assertRaises(IntegrityError): |
||
| 220 | model.objects.create(title=dict(nl='dutch')) |
||
| 221 | |||
| 222 | with self.assertRaises(IntegrityError): |
||
| 223 | model.objects.create(title=dict(random='random')) |
||
| 224 | |||
| 225 | with self.assertRaises(IntegrityError): |
||
| 226 | model.objects.create(title=dict()) |
||
| 227 | |||
| 228 | with self.assertRaises(IntegrityError): |
||
| 229 | model.objects.create(title=None) |
||
| 230 | |||
| 231 | with self.assertRaises(IntegrityError): |
||
| 232 | model.objects.create(title='') |
||
| 233 | |||
| 234 | with self.assertRaises(IntegrityError): |
||
| 235 | model.objects.create(title=' ') |
||
| 236 | |||
| 237 | View Code Duplication | def test_required_some(self): |
|
| 238 | """Tests whether passing an array to required, |
||
| 239 | properly validates whether the specified languages |
||
| 240 | are marked as required.""" |
||
| 241 | |||
| 242 | model = get_fake_model(dict( |
||
| 243 | title=LocalizedField(required=['nl', 'ro']) |
||
| 244 | )) |
||
| 245 | |||
| 246 | with self.assertRaises(IntegrityError): |
||
| 247 | model.objects.create(title=dict(ro='romanian', nl='dutch')) |
||
| 248 | |||
| 249 | with self.assertRaises(IntegrityError): |
||
| 250 | model.objects.create(title=dict(nl='dutch')) |
||
| 251 | |||
| 252 | with self.assertRaises(IntegrityError): |
||
| 253 | model.objects.create(title=dict(random='random')) |
||
| 254 | |||
| 255 | with self.assertRaises(IntegrityError): |
||
| 256 | model.objects.create(title=dict()) |
||
| 257 | |||
| 258 | with self.assertRaises(IntegrityError): |
||
| 259 | model.objects.create(title=None) |
||
| 260 | |||
| 261 | with self.assertRaises(IntegrityError): |
||
| 262 | model.objects.create(title='') |
||
| 263 | |||
| 264 | with self.assertRaises(IntegrityError): |
||
| 265 | model.objects.create(title=' ') |
||
| 266 |
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.