[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'
"""cubicweb.migration unit tests
: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
"""
from os.path import abspath
from logilab.common.testlib import TestCase, unittest_main
from cubicweb.devtools import TestServerConfiguration
from cubicweb.cwconfig import CubicWebConfiguration
from cubicweb.migration import MigrationHelper, filter_scripts
from cubicweb.server.migractions import ServerMigrationHelper
class Schema(dict):
def has_entity(self, e_type):
return self.has_key(e_type)
SMIGRDIR = abspath('data/server_migration') + '/'
TMIGRDIR = abspath('data/migration') + '/'
class MigrTestConfig(TestServerConfiguration):
verbosity = 0
def migration_scripts_dir(cls):
return SMIGRDIR
def cube_migration_scripts_dir(cls, cube):
return TMIGRDIR
class MigrationToolsTC(TestCase):
def setUp(self):
self.config = MigrTestConfig('data')
from yams.schema import Schema
self.config.load_schema = lambda expand_cubes=False: Schema('test')
self.config.__class__.cubicweb_appobject_path = frozenset()
self.config.__class__.cube_appobject_path = frozenset()
def test_filter_scripts_base(self):
self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,3,0), (2,4,0)),
[])
self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,4,0), (2,5,0)),
[((2, 5, 0), SMIGRDIR+'2.5.0_Any.sql')])
self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,5,0), (2,6,0)),
[((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql')])
self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,4,0), (2,6,0)),
[((2, 5, 0), SMIGRDIR+'2.5.0_Any.sql'),
((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql')])
self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,5,0), (2,5,1)),
[])
self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,5,0), (2,10,2)),
[((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql'),
((2, 10, 2), SMIGRDIR+'2.10.2_Any.sql')])
self.assertListEquals(filter_scripts(self.config, SMIGRDIR, (2,5,1), (2,6,0)),
[((2, 6, 0), SMIGRDIR+'2.6.0_Any.sql')])
self.assertListEquals(filter_scripts(self.config, TMIGRDIR, (0,0,2), (0,0,3)),
[((0, 0, 3), TMIGRDIR+'0.0.3_Any.py')])
self.assertListEquals(filter_scripts(self.config, TMIGRDIR, (0,0,2), (0,0,4)),
[((0, 0, 3), TMIGRDIR+'0.0.3_Any.py'),
((0, 0, 4), TMIGRDIR+'0.0.4_Any.py')])
def test_filter_scripts_for_mode(self):
config = CubicWebConfiguration('data')
config.verbosity = 0
self.assert_(not isinstance(config.migration_handler(), ServerMigrationHelper))
self.assertIsInstance(config.migration_handler(), MigrationHelper)
config = self.config
config.__class__.name = 'twisted'
self.assertListEquals(filter_scripts(config, TMIGRDIR, (0,0,4), (0,1,0)),
[((0, 1 ,0), TMIGRDIR+'0.1.0_common.py'),
((0, 1 ,0), TMIGRDIR+'0.1.0_web.py')])
config.__class__.name = 'repository'
self.assertListEquals(filter_scripts(config, TMIGRDIR, (0,0,4), (0,1,0)),
[((0, 1 ,0), TMIGRDIR+'0.1.0_Any.py'),
((0, 1 ,0), TMIGRDIR+'0.1.0_common.py'),
((0, 1 ,0), TMIGRDIR+'0.1.0_repository.py')])
config.__class__.name = 'all-in-one'
self.assertListEquals(filter_scripts(config, TMIGRDIR, (0,0,4), (0,1,0)),
[((0, 1 ,0), TMIGRDIR+'0.1.0_Any.py'),
((0, 1 ,0), TMIGRDIR+'0.1.0_common.py'),
((0, 1 ,0), TMIGRDIR+'0.1.0_repository.py'),
((0, 1 ,0), TMIGRDIR+'0.1.0_web.py')])
config.__class__.name = 'repository'
from cubicweb.devtools import ApptestConfiguration, init_test_database, cleanup_sqlite
class BaseCreationTC(TestCase):
def test_db_creation(self):
"""make sure database can be created"""
config = ApptestConfiguration('data')
source = config.sources()['system']
self.assertEquals(source['db-driver'], 'sqlite')
cleanup_sqlite(source['db-name'], removetemplate=True)
init_test_database(config=config)
if __name__ == '__main__':
unittest_main()