Commit d987237a authored by Jérome Perrin's avatar Jérome Perrin

Simplify the regexp for t_KEYWORD and fix some problems, the one from r19669

didn't parse "c%" correctly.
Add some more tests.



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@19706 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent ba4fa5ce
......@@ -101,7 +101,7 @@ class KeyWordKey(SearchKey):
return t
def t_KEYWORD(self, t):
r'(%\S*|([^!<>=\s%]\S+|!([^=\s]\S+)?)%)'
r'(%\S*|[^!<>=\s%]*%)(?!\S)'
# KEYWORD must start and/or end with '%'.
# It may contain arbitrary letters and numbers without white space
value = t.value.strip()
......@@ -155,8 +155,10 @@ class KeyWordKey(SearchKey):
range = '='
right_side_expression = first_token.value[1:]
elif first_token.type in ('WORDSET', 'WORD',) and range == 'like':
# add trailing and leading '%' to get more results
right_side_expression = '%%%s%%' %right_side_expression
if '%' not in right_side_expression:
# If the search string doesn't already contain '%', add trailing and
# leading '%' to get more results
right_side_expression = '%%%s%%' % right_side_expression
query_kw = {key: right_side_expression,
'range': range}
query_list.append(Query(**query_kw))
......
......@@ -244,12 +244,22 @@ class TestQuery(unittest.TestCase):
full_text_search_keys=[]))
def testQueryKeywordSearchKeyWithPercent(self):
q = Query(title='Fo%oo')
self.assertEquals(dict(where_expression="((((title LIKE 'Fo%oo'))))",
select_expression_list=[]),
q.asSQLExpression(keyword_search_keys=['title'],))
def testQueryKeywordSearchKeyWithPercentAndOnlyOneLetter(self):
q = Query(title='F%o')
self.assertEquals(dict(where_expression="((((title LIKE '%F%o%'))))",
self.assertEquals(dict(where_expression="((((title LIKE 'F%o'))))",
select_expression_list=[]),
q.asSQLExpression(keyword_search_keys=['title'],
datetime_search_keys = [],
full_text_search_keys=[]))
q.asSQLExpression(keyword_search_keys=['title']))
def testQueryKeywordSearchKeyWithPercentOnly(self):
q = Query(title='%')
self.assertEquals(dict(where_expression="((((title LIKE '%'))))",
select_expression_list=[]),
q.asSQLExpression(keyword_search_keys=['title'],))
def testQueryKeywordSearchKeyWithMinus(self):
q = Query(title='F-o')
......@@ -267,6 +277,26 @@ class TestQuery(unittest.TestCase):
datetime_search_keys = [],
full_text_search_keys=[]))
def testQueryKeywordSearchKeyWithPercentAtTheEnd(self):
q = Query(title='F%')
self.assertEquals(dict(where_expression="((((title LIKE 'F%'))))",
select_expression_list=[]),
q.asSQLExpression(keyword_search_keys=['title'],))
q = Query(title='Fo%')
self.assertEquals(dict(where_expression="((((title LIKE 'Fo%'))))",
select_expression_list=[]),
q.asSQLExpression(keyword_search_keys=['title'],))
def testQueryKeywordSearchKeyWithPercentAtTheBeginning(self):
q = Query(title='%o')
self.assertEquals(dict(where_expression="((((title LIKE '%o'))))",
select_expression_list=[]),
q.asSQLExpression(keyword_search_keys=['title'],))
q = Query(title='%oo')
self.assertEquals(dict(where_expression="((((title LIKE '%oo'))))",
select_expression_list=[]),
q.asSQLExpression(keyword_search_keys=['title'],))
def testNegatedQuery(self):
q1 = Query(title='Foo')
q = NegatedQuery(q1)
......
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