-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathtest_field_translation.py
More file actions
55 lines (45 loc) · 2.04 KB
/
test_field_translation.py
File metadata and controls
55 lines (45 loc) · 2.04 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
"""
Test the case when a lazily-translated string is given as a default for
an XBlock String field.
"""
import warnings
from unittest.mock import Mock
from django.test import TestCase
from django.utils import translation
from django.utils.translation import gettext_lazy as _
from xblock.core import XBlock
from xblock.fields import FailingEnforceTypeWarning, Scope, String, ScopeIds
from xblock.runtime import (
DictKeyValueStore,
KvsFieldData,
)
from xblock.test.tools import TestRuntime, TestKey
class TestXBlockStringFieldDefaultTranslation(TestCase):
"""
Tests for an XBlock String field with a lazily-translated default value.
"""
def test_lazy_translation(self):
with warnings.catch_warnings(record=True) as caught_warnings:
warnings.simplefilter('always', FailingEnforceTypeWarning)
class XBlockTest(XBlock):
"""
Set up a class that contains a single string field with a translated
default.
"""
STR_DEFAULT_ENG = 'ENG: String to be translated'
str_field = String(scope=Scope.settings, default=_('ENG: String to be translated'))
# No FailingEnforceTypeWarning should have been triggered
assert not caught_warnings
# Construct a runtime and an XBlock using it.
key_store = DictKeyValueStore()
field_data = KvsFieldData(key_store)
runtime = TestRuntime(Mock(), services={'field-data': field_data})
# Change language to 'de'.
user_language = 'de'
with translation.override(user_language):
test_key = TestKey("XBlockTest", "k0")
tester = runtime.construct_xblock_from_class(XBlockTest, ScopeIds('s0', 'XBlockTest', test_key, test_key))
# Assert instantiated XBlock str_field value is not yet evaluated.
assert 'django.utils.functional.' in str(type(tester.str_field))
# Assert str_field *is* translated when the value is used.
assert str(tester.str_field) == 'DEU: Translated string'