Passed
Push — master ( 366dce...bf2995 )
by Swen
46s
created

LocalizedFieldTestCase   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 136
rs 8.2608
wmc 40

12 Methods

Rating   Name   Duplication   Size   Complexity  
A test_from_db_value_none_return_none() 0 9 3
A test_from_db_value() 0 10 3
A test_get_prep_value_none() 0 7 2
A test_get_prep_value() 0 13 4
A test_to_python() 0 10 3
A test_formfield() 0 8 2
A test_from_db_value_none() 0 9 3
A test_to_python_none() 0 11 4
B test_to_python_str() 0 13 5
A test_get_prep_value_no_localized_value() 0 8 2
A test_to_python_non_dict() 0 11 4
B test_get_prep_value_clean() 0 13 5

How to fix   Complexity   

Complex Class

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
2
from django.conf import settings
0 ignored issues
show
Configuration introduced by
The import django.conf could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
3
from django.db.utils import IntegrityError
0 ignored issues
show
Configuration introduced by
The import django.db.utils could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4
from django.test import TestCase
0 ignored issues
show
Configuration introduced by
The import django.test could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5
from django.utils import translation
0 ignored issues
show
Configuration introduced by
The import django.utils could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
6
7
from localized_fields import LocalizedField, LocalizedFieldForm, LocalizedValue
8
9
10
def get_init_values() -> dict:
11
    """Gets a test dictionary containing a key
12
    for every language."""
13
14
    keys = {}
15
16
    for lang_code, lang_name in settings.LANGUAGES:
17
        keys[lang_code] = 'value in %s' % lang_name
18
19
    return keys
20
21
22
class LocalizedValueTestCase(TestCase):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
23
    """Tests the :see:LocalizedValue class."""
24
25
    @staticmethod
26
    def tearDown():
27
        """Assures that the current language
28
        is set back to the default."""
29
30
        translation.activate(settings.LANGUAGE_CODE)
31
32
    @staticmethod
33
    def test_init():
34
        """Tests whether the __init__ function
35
        of the :see:LocalizedValue class works
36
        as expected."""
37
38
        keys = get_init_values()
39
        value = LocalizedValue(keys)
40
41
        for lang_code, _ in settings.LANGUAGES:
42
            assert getattr(value, lang_code, None) == keys[lang_code]
43
44
    @staticmethod
45
    def test_init_default_values():
46
        """Tests wehther the __init__ function
47
        of the :see:LocalizedValue accepts the
48
        default value or an empty dict properly."""
49
50
        value = LocalizedValue()
51
52
        for lang_code, _ in settings.LANGUAGES:
53
            assert getattr(value, lang_code) is None
54
55
    @staticmethod
56
    def test_get_explicit():
57
        """Tests whether the the :see:LocalizedValue
58
        class's :see:get function works properly
59
        when specifying an explicit value."""
60
61
        keys = get_init_values()
62
        localized_value = LocalizedValue(keys)
63
64
        for language, value in keys.items():
65
            assert localized_value.get(language) == value
66
67
    @staticmethod
68
    def test_get_default_language():
69
        """Tests whether the :see:LocalizedValue
70
        class's see:get function properly
71
        gets the value in the default language."""
72
73
        keys = get_init_values()
74
        localized_value = LocalizedValue(keys)
75
76
        for language, _ in keys.items():
77
            translation.activate(language)
78
            assert localized_value.get() == keys[settings.LANGUAGE_CODE]
79
80
    @staticmethod
81
    def test_set():
82
        """Tests whether the :see:LocalizedValue
83
        class's see:set function works properly."""
84
85
        localized_value = LocalizedValue()
86
87
        for language, value in get_init_values():
88
            localized_value.set(language, value)
89
            assert localized_value.get(language) == value
90
            assert getattr(localized_value, language) == value
91
92
    @staticmethod
93
    def test_str():
94
        """Tests whether the :see:LocalizedValue
95
        class's __str__ works properly."""
96
97
        keys = get_init_values()
98
        localized_value = LocalizedValue(keys)
99
100
        for language, value in keys.items():
101
            translation.activate(language)
102
            assert str(localized_value) == value
103
104
    @staticmethod
105
    def test_eq():
106
        """Tests whether the __eq__ operator
107
        of :see:LocalizedValue works properly."""
108
109
        a = LocalizedValue({'en': 'a', 'ar': 'b'})
110
        b = LocalizedValue({'en': 'a', 'ar': 'b'})
111
112
        assert a == b
113
114
        b.en = 'b'
115
        assert a != b
116
117
    @staticmethod
118
    def test_str_fallback():
119
        """Tests whether the :see:LocalizedValue
120
        class's __str__'s fallback functionality
121
        works properly."""
122
123
        test_value = 'myvalue'
124
125
        localized_value = LocalizedValue({
126
            settings.LANGUAGE_CODE: test_value
127
        })
128
129
        other_language = settings.LANGUAGES[-1][0]
130
131
        # make sure that, by default it returns
132
        # the value in the default language
133
        assert str(localized_value) == test_value
134
135
        # make sure that it falls back to the
136
        # primary language when there's no value
137
        # available in the current language
138
        translation.activate(other_language)
139
        assert str(localized_value) == test_value
140
141
        # make sure that it's just __str__ falling
142
        # back and that for the other language
143
        # there's no actual value
144
        assert localized_value.get(other_language) != test_value
145
146
    @staticmethod
147
    def test_deconstruct():
148
        """Tests whether the :see:LocalizedValue
149
        class's :see:deconstruct function works properly."""
150
151
        keys = get_init_values()
152
        value = LocalizedValue(keys)
153
154
        path, args, kwargs = value.deconstruct()
0 ignored issues
show
Unused Code introduced by
The variable kwargs seems to be unused.
Loading history...
Unused Code introduced by
The variable path seems to be unused.
Loading history...
155
156
        assert args[0] == keys
157
158
    @staticmethod
159
    def test_construct_string():
160
        """Tests whether the :see:LocalizedValue's constructor
161
        assumes the primary language when passing a single string."""
162
163
        value = LocalizedValue('beer')
164
        assert value.get(settings.LANGUAGE_CODE) == 'beer'
165
166
167
class LocalizedFieldTestCase(TestCase):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
168
    """Tests the :see:LocalizedField class."""
169
170
    @staticmethod
171
    def test_from_db_value():
172
        """Tests whether the :see:from_db_value function
173
        produces the expected :see:LocalizedValue."""
174
175
        input_data = get_init_values()
176
        localized_value = LocalizedField().from_db_value(input_data)
177
178
        for lang_code, _ in settings.LANGUAGES:
179
            assert getattr(localized_value, lang_code) == input_data[lang_code]
180
181
    @staticmethod
182
    def test_from_db_value_none():
183
        """Tests whether the :see:from_db_value function
184
        correctly handles None values."""
185
186
        localized_value = LocalizedField().from_db_value(None)
187
188
        for lang_code, _ in settings.LANGUAGES:
189
            assert localized_value.get(lang_code) is None
190
191
    def test_from_db_value_none_return_none(self):
192
        """Tests whether the :see:from_db_value function
193
        correctly handles None values when LOCALIZED_FIELDS_EXPERIMENTAL
194
        is set to True."""
195
196
        with self.settings(LOCALIZED_FIELDS_EXPERIMENTAL=True):
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldTestCase does not seem to have a member named settings.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
197
            localized_value = LocalizedField.from_db_value(None)
198
199
        assert localized_value is None
200
201
    @staticmethod
202
    def test_to_python():
203
        """Tests whether the :see:to_python function
204
        produces the expected :see:LocalizedValue."""
205
206
        input_data = get_init_values()
207
        localized_value = LocalizedField().to_python(input_data)
208
209
        for language, value in input_data.items():
210
            assert localized_value.get(language) == value
211
212
    @staticmethod
213
    def test_to_python_none():
214
        """Tests whether the :see:to_python function
215
        produces the expected :see:LocalizedValue
216
        instance when it is passes None."""
217
218
        localized_value = LocalizedField().to_python(None)
219
        assert localized_value
220
221
        for lang_code, _ in settings.LANGUAGES:
222
            assert localized_value.get(lang_code) is None
223
224
    @staticmethod
225
    def test_to_python_non_dict():
226
        """Tests whether the :see:to_python function produces
227
        the expected :see:LocalizedValue when it is
228
        passed a non-dictionary value."""
229
230
        localized_value = LocalizedField().to_python(list())
231
        assert localized_value
232
233
        for lang_code, _ in settings.LANGUAGES:
234
            assert localized_value.get(lang_code) is None
235
236
    @staticmethod
237
    def test_to_python_str():
238
        """Tests whether the :see:to_python function produces
239
        the expected :see:LocalizedValue when it is
240
        passed serialized string value."""
241
242
        serialized_str = json.dumps(get_init_values())
243
        localized_value = LocalizedField().to_python(serialized_str)
244
        assert isinstance(localized_value, LocalizedValue)
245
246
        for language, value in get_init_values().items():
247
            assert localized_value.get(language) == value
248
            assert getattr(localized_value, language) == value
249
250
    @staticmethod
251
    def test_get_prep_value():
252
        """"Tests whether the :see:get_prep_value function
253
        produces the expected dictionary."""
254
255
        input_data = get_init_values()
256
        localized_value = LocalizedValue(input_data)
257
258
        output_data = LocalizedField().get_prep_value(localized_value)
259
260
        for language, value in input_data.items():
261
            assert language in output_data
262
            assert output_data.get(language) == value
263
264
    @staticmethod
265
    def test_get_prep_value_none():
266
        """Tests whether the :see:get_prep_value function
267
        produces the expected output when it is passed None."""
268
269
        output_data = LocalizedField().get_prep_value(None)
270
        assert not output_data
271
272
    @staticmethod
273
    def test_get_prep_value_no_localized_value():
274
        """Tests whether the :see:get_prep_value function
275
        produces the expected output when it is passed a
276
        non-LocalizedValue value."""
277
278
        output_data = LocalizedField().get_prep_value(['huh'])
279
        assert not output_data
280
281
    def test_get_prep_value_clean(self):
282
        """Tests whether the :see:get_prep_value produces
283
        None as the output when it is passed an empty, but
284
        valid LocalizedValue value but, only when null=True."""
285
286
        localized_value = LocalizedValue()
287
288
        with self.assertRaises(IntegrityError):
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldTestCase does not seem to have a member named assertRaises.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
289
            LocalizedField(null=False).get_prep_value(localized_value)
290
291
        assert not LocalizedField(null=True).get_prep_value(localized_value)
292
        assert not LocalizedField().clean(None)
293
        assert not LocalizedField().clean(['huh'])
294
295
    @staticmethod
296
    def test_formfield():
297
        """Tests whether the :see:formfield function
298
        correctly returns a valid form."""
299
300
        assert isinstance(
301
            LocalizedField().formfield(),
302
            LocalizedFieldForm
303
        )
304