Passed
Push — master ( 2c5090...ccaacd )
by Swen
01:33
created

LocalizedValueTestCase.test_str()   A

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
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 import LocalizedField, LocalizedFieldForm, LocalizedValue
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
145
class LocalizedFieldTestCase(TestCase):
146
    """Tests the :see:LocalizedField class."""
147
148
    @staticmethod
149
    def test_from_db_value():
150
        """Tests whether the :see:from_db_value function
151
        produces the expected :see:LocalizedValue."""
152
153
        input_data = get_init_values()
154
        localized_value = LocalizedField.from_db_value(input_data)
155
156
        for lang_code, _ in settings.LANGUAGES:
157
            assert getattr(localized_value, lang_code) == input_data[lang_code]
158
159
    @staticmethod
160
    def test_from_db_value_none():
161
        """Tests whether the :see:from_db_valuei function
162
        correctly handles None values."""
163
164
        localized_value = LocalizedField.from_db_value(None)
165
166
        for lang_code, _ in settings.LANGUAGES:
167
            assert localized_value.get(lang_code) is None
168
169
    @staticmethod
170
    def test_to_python():
171
        """Tests whether the :see:to_python function
172
        produces the expected :see:LocalizedValue."""
173
174
        input_data = get_init_values()
175
        localized_value = LocalizedField().to_python(input_data)
176
177
        for language, value in input_data.items():
178
            assert localized_value.get(language) == value
179
180
    @staticmethod
181
    def test_to_python_none():
182
        """Tests whether the :see:to_python function
183
        produces the expected :see:LocalizedValue
184
        instance when it is passes None."""
185
186
        localized_value = LocalizedField().to_python(None)
187
        assert localized_value
188
189
        for lang_code, _ in settings.LANGUAGES:
190
            assert localized_value.get(lang_code) is None
191
192
    @staticmethod
193
    def test_to_python_non_dict():
194
        """Tests whether the :see:to_python function produces
195
        the expected :see:LocalizedValue when it is
196
        passed a non-dictionary value."""
197
198
        localized_value = LocalizedField().to_python(list())
199
        assert localized_value
200
201
        for lang_code, _ in settings.LANGUAGES:
202
            assert localized_value.get(lang_code) is None
203
204
    @staticmethod
205
    def test_get_prep_value():
206
        """"Tests whether the :see:get_prep_value function
207
        produces the expected dictionary."""
208
209
        input_data = get_init_values()
210
        localized_value = LocalizedValue(input_data)
211
212
        output_data = LocalizedField().get_prep_value(localized_value)
213
214
        for language, value in input_data.items():
215
            assert language in output_data
216
            assert output_data.get(language) == value
217
218
    @staticmethod
219
    def test_get_prep_value_none():
220
        """Tests whether the :see:get_prep_value function
221
        produces the expected output when it is passed None."""
222
223
        output_data = LocalizedField().get_prep_value(None)
224
        assert not output_data
225
226
    @staticmethod
227
    def test_get_prep_value_no_localized_value():
228
        """Tests whether the :see:get_prep_value function
229
        produces the expected output when it is passed a
230
        non-LocalizedValue value."""
231
232
        output_data = LocalizedField().get_prep_value(['huh'])
233
        assert not output_data
234
235
    def test_get_prep_value_clean(self):
236
        """Tests whether the :see:get_prep_value produces
237
        None as the output when it is passed an empty, but
238
        valid LocalizedValue value but, only when null=True."""
239
240
        localized_value = LocalizedValue()
241
242
        with self.assertRaises(IntegrityError):
243
            LocalizedField(null=False).get_prep_value(localized_value)
244
245
        assert not LocalizedField(null=True).get_prep_value(localized_value)
246
        assert not LocalizedField().clean(None)
247
        assert not LocalizedField().clean(['huh'])
248
249
    @staticmethod
250
    def test_formfield():
251
        """Tests whether the :see:formfield function
252
        correctly returns a valid form."""
253
254
        assert isinstance(
255
            LocalizedField().formfield(),
256
            LocalizedFieldForm
257
        )
258