Allow None in Greater/Less PYSON

As all statements are evaluated even if surrounded by 'If', it is not possible
to use numeric field with Greater/Less because they can be None.

issue5711
review29381004
This commit is contained in:
Cédric Krier
2016-07-19 07:40:50 +07:00
parent 6477227a82
commit 8555aa5b0b
4 changed files with 21 additions and 2 deletions
+1
View File
@@ -1,3 +1,4 @@
* Allow None in Greater/Less PYSON
* Add option to set admin password with trytond-admin
* Remove super password
* Remove database management from RPC
+4
View File
@@ -126,6 +126,8 @@ true, then returns also true when both values of statements named by
``statement1`` and ``statement2`` are equal. In this case
:class:`Greater` works as a *greater-than or equal* operator.
.. note:: `None` value is replaced by `0` for the comparison.
.. class:: Less(statement1, statement2[, equal])
A :class:`Less` object represents the PYSON ``Less()``
@@ -137,6 +139,8 @@ then returns also true when both values of the statements named by
``statement1`` and ``statement2`` are equal. In this case
:class:`Less` works as a *less-than or equal* operator.
.. note:: `None` value is replaced by `0` for the comparison.
.. class:: If(condition, then_statement, else_statement)
An :class:`If` object represents the PYSON ``If()``
+4 -2
View File
@@ -313,10 +313,10 @@ class Greater(PYSON):
super(Greater, self).__init__()
for i in (statement1, statement2):
if isinstance(i, PYSON):
assert i.types().issubset(set([int, long, float])), \
assert i.types().issubset({int, long, float, type(None)}), \
'statement must be an integer or a float'
else:
assert isinstance(i, (int, long, float)), \
assert isinstance(i, (int, long, float, type(None))), \
'statement must be an integer or a float'
if isinstance(equal, PYSON):
assert equal.types() == set([bool])
@@ -344,6 +344,8 @@ class Greater(PYSON):
@staticmethod
def _convert(dct):
for i in ('s1', 's2'):
if dct[i] is None:
dct[i] = 0.0
if not isinstance(dct[i], (int, long, float)):
dct = dct.copy()
dct[i] = float(dct[i])
+12
View File
@@ -243,6 +243,12 @@ class PYSONTestCase(unittest.TestCase):
eval = pyson.PYSONEncoder().encode(pyson.Greater(1, 1, True))
self.assertTrue(pyson.PYSONDecoder().decode(eval))
eval = pyson.PYSONEncoder().encode(pyson.Greater(None, 1))
self.assertFalse(pyson.PYSONDecoder().decode(eval))
eval = pyson.PYSONEncoder().encode(pyson.Greater(1, None))
self.assertTrue(pyson.PYSONDecoder().decode(eval))
self.assertEqual(repr(pyson.Greater(1, 0)), 'Greater(1, 0, False)')
def test_Less(self):
@@ -278,6 +284,12 @@ class PYSONTestCase(unittest.TestCase):
eval = pyson.PYSONEncoder().encode(pyson.Less(1, 1, True))
self.assertTrue(pyson.PYSONDecoder().decode(eval))
eval = pyson.PYSONEncoder().encode(pyson.Less(None, 1))
self.assertTrue(pyson.PYSONDecoder().decode(eval))
eval = pyson.PYSONEncoder().encode(pyson.Less(1, None))
self.assertFalse(pyson.PYSONDecoder().decode(eval))
self.assertEqual(repr(pyson.Less(0, 1)), 'Less(0, 1, False)')
def test_If(self):