Skip to content

Commit

Permalink
Fix representation of sql.Placeholder
Browse files Browse the repository at this point in the history
Fix also Placeholder tests, including an error which made an assert
always true, which made us miss the regression in #1291.
  • Loading branch information
dvarrazzo committed Jun 16, 2021
1 parent 2df79c5 commit b241def
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
5 changes: 4 additions & 1 deletion lib/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 12 additions & 16 deletions tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit b241def

Please sign in to comment.