Projet

Général

Profil

0005-to-be-fixed-up-return-error-if-there-were-no-results.patch

Benjamin Dauvergne, 13 juillet 2021 16:42

Télécharger (2,91 ko)

Voir les différences:

Subject: [PATCH 5/5] to be fixed up: return error if there were no results,
 and log a warning if ban_id is not found

 passerelle/apps/base_adresse/models.py |  9 ++++++---
 tests/test_base_adresse.py             | 12 +++++++++++-
 2 files changed, 17 insertions(+), 4 deletions(-)
passerelle/apps/base_adresse/models.py
187 187
            # in the hypothetical case that the ban_id has changed, instead of
188 188
            # returning an error we bet that the first result is the good one,
189 189
            # at least the nearest from the one we search.
190
            return results
191
        else:  # retrocompatibility with raw BAN id
192
            return {'err': _('Address ID not found')}
190
            self.logger.warning(
191
                'get_by_id: id %s was not exactly found, returning all best matches %s', id, results
192
            )
193
            if results:
194
                return results
195
        return {'err': _('Address ID not found')}
193 196

  
194 197
    @endpoint(
195 198
        pattern='(?P<q>.+)?$',
tests/test_base_adresse.py
833 833
    assert 'lon=43' in mocked_get.call_args[0][0]
834 834

  
835 835

  
836
def test_base_adresse_addresses_cache(app, base_adresse, mock_api_adresse_data_gouv_fr_search):
836
def test_base_adresse_addresses_cache(app, base_adresse, mock_api_adresse_data_gouv_fr_search, caplog):
837 837
    resp = app.get('/base-adresse/%s/addresses?q=plop' % base_adresse.slug)
838 838
    assert mock_api_adresse_data_gouv_fr_search.call['count'] == 1
839 839

  
......
861 861
    assert data['text'] == 'Rue Roger Halope 49000 Angers'
862 862
    assert 'address' in data
863 863

  
864
    # no cache and id has changed
865
    AddressCacheModel.objects.all().delete()
866
    api_id = '49007_XXXX_be54bd~47.474633~-0.593775~Rue%20Roger%20Halope%2049000%20Angers'
867
    resp = app.get('/base-adresse/%s/addresses?id=%s' % (base_adresse.slug, api_id))
868
    assert AddressCacheModel.objects.count() == 1
869
    assert mock_api_adresse_data_gouv_fr_search.call['count'] == 4
870
    assert data['text'] == 'Rue Roger Halope 49000 Angers'
871
    assert 'address' in data
872
    assert 'all best matches' in caplog.text
873

  
864 874

  
865 875
def test_base_adresse_addresses_cache_err(app, base_adresse, mock_api_adresse_data_gouv_fr_search):
866 876
    resp = app.get('/base-adresse/%s/addresses?id=%s' % (base_adresse.slug, 'wrong_id'))
867
-