-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathtests.py
More file actions
173 lines (137 loc) · 6.1 KB
/
Copy pathtests.py
File metadata and controls
173 lines (137 loc) · 6.1 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
# -*- coding: utf-8 -*-
from __future__ import with_statement
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import unittest
from decimal import Decimal
import flask
from datetime import datetime
from flaskext import babel
from flaskext.babel import gettext, ngettext, lazy_gettext
class DateFormattingTestCase(unittest.TestCase):
def test_basics(self):
app = flask.Flask(__name__)
b = babel.Babel(app)
d = datetime(2010, 4, 12, 13, 46)
with app.test_request_context():
assert babel.format_datetime(d) == 'Apr 12, 2010 1:46:00 PM'
assert babel.format_date(d) == 'Apr 12, 2010'
assert babel.format_time(d) == '1:46:00 PM'
with app.test_request_context():
app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna'
assert babel.format_datetime(d) == 'Apr 12, 2010 3:46:00 PM'
assert babel.format_date(d) == 'Apr 12, 2010'
assert babel.format_time(d) == '3:46:00 PM'
with app.test_request_context():
app.config['BABEL_DEFAULT_LOCALE'] = 'de_DE'
assert babel.format_datetime(d, 'long') == \
'12. April 2010 15:46:00 MESZ'
def test_init_app(self):
b = babel.Babel()
app = flask.Flask(__name__)
b.init_app(app)
d = datetime(2010, 4, 12, 13, 46)
with app.test_request_context():
assert babel.format_datetime(d) == 'Apr 12, 2010 1:46:00 PM'
assert babel.format_date(d) == 'Apr 12, 2010'
assert babel.format_time(d) == '1:46:00 PM'
with app.test_request_context():
app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna'
assert babel.format_datetime(d) == 'Apr 12, 2010 3:46:00 PM'
assert babel.format_date(d) == 'Apr 12, 2010'
assert babel.format_time(d) == '3:46:00 PM'
with app.test_request_context():
app.config['BABEL_DEFAULT_LOCALE'] = 'de_DE'
assert babel.format_datetime(d, 'long') == \
'12. April 2010 15:46:00 MESZ'
def test_custom_formats(self):
app = flask.Flask(__name__)
app.config.update(
BABEL_DEFAULT_LOCALE='en_US',
BABEL_DEFAULT_TIMEZONE='Pacific/Johnston'
)
b = babel.Babel(app)
b.date_formats['datetime'] = 'long'
b.date_formats['datetime.long'] = 'MMMM d, yyyy h:mm:ss a'
d = datetime(2010, 4, 12, 13, 46)
with app.test_request_context():
assert babel.format_datetime(d) == 'April 12, 2010 3:46:00 AM'
def test_custom_locale_selector(self):
app = flask.Flask(__name__)
b = babel.Babel(app)
d = datetime(2010, 4, 12, 13, 46)
the_timezone = 'UTC'
the_locale = 'en_US'
@b.localeselector
def select_locale():
return the_locale
@b.timezoneselector
def select_timezone():
return the_timezone
with app.test_request_context():
assert babel.format_datetime(d) == 'Apr 12, 2010 1:46:00 PM'
the_locale = 'de_DE'
the_timezone = 'Europe/Vienna'
with app.test_request_context():
assert babel.format_datetime(d) == '12.04.2010 15:46:00'
def test_refreshing(self):
app = flask.Flask(__name__)
b = babel.Babel(app)
d = datetime(2010, 4, 12, 13, 46)
with app.test_request_context():
assert babel.format_datetime(d) == 'Apr 12, 2010 1:46:00 PM'
app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna'
babel.refresh()
assert babel.format_datetime(d) == 'Apr 12, 2010 3:46:00 PM'
class NumberFormattingTestCase(unittest.TestCase):
def test_basics(self):
app = flask.Flask(__name__)
b = babel.Babel(app)
n = 1099
with app.test_request_context():
assert babel.format_number(n) == u'1,099'
assert babel.format_decimal(Decimal('1010.99')) == u'1,010.99'
assert babel.format_currency(n, 'USD') == '$1,099.00'
assert babel.format_percent(0.19) == '19%'
assert babel.format_scientific(10000) == u'1E4'
class GettextTestCase(unittest.TestCase):
def test_basics(self):
app = flask.Flask(__name__)
b = babel.Babel(app, default_locale='de_DE')
with app.test_request_context():
assert gettext(u'Hello %(name)s!', name='Peter') == 'Hallo Peter!'
assert ngettext(u'%(num)s Apple', u'%(num)s Apples', 3) == u'3 Äpfel'
assert ngettext(u'%(num)s Apple', u'%(num)s Apples', 1) == u'1 Apfel'
def test_template_basics(self):
app = flask.Flask(__name__)
b = babel.Babel(app, default_locale='de_DE')
t = lambda x: flask.render_template_string('{{ %s }}' % x)
with app.test_request_context():
assert t("gettext('Hello %(name)s!', name='Peter')") == 'Hallo Peter!'
assert t("ngettext('%(num)s Apple', '%(num)s Apples', 3)") == u'3 Äpfel'
assert t("ngettext('%(num)s Apple', '%(num)s Apples', 1)") == u'1 Apfel'
assert flask.render_template_string('''
{% trans %}Hello {{ name }}!{% endtrans %}
''', name='Peter').strip() == 'Hallo Peter!'
assert flask.render_template_string('''
{% trans num=3 %}{{ num }} Apple
{%- pluralize %}{{ num }} Apples{% endtrans %}
''', name='Peter').strip() == u'3 Äpfel'
def test_lazy_gettext(self):
app = flask.Flask(__name__)
b = babel.Babel(app, default_locale='de_DE')
yes = lazy_gettext(u'Yes')
with app.test_request_context():
assert unicode(yes) == 'Ja'
app.config['BABEL_DEFAULT_LOCALE'] = 'en_US'
with app.test_request_context():
assert unicode(yes) == 'Yes'
def test_list_translations(self):
app = flask.Flask(__name__)
b = babel.Babel(app, default_locale='de_DE')
translations = b.list_translations()
assert len(translations) == 1
assert str(translations[0]) == 'de'
if __name__ == '__main__':
unittest.main()