Skip to content

Commit

Permalink
Merge pull request #758 from c2corg/feature/sort_results
Browse files Browse the repository at this point in the history
Feature: sort result #757
  • Loading branch information
cbeauchesne authored Dec 9, 2019
2 parents 804c327 + 7901b81 commit bc1417c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
5 changes: 4 additions & 1 deletion c2corg_api/search/search_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def build_query(url_params, meta_params, doc_type):
if bbox_filter:
search = search.filter(bbox_filter)

if not search_term:
if url_params.get('sort'):
keys_to_sort = url_params.get('sort').split(',')
search = search.sort(*keys_to_sort)
elif not search_term:
# if a search term is given, the documents are sorted by a relevance
# score. if not explicitly sort by id/date.
if doc_type == OUTING_TYPE:
Expand Down
72 changes: 69 additions & 3 deletions c2corg_api/tests/views/test_outing.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,59 @@ def test_get_edit(self):
def test_get_version(self):
self.get_version(self.outing, self.outing_version)

def test_get_sort_asc(self):
""" Test ascending sorting of results for height_diff_up keyword. """
reset_search_index(self.session)
response = self.app.get(self._prefix + '?sort=height_diff_up',
status=200)
response_ids = [d['document_id'] for d in response.json['documents']]
outing_ids = [d.document_id for d in [self.outing3, self.outing4,
self.outing2, self.outing]]
self.assertEqual(response_ids, outing_ids)

def test_get_sort_desc(self):
""" Test descending sorting of results for elevation max keyword. """
reset_search_index(self.session)
response = self.app.get(self._prefix + '?sort=-elevation_max',
status=200)
response_ids = [d['document_id'] for d in response.json['documents']]
outing_ids = [d.document_id for d in [self.outing2, self.outing,
self.outing4, self.outing3]]
self.assertEqual(response_ids, outing_ids)

def test_get_sort_multi(self):
""" Test multi-criteria sorting (elevation_max: desc,
height_diff_up: asc) """
reset_search_index(self.session)
response = self.app.get(self._prefix
+ '?sort=-elevation_max,height_diff_up',
status=200)
response_ids = [d['document_id'] for d in response.json['documents']]
outing_ids = [d.document_id for d in [self.outing2, self.outing4,
self.outing, self.outing3]]
self.assertEqual(response_ids, outing_ids)

def test_get_sort_numeric_enum(self):
""" Test sorting with two different criteria:
numeric (elevation_access) and enum (condition_rating) """
reset_search_index(self.session)
response = self.app.get(self._prefix
+ '?sort=-elevation_access,condition_rating',
status=200)
response_ids = [d['document_id'] for d in response.json['documents']]
outing_ids = [d.document_id for d in [self.outing, self.outing4,
self.outing3, self.outing2]]
self.assertEqual(response_ids, outing_ids)

def test_get_sort_error(self):
""" Test failure of request (status 500) if an unknown
keyword is used.
"""
reset_search_index(self.session)
self.app.get(self._prefix
+ '?sort=-elevation_axess',
status=500)

def test_get_version_without_activity(self):
""" Tests that old outings versions without activity include the fields
of all activities.
Expand Down Expand Up @@ -871,6 +924,8 @@ def test_put_success_lang_only(self):
'elevation_max': 1500,
'height_diff_up': 800,
'height_diff_down': 800,
'elevation_access': 900,
'condition_rating': 'good',
'locales': [
{'lang': 'en', 'title': 'Mont Blanc from the air',
'description': '...', 'weather': 'mostly sunny',
Expand Down Expand Up @@ -905,6 +960,8 @@ def test_put_success_new_lang(self):
'elevation_max': 1500,
'height_diff_up': 800,
'height_diff_down': 800,
'elevation_access': 900,
'condition_rating': 'good',
'locales': [
{'lang': 'es', 'title': 'Mont Blanc del cielo',
'description': '...', 'weather': 'soleado'}
Expand Down Expand Up @@ -938,6 +995,8 @@ def test_put_success_association_update(self):
'elevation_max': 1500,
'height_diff_up': 800,
'height_diff_down': 800,
'elevation_access': 900,
'condition_rating': 'good',
'locales': [
{'lang': 'en', 'title': 'Mont Blanc from the air',
'description': '...', 'weather': 'sunny',
Expand Down Expand Up @@ -1058,7 +1117,8 @@ def _add_test_data(self):
self.outing = Outing(
activities=['skitouring'], date_start=datetime.date(2016, 1, 1),
date_end=datetime.date(2016, 1, 1), elevation_max=1500,
elevation_min=700, height_diff_up=800, height_diff_down=800
elevation_min=700, height_diff_up=800, height_diff_down=800,
elevation_access=900, condition_rating='good'
)
self.locale_en = OutingLocale(
lang='en', title='Mont Blanc from the air', description='...',
Expand Down Expand Up @@ -1087,6 +1147,8 @@ def _add_test_data(self):
self.outing2 = Outing(
activities=['skitouring'], date_start=datetime.date(2016, 2, 1),
date_end=datetime.date(2016, 2, 1),
height_diff_up=600, elevation_max=1800, elevation_access=700,
condition_rating='average',
locales=[
OutingLocale(
lang='en', title='Mont Blanc from the air',
Expand All @@ -1101,12 +1163,16 @@ def _add_test_data(self):

self.outing3 = Outing(
activities=['skitouring'], date_start=datetime.date(2016, 2, 1),
date_end=datetime.date(2016, 2, 2)
date_end=datetime.date(2016, 2, 2),
height_diff_up=200, elevation_max=1200, elevation_access=800,
condition_rating='poor'
)
self.session.add(self.outing3)
self.outing4 = Outing(
activities=['skitouring'], date_start=datetime.date(2016, 2, 1),
date_end=datetime.date(2016, 2, 3)
date_end=datetime.date(2016, 2, 3),
height_diff_up=500, elevation_max=1500, elevation_access=800,
condition_rating='excellent'
)
self.outing4.locales.append(OutingLocale(
lang='en', title='Mont Granier (en)', description='...'))
Expand Down

0 comments on commit bc1417c

Please sign in to comment.