Passed
Push — master ( e74d21...666335 )
by Swen
01:46
created

test_default_value_override()   B

Complexity

Conditions 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 13
rs 8.5454
1
from django.conf import settings
2
from django.db.utils import IntegrityError
3
from django.test import TestCase
4
from django.utils import translation
5
6
from localized_fields.fields import LocalizedField, LocalizedValue
7
from localized_fields.forms import LocalizedFieldForm
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):
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_current_language():
69
        """Tests whether the :see:LocalizedValue
70
        class's see:get function properly
71
        gets the value in the current language."""
72
73
        keys = get_init_values()
74
        localized_value = LocalizedValue(keys)
75
76
        for language, value in keys.items():
77
            translation.activate(language)
78
            assert localized_value.get() == value
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_str_fallback():
106
        """Tests whether the :see:LocalizedValue
107
        class's __str__'s fallback functionality
108
        works properly."""
109
110
        test_value = 'myvalue'
111
112
        localized_value = LocalizedValue({
113
            settings.LANGUAGE_CODE: test_value
114
        })
115
116
        other_language = settings.LANGUAGES[-1][0]
117
118
        # make sure that, by default it returns
119
        # the value in the default language
120
        assert str(localized_value) == test_value
121
122
        # make sure that it falls back to the
123
        # primary language when there's no value
124
        # available in the current language
125
        translation.activate(other_language)
126
        assert str(localized_value) == test_value
127
128
        # make sure that it's just __str__ falling
129
        # back and that for the other language
130
        # there's no actual value
131
        assert localized_value.get(other_language) != test_value
132
133
134
class LocalizedFieldTestCase(TestCase):
135
    """Tests the :see:LocalizedField class."""
136
137
    @staticmethod
138
    def test_from_db_value():
139
        """Tests whether the :see:from_db_value function
140
        produces the expected :see:LocalizedValue."""
141
142
        input_data = get_init_values()
143
        localized_value = LocalizedField.from_db_value(input_data)
144
145
        for lang_code, _ in settings.LANGUAGES:
146
            assert getattr(localized_value, lang_code) == input_data[lang_code]
147
148
    @staticmethod
149
    def test_from_db_value_none():
150
        """Tests whether the :see:from_db_valuei function
151
        correctly handles None values."""
152
153
        localized_value = LocalizedField.from_db_value(None)
154
155
        for lang_code, _ in settings.LANGUAGES:
156
            assert localized_value.get(lang_code) is None
157
158
    @staticmethod
159
    def test_to_python():
160
        """Tests whether the :see:to_python function
161
        produces the expected :see:LocalizedValue."""
162
163
        input_data = get_init_values()
164
        localized_value = LocalizedField().to_python(input_data)
165
166
        for language, value in input_data.items():
167
            assert localized_value.get(language) == value
168
169
    @staticmethod
170
    def test_to_python_none():
171
        """Tests whether the :see:to_python function
172
        produces the expected :see:LocalizedValue
173
        instance when it is passes None."""
174
175
        localized_value = LocalizedField().to_python(None)
176
        assert localized_value
177
178
        for lang_code, _ in settings.LANGUAGES:
179
            assert localized_value.get(lang_code) is None
180
181
    @staticmethod
182
    def test_to_python_non_dict():
183
        """Tests whether the :see:to_python function produces
184
        the expected :see:LocalizedValue when it is
185
        passed a non-dictionary value."""
186
187
        localized_value = LocalizedField().to_python(list())
188
        assert localized_value
189
190
        for lang_code, _ in settings.LANGUAGES:
191
            assert localized_value.get(lang_code) is None
192
193
    @staticmethod
194
    def test_get_prep_value():
195
        """"Tests whether the :see:get_prep_value function
196
        produces the expected dictionary."""
197
198
        input_data = get_init_values()
199
        localized_value = LocalizedValue(input_data)
200
201
        output_data = LocalizedField().get_prep_value(localized_value)
202
203
        for language, value in input_data.items():
204
            assert language in output_data
205
            assert output_data.get(language) == value
206
207
    @staticmethod
208
    def test_get_prep_value_none():
209
        """Tests whether the :see:get_prep_value function
210
        produces the expected output when it is passed None."""
211
212
        output_data = LocalizedField().get_prep_value(None)
213
        assert not output_data
214
215
    @staticmethod
216
    def test_get_prep_value_no_localized_value():
217
        """Tests whether the :see:get_prep_value function
218
        produces the expected output when it is passed a
219
        non-LocalizedValue value."""
220
221
        output_data = LocalizedField().get_prep_value(['huh'])
222
        assert not output_data
223
224
    def test_get_prep_value_clean(self):
225
        """Tests whether the :see:get_prep_value produces
226
        None as the output when it is passed an empty, but
227
        valid LocalizedValue value but, only when null=True."""
228
229
        localized_value = LocalizedValue()
230
231
        with self.assertRaises(IntegrityError):
232
            LocalizedField(null=False).get_prep_value(localized_value)
233
234
        assert not LocalizedField(null=True).get_prep_value(localized_value)
235
        assert not LocalizedField().clean(None)
236
        assert not LocalizedField().clean(['huh'])
237
238
    @staticmethod
239
    def test_default_value():
240
        """Tests whether the default value is a :see:LocalizedValue
241
        instance."""
242
243
        field = LocalizedField()
244
245
        assert field.default
246
        assert isinstance(field.default, LocalizedValue)
247
248
        for lang_code, _ in settings.LANGUAGES:
249
            assert not field.default.get(lang_code)
250
251
    @staticmethod
252
    def test_default_value_override():
253
        """Tests whether the default value of a field
254
        can correctly be overriden."""
255
256
        default_value = LocalizedValue(get_init_values())
257
        field = LocalizedField(default=default_value)
258
259
        assert field.default
260
        assert isinstance(field.default, LocalizedValue)
261
262
        for lang_code, _ in settings.LANGUAGES:
263
            assert default_value.get(lang_code) == field.default.get(lang_code)
264
265
    @staticmethod
266
    def test_formfield():
267
        """Tests whether the :see:formfield function
268
        correctly returns a valid form."""
269
270
        assert isinstance(
271
            LocalizedField().formfield(),
272
            LocalizedFieldForm
273
        )
274