From 4a1593171ea3f475780b091654e97c7705271d7c Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Wed, 28 Jul 2021 15:47:09 +0200
Subject: [PATCH 3/3] opendatasoft: do not send sort parameter with q (#54442)

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

diff --git a/passerelle/apps/opendatasoft/models.py b/passerelle/apps/opendatasoft/models.py
index 68571a76..6756503f 100644
--- a/passerelle/apps/opendatasoft/models.py
+++ b/passerelle/apps/opendatasoft/models.py
@@ -75,17 +75,17 @@ class OpenDataSoft(BaseResource):
         if id is not None:
             params.update({'q': 'recordid:%s' % id})
         elif q is not None:
             params.update({'q': q})
         if self.api_key:
             params.update({'apikey': self.api_key})
         if limit:
             params.update({'rows': limit})
-        if sort:
+        if sort and not (q or id):
             params.update({'sort': sort})
         params.update(urlparse.parse_qs(filter_expression))
 
         result_response = self.requests.get(url, params=params)
         err_desc = result_response.json().get('error')
         if err_desc:
             raise APIError(err_desc, http_status=200)
 
diff --git a/tests/test_opendatasoft.py b/tests/test_opendatasoft.py
index 4c03b2a4..00dd0afa 100644
--- a/tests/test_opendatasoft.py
+++ b/tests/test_opendatasoft.py
@@ -236,17 +236,16 @@ def test_search_using_q(mocked_get, app, connector):
         'q': "rue de l'aubepine",
         'limit': 3,
     }
     mocked_get.return_value = utils.FakedResponse(content=FAKED_CONTENT_Q_SEARCH, status_code=200)
     resp = app.get(endpoint, params=params, status=200)
     assert mocked_get.call_args[1]['params'] == {
         'apikey': 'my_secret',
         'dataset': 'referentiel-adresse-test',
-        'sort': '-nom_rue',
         'q': "rue de l'aubepine",
         'rows': '3',
     }
     assert not resp.json['err']
     assert len(resp.json['data']) == 3
     # check order is kept
     assert [x['id'] for x in resp.json['data']] == [
         'e00cf6161e52a4c8fe510b2b74d4952036cb3473',
@@ -326,17 +325,16 @@ def test_query_q_using_q(mocked_get, app, query):
     }
     mocked_get.return_value = utils.FakedResponse(content=FAKED_CONTENT_Q_SEARCH, status_code=200)
     resp = app.get(endpoint, params=params, status=200)
     assert mocked_get.call_args[1]['params'] == {
         'apikey': 'my_secret',
         'dataset': 'referentiel-adresse-test',
         'refine.source': ['Ville et Eurométropole de Strasbourg'],
         'exclude.numero': ['42', '43'],
-        'sort': '-nom_rue',
         'q': "rue de l'aubepine",
         'rows': '3',
     }
     assert not resp.json['err']
     assert len(resp.json['data']) == 3
     # check order is kept
     assert [x['id'] for x in resp.json['data']] == [
         'e00cf6161e52a4c8fe510b2b74d4952036cb3473',
@@ -357,22 +355,21 @@ def test_query_q_using_q(mocked_get, app, query):
 def test_query_q_using_id(mocked_get, app, query):
     endpoint = '/opendatasoft/my_connector/q/my_query/'
     params = {
         'id': '7cafcd5c692773e8b863587b2d38d6be82e023d8',
     }
     mocked_get.return_value = utils.FakedResponse(content=FAKED_CONTENT_ID_SEARCH, status_code=200)
     resp = app.get(endpoint, params=params, status=200)
     assert mocked_get.call_args[1]['params'] == {
-        'dataset': 'referentiel-adresse-test',
-        'q': 'recordid:7cafcd5c692773e8b863587b2d38d6be82e023d8',
         'apikey': 'my_secret',
+        'dataset': 'referentiel-adresse-test',
         'refine.source': ['Ville et Eurométropole de Strasbourg'],
         'exclude.numero': ['42', '43'],
-        'sort': '-nom_rue',
+        'q': 'recordid:7cafcd5c692773e8b863587b2d38d6be82e023d8',
     }
     assert len(resp.json['data']) == 1
     assert resp.json['data'][0]['text'] == "19 RUE DE L'AUBEPINE Lipsheim"
 
 
 def test_opendatasoft_query_unicity(admin_user, app, connector, query):
     connector2 = OpenDataSoft.objects.create(
         slug='my_connector2',
-- 
2.30.2

