From 03de4cc7d2db9e2ef6b28a3dd7d0025d566a6d51 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 13 Jul 2021 16:36:25 +0200 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(-) diff --git a/passerelle/apps/base_adresse/models.py b/passerelle/apps/base_adresse/models.py index 43153dbc..7d58bb3c 100644 --- a/passerelle/apps/base_adresse/models.py +++ b/passerelle/apps/base_adresse/models.py @@ -187,9 +187,12 @@ class BaseAdresse(BaseResource): # in the hypothetical case that the ban_id has changed, instead of # returning an error we bet that the first result is the good one, # at least the nearest from the one we search. - return results - else: # retrocompatibility with raw BAN id - return {'err': _('Address ID not found')} + self.logger.warning( + 'get_by_id: id %s was not exactly found, returning all best matches %s', id, results + ) + if results: + return results + return {'err': _('Address ID not found')} @endpoint( pattern='(?P.+)?$', diff --git a/tests/test_base_adresse.py b/tests/test_base_adresse.py index d06e0548..d500343e 100644 --- a/tests/test_base_adresse.py +++ b/tests/test_base_adresse.py @@ -833,7 +833,7 @@ def test_base_adresse_addresses_qs_coordinates(mocked_get, app, base_adresse_coo assert 'lon=43' in mocked_get.call_args[0][0] -def test_base_adresse_addresses_cache(app, base_adresse, mock_api_adresse_data_gouv_fr_search): +def test_base_adresse_addresses_cache(app, base_adresse, mock_api_adresse_data_gouv_fr_search, caplog): resp = app.get('/base-adresse/%s/addresses?q=plop' % base_adresse.slug) assert mock_api_adresse_data_gouv_fr_search.call['count'] == 1 @@ -861,6 +861,16 @@ def test_base_adresse_addresses_cache(app, base_adresse, mock_api_adresse_data_g assert data['text'] == 'Rue Roger Halope 49000 Angers' assert 'address' in data + # no cache and id has changed + AddressCacheModel.objects.all().delete() + api_id = '49007_XXXX_be54bd~47.474633~-0.593775~Rue%20Roger%20Halope%2049000%20Angers' + resp = app.get('/base-adresse/%s/addresses?id=%s' % (base_adresse.slug, api_id)) + assert AddressCacheModel.objects.count() == 1 + assert mock_api_adresse_data_gouv_fr_search.call['count'] == 4 + assert data['text'] == 'Rue Roger Halope 49000 Angers' + assert 'address' in data + assert 'all best matches' in caplog.text + def test_base_adresse_addresses_cache_err(app, base_adresse, mock_api_adresse_data_gouv_fr_search): resp = app.get('/base-adresse/%s/addresses?id=%s' % (base_adresse.slug, 'wrong_id')) -- 2.32.0.rc0