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.
This commit is contained in:
Daniele Varrazzo 2021-06-17 00:28:14 +01:00
parent 2df79c5a5f
commit b241def64b
2 changed files with 16 additions and 17 deletions

View File

@ -438,7 +438,10 @@ class Placeholder(Composable):
return self._wrapped return self._wrapped
def __repr__(self): 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): def as_string(self, context):
if self._wrapped is not None: if self._wrapped is not None:

View File

@ -373,29 +373,25 @@ class PlaceholderTest(ConnectingTestCase):
self.assertEqual(sql.Placeholder('foo').name, 'foo') self.assertEqual(sql.Placeholder('foo').name, 'foo')
def test_repr(self): def test_repr(self):
self.assert_(str(sql.Placeholder()), 'Placeholder()') self.assertEqual(str(sql.Placeholder()), 'Placeholder()')
self.assert_(repr(sql.Placeholder()), 'Placeholder()') self.assertEqual(repr(sql.Placeholder()), 'Placeholder()')
self.assert_(sql.Placeholder().as_string(self.conn), '%s') self.assertEqual(sql.Placeholder().as_string(self.conn), '%s')
def test_repr_name(self): def test_repr_name(self):
self.assert_(str(sql.Placeholder('foo')), "Placeholder('foo')") self.assertEqual(str(sql.Placeholder('foo')), "Placeholder('foo')")
self.assert_(repr(sql.Placeholder('foo')), "Placeholder('foo')") self.assertEqual(repr(sql.Placeholder('foo')), "Placeholder('foo')")
self.assert_(sql.Placeholder('foo').as_string(self.conn), '%(foo)s') self.assertEqual(sql.Placeholder('foo').as_string(self.conn), '%(foo)s')
def test_bad_name(self): def test_bad_name(self):
self.assertRaises(ValueError, sql.Placeholder, ')') self.assertRaises(ValueError, sql.Placeholder, ')')
def test_eq(self): def test_eq(self):
self.assert_(sql.Placeholder('foo') == sql.Placeholder('foo')) self.assertEqual(sql.Placeholder('foo'), sql.Placeholder('foo'))
self.assert_(sql.Placeholder('foo') != sql.Placeholder('bar')) self.assertNotEqual(sql.Placeholder('foo'), sql.Placeholder('bar'))
self.assert_(sql.Placeholder('foo') != 'foo') self.assertNotEqual(sql.Placeholder('foo'), 'foo')
self.assert_(sql.Placeholder() == sql.Placeholder()) self.assertEqual(sql.Placeholder(), sql.Placeholder())
self.assert_(sql.Placeholder('foo') != sql.Placeholder()) self.assertNotEqual(sql.Placeholder('foo'), sql.Placeholder())
self.assert_(sql.Placeholder('foo') != sql.Literal('foo')) self.assertNotEqual(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")
class ValuesTest(ConnectingTestCase): class ValuesTest(ConnectingTestCase):