From 5d74a9cee083d19f49e613996b7fdbae8393813d Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 11 Jun 2021 12:16:45 +0200
Subject: [PATCH] opendatasoft: order search results by text (#54442)

---
 passerelle/apps/opendatasoft/models.py |  3 +++
 tests/test_opendatasoft.py             | 16 ++++++++--------
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/passerelle/apps/opendatasoft/models.py b/passerelle/apps/opendatasoft/models.py
index a9fd3473..68a1d416 100644
--- a/passerelle/apps/opendatasoft/models.py
+++ b/passerelle/apps/opendatasoft/models.py
@@ -9,16 +9,18 @@
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU Affero General Public License for more details.
 #
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from operator import itemgetter
+
 from django.db import models
 from django.shortcuts import get_object_or_404
 from django.urls import reverse
 from django.utils.six.moves.urllib import parse as urlparse
 from django.utils.translation import ugettext_lazy as _
 
 from passerelle.base.models import BaseQuery, BaseResource
 from passerelle.utils.api import endpoint
@@ -101,16 +103,17 @@ class OpenDataSoft(BaseResource):
         for record in result_response.json().get('records'):
             data = {}
             for key, value in record.get('fields').items():
                 data[key] = value
             data['id'] = record.get('recordid')
             data['text'] = render_to_string(text_template, data).strip()
             result.append(data)
 
+        result.sort(key=itemgetter('text'))
         return {'data': result}
 
     @endpoint(
         name='q',
         description=_('Query'),
         pattern=r'^(?P<query_slug>[\w:_-]+)/$',
         perm='can_access',
         show=False,
diff --git a/tests/test_opendatasoft.py b/tests/test_opendatasoft.py
index 69eb6cfb..da56ffec 100644
--- a/tests/test_opendatasoft.py
+++ b/tests/test_opendatasoft.py
@@ -192,30 +192,30 @@ def test_search_using_q(mocked_get, app, connector):
         'text_template': '{{numero}} {{nom_rue}} {{nom_commun}}',
         'q': "rue de l'aubepine",
         'rows': 3,
     }
     mocked_get.return_value = utils.FakedResponse(content=FAKED_CONTENT_Q_SEARCH, status_code=200)
     resp = app.get(endpoint, params=params, status=200)
     assert not resp.json['err']
     assert len(resp.json['data']) == 3
-    # check order is kept
+    # check ordering
     assert [x['id'] for x in resp.json['data']] == [
-        'e00cf6161e52a4c8fe510b2b74d4952036cb3473',
         '7cafcd5c692773e8b863587b2d38d6be82e023d8',
         '0984a5e1745701f71c91af73ce764e1f7132e0ff',
+        'e00cf6161e52a4c8fe510b2b74d4952036cb3473',
     ]
     # check text results
     assert [x['text'] for x in resp.json['data']] == [
-        "33 RUE DE L'AUBEPINE Strasbourg",
         "19 RUE DE L'AUBEPINE Lipsheim",
         "29 RUE DE L'AUBEPINE Strasbourg",
+        "33 RUE DE L'AUBEPINE Strasbourg",
     ]
     # check additional attributes
-    assert [x['numero'] for x in resp.json['data']] == ['33', '19', '29']
+    assert [x['numero'] for x in resp.json['data']] == ['19', '29', '33']
 
 
 @mock.patch('passerelle.utils.Request.get')
 def test_search_using_id(mocked_get, app, connector):
     endpoint = utils.generic_endpoint_url('opendatasoft', 'search', slug=connector.slug)
     assert endpoint == '/opendatasoft/my_connector/search'
     params = {
         'dataset': 'referentiel-adresse-test',
@@ -234,30 +234,30 @@ def test_query_q_using_q(mocked_get, app, query):
     params = {
         'q': "rue de l'aubepine",
         'rows': 3,
     }
     mocked_get.return_value = utils.FakedResponse(content=FAKED_CONTENT_Q_SEARCH, status_code=200)
     resp = app.get(endpoint, params=params, status=200)
     assert not resp.json['err']
     assert len(resp.json['data']) == 3
-    # check order is kept
+    # check ordering
     assert [x['id'] for x in resp.json['data']] == [
-        'e00cf6161e52a4c8fe510b2b74d4952036cb3473',
         '7cafcd5c692773e8b863587b2d38d6be82e023d8',
         '0984a5e1745701f71c91af73ce764e1f7132e0ff',
+        'e00cf6161e52a4c8fe510b2b74d4952036cb3473',
     ]
     # check text results
     assert [x['text'] for x in resp.json['data']] == [
-        "33 RUE DE L'AUBEPINE Strasbourg",
         "19 RUE DE L'AUBEPINE Lipsheim",
         "29 RUE DE L'AUBEPINE Strasbourg",
+        "33 RUE DE L'AUBEPINE Strasbourg",
     ]
     # check additional attributes
-    assert [x['numero'] for x in resp.json['data']] == ['33', '19', '29']
+    assert [x['numero'] for x in resp.json['data']] == ['19', '29', '33']
 
 
 @mock.patch('passerelle.utils.Request.get')
 def test_query_q_using_id(mocked_get, app, query):
     endpoint = '/opendatasoft/my_connector/q/my_query/'
     params = {
         'id': '7cafcd5c692773e8b863587b2d38d6be82e023d8',
     }
-- 
2.30.2

