-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_core.py
More file actions
406 lines (367 loc) · 14.6 KB
/
test_core.py
File metadata and controls
406 lines (367 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# -*- coding: utf-8 -*-
import pytest
from mock import MagicMock, call, patch
from transifex.common.utils import generate_hashed_key, generate_key
from transifex.native.cache import MemoryCache
from transifex.native.cds import TRANSIFEX_CDS_HOST
from transifex.native.core import NotInitializedError, TxNative
from transifex.native.parsing import SourceString
from transifex.native.rendering import (PseudoTranslationPolicy,
SourceStringPolicy)
from transifex.native.settings import parse_error_policy
class TestSourceString(object):
"""Tests the functionality of the SourceString class."""
def test_default_values(self):
string = SourceString('something')
assert string.string == 'something'
assert string.context is None
assert string.meta == {}
assert string.developer_comment is None
assert string.character_limit is None
assert string.tags == []
assert string.occurrences == []
def test_custom_meta(self):
string = SourceString(
'something',
_context='one,two,three',
_comment='A crucial comment',
_charlimit=33,
_tags=' t1,t2 , t3',
custom='custom',
)
assert string.string == 'something'
assert string.context == ['one', 'two', 'three']
assert string.developer_comment == 'A crucial comment'
assert string.character_limit == 33
assert string.tags == ['t1', 't2', 't3']
assert string.meta.get('custom') is None
def test_tag_list(self):
string = SourceString(
'something',
_tags=['t1', 't2', 't3'],
)
assert string.tags == ['t1', 't2', 't3']
def test_append_values(self):
string1 = SourceString(
'something',
_tags=['t1', 't2', 't3'],
_occurrences=['f1', 'f2']
)
string2 = SourceString(
'something',
_tags=['t2', 't3', 't4'],
_occurrences=['f2', 'f3', 'f4']
)
string1.tags = string2.tags
string1.occurrences = string2.occurrences
assert sorted(string1.tags) == sorted(['t1', 't2', 't3', 't4'])
assert sorted(string1.occurrences) == sorted(['f4', 'f2', 'f3', 'f1'])
class TestNative(object):
"""Tests the TxNative class."""
def _get_tx(self, **kwargs):
mytx = TxNative()
mytx.init(['en', 'el'], 'cds_token', **kwargs)
return mytx
def test_uninitialized(self):
mytx = TxNative()
with pytest.raises(NotInitializedError):
mytx.translate('string', 'en')
with pytest.raises(NotInitializedError):
mytx.fetch_translations()
with pytest.raises(NotInitializedError):
mytx.push_source_strings([], False)
def test_default_init(self):
mytx = self._get_tx()
assert mytx.initialized is True
assert mytx._languages == ['en', 'el']
assert isinstance(mytx._missing_policy, SourceStringPolicy)
assert isinstance(mytx._cache, MemoryCache)
assert mytx._cds_handler.token == 'cds_token'
assert mytx._cds_handler.host == TRANSIFEX_CDS_HOST
def test_custom_init(self):
missing_policy = PseudoTranslationPolicy()
mytx = self._get_tx(cds_host='myhost', missing_policy=missing_policy)
assert mytx.initialized is True
assert mytx._languages == ['en', 'el']
assert mytx._missing_policy == missing_policy
assert isinstance(mytx._cache, MemoryCache)
assert mytx._cds_handler.token == 'cds_token'
assert mytx._cds_handler.host == 'myhost'
@patch('transifex.native.core.StringRenderer.render')
def test_translate_source_language_reaches_renderer(self, mock_render):
mytx = self._get_tx()
mytx.translate('My String', 'en', is_source=True)
mock_render.assert_called_once_with(
source_string='My String',
string_to_render='My String',
language_code='en',
escape=True,
missing_policy=mytx._missing_policy,
params={},
)
@patch('transifex.native.core.MemoryCache.get')
@patch('transifex.native.core.StringRenderer.render')
def test_translate_target_language_missing_reaches_renderer(self, mock_render,
mock_cache):
mock_cache.return_value = None
mytx = self._get_tx()
mytx.translate('My String', 'en', is_source=False)
mock_cache.assert_has_calls([
call(generate_key(string='My String'), 'en'),
call(generate_hashed_key(string='My String'), 'en')
])
mock_render.assert_called_once_with(
source_string='My String',
string_to_render=None,
language_code='en',
escape=True,
missing_policy=mytx._missing_policy,
params={},
)
def test_translate_target_language_missing_reaches_missing_policy(self):
missing_policy = MagicMock()
mytx = self._get_tx(missing_policy=missing_policy)
mytx.translate('My String', 'en', is_source=False)
missing_policy.get.assert_called_once_with('My String')
@patch('transifex.native.core.StringRenderer')
def test_translate_error_reaches_error_policy(self, mock_renderer):
error_policy = MagicMock()
mock_renderer.render.side_effect = Exception
mytx = self._get_tx(error_policy=error_policy)
mytx.translate('My String', 'en', is_source=False)
error_policy.get.assert_called_once_with(
source_string='My String', translation=None, language_code='en',
escape=True, params={},
)
def test_translate_error_reaches_source_string_error_policy(
self
):
# Trigger a random error in rendering to fallback to the
# error policy, e.g. an error in missing_policy
mock_missing_policy = MagicMock()
mock_missing_policy.get.side_effect = Exception
mytx = self._get_tx(missing_policy=mock_missing_policy)
result = mytx.translate('My String', 'en', is_source=False)
assert result == 'My String'
@patch('transifex.native.core.StringRenderer')
@patch('transifex.native.rendering.StringRenderer')
def test_source_string_policy_custom_text(
self, mock_renderer1, mock_renderer2
):
error_policy_identifier = (
'transifex.native.rendering.SourceStringErrorPolicy',
{'default_text': 'my-default-text'}
)
error_policy = parse_error_policy(error_policy_identifier)
mock_renderer1.render.side_effect = Exception
mock_renderer2.render.side_effect = Exception
mytx = self._get_tx(
error_policy=error_policy
)
result = mytx.translate('My String', 'en', is_source=False)
assert result == 'my-default-text'
def test_translate_source_language_renders_icu(self):
mytx = self._get_tx()
translation = mytx.translate(
u'{cnt, plural, one {{cnt} duck} other {{cnt} ducks}}',
'en',
is_source=True,
params={'cnt': 1}
)
assert translation == '1 duck'
@patch('transifex.native.core.MemoryCache.get')
def test_translate_target_language_renders_icu(self, mock_cache):
mock_cache.return_value = u'{cnt, plural, one {{cnt} παπί} other {{cnt} παπιά}}'
mytx = self._get_tx()
translation = mytx.translate(
u'{cnt, plural, one {{cnt} duck} other {{cnt} ducks}}',
'en',
is_source=False,
params={'cnt': 1}
)
assert translation == u'1 παπί'
def test_translate_source_language_escape_html_true(self):
mytx = self._get_tx()
translation = mytx.translate(
u'<script type="text/javascript">alert(1)</script>',
'en',
is_source=True,
escape=True,
params={'cnt': 1}
)
assert translation == (
u'<script type="text/javascript">alert(1)</script>'
)
def test_translate_source_language_escape_html_false(self):
mytx = self._get_tx()
translation = mytx.translate(
u'<script type="text/javascript">alert(1)</script>',
'en',
is_source=True,
escape=False,
params={'cnt': 1}
)
assert translation == u'<script type="text/javascript">alert(1)</script>'
def test_translate_source_language_returns_overridden_translation(self):
"""If there is a newer translation in the cache for the source language
use that instead of the original source string.
"""
# Populate the cache with two strings in the source language,
# essentially providing custom translations for the source strings
cache = MemoryCache()
# Source based key
source_string1 = u'{cnt, plural, one {{cnt} duck} other {{cnt} ducks}}'
updated_string1 = u'{???, plural, one {# goose} other {# geese}}'
key1 = generate_key(source_string1)
# Hash based key
source_string2 = u'Hello {username}'
updated_string2 = u'Hello to you, {username}!'
key2 = generate_hashed_key(source_string2)
data = {
'en': (
True, {
key1: {'string': updated_string1},
key2: {'string': updated_string2},
}
)
}
cache.update(data)
# Make sure the updated translations are returned
mytx = self._get_tx(cache=cache)
translation = mytx.translate(
source_string1,
'en',
is_source=True,
params={'cnt': 1}
)
assert translation == u'1 goose'
translation = mytx.translate(
source_string1,
'en',
is_source=True,
params={'cnt': 10}
)
assert translation == u'10 geese'
translation = mytx.translate(
source_string2,
'en',
is_source=True,
params={'username': 'Jane'}
)
assert translation == u'Hello to you, Jane!'
def test_translate_source_language_returns_original_string(self):
"""If there is no newer translation in the cache for the source language
use the original source string.
"""
source_string1 = u'{cnt, plural, one {{cnt} duck} other {{cnt} ducks}}'
source_string2 = u'Hello {username}'
# Make sure the updated translations are returned
mytx = self._get_tx()
translation = mytx.translate(
source_string1,
'en',
is_source=True,
params={'cnt': 1}
)
assert translation == u'1 duck'
translation = mytx.translate(
source_string1,
'en',
is_source=True,
params={'cnt': 10}
)
assert translation == u'10 ducks'
translation = mytx.translate(
source_string2,
'en',
is_source=True,
params={'username': 'Jane'}
)
assert translation == u'Hello Jane'
def test_translate_with_custom_keys(self):
# Populate the cache with two strings in the source language,
# essentially providing custom translations for the source strings
cache = MemoryCache()
source_string1 = u'Table'
source_string2 = u'Hello {username}'
data = {
'en': (
True, {
'mykey_1': {'string': u'A table'},
'mykey_2': {'string': u'Hello {username}!'},
}
),
'el': (
True, {
'mykey_1': {'string': u'Ένα τραπέζι'},
'mykey_2': {'string': u'Γεια σου, {username}!'},
}
)
}
cache.update(data)
mytx = self._get_tx(cache=cache)
translation = mytx.translate(
source_string1,
'en',
is_source=True,
_key='mykey_1',
)
assert translation == u'A table'
translation = mytx.translate(
source_string1,
'el',
_key='mykey_1',
)
assert translation == u'Ένα τραπέζι'
translation = mytx.translate(
source_string2,
'en',
is_source=True,
params={'username': 'Jane'},
_key='mykey_2',
)
assert translation == u'Hello Jane!'
translation = mytx.translate(
source_string2,
'el',
params={'username': 'Jane'},
_key='mykey_2',
)
assert translation == u'Γεια σου, Jane!'
@patch('transifex.native.core.CDSHandler.get_push_status')
@patch('transifex.native.core.CDSHandler.push_source_strings')
def test_push_strings_reaches_cds_handler(
self, mock_push_strings, mock_get_status
):
response = MagicMock()
response.status_code = 202
response.content = '{"data":{"links":{"job":"/job"}}}'
mock_push_strings.return_value = response
response = MagicMock()
response.status_code = 200
response.content = '{"data":{"status":"completed","details":{}}}'
mock_get_status.return_value = response
strings = [SourceString('a'), SourceString('b')]
mytx = self._get_tx()
mytx.push_source_strings(strings, False, True, True, True, False)
mock_push_strings.assert_called_once_with(strings, False, True, True, True, False)
@patch('transifex.native.core.MemoryCache.update')
@patch('transifex.native.core.CDSHandler.fetch_translations')
def test_fetch_translations_reaches_cds_handler_and_cache(self, mock_cds,
mock_cache):
mytx = self._get_tx()
mytx.fetch_translations()
assert mock_cds.call_count == 1
assert mock_cache.call_count > 0
@patch('transifex.native.core.MemoryCache.get')
def test_plural(self, cache_mock):
cache_mock.return_value = u'{???, plural, one {ONE} other {OTHER}}'
tx = self._get_tx()
translation = tx.translate(u'{cnt, plural, one {one} other {other}}',
u"fr_FR",
params={'cnt': 1})
assert translation == u'ONE'
translation = tx.translate(u'{cnt, plural, one {one} other {other}}',
u"fr_FR",
params={'cnt': 2})
assert translation == u'OTHER'