diff --git a/app/services/api_geo_service.rb b/app/services/api_geo_service.rb index 2cdd72a18..a248ad924 100644 --- a/app/services/api_geo_service.rb +++ b/app/services/api_geo_service.rb @@ -101,7 +101,7 @@ class APIGeoService department_code:, region_name: region_name(region_code), region_code:, - city_name: commune_name(department_code, city_code), + city_name: commune_name(department_code, city_code) || properties['city'], # fallback to city name if commune not found city_code: } else diff --git a/spec/services/api_geo_service_spec.rb b/spec/services/api_geo_service_spec.rb index 9a4daf1df..3a06f86cc 100644 --- a/spec/services/api_geo_service_spec.rb +++ b/spec/services/api_geo_service_spec.rb @@ -87,4 +87,24 @@ describe APIGeoService do expect(APIGeoService.epcis('01').first).to eq(code: '200042935', name: 'CA Haut - Bugey Agglomération') end end + + describe 'parse_ban_address' do + let(:features) { JSON.parse(Rails.root.join('spec/fixtures/files/api_address/address.json').read)['features'] } + let(:feature) { features.first } + subject { APIGeoService.parse_ban_address(feature) } + + context 'with a valid code insee' do + it { expect(subject[:city_name]).to eq('Paris') } + end + + context 'with an invalid code insee' do + let(:feature) do + features.first.tap { + _1['properties']['citycode'] = '0000' + } + end + + it { expect(subject[:city_name]).to eq('Paris') } + end + end end