Commit 50f43e1f authored by Tres Seaver's avatar Tres Seaver

Collector #556: <dtml-sqlvar> now returns 'null' instead of 'None'

Patch from Dieter Maurer, with tests sponsored by a bounty from Logicalware.
parent 7bf5a9ce
...@@ -26,6 +26,10 @@ Zope Changes ...@@ -26,6 +26,10 @@ Zope Changes
Bugs Fixed Bugs Fixed
- Collector #556: <dtml-sqlvar> now returns 'null' instead of 'None'
for values which are None in Python (sponsored by a bounty from
Logicalware).
- Collector #1182: BBB Forward port fix from 2.7 branch (19 months - Collector #1182: BBB Forward port fix from 2.7 branch (19 months
ago!), reverting 'guarded_getitem' to pass the 'index' argument as ago!), reverting 'guarded_getitem' to pass the 'index' argument as
the name to 'validate'. This change is *not* propagated to the the name to 'validate'. This change is *not* propagated to the
......
...@@ -98,6 +98,9 @@ class SQLVar: ...@@ -98,6 +98,9 @@ class SQLVar:
raise raise
raise ValueError, 'Missing input variable, <em>%s</em>' % name raise ValueError, 'Missing input variable, <em>%s</em>' % name
if v is None:
return 'null'
if t=='int': if t=='int':
try: try:
if type(v) is StringType: if type(v) is StringType:
......
##############################################################################
#
# Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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 UserDict 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 SQLVarTests(unittest.TestCase):
def _getTargetClass(self):
from Shared.DC.ZRDB.sqlvar import SQLVar
return SQLVar
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_constructor_no_type(self):
from DocumentTemplate.DT_Util import ParseError
self.assertRaises(ParseError, self._makeOne, 'foo')
def test_constructor_invalid_type(self):
from DocumentTemplate.DT_Util import ParseError
self.assertRaises(ParseError, self._makeOne, 'foo type="nonesuch"')
def test_constructor_valid_type(self):
from DocumentTemplate.DT_Util import ParseError
v = self._makeOne('foo type="string"')
self.assertEqual(v.__name__, 'foo')
self.assertEqual(v.expr, 'foo')
self.assertEqual(v.args['type'], 'string')
def test_render_name_returns_value(self):
v = self._makeOne('foo type="string"')
self.assertEqual(v.render(FauxMultiDict(foo='FOO')), '"FOO"')
def test_render_name_missing_required_raises_ValueError(self):
v = self._makeOne('foo type="string"')
self.assertRaises(ValueError, v.render, FauxMultiDict())
def test_render_name_missing_optional_returns_null(self):
v = self._makeOne('foo type="string" optional')
self.assertEqual(v.render(FauxMultiDict()), 'null')
def test_render_expr_returns_value(self):
v = self._makeOne('expr="foo" type="string"')
self.assertEqual(v.render(FauxMultiDict(foo='FOO')), '"FOO"')
def test_render_expr_missing_required_raises_NameError(self):
v = self._makeOne('expr="foo" type="string"')
self.assertRaises(NameError, v.render, FauxMultiDict())
def test_render_expr_missing_optional_returns_null(self):
v = self._makeOne('expr="foo" type="string" optional')
self.assertEqual(v.render(FauxMultiDict()), 'null')
def test_render_int_returns_int_without_quoting(self):
v = self._makeOne('expr="foo" type="int"')
self.assertEqual(v.render(FauxMultiDict(foo=42)), '42')
def test_render_int_with_long_returns_value_without_L(self):
v = self._makeOne('expr="foo" type="int"')
self.assertEqual(v.render(FauxMultiDict(foo='42L')), '42')
def test_render_int_required_invalid_raises_ValueError(self):
v = self._makeOne('expr="foo" type="int"')
self.assertRaises(ValueError, v.render, FauxMultiDict(foo=''))
def test_render_int_optional_invalid_returns_null(self):
v = self._makeOne('expr="foo" type="int" optional')
self.assertEqual(v.render(FauxMultiDict(foo='')), 'null')
def test_render_float_returns_float_without_quoting(self):
v = self._makeOne('expr="foo" type="float"')
self.assertEqual(v.render(FauxMultiDict(foo=3.1415)), '3.1415')
def test_render_float_with_long_returns_value_without_L(self):
v = self._makeOne('expr="foo" type="float"')
self.assertEqual(v.render(FauxMultiDict(foo='42L')), '42')
def test_render_float_required_invalid_raises_ValueError(self):
v = self._makeOne('expr="foo" type="float"')
self.assertRaises(ValueError, v.render, FauxMultiDict(foo=''))
def test_render_float_optional_invalid_returns_null(self):
v = self._makeOne('expr="foo" type="float" optional')
self.assertEqual(v.render(FauxMultiDict(foo='')), 'null')
def test_render_nb_required_with_blank_raises_ValueError(self):
v = self._makeOne('expr="foo" type="nb"')
self.assertRaises(ValueError, v.render, FauxMultiDict(foo=''))
def test_render_nb_optional_with_blank_returns_null(self):
v = self._makeOne('expr="foo" type="nb" optional')
self.assertEqual(v.render(FauxMultiDict(foo='')), 'null')
def test_render_name_with_none_returns_null(self):
# Collector #556, patch from Dieter Maurer
v = self._makeOne('foo type="string"')
self.assertEqual(v.render(FauxMultiDict(foo=None)), 'null')
def test_render_expr_with_none_returns_null(self):
# Collector #556, patch from Dieter Maurer
v = self._makeOne('expr="foo" type="string"')
self.assertEqual(v.render(FauxMultiDict(foo=None)), 'null')
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(SQLVarTests))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment