Projet

Général

Profil

0001-opendatasoft-order-search-results-by-text-54442.patch

Nicolas Roche, 11 juin 2021 12:36

Télécharger (5,01 ko)

Voir les différences:

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(-)
passerelle/apps/opendatasoft/models.py
9 9
# This program is distributed in the hope that it will be useful,
10 10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 12
# GNU Affero General Public License for more details.
13 13
#
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
from operator import itemgetter
18

  
17 19
from django.db import models
18 20
from django.shortcuts import get_object_or_404
19 21
from django.urls import reverse
20 22
from django.utils.six.moves.urllib import parse as urlparse
21 23
from django.utils.translation import ugettext_lazy as _
22 24

  
23 25
from passerelle.base.models import BaseQuery, BaseResource
24 26
from passerelle.utils.api import endpoint
......
101 103
        for record in result_response.json().get('records'):
102 104
            data = {}
103 105
            for key, value in record.get('fields').items():
104 106
                data[key] = value
105 107
            data['id'] = record.get('recordid')
106 108
            data['text'] = render_to_string(text_template, data).strip()
107 109
            result.append(data)
108 110

  
111
        result.sort(key=itemgetter('text'))
109 112
        return {'data': result}
110 113

  
111 114
    @endpoint(
112 115
        name='q',
113 116
        description=_('Query'),
114 117
        pattern=r'^(?P<query_slug>[\w:_-]+)/$',
115 118
        perm='can_access',
116 119
        show=False,
tests/test_opendatasoft.py
192 192
        'text_template': '{{numero}} {{nom_rue}} {{nom_commun}}',
193 193
        'q': "rue de l'aubepine",
194 194
        'rows': 3,
195 195
    }
196 196
    mocked_get.return_value = utils.FakedResponse(content=FAKED_CONTENT_Q_SEARCH, status_code=200)
197 197
    resp = app.get(endpoint, params=params, status=200)
198 198
    assert not resp.json['err']
199 199
    assert len(resp.json['data']) == 3
200
    # check order is kept
200
    # check ordering
201 201
    assert [x['id'] for x in resp.json['data']] == [
202
        'e00cf6161e52a4c8fe510b2b74d4952036cb3473',
203 202
        '7cafcd5c692773e8b863587b2d38d6be82e023d8',
204 203
        '0984a5e1745701f71c91af73ce764e1f7132e0ff',
204
        'e00cf6161e52a4c8fe510b2b74d4952036cb3473',
205 205
    ]
206 206
    # check text results
207 207
    assert [x['text'] for x in resp.json['data']] == [
208
        "33 RUE DE L'AUBEPINE Strasbourg",
209 208
        "19 RUE DE L'AUBEPINE Lipsheim",
210 209
        "29 RUE DE L'AUBEPINE Strasbourg",
210
        "33 RUE DE L'AUBEPINE Strasbourg",
211 211
    ]
212 212
    # check additional attributes
213
    assert [x['numero'] for x in resp.json['data']] == ['33', '19', '29']
213
    assert [x['numero'] for x in resp.json['data']] == ['19', '29', '33']
214 214

  
215 215

  
216 216
@mock.patch('passerelle.utils.Request.get')
217 217
def test_search_using_id(mocked_get, app, connector):
218 218
    endpoint = utils.generic_endpoint_url('opendatasoft', 'search', slug=connector.slug)
219 219
    assert endpoint == '/opendatasoft/my_connector/search'
220 220
    params = {
221 221
        'dataset': 'referentiel-adresse-test',
......
234 234
    params = {
235 235
        'q': "rue de l'aubepine",
236 236
        'rows': 3,
237 237
    }
238 238
    mocked_get.return_value = utils.FakedResponse(content=FAKED_CONTENT_Q_SEARCH, status_code=200)
239 239
    resp = app.get(endpoint, params=params, status=200)
240 240
    assert not resp.json['err']
241 241
    assert len(resp.json['data']) == 3
242
    # check order is kept
242
    # check ordering
243 243
    assert [x['id'] for x in resp.json['data']] == [
244
        'e00cf6161e52a4c8fe510b2b74d4952036cb3473',
245 244
        '7cafcd5c692773e8b863587b2d38d6be82e023d8',
246 245
        '0984a5e1745701f71c91af73ce764e1f7132e0ff',
246
        'e00cf6161e52a4c8fe510b2b74d4952036cb3473',
247 247
    ]
248 248
    # check text results
249 249
    assert [x['text'] for x in resp.json['data']] == [
250
        "33 RUE DE L'AUBEPINE Strasbourg",
251 250
        "19 RUE DE L'AUBEPINE Lipsheim",
252 251
        "29 RUE DE L'AUBEPINE Strasbourg",
252
        "33 RUE DE L'AUBEPINE Strasbourg",
253 253
    ]
254 254
    # check additional attributes
255
    assert [x['numero'] for x in resp.json['data']] == ['33', '19', '29']
255
    assert [x['numero'] for x in resp.json['data']] == ['19', '29', '33']
256 256

  
257 257

  
258 258
@mock.patch('passerelle.utils.Request.get')
259 259
def test_query_q_using_id(mocked_get, app, query):
260 260
    endpoint = '/opendatasoft/my_connector/q/my_query/'
261 261
    params = {
262 262
        'id': '7cafcd5c692773e8b863587b2d38d6be82e023d8',
263 263
    }
264
-