Passed
Push — master ( db4324...def7da )
by Swen
01:53
created

LocalizedValueTestCase.test_translate_none()   B

Complexity

Conditions 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.3411
cc 5
1
from django.db.models import F
0 ignored issues
show
Configuration introduced by
The import django.db.models 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...
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.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...
4
from django.test import TestCase, override_settings
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
6
from localized_fields.value import LocalizedValue
7
8
from .data import get_init_values
9
10
11
class LocalizedValueTestCase(TestCase):
0 ignored issues
show
Coding Style introduced by
This class has no __init__ method.
Loading history...
12
    """Tests the :see:LocalizedValue class."""
13
14
    @staticmethod
15
    def tearDown():
16
        """Assures that the current language
17
        is set back to the default."""
18
19
        translation.activate(settings.LANGUAGE_CODE)
20
21
    @staticmethod
22
    def test_init():
23
        """Tests whether the __init__ function
24
        of the :see:LocalizedValue class works
25
        as expected."""
26
27
        keys = get_init_values()
28
        value = LocalizedValue(keys)
29
30
        for lang_code, _ in settings.LANGUAGES:
31
            assert getattr(value, lang_code, None) == keys[lang_code]
32
33
    @staticmethod
34
    def test_init_default_values():
35
        """Tests whether the __init__ function
36
        of the :see:LocalizedValue accepts the
37
        default value or an empty dict properly."""
38
39
        value = LocalizedValue()
40
41
        for lang_code, _ in settings.LANGUAGES:
42
            assert getattr(value, lang_code) is None
43
44
    @staticmethod
45
    def test_init_array():
46
        """Tests whether the __init__ function
47
        of :see:LocalizedValue properly handles an
48
        array.
49
50
        Arrays can be passed to LocalizedValue as
51
        a result of a ArrayAgg operation."""
52
53
        value = LocalizedValue(['my value'])
54
        assert value.get(settings.LANGUAGE_CODE) == 'my value'
55
56
    @staticmethod
57
    def test_get_explicit():
58
        """Tests whether the the :see:LocalizedValue
59
        class's :see:get function works properly
60
        when specifying an explicit value."""
61
62
        keys = get_init_values()
63
        localized_value = LocalizedValue(keys)
64
65
        for language, value in keys.items():
66
            assert localized_value.get(language) == value
67
68
    @staticmethod
69
    def test_get_default_language():
70
        """Tests whether the :see:LocalizedValue
71
        class's see:get function properly
72
        gets the value in the default language."""
73
74
        keys = get_init_values()
75
        localized_value = LocalizedValue(keys)
76
77
        for language, _ in keys.items():
78
            translation.activate(language)
79
            assert localized_value.get() == keys[settings.LANGUAGE_CODE]
80
81
    @staticmethod
82
    def test_set():
83
        """Tests whether the :see:LocalizedValue
84
        class's see:set function works properly."""
85
86
        localized_value = LocalizedValue()
87
88
        for language, value in get_init_values():
89
            localized_value.set(language, value)
90
            assert localized_value.get(language) == value
91
            assert getattr(localized_value, language) == value
92
93
    @staticmethod
94
    def test_eq():
95
        """Tests whether the __eq__ operator
96
        of :see:LocalizedValue works properly."""
97
98
        a = LocalizedValue({'en': 'a', 'ar': 'b'})
99
        b = LocalizedValue({'en': 'a', 'ar': 'b'})
100
101
        assert a == b
102
103
        b.en = 'b'
104
        assert a != b
105
106
    @staticmethod
107
    def test_translate():
108
        """Tests whether the :see:LocalizedValue
109
        class's __str__ works properly."""
110
111
        keys = get_init_values()
112
        localized_value = LocalizedValue(keys)
113
114
        for language, value in keys.items():
115
            translation.activate(language)
116
            assert localized_value.translate() == value
117
118
    @staticmethod
119
    def test_translate_fallback():
120
        """Tests whether the :see:LocalizedValue
121
        class's translate()'s fallback functionality
122
        works properly."""
123
124
        test_value = 'myvalue'
125
126
        localized_value = LocalizedValue({
127
            settings.LANGUAGE_CODE: test_value
128
        })
129
130
        other_language = settings.LANGUAGES[-1][0]
131
132
        # make sure that, by default it returns
133
        # the value in the default language
134
        assert localized_value.translate() == test_value
135
136
        # make sure that it falls back to the
137
        # primary language when there's no value
138
        # available in the current language
139
        translation.activate(other_language)
140
        assert localized_value.translate() == test_value
141
142
        # make sure that it's just __str__ falling
143
        # back and that for the other language
144
        # there's no actual value
145
        assert localized_value.get(other_language) != test_value
146
147
    @staticmethod
148
    def test_translate_none():
149
        """Tests whether the :see:LocalizedValue
150
        class's translate() method properly returns
151
        None when there is no value."""
152
153
        # with no value, we always expect it to return None
154
        localized_value = LocalizedValue()
155
        assert localized_value.translate() == None
156
        assert str(localized_value) == ''
157
158
        # with no value for the default language, the default
159
        # behavior is to return None, unless a custom fallback
160
        # chain is configured, which there is not for this test
161
        other_language = settings.LANGUAGES[-1][0]
162
        localized_value = LocalizedValue({
163
            other_language: 'hey'
164
        })
165
166
        translation.activate(settings.LANGUAGE_CODE)
167
        assert localized_value.translate() == None
168
        assert str(localized_value) == ''
169
170
    @staticmethod
171
    def test_translate_fallback_custom_fallback():
172
        """Tests whether the :see:LocalizedValue class's
173
        translate()'s fallback functionality properly respects
174
        the LOCALIZED_FIELDS_FALLBACKS setting."""
175
176
        fallbacks = {
177
            'nl': ['ro']
178
        }
179
180
        localized_value = LocalizedValue({
181
            settings.LANGUAGE_CODE: settings.LANGUAGE_CODE,
182
            'ro': 'ro'
183
        })
184
185
        with override_settings(LOCALIZED_FIELDS_FALLBACKS=fallbacks):
186
            with translation.override('nl'):
187
                assert localized_value.translate() == 'ro'
188
189
    @staticmethod
190
    def test_deconstruct():
191
        """Tests whether the :see:LocalizedValue
192
        class's :see:deconstruct function works properly."""
193
194
        keys = get_init_values()
195
        value = LocalizedValue(keys)
196
197
        path, args, kwargs = value.deconstruct()
0 ignored issues
show
Unused Code introduced by
The variable path seems to be unused.
Loading history...
Unused Code introduced by
The variable kwargs seems to be unused.
Loading history...
198
199
        assert args[0] == keys
200
201
    @staticmethod
202
    def test_construct_string():
203
        """Tests whether the :see:LocalizedValue's constructor
204
        assumes the primary language when passing a single string."""
205
206
        value = LocalizedValue('beer')
207
        assert value.get(settings.LANGUAGE_CODE) == 'beer'
208
209
    @staticmethod
210
    def test_construct_expression():
211
        """Tests whether passing expressions as values
212
        works properly and are not converted to string."""
213
214
        value = LocalizedValue(dict(en=F('other')))
215
        assert isinstance(value.en, F)
216