]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- handle parameter sets that aren't correctly formed, so that
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 17 Feb 2016 21:53:01 +0000 (16:53 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 17 Feb 2016 21:53:01 +0000 (16:53 -0500)
for example an exception object made within a test suite can
still repr (error seen in Keystone)

lib/sqlalchemy/sql/util.py
lib/sqlalchemy/testing/__init__.py
lib/sqlalchemy/testing/assertions.py
test/engine/test_logging.py

index d40bf48f1fac6a4414a35e8d0af98affc6a56aae..5f180646c2393e8c15b84839c916375aa000dec4 100644 (file)
@@ -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 ... "
index a2663b81089ffd94cd60fb8d9f5518f8fd0f6596..f4a23d2381628e14aae31e865c5bede348eb0e01 100644 (file)
@@ -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, \
index 21f9f68fb8bb5ef6c26330b46ad23856fe2df9cf..ea50c07386495337b28ccb9c545fe1c120936f79 100644 (file)
@@ -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)
index efdee8aef676dbc4f8746fd17b686d3e615926b3..51ebc5250648bcd1270ccbe5a1287f2045dc5b8f 100644 (file)
@@ -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))