Merge pull request #10294 from demarches-simplifiees/fix_city_address_again

Tech: correction d un bug concernant les city_name
This commit is contained in:
LeSim 2024-04-10 09:15:19 +00:00 committed by GitHub
commit 137601448b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 44 additions and 5 deletions

View file

@ -27,11 +27,11 @@ module Types
field :geometry, Types::GeoJSON, "coordonnées géographique", null: true
def city_name
if object['department_code'].present? && object['city_code'].present?
APIGeoService.commune_name(object.fetch('department_code'), object.fetch('city_code'))
else
APIGeoService.safely_normalize_city_name(
object['department_code'],
object['city_code'],
object['city_name']
end
)
end
def department_name

View file

@ -101,7 +101,7 @@ class APIGeoService
department_code:,
region_name: region_name(region_code),
region_code:,
city_name: commune_name(department_code, city_code) || properties['city'], # fallback to city name if commune not found
city_name: safely_normalize_city_name(department_code, city_code, properties['city']),
city_code:
}
else
@ -122,6 +122,15 @@ class APIGeoService
}.merge(territory)
end
def safely_normalize_city_name(department_code, city_code, fallback)
return fallback if department_code.nil? || city_code.nil?
commune_name(department_code, city_code) || fallback
rescue StandardError
fallback
end
private
def communes_by_postal_code_map

View file

@ -107,4 +107,34 @@ describe APIGeoService do
it { expect(subject[:city_name]).to eq('Paris') }
end
end
describe 'safely_normalize_city_name' do
let(:department_code) { '75' }
let(:city_code) { '75056' }
let(:fallback) { 'Paris' }
subject { APIGeoService.safely_normalize_city_name(department_code, city_code, fallback) }
context 'nominal' do
it { is_expected.to eq('Paris') }
end
context 'without department' do
let(:department_code) { nil }
it { is_expected.to eq('Paris') }
end
context 'without city_code' do
let(:city_code) { nil }
it { is_expected.to eq('Paris') }
end
context 'with a wrong department' do
let(:department_code) { 'wrong' }
it { is_expected.to eq('Paris') }
end
end
end