Passed
Pull Request — master (#29)
by
unknown
01:40
created

LocalizedFieldTestCase.test_formfield()   F

Complexity

Conditions 16

Size

Total Lines 37

Duplication

Lines 36
Ratio 97.3 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 36
loc 37
rs 2.7326
cc 16

How to fix   Complexity   

Complexity

Complex classes like LocalizedFieldTestCase.test_formfield() 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
3
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...
4
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...
5
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...
6
7
from localized_fields.fields import LocalizedField
8
from localized_fields.forms import LocalizedFieldForm
9
from localized_fields.value import LocalizedValue
10
11
from .data import get_init_values
12
from .fake_model import get_fake_model
13
14
15
class LocalizedFieldTestCase(TestCase):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
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):
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...
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 View Code Duplication
        localized_value = LocalizedValue()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
162
163
        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...
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 View Code Duplication
            assert not field.required
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
    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):
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...
217
            model.objects.create(title=dict(ro='romanian', nl='dutch'))
218
219
        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...
220
            model.objects.create(title=dict(nl='dutch'))
221
222
        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...
223
            model.objects.create(title=dict(random='random'))
224
225
        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...
226
            model.objects.create(title=dict())
227
228
        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...
229
            model.objects.create(title=None)
230
231
        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...
232
            model.objects.create(title='')
233
234
        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...
235
            model.objects.create(title='         ')
236
237
    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):
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...
247
            model.objects.create(title=dict(ro='romanian', nl='dutch'))
248
249
        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...
250
            model.objects.create(title=dict(nl='dutch'))
251
252
        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...
253
            model.objects.create(title=dict(random='random'))
254
255
        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...
256
            model.objects.create(title=dict())
257
258
        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...
259
            model.objects.create(title=None)
260
261
        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...
262
            model.objects.create(title='')
263
264
        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...
265
            model.objects.create(title='         ')
266