Commit d950213c authored by Vincent Pelletier's avatar Vincent Pelletier

Improve usability of SimpleQuery when directly instanciated: automaticaly...

Improve usability of SimpleQuery when directly instanciated: automaticaly convert an "=" comparison operator on a None value into an "is" comparison operator.
Update code comment, as it is not about backward compatibility but about usability.
Add a test.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@27070 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent be2f5359
...@@ -62,7 +62,7 @@ class SimpleQuery(Query): ...@@ -62,7 +62,7 @@ class SimpleQuery(Query):
if len(kw) != 1: if len(kw) != 1:
raise ValueError, 'SimpleQuery can support one and one only column. Got %r.' % (kw, ) raise ValueError, 'SimpleQuery can support one and one only column. Got %r.' % (kw, )
self.column, value = kw.popitem() self.column, value = kw.popitem()
# Backward compatibility code (those changes should not be needed when # Usability improvement code (those changes should not be needed when
# this Query is instanciated by a SearchKey, as operator should be correct # this Query is instanciated by a SearchKey, as operator should be correct
# already). # already).
comparison_operator = comparison_operator.lower() comparison_operator = comparison_operator.lower()
...@@ -83,6 +83,13 @@ class SimpleQuery(Query): ...@@ -83,6 +83,13 @@ class SimpleQuery(Query):
value = value[0] value = value[0]
else: else:
comparison_operator = 'in' comparison_operator = 'in'
if value is None:
if comparison_operator == '=':
comparison_operator = 'is'
elif comparison_operator != 'is':
raise ValueError, 'None value with a non-"=" comparison_operator (%r). Not sure what to do.' % (comparison_operator, )
elif comparison_operator == 'is':
raise ValueError, 'Non-None value (%r) with "is" comparison_operator. Not sure what to do.' % (value, )
self.value = value self.value = value
self.comparison_operator = comparison_operator self.comparison_operator = comparison_operator
self.group = group self.group = group
......
...@@ -406,6 +406,22 @@ class TestSQLCatalog(unittest.TestCase): ...@@ -406,6 +406,22 @@ class TestSQLCatalog(unittest.TestCase):
self.catalog(ReferenceQuery(ReferenceQuery(ReferenceQuery(operator='match', fulltext='a b'), operator='not'), operator='and'), self.catalog(ReferenceQuery(ReferenceQuery(ReferenceQuery(operator='match', fulltext='a b'), operator='not'), operator='and'),
{'fulltext': 'NOT (a b)'}) {'fulltext': 'NOT (a b)'})
def test_NoneValueToSimpleQuery(self):
"""
When a SimpleQuery receives a python None value and an "=" comparison
operator (be it the default or explictely provided), it must change that
operator into an "is" operator.
If "is" compariton operator is explicitely provided with a non-None
value, raise.
If non-"=" compariton operator is provided with a None value, raise.
"""
self.assertEqual(ReferenceQuery(operator='is', default=None),
SimpleQuery(default=None))
self.assertEqual(ReferenceQuery(operator='is', default=None),
SimpleQuery(default=None, comparison_operator='='))
self.assertRaises(ValueError, SimpleQuery, default=None, comparison_operator='>=')
self.assertRaises(ValueError, SimpleQuery, default=1, comparison_operator='is')
##return catalog(title=Query(title='a', operator='not')) ##return catalog(title=Query(title='a', operator='not'))
#return catalog(title={'query': 'a', 'operator': 'not'}) #return catalog(title={'query': 'a', 'operator': 'not'})
#return catalog(title={'query': ['a', 'b'], 'operator': 'not'}) #return catalog(title={'query': ['a', 'b'], 'operator': 'not'})
......
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