[F] views: fix 2 unicode errors
1. You can now use valid unicode strings in ValidationError exception.
Previously, if 'err' contains unicode, UnicodeDecodeError was raised by format_errors()
>>> templstr = '<li>%s</li>\n'
>>> e = ValidationError(None, {None: u'oué, une exception en unicode!'})
>>> templstr % e
'<li>None (None): ou\xc3\xa9, une exception en unicode!</li>\n'
>>> templstr = u'<li>%s</li>\n'
>>> templstr % e
u'<li>None (None): ou\xe9, une exception en unicode!</li>\n'
2. The message of an Exception can contains unicode. But it now properly managed by “informal” string representation.
We can easily fix the problem by using the Exception.message attribute that still contains the original message.
>>> a = AssertionError(u'séfdsdf')
>>> a.message
u's\xe9fdsdf'
>>> str(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128)
>>> a = ValueError(u'fsdfsdéfsdfs')
>>> str(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 6: ordinal not in range(128)
>>> a
ValueError(u'fsdfsd\xe9fsdfs',)
>>> unicode(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 6: ordinal not in range(128)
>>> a.message
u'fsdfsd\xe9fsdfs'
"""unit tests for module cubicweb.utils
:organization: Logilab
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
"""
import re
import decimal
import datetime
from logilab.common.testlib import TestCase, unittest_main
from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList
try:
import simplejson
from cubicweb.utils import CubicWebJsonEncoder
except ImportError:
simplejson = None
class MakeUidTC(TestCase):
def test_1(self):
self.assertNotEquals(make_uid('xyz'), make_uid('abcd'))
self.assertNotEquals(make_uid('xyz'), make_uid('xyz'))
def test_2(self):
d = set()
while len(d)<10000:
uid = make_uid('xyz')
if uid in d:
self.fail(len(d))
if re.match('\d', uid):
self.fail('make_uid must not return something begining with '
'some numeric character, got %s' % uid)
d.add(uid)
class UStringIOTC(TestCase):
def test_boolean_value(self):
self.assert_(UStringIO())
class SizeConstrainedListTC(TestCase):
def test_append(self):
l = SizeConstrainedList(10)
for i in xrange(12):
l.append(i)
self.assertEquals(l, range(2, 12))
def test_extend(self):
testdata = [(range(5), range(5)),
(range(10), range(10)),
(range(12), range(2, 12)),
]
for extension, expected in testdata:
l = SizeConstrainedList(10)
l.extend(extension)
yield self.assertEquals, l, expected
class JSONEncoerTests(TestCase):
def setUp(self):
if simplejson is None:
self.skip('simplejson not available')
def encode(self, value):
return simplejson.dumps(value, cls=CubicWebJsonEncoder)
def test_encoding_dates(self):
self.assertEquals(self.encode(datetime.datetime(2009, 9, 9, 20, 30)),
'"2009/09/09 20:30:00"')
self.assertEquals(self.encode(datetime.date(2009, 9, 9)),
'"2009/09/09"')
self.assertEquals(self.encode(datetime.time(20, 30)),
'"20:30:00"')
def test_encoding_decimal(self):
self.assertEquals(self.encode(decimal.Decimal('1.2')), '1.2')
def test_encoding_unknown_stuff(self):
self.assertEquals(self.encode(TestCase), 'null')
if __name__ == '__main__':
unittest_main()