diff --git a/oslo_db/sqlalchemy/exc_filters.py b/oslo_db/sqlalchemy/exc_filters.py index 4ad6cf2..a728820 100644 --- a/oslo_db/sqlalchemy/exc_filters.py +++ b/oslo_db/sqlalchemy/exc_filters.py @@ -282,6 +282,8 @@ def _raise_mysql_table_doesnt_exist_asis( r".*1264.*Out of range value for column.*") @filters("mysql", sqla_exc.InternalError, r"^.*1366.*Incorrect string value:*") +@filters("sqlite", sqla_exc.ProgrammingError, + r"(?i).*You must not use 8-bit bytestrings*") def _raise_data_error(error, match, engine_name, is_disconnect): """Raise DBDataError exception for different data errors.""" diff --git a/oslo_db/tests/sqlalchemy/test_exc_filters.py b/oslo_db/tests/sqlalchemy/test_exc_filters.py index 7e6caf4..4593c04 100644 --- a/oslo_db/tests/sqlalchemy/test_exc_filters.py +++ b/oslo_db/tests/sqlalchemy/test_exc_filters.py @@ -452,6 +452,42 @@ class TestReferenceErrorMySQL(TestReferenceErrorSQLite, self.assertEqual("resource_foo", matched.key_table) +class TestDBDataErrorSQLite(_SQLAExceptionMatcher, test_base.DbTestCase): + + def setUp(self): + super(TestDBDataErrorSQLite, self).setUp() + + if six.PY3: + self.skip("SQLite database supports unicode value for python3") + + meta = sqla.MetaData(bind=self.engine) + + self.table_1 = sqla.Table( + "resource_foo", meta, + sqla.Column("name", sqla.String), + ) + self.table_1.create() + + def test_raise(self): + + matched = self.assertRaises( + exception.DBDataError, + self.engine.execute, + self.table_1.insert({'name': u'\u2713'.encode('utf-8')}) + ) + + self.assertInnerException( + matched, + "ProgrammingError", + "You must not use 8-bit bytestrings unless you use a " + "text_factory that can interpret 8-bit bytestrings " + "(like text_factory = str). It is highly recommended that " + "you instead just switch your application to Unicode strings.", + "INSERT INTO resource_foo (name) VALUES (?)", + (u'\u2713'.encode('utf-8'),) + ) + + class TestConstraint(TestsExceptionFilter): def test_postgresql(self): matched = self._run_test(