| Total Complexity | 58 |
| Total Lines | 204 |
| Duplicated Lines | 27.94 % |
| Changes | 9 | ||
| 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_from_db_value(): |
||
| 20 | """Tests whether the :see:from_db_value function |
||
| 21 | produces the expected :see:LocalizedValue.""" |
||
| 22 | |||
| 23 | input_data = get_init_values() |
||
| 24 | localized_value = LocalizedField().from_db_value(input_data) |
||
| 25 | |||
| 26 | for lang_code, _ in settings.LANGUAGES: |
||
| 27 | assert getattr(localized_value, lang_code) == input_data[lang_code] |
||
| 28 | |||
| 29 | @staticmethod |
||
| 30 | def test_from_db_value_none(): |
||
| 31 | """Tests whether the :see:from_db_value function |
||
| 32 | correctly handles None values.""" |
||
| 33 | |||
| 34 | localized_value = LocalizedField().from_db_value(None) |
||
| 35 | |||
| 36 | for lang_code, _ in settings.LANGUAGES: |
||
| 37 | assert localized_value.get(lang_code) is None |
||
| 38 | |||
| 39 | def test_from_db_value_none_return_none(self): |
||
| 40 | """Tests whether the :see:from_db_value function |
||
| 41 | correctly handles None values when LOCALIZED_FIELDS_EXPERIMENTAL |
||
| 42 | is set to True.""" |
||
| 43 | |||
| 44 | with self.settings(LOCALIZED_FIELDS_EXPERIMENTAL=True): |
||
| 45 | localized_value = LocalizedField.from_db_value(None) |
||
| 46 | |||
| 47 | assert localized_value is None |
||
| 48 | |||
| 49 | @staticmethod |
||
| 50 | def test_to_python(): |
||
| 51 | """Tests whether the :see:to_python function |
||
| 52 | produces the expected :see:LocalizedValue.""" |
||
| 53 | |||
| 54 | input_data = get_init_values() |
||
| 55 | localized_value = LocalizedField().to_python(input_data) |
||
| 56 | |||
| 57 | for language, value in input_data.items(): |
||
| 58 | assert localized_value.get(language) == value |
||
| 59 | |||
| 60 | @staticmethod |
||
| 61 | def test_to_python_non_json(): |
||
| 62 | """Tests whether the :see:to_python function |
||
| 63 | properly handles a string that is not JSON.""" |
||
| 64 | |||
| 65 | localized_value = LocalizedField().to_python('my value') |
||
| 66 | assert localized_value.get() == 'my value' |
||
| 67 | |||
| 68 | @staticmethod |
||
| 69 | def test_to_python_none(): |
||
| 70 | """Tests whether the :see:to_python function |
||
| 71 | produces the expected :see:LocalizedValue |
||
| 72 | instance when it is passes None.""" |
||
| 73 | |||
| 74 | localized_value = LocalizedField().to_python(None) |
||
| 75 | assert localized_value |
||
| 76 | |||
| 77 | for lang_code, _ in settings.LANGUAGES: |
||
| 78 | assert localized_value.get(lang_code) is None |
||
| 79 | |||
| 80 | @staticmethod |
||
| 81 | def test_to_python_non_dict(): |
||
| 82 | """Tests whether the :see:to_python function produces |
||
| 83 | the expected :see:LocalizedValue when it is |
||
| 84 | passed a non-dictionary value.""" |
||
| 85 | |||
| 86 | localized_value = LocalizedField().to_python(list()) |
||
| 87 | assert localized_value |
||
| 88 | |||
| 89 | for lang_code, _ in settings.LANGUAGES: |
||
| 90 | assert localized_value.get(lang_code) is None |
||
| 91 | |||
| 92 | @staticmethod |
||
| 93 | def test_to_python_str(): |
||
| 94 | """Tests whether the :see:to_python function produces |
||
| 95 | the expected :see:LocalizedValue when it is |
||
| 96 | passed serialized string value.""" |
||
| 97 | |||
| 98 | serialized_str = json.dumps(get_init_values()) |
||
| 99 | localized_value = LocalizedField().to_python(serialized_str) |
||
| 100 | assert isinstance(localized_value, LocalizedValue) |
||
| 101 | |||
| 102 | for language, value in get_init_values().items(): |
||
| 103 | assert localized_value.get(language) == value |
||
| 104 | assert getattr(localized_value, language) == value |
||
| 105 | |||
| 106 | @staticmethod |
||
| 107 | def test_get_prep_value(): |
||
| 108 | """"Tests whether the :see:get_prep_value function |
||
| 109 | produces the expected dictionary.""" |
||
| 110 | |||
| 111 | input_data = get_init_values() |
||
| 112 | localized_value = LocalizedValue(input_data) |
||
| 113 | |||
| 114 | output_data = LocalizedField().get_prep_value(localized_value) |
||
| 115 | |||
| 116 | for language, value in input_data.items(): |
||
| 117 | assert language in output_data |
||
| 118 | assert output_data.get(language) == value |
||
| 119 | |||
| 120 | @staticmethod |
||
| 121 | def test_get_prep_value_none(): |
||
| 122 | """Tests whether the :see:get_prep_value function |
||
| 123 | produces the expected output when it is passed None.""" |
||
| 124 | |||
| 125 | output_data = LocalizedField().get_prep_value(None) |
||
| 126 | assert not output_data |
||
| 127 | |||
| 128 | @staticmethod |
||
| 129 | def test_get_prep_value_no_localized_value(): |
||
| 130 | """Tests whether the :see:get_prep_value function |
||
| 131 | produces the expected output when it is passed a |
||
| 132 | non-LocalizedValue value.""" |
||
| 133 | |||
| 134 | output_data = LocalizedField().get_prep_value(['huh']) |
||
| 135 | assert not output_data |
||
| 136 | |||
| 137 | def test_get_prep_value_clean(self): |
||
| 138 | """Tests whether the :see:get_prep_value produces |
||
| 139 | None as the output when it is passed an empty, but |
||
| 140 | valid LocalizedValue value but, only when null=True.""" |
||
| 141 | |||
| 142 | localized_value = LocalizedValue() |
||
| 143 | |||
| 144 | with self.assertRaises(IntegrityError): |
||
| 145 | LocalizedField(null=False).get_prep_value(localized_value) |
||
| 146 | |||
| 147 | assert not LocalizedField(null=True).get_prep_value(localized_value) |
||
| 148 | assert not LocalizedField().clean(None) |
||
| 149 | assert not LocalizedField().clean(['huh']) |
||
| 150 | |||
| 151 | @staticmethod |
||
| 152 | def test_formfield(): |
||
| 153 | """Tests whether the :see:formfield function |
||
| 154 | correctly returns a valid form.""" |
||
| 155 | |||
| 156 | assert isinstance( |
||
| 157 | LocalizedField().formfield(), |
||
| 158 | LocalizedFieldForm |
||
| 159 | ) |
||
| 160 | |||
| 161 | View Code Duplication | def test_required_all(self): |
|
| 162 | """Tests whether passing required=True properly validates |
||
| 163 | that all languages are filled in.""" |
||
| 164 | |||
| 165 | model = get_fake_model(dict( |
||
| 166 | title=LocalizedField(required=True) |
||
| 167 | )) |
||
| 168 | |||
| 169 | with self.assertRaises(IntegrityError): |
||
| 170 | model.objects.create(title=dict(ro='romanian', nl='dutch')) |
||
| 171 | |||
| 172 | with self.assertRaises(IntegrityError): |
||
| 173 | model.objects.create(title=dict(nl='dutch')) |
||
| 174 | |||
| 175 | with self.assertRaises(IntegrityError): |
||
| 176 | model.objects.create(title=dict(random='random')) |
||
| 177 | |||
| 178 | with self.assertRaises(IntegrityError): |
||
| 179 | model.objects.create(title=dict()) |
||
| 180 | |||
| 181 | with self.assertRaises(IntegrityError): |
||
| 182 | model.objects.create(title=None) |
||
| 183 | |||
| 184 | with self.assertRaises(IntegrityError): |
||
| 185 | model.objects.create(title='') |
||
| 186 | |||
| 187 | with self.assertRaises(IntegrityError): |
||
| 188 | model.objects.create(title=' ') |
||
| 189 | |||
| 190 | View Code Duplication | def test_required_some(self): |
|
| 191 | """Tests whether passing an array to required, |
||
| 192 | properly validates whether the specified languages |
||
| 193 | are marked as required.""" |
||
| 194 | |||
| 195 | model = get_fake_model(dict( |
||
| 196 | title=LocalizedField(required=['nl', 'ro']) |
||
| 197 | )) |
||
| 198 | |||
| 199 | with self.assertRaises(IntegrityError): |
||
| 200 | model.objects.create(title=dict(ro='romanian', nl='dutch')) |
||
| 201 | |||
| 202 | with self.assertRaises(IntegrityError): |
||
| 203 | model.objects.create(title=dict(nl='dutch')) |
||
| 204 | |||
| 205 | with self.assertRaises(IntegrityError): |
||
| 206 | model.objects.create(title=dict(random='random')) |
||
| 207 | |||
| 208 | with self.assertRaises(IntegrityError): |
||
| 209 | model.objects.create(title=dict()) |
||
| 210 | |||
| 211 | with self.assertRaises(IntegrityError): |
||
| 212 | model.objects.create(title=None) |
||
| 213 | |||
| 214 | with self.assertRaises(IntegrityError): |
||
| 215 | model.objects.create(title='') |
||
| 216 | |||
| 217 | with self.assertRaises(IntegrityError): |
||
| 218 | model.objects.create(title=' ') |
||
| 219 |
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.