Projet

Général

Profil

0001-base_adresse-set-result-id-to-lookup-id-72263.patch

Benjamin Dauvergne, 12 décembre 2022 10:33

Télécharger (3,82 ko)

Voir les différences:

Subject: [PATCH] base_adresse: set result id to lookup id (#72263)

base_adresse data source ids are not canonical, many can match the same
adress. But clients of the API does not handle this well, so it's better
to always return the same id event if we know it has changed.
 passerelle/apps/base_adresse/models.py | 17 ++++++++++++++++-
 tests/test_base_adresse.py             | 16 ++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)
passerelle/apps/base_adresse/models.py
192 192
        else:
193 193
            self.sectorize(address.data)  # if sectors have been updated since caching
194 194
            address.update_timestamp()
195
            return {'data': [address.data]}
195
            result = address.data
196
            # Keep the original id if the client revalidate the
197
            # response before accepting it (like w.c.s. does).
198
            # id can change if street name changes (case change for
199
            # example).
200
            # See https://dev.entrouvert.org/issues/72263
201
            result = result.copy()
202
            result['id'] = id
203
            return {'data': [result]}
196 204
        # Use search with label as q and lat/lon as geographic hint
197 205
        if q and lat and lon:
198 206
            results = self.addresses(request, q=q, lat=lat, lon=lon, citycode=citycode)['data']
199 207
            for result in results:  # match by id if possible
200 208
                if result['ban_id'] == ban_id:
209
                    # Keep the original id if the client revalidate the
210
                    # response before accepting it (like w.c.s. does).
211
                    # id can change if street name changes (case change for
212
                    # example).
213
                    # See https://dev.entrouvert.org/issues/72263
214
                    result = result.copy()
215
                    result['id'] = id
201 216
                    return {'data': [result]}
202 217
            self.logger.error('get_by_id: id %s was not found', id)
203 218
        return {'err': _('Address ID not found')}
tests/test_base_adresse.py
1053 1053
    first_timestamp = AddressCacheModel.objects.get().timestamp
1054 1054

  
1055 1055
    resp = app.get('/base-adresse/%s/addresses?id=%s' % (base_adresse.slug, api_id))
1056
    data = resp.json['data'][0]
1056 1057
    assert mock_api_adresse_data_gouv_fr_search.call['count'] == 0
1057 1058
    assert data['text'] == 'Rue Roger Halope 49000 Angers'
1058 1059
    assert 'address' in data
......
1062 1063
    resp = app.get('/base-adresse/%s/reverse?lon=-0.593775&lat=47.474633' % base_adresse.slug)
1063 1064
    assert mock_api_adresse_data_gouv_fr_reverse.call['count'] == 2
1064 1065
    assert AddressCacheModel.objects.get().timestamp > first_timestamp
1066

  
1067
    # check lookup id is kept
1068
    resp = app.get('/base-adresse/%s/addresses?id=%s' % (base_adresse.slug, api_id.lower()))
1069
    data = resp.json['data'][0]
1070
    assert mock_api_adresse_data_gouv_fr_search.call['count'] == 0
1071
    assert data['id'] != api_id
1072
    assert data['id'] == api_id.lower()
1073

  
1074
    # without cache
1075
    assert AddressCacheModel.objects.all().delete()
1076
    resp = app.get('/base-adresse/%s/addresses?id=%s' % (base_adresse.slug, api_id.lower()))
1077
    data = resp.json['data'][0]
1078
    assert mock_api_adresse_data_gouv_fr_search.call['count'] == 1
1079
    assert data['id'] != api_id
1080
    assert data['id'] == api_id.lower()
1065
-