-
Notifications
You must be signed in to change notification settings - Fork 148
/
test_i18n.py
87 lines (61 loc) · 2.64 KB
/
test_i18n.py
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
import datetime as dt
import importlib
import pytest
import humanize
def test_i18n():
three_seconds = dt.timedelta(seconds=3)
one_min_three_seconds = dt.timedelta(milliseconds=67_000)
assert humanize.naturaltime(three_seconds) == "3 seconds ago"
assert humanize.ordinal(5) == "5th"
assert humanize.precisedelta(one_min_three_seconds) == "1 minute and 7 seconds"
try:
humanize.i18n.activate("ru_RU")
assert humanize.naturaltime(three_seconds) == "3 секунды назад"
assert humanize.ordinal(5) == "5ый"
assert humanize.precisedelta(one_min_three_seconds) == "1 минута и 7 секунд"
except FileNotFoundError:
pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")
finally:
humanize.i18n.deactivate()
assert humanize.naturaltime(three_seconds) == "3 seconds ago"
assert humanize.ordinal(5) == "5th"
assert humanize.precisedelta(one_min_three_seconds) == "1 minute and 7 seconds"
def test_intcomma():
number = 10_000_000
assert humanize.intcomma(number) == "10,000,000"
try:
humanize.i18n.activate("fr_FR")
assert humanize.intcomma(number) == "10 000 000"
except FileNotFoundError:
pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")
finally:
humanize.i18n.deactivate()
assert humanize.intcomma(number) == "10,000,000"
def test_default_locale_path_defined__file__():
i18n = importlib.import_module("humanize.i18n")
assert i18n._get_default_locale_path() is not None
def test_default_locale_path_null__file__():
i18n = importlib.import_module("humanize.i18n")
i18n.__file__ = None
assert i18n._get_default_locale_path() is None
def test_default_locale_path_undefined__file__():
i18n = importlib.import_module("humanize.i18n")
del i18n.__file__
i18n._get_default_locale_path() is None
class TestActivate:
expected_msg = (
"Humanize cannot determinate the default location of the"
" 'locale' folder. You need to pass the path explicitly."
)
def test_default_locale_path_null__file__(self):
i18n = importlib.import_module("humanize.i18n")
i18n.__file__ = None
with pytest.raises(Exception) as excinfo:
i18n.activate("ru_RU")
assert str(excinfo.value) == self.expected_msg
def test_default_locale_path_undefined__file__(self):
i18n = importlib.import_module("humanize.i18n")
del i18n.__file__
with pytest.raises(Exception) as excinfo:
i18n.activate("ru_RU")
assert str(excinfo.value) == self.expected_msg