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