Passed
Push — master ( 69718c...20d9e6 )
by Swen
02:10
created

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