Passed
Push — master ( 9a976d...d740d4 )
by Swen
02:13
created

LocalizedValueTestCase.test_construct_string()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
1
from django.conf import settings
2
from django.test import TestCase
3
from django.utils import translation
4
from django.db.utils import IntegrityError
5
6
from localized_fields import LocalizedField, LocalizedValue, LocalizedFieldForm
7
8
9
def get_init_values() -> dict:
10
    """Gets a test dictionary containing a key
11
    for every language."""
12
13
    keys = {}
14
15
    for lang_code, lang_name in settings.LANGUAGES:
16
        keys[lang_code] = 'value in %s' % lang_name
17
18
    return keys
19
20
21
class LocalizedValueTestCase(TestCase):
22
    """Tests the :see:LocalizedValue class."""
23
24
    @staticmethod
25
    def tearDown():
26
        """Assures that the current language
27
        is set back to the default."""
28
29
        translation.activate(settings.LANGUAGE_CODE)
30
31
    @staticmethod
32
    def test_init():
33
        """Tests whether the __init__ function
34
        of the :see:LocalizedValue class works
35
        as expected."""
36
37
        keys = get_init_values()
38
        value = LocalizedValue(keys)
39
40
        for lang_code, _ in settings.LANGUAGES:
41
            assert getattr(value, lang_code, None) == keys[lang_code]
42
43
    @staticmethod
44
    def test_init_default_values():
45
        """Tests wehther the __init__ function
46
        of the :see:LocalizedValue accepts the
47
        default value or an empty dict properly."""
48
49
        value = LocalizedValue()
50
51
        for lang_code, _ in settings.LANGUAGES:
52
            assert getattr(value, lang_code) is None
53
54
    @staticmethod
55
    def test_get_explicit():
56
        """Tests whether the the :see:LocalizedValue
57
        class's :see:get function works properly
58
        when specifying an explicit value."""
59
60
        keys = get_init_values()
61
        localized_value = LocalizedValue(keys)
62
63
        for language, value in keys.items():
64
            assert localized_value.get(language) == value
65
66
    @staticmethod
67
    def test_get_default_language():
68
        """Tests whether the :see:LocalizedValue
69
        class's see:get function properly
70
        gets the value in the default language."""
71
72
        keys = get_init_values()
73
        localized_value = LocalizedValue(keys)
74
75
        for language, _ in keys.items():
76
            translation.activate(language)
77
            assert localized_value.get() == keys[settings.LANGUAGE_CODE]
78
79
    @staticmethod
80
    def test_set():
81
        """Tests whether the :see:LocalizedValue
82
        class's see:set function works properly."""
83
84
        localized_value = LocalizedValue()
85
86
        for language, value in get_init_values():
87
            localized_value.set(language, value)
88
            assert localized_value.get(language) == value
89
            assert getattr(localized_value, language) == value
90
91
    @staticmethod
92
    def test_str():
93
        """Tests whether the :see:LocalizedValue
94
        class's __str__ works properly."""
95
96
        keys = get_init_values()
97
        localized_value = LocalizedValue(keys)
98
99
        for language, value in keys.items():
100
            translation.activate(language)
101
            assert str(localized_value) == value
102
103
    @staticmethod
104
    def test_str_fallback():
105
        """Tests whether the :see:LocalizedValue
106
        class's __str__'s fallback functionality
107
        works properly."""
108
109
        test_value = 'myvalue'
110
111
        localized_value = LocalizedValue({
112
            settings.LANGUAGE_CODE: test_value
113
        })
114
115
        other_language = settings.LANGUAGES[-1][0]
116
117
        # make sure that, by default it returns
118
        # the value in the default language
119
        assert str(localized_value) == test_value
120
121
        # make sure that it falls back to the
122
        # primary language when there's no value
123
        # available in the current language
124
        translation.activate(other_language)
125
        assert str(localized_value) == test_value
126
127
        # make sure that it's just __str__ falling
128
        # back and that for the other language
129
        # there's no actual value
130
        assert localized_value.get(other_language) != test_value
131
132
    @staticmethod
133
    def test_deconstruct():
134
        """Tests whether the :see:LocalizedValue
135
        class's :see:deconstruct function works properly."""
136
137
        keys = get_init_values()
138
        value = LocalizedValue(keys)
139
140
        path, args, kwargs = value.deconstruct()
141
142
        assert args[0] == keys
143
144
    @staticmethod
145
    def test_construct_string():
146
        """Tests whether the :see:LocalizedValue's constructor
147
        assumes the primary language when passing a single string."""
148
149
        value = LocalizedValue('beer')
150
        assert value.get(settings.LANGUAGE_CODE) == 'beer'
151
152
153
class LocalizedFieldTestCase(TestCase):
154
    """Tests the :see:LocalizedField class."""
155
156
    @staticmethod
157
    def test_from_db_value():
158
        """Tests whether the :see:from_db_value function
159
        produces the expected :see:LocalizedValue."""
160
161
        input_data = get_init_values()
162
        localized_value = LocalizedField.from_db_value(input_data)
163
164
        for lang_code, _ in settings.LANGUAGES:
165
            assert getattr(localized_value, lang_code) == input_data[lang_code]
166
167
    @staticmethod
168
    def test_from_db_value_none():
169
        """Tests whether the :see:from_db_valuei function
170
        correctly handles None values."""
171
172
        localized_value = LocalizedField.from_db_value(None)
173
174
        for lang_code, _ in settings.LANGUAGES:
175
            assert localized_value.get(lang_code) is None
176
177
    @staticmethod
178
    def test_to_python():
179
        """Tests whether the :see:to_python function
180
        produces the expected :see:LocalizedValue."""
181
182
        input_data = get_init_values()
183
        localized_value = LocalizedField().to_python(input_data)
184
185
        for language, value in input_data.items():
186
            assert localized_value.get(language) == value
187
188
    @staticmethod
189
    def test_to_python_none():
190
        """Tests whether the :see:to_python function
191
        produces the expected :see:LocalizedValue
192
        instance when it is passes None."""
193
194
        localized_value = LocalizedField().to_python(None)
195
        assert localized_value
196
197
        for lang_code, _ in settings.LANGUAGES:
198
            assert localized_value.get(lang_code) is None
199
200
    @staticmethod
201
    def test_to_python_non_dict():
202
        """Tests whether the :see:to_python function produces
203
        the expected :see:LocalizedValue when it is
204
        passed a non-dictionary value."""
205
206
        localized_value = LocalizedField().to_python(list())
207
        assert localized_value
208
209
        for lang_code, _ in settings.LANGUAGES:
210
            assert localized_value.get(lang_code) is None
211
212
    @staticmethod
213
    def test_get_prep_value():
214
        """"Tests whether the :see:get_prep_value function
215
        produces the expected dictionary."""
216
217
        input_data = get_init_values()
218
        localized_value = LocalizedValue(input_data)
219
220
        output_data = LocalizedField().get_prep_value(localized_value)
221
222
        for language, value in input_data.items():
223
            assert language in output_data
224
            assert output_data.get(language) == value
225
226
    @staticmethod
227
    def test_get_prep_value_none():
228
        """Tests whether the :see:get_prep_value function
229
        produces the expected output when it is passed None."""
230
231
        output_data = LocalizedField().get_prep_value(None)
232
        assert not output_data
233
234
    @staticmethod
235
    def test_get_prep_value_no_localized_value():
236
        """Tests whether the :see:get_prep_value function
237
        produces the expected output when it is passed a
238
        non-LocalizedValue value."""
239
240
        output_data = LocalizedField().get_prep_value(['huh'])
241
        assert not output_data
242
243
    def test_get_prep_value_clean(self):
244
        """Tests whether the :see:get_prep_value produces
245
        None as the output when it is passed an empty, but
246
        valid LocalizedValue value but, only when null=True."""
247
248
        localized_value = LocalizedValue()
249
250
        with self.assertRaises(IntegrityError):
251
            LocalizedField(null=False).get_prep_value(localized_value)
252
253
        assert not LocalizedField(null=True).get_prep_value(localized_value)
254
        assert not LocalizedField().clean(None)
255
        assert not LocalizedField().clean(['huh'])
256
257
    @staticmethod
258
    def test_formfield():
259
        """Tests whether the :see:formfield function
260
        correctly returns a valid form."""
261
262
        assert isinstance(
263
            LocalizedField().formfield(),
264
            LocalizedFieldForm
265
        )
266