From: Mike Bayer Date: Wed, 17 Feb 2016 21:53:01 +0000 (-0500) Subject: - handle parameter sets that aren't correctly formed, so that X-Git-Tag: rel_1_1_0b1~98^2~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2965da0a5d89119787bd45ac6f5459a7b755656d;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - handle parameter sets that aren't correctly formed, so that for example an exception object made within a test suite can still repr (error seen in Keystone) --- diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py index d40bf48f1f..5f180646c2 100644 --- a/lib/sqlalchemy/sql/util.py +++ b/lib/sqlalchemy/sql/util.py @@ -345,7 +345,7 @@ class _repr_params(_repr_base): typ = self._DICT ismulti = False else: - assert False, "Unknown parameter type %s" % (type(self.params), ) + return self.trunc(self.params) if ismulti and len(self.params) > self.batches: msg = " ... displaying %i of %i total bound parameter sets ... " diff --git a/lib/sqlalchemy/testing/__init__.py b/lib/sqlalchemy/testing/__init__.py index a2663b8108..f4a23d2381 100644 --- a/lib/sqlalchemy/testing/__init__.py +++ b/lib/sqlalchemy/testing/__init__.py @@ -22,7 +22,7 @@ from .assertions import emits_warning, emits_warning_on, uses_deprecated, \ eq_, ne_, le_, is_, is_not_, startswith_, assert_raises, \ assert_raises_message, AssertsCompiledSQL, ComparesTables, \ AssertsExecutionResults, expect_deprecated, expect_warnings, \ - in_, not_in_, eq_ignore_whitespace + in_, not_in_, eq_ignore_whitespace, eq_regex from .util import run_as_contextmanager, rowset, fail, \ provide_metadata, adict, force_drop_names, \ diff --git a/lib/sqlalchemy/testing/assertions.py b/lib/sqlalchemy/testing/assertions.py index 21f9f68fb8..ea50c07386 100644 --- a/lib/sqlalchemy/testing/assertions.py +++ b/lib/sqlalchemy/testing/assertions.py @@ -204,6 +204,10 @@ def _assert_no_stray_pool_connections(): _STRAY_CONNECTION_FAILURES = 0 +def eq_regex(a, b, msg=None): + assert re.match(b, a), msg or "%r !~ %r" % (a, b) + + def eq_(a, b, msg=None): """Assert a == b, with repr messaging on failure.""" assert a == b, msg or "%r != %r" % (a, b) diff --git a/test/engine/test_logging.py b/test/engine/test_logging.py index efdee8aef6..51ebc52506 100644 --- a/test/engine/test_logging.py +++ b/test/engine/test_logging.py @@ -1,4 +1,4 @@ -from sqlalchemy.testing import eq_, assert_raises_message +from sqlalchemy.testing import eq_, assert_raises_message, eq_regex from sqlalchemy import select import sqlalchemy as tsa from sqlalchemy.testing import engines @@ -8,6 +8,7 @@ from sqlalchemy.testing import mock from sqlalchemy.testing.util import lazy_gc from sqlalchemy import util + class LogParamsTest(fixtures.TestBase): __only_on__ = 'sqlite' __requires__ = 'ad_hoc_engines', @@ -106,6 +107,31 @@ class LogParamsTest(fixtures.TestBase): ) ) + def test_exception_format_dict_param(self): + exception = tsa.exc.IntegrityError("foo", {"x": "y"}, None) + eq_regex( + str(exception), + r"\(.*.NoneType\) None \[SQL: 'foo'\] \[parameters: {'x': 'y'}\]" + ) + + def test_exception_format_unexpected_parameter(self): + # test that if the parameters aren't any known type, we just + # run through repr() + exception = tsa.exc.IntegrityError("foo", "bar", "bat") + eq_regex( + str(exception), + r"\(.*.str\) bat \[SQL: 'foo'\] \[parameters: 'bar'\]" + ) + + def test_exception_format_unexpected_member_parameter(self): + # test that if the parameters aren't any known type, we just + # run through repr() + exception = tsa.exc.IntegrityError("foo", ["bar", "bat"], "hoho") + eq_regex( + str(exception), + r"\(.*.str\) hoho \[SQL: 'foo'\] \[parameters: \['bar', 'bat'\]\]" + ) + def test_result_large_param(self): import random largeparam = ''.join(chr(random.randint(52, 85)) for i in range(5000))