Commit 53e83f36 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

Improve buildOrderByList() that now returns a more structured result with order_by_expression.

parent a2759acd
...@@ -2346,7 +2346,13 @@ class Catalog(Folder, ...@@ -2346,7 +2346,13 @@ class Catalog(Folder,
elif order_by_expression is not None: elif order_by_expression is not None:
if not isinstance(order_by_expression, basestring): if not isinstance(order_by_expression, basestring):
raise TypeError, 'order_by_expression must be a basestring instance. Got %r.' % (order_by_expression, ) raise TypeError, 'order_by_expression must be a basestring instance. Got %r.' % (order_by_expression, )
order_by_list = [[x.strip()] for x in order_by_expression.split(',')] for x in order_by_expression.split(','):
x = x.strip()
item = x.rsplit(None, 1)
if len(item) > 1 and item[-1].upper() in ('ASC', 'DESC'):
append(item)
else:
append([x])
return order_by_list return order_by_list
def buildEntireQuery(self, kw, query_table='catalog', ignore_empty_string=1, def buildEntireQuery(self, kw, query_table='catalog', ignore_empty_string=1,
......
...@@ -791,6 +791,36 @@ class TestSQLCatalog(ERP5TypeTestCase): ...@@ -791,6 +791,36 @@ class TestSQLCatalog(ERP5TypeTestCase):
self._searchTextInDictQuery('date') self._searchTextInDictQuery('date')
self._searchTextInDictQuery('related_date') self._searchTextInDictQuery('related_date')
def test_buildOrderByList(self):
order_by_list = self._catalog.buildOrderByList(
sort_on='default',
)
self.assertEqual(order_by_list, [['default']])
order_by_list = self._catalog.buildOrderByList(
sort_on='default',
sort_order='DESC',
)
self.assertEqual(order_by_list, [['default', 'DESC']])
order_by_list = self._catalog.buildOrderByList(
sort_on=[['default', 'DESC', 'INT']]
)
self.assertEqual(order_by_list, [['default', 'DESC', 'INT']])
order_by_list = self._catalog.buildOrderByList(
order_by_expression='default'
)
order_by_list = self._catalog.buildOrderByList(
order_by_expression='default DESC'
)
self.assertEqual(order_by_list, [['default', 'DESC']])
order_by_list = self._catalog.buildOrderByList(
order_by_expression='CAST(default AS INT) DESC'
)
self.assertEqual(order_by_list, [['CAST(default AS INT)', 'DESC']])
order_by_list = self._catalog.buildOrderByList(
order_by_expression='CAST(default AS INT)'
)
self.assertEqual(order_by_list, [['CAST(default AS INT)']])
##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