From b241def64bdc9ac350ecae4b665d9eec11a48229 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 17 Jun 2021 00:28:14 +0100 Subject: [PATCH] Fix representation of sql.Placeholder Fix also Placeholder tests, including an error which made an assert always true, which made us miss the regression in #1291. --- lib/sql.py | 5 ++++- tests/test_sql.py | 28 ++++++++++++---------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/sql.py b/lib/sql.py index 13548da4f..69b352b79 100644 --- a/lib/sql.py +++ b/lib/sql.py @@ -438,7 +438,10 @@ def name(self): return self._wrapped def __repr__(self): - return f"Placeholder({self._wrapped if self._wrapped is not None else ''!r})" + if self._wrapped is None: + return f"{self.__class__.__name__}()" + else: + return f"{self.__class__.__name__}({self._wrapped!r})" def as_string(self, context): if self._wrapped is not None: diff --git a/tests/test_sql.py b/tests/test_sql.py index 407c07dff..4519b4a6e 100755 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -373,29 +373,25 @@ def test_name(self): self.assertEqual(sql.Placeholder('foo').name, 'foo') def test_repr(self): - self.assert_(str(sql.Placeholder()), 'Placeholder()') - self.assert_(repr(sql.Placeholder()), 'Placeholder()') - self.assert_(sql.Placeholder().as_string(self.conn), '%s') + self.assertEqual(str(sql.Placeholder()), 'Placeholder()') + self.assertEqual(repr(sql.Placeholder()), 'Placeholder()') + self.assertEqual(sql.Placeholder().as_string(self.conn), '%s') def test_repr_name(self): - self.assert_(str(sql.Placeholder('foo')), "Placeholder('foo')") - self.assert_(repr(sql.Placeholder('foo')), "Placeholder('foo')") - self.assert_(sql.Placeholder('foo').as_string(self.conn), '%(foo)s') + self.assertEqual(str(sql.Placeholder('foo')), "Placeholder('foo')") + self.assertEqual(repr(sql.Placeholder('foo')), "Placeholder('foo')") + self.assertEqual(sql.Placeholder('foo').as_string(self.conn), '%(foo)s') def test_bad_name(self): self.assertRaises(ValueError, sql.Placeholder, ')') def test_eq(self): - self.assert_(sql.Placeholder('foo') == sql.Placeholder('foo')) - self.assert_(sql.Placeholder('foo') != sql.Placeholder('bar')) - self.assert_(sql.Placeholder('foo') != 'foo') - self.assert_(sql.Placeholder() == sql.Placeholder()) - self.assert_(sql.Placeholder('foo') != sql.Placeholder()) - self.assert_(sql.Placeholder('foo') != sql.Literal('foo')) - - def test_as_string(self): - self.assertEqual(sql.Placeholder().as_string(self.conn), "%s") - self.assertEqual(sql.Placeholder('foo').as_string(self.conn), "%(foo)s") + self.assertEqual(sql.Placeholder('foo'), sql.Placeholder('foo')) + self.assertNotEqual(sql.Placeholder('foo'), sql.Placeholder('bar')) + self.assertNotEqual(sql.Placeholder('foo'), 'foo') + self.assertEqual(sql.Placeholder(), sql.Placeholder()) + self.assertNotEqual(sql.Placeholder('foo'), sql.Placeholder()) + self.assertNotEqual(sql.Placeholder('foo'), sql.Literal('foo')) class ValuesTest(ConnectingTestCase):