source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/gluon/tests/test_languages.py @ 42095c5

mainqndtest v1.1.1
Last change on this file since 42095c5 was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100755
File size: 13.7 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""
5    Unit tests for gluon.languages
6"""
7
8import sys
9import os
10import shutil
11import tempfile
12import unittest
13
14from gluon import languages
15from gluon._compat import PY2, to_unicode, to_bytes
16from gluon.storage import Messages
17from gluon.html import SPAN
18
19MP_WORKING = 0
20try:
21    import multiprocessing
22    MP_WORKING = 1
23    #due to http://bugs.python.org/issue10845, testing multiprocessing in python is impossible
24    if sys.platform.startswith('win'):
25        MP_WORKING = 0
26    #multiprocessing is also not available on GAE. Since tests randomly
27    #fail, let's not make them on it too
28    if 'datastore' in os.getenv('DB', ''):
29        MP_WORKING = 0
30except ImportError:
31    pass
32
33
34def read_write(args):
35    (filename, iterations) = args
36    for i in range(0, iterations):
37        content = languages.read_dict(filename)
38        if not len(content):
39            return False
40        languages.write_dict(filename, content)
41    return True
42
43
44class TestLanguagesParallel(unittest.TestCase):
45
46    def setUp(self):
47        self.filename = tempfile.mktemp()
48        contents = dict()
49        for i in range(1000):
50            contents["key%d" % i] = "value%d" % i
51        languages.write_dict(self.filename, contents)
52        languages.read_dict(self.filename)
53
54    def tearDown(self):
55        try:
56            os.remove(self.filename)
57        except:
58            pass
59
60    @unittest.skipIf(MP_WORKING == 0, 'multiprocessing tests unavailable')
61    def test_reads_and_writes(self):
62        readwriters = 10
63        pool = multiprocessing.Pool(processes=readwriters)
64        results = pool.map(read_write, [[self.filename, 10]] * readwriters)
65        for result in results:
66            self.assertTrue(result)
67
68    @unittest.skipIf(MP_WORKING == 1, 'multiprocessing tests available')
69    def test_reads_and_writes_no_mp(self):
70        results = []
71        for i in range(10):
72            results.append(read_write([self.filename, 10]))
73        for result in results:
74            self.assertTrue(result)
75
76
77class TestTranslations(unittest.TestCase):
78
79    def setUp(self):
80        if os.path.isdir('gluon'):
81            self.langpath = 'applications/welcome/languages'
82        else:
83            self.langpath = os.path.realpath(
84                '../../applications/welcome/languages')
85        self.http_accept_language = 'en'
86
87    def tearDown(self):
88        pass
89
90    def test_plain(self):
91        T = languages.TranslatorFactory(self.langpath, self.http_accept_language)
92        self.assertEqual(str(T('Hello World')),
93                         'Hello World')
94        self.assertEqual(str(T('Hello World## comment')),
95                         'Hello World')
96        self.assertEqual(str(T.M('**Hello World**')),
97                         '<strong>Hello World</strong>')
98                # sub_tuple testing
99        self.assertEqual(str(T('%s %%{shop}', 1)),
100                         '1 shop')
101        self.assertEqual(str(T('%s %%{shop}', 2)),
102                         '2 shops')
103        self.assertEqual(str(T('%%{quark(%s)}', 1)),
104                         'quark')
105        self.assertEqual(str(T('%%{quark(%i)}', 2)),
106                         'quarks')
107        self.assertEqual(str(T('%%{!quark(%s)}', 1)),
108                         'Quark')
109        self.assertEqual(str(T('%%{!!quark(%i)}', 2)),
110                         'Quarks')
111        self.assertEqual(str(T('%%{!!!quark(%s)}', 0)),
112                         'QUARKS')
113        self.assertEqual(str(T('%%{?an?%i}', 1)),
114                         'an')
115        self.assertEqual(str(T('%%{?an?%s}', 0)),
116                         '0')
117        self.assertEqual(str(T('%%{??%i}', 1)),
118                         '')
119        self.assertEqual(str(T('%%{??%s}', 2)),
120                         '2')
121        self.assertEqual(str(T('%%{?%i}', 1)),
122                         '')
123        self.assertEqual(str(T('%%{?%s}', 0)),
124                         '0')
125        self.assertEqual(str(T('%%{?one?%i?zero}', 1)),
126                         'one')
127        self.assertEqual(str(T('%%{?one?%s?zero}', 23)),
128                         '23')
129        self.assertEqual(str(T('%%{?one?%i?zero}', 0)),
130                         'zero')
131        self.assertEqual(str(T('%%{?one?%s?}', 1)),
132                         'one')
133        self.assertEqual(str(T('%%{?one?%i?}', 23)),
134                         '23')
135        self.assertEqual(str(T('%%{?one?%s?}', 0)),
136                         '')
137        self.assertEqual(str(T('%%{??%i?zero}', 1)),
138                         '')
139        self.assertEqual(str(T('%%{??%s?zero}', 23)),
140                         '23')
141        self.assertEqual(str(T('%%{??%i?zero}', 0)),
142                         'zero')
143        self.assertEqual(str(T('%%{??1?}%s', '')),
144                         '')
145        self.assertEqual(str(T('%%{??%s?}', 23)),
146                         '23')
147        self.assertEqual(str(T('%%{??0?}%s', '')),
148                         '')
149        self.assertEqual(str(T('%s %%{shop[0]}', 1)),
150                         '1 shop')
151        self.assertEqual(str(T('%s %%{shop[0]}', 2)),
152                         '2 shops')
153        self.assertEqual(str(T('%i %%{?one?not_one[0]}', 1)),
154                         '1 one')
155        self.assertEqual(str(T('%i %%{?one?not_one[0]}', 2)),
156                         '2 not_one')
157        self.assertEqual(str(T('%%{??on[0]} %i', 1)),
158                         ' 1')
159        self.assertEqual(str(T('%%{??on[0]} %s', 0)),
160                         'on 0')
161        self.assertEqual(str(T('%%{?on[0]} %s', 1)),
162                         ' 1')
163        self.assertEqual(str(T('%%{?on[0]} %i', 2)),
164                         'on 2')
165        self.assertEqual(str(T('%i %%{?one?or_more?zero[0]}', 1)),
166                         '1 one')
167        self.assertEqual(str(T('%i %%{?one?or_more?zero[0]}', 2)),
168                         '2 or_more')
169        self.assertEqual(str(T('%i %%{?one?or_more?zero[0]}', 0)),
170                         '0 zero')
171        self.assertEqual(str(T('%i %%{?one?hands?[0]}', 1)),
172                         '1 one')
173        self.assertEqual(str(T('%s %%{?one?hands?[0]}', 2)),
174                         '2 hands')
175        self.assertEqual(str(T('%i %%{?one?hands?[0]}', 0)),
176                         '0 ')
177        self.assertEqual(str(T('%s %%{??or_more?zero[0]}', 1)),
178                         '1 ')
179        self.assertEqual(str(T('%i %%{??or_more?zero[0]}', 2)),
180                         '2 or_more')
181        self.assertEqual(str(T('%s %%{??or_more?zero[0]}', 0)),
182                         '0 zero')
183        self.assertEqual(str(T('%i%%{??nd?[0]}', 1)),
184                         '1')
185        self.assertEqual(str(T('%i%%{??nd?[0]}', 2)),
186                         '2nd')
187        self.assertEqual(str(T('%i%%{??nd?[0]}', 0)),
188                         '0')
189        self.assertEqual(str(T('%i%%{?st?[0]}', 1)),
190                         '1st')
191        self.assertEqual(str(T('%i%%{?st?[0]}', 2)),
192                         '2')
193        self.assertEqual(str(T('%i%%{?st?[0]}', 0)),
194                         '0')
195                # sub_dict testing
196        self.assertEqual(str(T('%(key)s %%{is(key)}', dict(key=1))),
197                         '1 is')
198        self.assertEqual(str(T('%(key)i %%{is(key)}', dict(key=2))),
199                         '2 are')
200        self.assertEqual(str(T('%%{!!!is(%(key)s)}', dict(key=2))),
201                         'ARE')
202        self.assertEqual(str(T('%(key)i %%{?not_one(key)}', dict(key=1))),
203                         '1 ')
204        self.assertEqual(str(T('%(key)s %%{?not_one(key)}', dict(key=2))),
205                         '2 not_one')
206        self.assertEqual(str(T('%(key)i %%{?not_one(key)}', dict(key=0))),
207                         '0 not_one')
208        self.assertEqual(str(T('%(key)s %%{?one?not_one(key)}', dict(key=1))),
209                         '1 one')
210        self.assertEqual(str(T('%(key)i %%{?one?not_one(key)}', dict(key=2))),
211                         '2 not_one')
212        self.assertEqual(str(T('%(key)s %%{?one?not_one(key)}', dict(key=0))),
213                         '0 not_one')
214        self.assertEqual(str(T('%(key)i %%{?one?(key)}', dict(key=1))),
215                         '1 one')
216        self.assertEqual(str(T('%(key)s %%{?one?(key)}', dict(key=2))),
217                         '2 ')
218        self.assertEqual(str(T('%(key)i %%{?one?(key)}', dict(key=0))),
219                         '0 ')
220        self.assertEqual(str(T('%(key)s %%{??not_one(key)}', dict(key=1))),
221                         '1 ')
222        self.assertEqual(str(T('%(key)i %%{??not_one(key)}', dict(key=2))),
223                         '2 not_one')
224        self.assertEqual(str(T('%(key)s %%{?not_one(key)}', dict(key=1))),
225                         '1 ')
226        self.assertEqual(str(T('%(key)i %%{?not_one(key)}', dict(key=0))),
227                         '0 not_one')
228        self.assertEqual(str(T('%(key)s %%{?one?other?zero(key)}', dict(key=1))),
229                         '1 one')
230        self.assertEqual(str(T('%(key)i %%{?one?other?zero(key)}', dict(key=4))),
231                         '4 other')
232        self.assertEqual(str(T('%(key)s %%{?one?other?zero(key)}', dict(key=0))),
233                         '0 zero')
234        self.assertEqual(str(T('%(key)i %%{?one?two_or_more?(key)}', dict(key=1))),
235                         '1 one')
236        self.assertEqual(str(T('%(key)s %%{?one?two_or_more?(key)}', dict(key=2))),
237                         '2 two_or_more')
238        self.assertEqual(str(T('%(key)i %%{?one?two_or_more?(key)}', dict(key=0))),
239                         '0 ')
240        self.assertEqual(str(T('%(key)s %%{??two_or_more?zero(key)}', dict(key=1))),
241                         '1 ')
242        self.assertEqual(str(T('%(key)i %%{??two_or_more?zero(key)}', dict(key=2))),
243                         '2 two_or_more')
244        self.assertEqual(str(T('%(key)s %%{??two_or_more?zero(key)}', dict(key=0))),
245                         '0 zero')
246        self.assertEqual(str(T('%(key)i %%{??two_or_more?(key)}', dict(key=1))),
247                         '1 ')
248        self.assertEqual(str(T('%(key)s %%{??two_or_more?(key)}', dict(key=0))),
249                         '0 ')
250        self.assertEqual(str(T('%(key)i %%{??two_or_more?(key)}', dict(key=2))),
251                         '2 two_or_more')
252        T.force('it')
253        self.assertEqual(str(T('Hello World')),
254                         'Salve Mondo')
255        self.assertEqual(to_unicode(T('Hello World')),
256                         'Salve Mondo')
257
258class TestDummyApp(unittest.TestCase):
259
260    def setUp(self):
261        pjoin = os.path.join
262        self.apppath = os.path.abspath(pjoin(os.path.dirname(os.path.abspath(__file__)), 'dummy'))
263        os.mkdir(self.apppath)
264        os.mkdir(pjoin(self.apppath, 'languages'))
265        os.mkdir(pjoin(self.apppath, 'models'))
266        os.mkdir(pjoin(self.apppath, 'controllers'))
267        os.mkdir(pjoin(self.apppath, 'views'))
268        os.mkdir(pjoin(self.apppath, 'views', 'default'))
269        os.mkdir(pjoin(self.apppath, 'modules'))
270        with open(pjoin(self.apppath, 'languages', 'en.py'), 'w') as testlang:
271            testlang.write(
272"""
273{}
274"""
275            )
276        with open(pjoin(self.apppath, 'languages', 'pt.py'), 'w') as testlang:
277            testlang.write(
278"""
279{}
280"""
281            )
282        with open(pjoin(self.apppath, 'modules', 'test.py'), 'w') as testmodule:
283            testmodule.write(
284"""
285from gluon import current
286
287hello = current.T('hello')
288"""         )
289        with open(pjoin(self.apppath, 'models', 'db.py'), 'w') as testmodel:
290            testmodel.write(
291"""
292world = T("world")
293"""
294            )
295        with open(pjoin(self.apppath, 'controllers', 'default.py'), 'w') as testcontroller:
296            testcontroller.write(
297"""
298def index():
299    message = T('%s %%{shop}', 2)
300    return dict(message=message)
301"""
302            )
303        with open(pjoin(self.apppath, 'views', 'default', 'index.html'), 'w') as testview:
304            testview.write(
305"""
306<html>
307    <head>
308    </head>
309    <body>
310    <h1>{{=T('ahoy')}}</h1>
311    </body>
312</html>
313"""
314            )
315
316    def tearDown(self):
317        shutil.rmtree(self.apppath)
318
319    def test_update_all_languages(self):
320        languages.update_all_languages(self.apppath)
321        en_file = os.path.join(self.apppath, 'languages', 'en.py')
322        pt_file = os.path.join(self.apppath, 'languages', 'pt.py')
323        en_dict = languages.read_dict(en_file)
324        pt_dict = languages.read_dict(pt_file)
325        for key in ['hello', 'world', '%s %%{shop}', 'ahoy']:
326            self.assertTrue(key in en_dict)
327            self.assertTrue(key in pt_dict)
328
329class TestMessages(unittest.TestCase):
330
331    def setUp(self):
332        if os.path.isdir('gluon'):
333            self.langpath = 'applications/welcome/languages'
334        else:
335            self.langpath = os.path.realpath(
336                '../../applications/welcome/languages')
337        self.http_accept_language = 'en'
338
339    def tearDown(self):
340        pass
341
342    def test_decode(self):
343        T = languages.TranslatorFactory(self.langpath, self.http_accept_language)
344        messages = Messages(T)
345        messages.update({'email_sent':'Email sent', 'test': "ä"})
346        self.assertEqual(to_unicode(messages.email_sent, 'utf-8'), 'Email sent')
347
348class TestHTMLTag(unittest.TestCase):
349
350    def setUp(self):
351        if os.path.isdir('gluon'):
352            self.langpath = 'applications/welcome/languages'
353        else:
354            self.langpath = os.path.realpath(
355                '../../applications/welcome/languages')
356        self.http_accept_language = 'en'
357
358    def tearDown(self):
359        pass
360
361    def test_decode(self):
362        T = languages.TranslatorFactory(self.langpath, self.http_accept_language)
363        elem = SPAN(T("Complete"))
364        self.assertEqual(elem.flatten(), "Complete")
365        elem = SPAN(T("Cannot be empty", language="ru"))
366        self.assertEqual(elem.xml(), to_bytes('<span>Пустое значение недопустимо</span>'))
367        self.assertEqual(elem.flatten(), 'Пустое значение недопустимо')
Note: See TracBrowser for help on using the repository browser.