|
1
|
|
|
from django.conf import settings |
|
|
|
|
|
|
2
|
|
|
from django.test import TestCase |
|
|
|
|
|
|
3
|
|
|
from django.utils import translation |
|
|
|
|
|
|
4
|
|
|
from django.db.models import F |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
from localized_fields.value import LocalizedValue |
|
7
|
|
|
|
|
8
|
|
|
from .data import get_init_values |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class LocalizedValueTestCase(TestCase): |
|
|
|
|
|
|
12
|
|
|
"""Tests the :see:LocalizedValue class.""" |
|
13
|
|
|
|
|
14
|
|
|
@staticmethod |
|
15
|
|
|
def tearDown(): |
|
16
|
|
|
"""Assures that the current language |
|
17
|
|
|
is set back to the default.""" |
|
18
|
|
|
|
|
19
|
|
|
translation.activate(settings.LANGUAGE_CODE) |
|
20
|
|
|
|
|
21
|
|
|
@staticmethod |
|
22
|
|
|
def test_init(): |
|
23
|
|
|
"""Tests whether the __init__ function |
|
24
|
|
|
of the :see:LocalizedValue class works |
|
25
|
|
|
as expected.""" |
|
26
|
|
|
|
|
27
|
|
|
keys = get_init_values() |
|
28
|
|
|
value = LocalizedValue(keys) |
|
29
|
|
|
|
|
30
|
|
|
for lang_code, _ in settings.LANGUAGES: |
|
31
|
|
|
assert getattr(value, lang_code, None) == keys[lang_code] |
|
32
|
|
|
|
|
33
|
|
|
@staticmethod |
|
34
|
|
|
def test_init_default_values(): |
|
35
|
|
|
"""Tests whether the __init__ function |
|
36
|
|
|
of the :see:LocalizedValue accepts the |
|
37
|
|
|
default value or an empty dict properly.""" |
|
38
|
|
|
|
|
39
|
|
|
value = LocalizedValue() |
|
40
|
|
|
|
|
41
|
|
|
for lang_code, _ in settings.LANGUAGES: |
|
42
|
|
|
assert getattr(value, lang_code) is None |
|
43
|
|
|
|
|
44
|
|
|
@staticmethod |
|
45
|
|
|
def test_init_array(): |
|
46
|
|
|
"""Tests whether the __init__ function |
|
47
|
|
|
of :see:LocalizedValue properly handles an |
|
48
|
|
|
array. |
|
49
|
|
|
|
|
50
|
|
|
Arrays can be passed to LocalizedValue as |
|
51
|
|
|
a result of a ArrayAgg operation.""" |
|
52
|
|
|
|
|
53
|
|
|
value = LocalizedValue(['my value']) |
|
54
|
|
|
assert value.get(settings.LANGUAGE_CODE) == 'my value' |
|
55
|
|
|
|
|
56
|
|
|
@staticmethod |
|
57
|
|
|
def test_get_explicit(): |
|
58
|
|
|
"""Tests whether the the :see:LocalizedValue |
|
59
|
|
|
class's :see:get function works properly |
|
60
|
|
|
when specifying an explicit value.""" |
|
61
|
|
|
|
|
62
|
|
|
keys = get_init_values() |
|
63
|
|
|
localized_value = LocalizedValue(keys) |
|
64
|
|
|
|
|
65
|
|
|
for language, value in keys.items(): |
|
66
|
|
|
assert localized_value.get(language) == value |
|
67
|
|
|
|
|
68
|
|
|
@staticmethod |
|
69
|
|
|
def test_get_default_language(): |
|
70
|
|
|
"""Tests whether the :see:LocalizedValue |
|
71
|
|
|
class's see:get function properly |
|
72
|
|
|
gets the value in the default language.""" |
|
73
|
|
|
|
|
74
|
|
|
keys = get_init_values() |
|
75
|
|
|
localized_value = LocalizedValue(keys) |
|
76
|
|
|
|
|
77
|
|
|
for language, _ in keys.items(): |
|
78
|
|
|
translation.activate(language) |
|
79
|
|
|
assert localized_value.get() == keys[settings.LANGUAGE_CODE] |
|
80
|
|
|
|
|
81
|
|
|
@staticmethod |
|
82
|
|
|
def test_set(): |
|
83
|
|
|
"""Tests whether the :see:LocalizedValue |
|
84
|
|
|
class's see:set function works properly.""" |
|
85
|
|
|
|
|
86
|
|
|
localized_value = LocalizedValue() |
|
87
|
|
|
|
|
88
|
|
|
for language, value in get_init_values(): |
|
89
|
|
|
localized_value.set(language, value) |
|
90
|
|
|
assert localized_value.get(language) == value |
|
91
|
|
|
assert getattr(localized_value, language) == value |
|
92
|
|
|
|
|
93
|
|
|
@staticmethod |
|
94
|
|
|
def test_str(): |
|
95
|
|
|
"""Tests whether the :see:LocalizedValue |
|
96
|
|
|
class's __str__ works properly.""" |
|
97
|
|
|
|
|
98
|
|
|
keys = get_init_values() |
|
99
|
|
|
localized_value = LocalizedValue(keys) |
|
100
|
|
|
|
|
101
|
|
|
for language, value in keys.items(): |
|
102
|
|
|
translation.activate(language) |
|
103
|
|
|
assert str(localized_value) == value |
|
104
|
|
|
|
|
105
|
|
|
@staticmethod |
|
106
|
|
|
def test_eq(): |
|
107
|
|
|
"""Tests whether the __eq__ operator |
|
108
|
|
|
of :see:LocalizedValue works properly.""" |
|
109
|
|
|
|
|
110
|
|
|
a = LocalizedValue({'en': 'a', 'ar': 'b'}) |
|
111
|
|
|
b = LocalizedValue({'en': 'a', 'ar': 'b'}) |
|
112
|
|
|
|
|
113
|
|
|
assert a == b |
|
114
|
|
|
|
|
115
|
|
|
b.en = 'b' |
|
116
|
|
|
assert a != b |
|
117
|
|
|
|
|
118
|
|
|
@staticmethod |
|
119
|
|
|
def test_str_fallback(): |
|
120
|
|
|
"""Tests whether the :see:LocalizedValue |
|
121
|
|
|
class's __str__'s fallback functionality |
|
122
|
|
|
works properly.""" |
|
123
|
|
|
|
|
124
|
|
|
test_value = 'myvalue' |
|
125
|
|
|
|
|
126
|
|
|
localized_value = LocalizedValue({ |
|
127
|
|
|
settings.LANGUAGE_CODE: test_value |
|
128
|
|
|
}) |
|
129
|
|
|
|
|
130
|
|
|
other_language = settings.LANGUAGES[-1][0] |
|
131
|
|
|
|
|
132
|
|
|
# make sure that, by default it returns |
|
133
|
|
|
# the value in the default language |
|
134
|
|
|
assert str(localized_value) == test_value |
|
135
|
|
|
|
|
136
|
|
|
# make sure that it falls back to the |
|
137
|
|
|
# primary language when there's no value |
|
138
|
|
|
# available in the current language |
|
139
|
|
|
translation.activate(other_language) |
|
140
|
|
|
assert str(localized_value) == test_value |
|
141
|
|
|
|
|
142
|
|
|
# make sure that it's just __str__ falling |
|
143
|
|
|
# back and that for the other language |
|
144
|
|
|
# there's no actual value |
|
145
|
|
|
assert localized_value.get(other_language) != test_value |
|
146
|
|
|
|
|
147
|
|
|
@staticmethod |
|
148
|
|
|
def test_str_fallback_custom_fallback(): |
|
149
|
|
|
"""Tests whether the :see:LocalizedValue class's |
|
150
|
|
|
__str__'s fallback functionality properly respects |
|
151
|
|
|
the LOCALIZED_FIELDS_FALLBACKS setting.""" |
|
152
|
|
|
|
|
153
|
|
|
settings.LOCALIZED_FIELDS_FALLBACKS = { |
|
154
|
|
|
'nl': ['ro'] |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
localized_value = LocalizedValue({ |
|
158
|
|
|
settings.LANGUAGE_CODE: settings.LANGUAGE_CODE, |
|
159
|
|
|
'ro': 'ro' |
|
160
|
|
|
}) |
|
161
|
|
|
|
|
162
|
|
|
with translation.override('nl'): |
|
163
|
|
|
assert str(localized_value) == 'ro' |
|
164
|
|
|
|
|
165
|
|
|
@staticmethod |
|
166
|
|
|
def test_deconstruct(): |
|
167
|
|
|
"""Tests whether the :see:LocalizedValue |
|
168
|
|
|
class's :see:deconstruct function works properly.""" |
|
169
|
|
|
|
|
170
|
|
|
keys = get_init_values() |
|
171
|
|
|
value = LocalizedValue(keys) |
|
172
|
|
|
|
|
173
|
|
|
path, args, kwargs = value.deconstruct() |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
assert args[0] == keys |
|
176
|
|
|
|
|
177
|
|
|
@staticmethod |
|
178
|
|
|
def test_construct_string(): |
|
179
|
|
|
"""Tests whether the :see:LocalizedValue's constructor |
|
180
|
|
|
assumes the primary language when passing a single string.""" |
|
181
|
|
|
|
|
182
|
|
|
value = LocalizedValue('beer') |
|
183
|
|
|
assert value.get(settings.LANGUAGE_CODE) == 'beer' |
|
184
|
|
|
|
|
185
|
|
|
@staticmethod |
|
186
|
|
|
def test_construct_expression(): |
|
187
|
|
|
"""Tests whether passing expressions as values |
|
188
|
|
|
works properly and are not converted to string.""" |
|
189
|
|
|
|
|
190
|
|
|
value = LocalizedValue(dict(en=F('other'))) |
|
191
|
|
|
assert isinstance(value.en, F) |
|
192
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.