Skip to content

Commit

Permalink
- fix sqltest behavior with bytes inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
dataflake committed Aug 5, 2019
1 parent 3ba46d1 commit b18e316
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -7,6 +7,9 @@ Changelog
- show rendered SQL output even if an exception occurred
(`#15 <https://github.com/zopefoundation/Products.ZSQLMethods/issues/15>`_)

- fix sqltest behavior with bytes inputs
(`#14 <https://github.com/zopefoundation/Products.ZSQLMethods/issues/14>`_)


3.0.7 (2019-04-26)
------------------
Expand Down
2 changes: 2 additions & 0 deletions src/Shared/DC/ZRDB/sqltest.py
Expand Up @@ -163,6 +163,8 @@ def render(self, md):
else:
if not isinstance(v, StringTypes):
v = str(v)
if isinstance(v, six.binary_type):
v = v.decode('utf-8')
v = md.getitem('sql_quote__', 0)(v)
# if v.find("\'") >= 0: v="''".(v.split("\'"))
# v="'%s'" % v
Expand Down
58 changes: 58 additions & 0 deletions src/Shared/DC/ZRDB/tests/test_sqltest.py
@@ -0,0 +1,58 @@
##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
import unittest

from six.moves import UserDict


def _sql_quote(v):
return '"%s"' % v


class FauxMultiDict(UserDict):

def getitem(self, key, call):
if key == 'sql_quote__':
return _sql_quote

v = self[key]
if v is not None:
if call and callable(v):
v = v()
return v


class SQLTestTests(unittest.TestCase):

def _getTargetClass(self):
from Shared.DC.ZRDB.sqltest import SQLTest
return SQLTest

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)

def test_simple(self):
tested = self._makeOne('foo type="string"')
self.assertEqual(tested.render(FauxMultiDict(foo='FOO')),
'foo = "FOO"')

def test_binary(self):
tested = self._makeOne('foo type="string"')
self.assertEqual(tested.render(FauxMultiDict(foo=b'FOO')),
'foo = "FOO"')


def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(SQLTestTests))
return suite

0 comments on commit b18e316

Please sign in to comment.