diff --git a/product/ZSQLCatalog/Query/SimpleQuery.py b/product/ZSQLCatalog/Query/SimpleQuery.py index 846ce96f05cd04874bd169b83c23544caa6c0479..ff33b8f1f526f5fe1faf5d3109cebdbccacef9a2 100644 --- a/product/ZSQLCatalog/Query/SimpleQuery.py +++ b/product/ZSQLCatalog/Query/SimpleQuery.py @@ -62,7 +62,7 @@ class SimpleQuery(Query): if len(kw) != 1: raise ValueError, 'SimpleQuery can support one and one only column. Got %r.' % (kw, ) 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 # already). comparison_operator = comparison_operator.lower() @@ -83,6 +83,13 @@ class SimpleQuery(Query): value = value[0] else: 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.comparison_operator = comparison_operator self.group = group diff --git a/product/ZSQLCatalog/tests/testSQLCatalog.py b/product/ZSQLCatalog/tests/testSQLCatalog.py index aac426c31b0f114d7004a00d655711f8f31a8117..b133afeeb83d7d5f76a9132263063886d4aa60d1 100644 --- a/product/ZSQLCatalog/tests/testSQLCatalog.py +++ b/product/ZSQLCatalog/tests/testSQLCatalog.py @@ -406,6 +406,22 @@ class TestSQLCatalog(unittest.TestCase): self.catalog(ReferenceQuery(ReferenceQuery(ReferenceQuery(operator='match', fulltext='a b'), operator='not'), operator='and'), {'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': 'a', 'operator': 'not'}) #return catalog(title={'query': ['a', 'b'], 'operator': 'not'})