dawarich/spec/services/reverse_geocoding/points/fetch_data_spec.rb

121 lines
3.3 KiB
Ruby
Raw Normal View History

2024-07-12 15:59:03 -04:00
# frozen_string_literal: true
require 'rails_helper'
2024-08-05 15:23:08 -04:00
RSpec.describe ReverseGeocoding::Points::FetchData do
2024-07-12 15:59:03 -04:00
subject(:fetch_data) { described_class.new(point.id).call }
2025-07-12 11:21:53 -04:00
let(:point) { create(:point) }
2024-07-12 15:59:03 -04:00
context 'when Geocoder returns city and country' do
2025-06-30 16:51:25 -04:00
let!(:germany) { create(:country, name: 'Germany', iso_a2: 'DE', iso_a3: 'DEU') }
2024-07-12 15:59:03 -04:00
before do
2025-05-15 12:23:24 -04:00
allow(Geocoder).to receive(:search).and_return(
[
2025-06-30 16:08:34 -04:00
double(
city: 'Berlin',
country: 'Germany',
data: {
'address' => 'Address',
'properties' => { 'countrycode' => 'DE' }
}
)
2025-05-15 12:23:24 -04:00
]
)
2024-07-12 15:59:03 -04:00
end
context 'when point does not have city and country' do
it 'updates point with city and country' do
expect { fetch_data }.to change { point.reload.city }
2025-06-30 16:08:34 -04:00
.from(nil).to('Berlin')
2025-06-30 16:51:25 -04:00
.and change { point.reload.country_id }.from(nil).to(germany.id)
2024-07-12 15:59:03 -04:00
end
2025-06-30 16:51:25 -04:00
it 'finds existing country' do
2025-06-30 16:08:34 -04:00
fetch_data
country = point.reload.country
expect(country.name).to eq('Germany')
expect(country.iso_a2).to eq('DE')
expect(country.iso_a3).to eq('DEU')
end
2024-07-12 15:59:03 -04:00
it 'updates point with geodata' do
2025-06-30 16:08:34 -04:00
expect { fetch_data }.to change { point.reload.geodata }.from({}).to(
'address' => 'Address',
'properties' => { 'countrycode' => 'DE' }
)
2024-07-12 15:59:03 -04:00
end
it 'calls Geocoder' do
fetch_data
expect(Geocoder).to have_received(:search).with([point.lat, point.lon])
2024-07-12 15:59:03 -04:00
end
end
context 'when point has city and country' do
2025-06-26 13:24:40 -04:00
let(:country) { create(:country, name: 'Test Country') }
let(:point) { create(:point, :with_geodata, city: 'Test City', country_id: country.id, reverse_geocoded_at: Time.current) }
2024-07-12 15:59:03 -04:00
before do
allow(Geocoder).to receive(:search).and_return(
2025-06-30 16:08:34 -04:00
[double(
geodata: { 'address' => 'Address' },
city: 'Berlin',
country: 'Germany',
data: {
'address' => 'Address',
'properties' => { 'countrycode' => 'DE' }
}
)]
2024-07-12 15:59:03 -04:00
)
end
it 'does not update point' do
expect { fetch_data }.not_to(change { point.reload.city })
end
it 'does not call Geocoder' do
fetch_data
expect(Geocoder).not_to have_received(:search)
end
end
end
2025-06-30 16:51:25 -04:00
context 'when Geocoder returns country name that does not exist in database' do
2025-06-30 16:08:34 -04:00
before do
allow(Geocoder).to receive(:search).and_return(
[
double(
city: 'Paris',
2025-06-30 16:51:25 -04:00
country: 'NonExistentCountry',
2025-06-30 16:08:34 -04:00
data: {
'address' => 'Address',
2025-06-30 16:51:25 -04:00
'properties' => { 'city' => 'Paris' }
2025-06-30 16:08:34 -04:00
}
)
]
)
end
2025-06-30 16:51:25 -04:00
it 'does not set country_id when country is not found' do
expect { fetch_data }.to change { point.reload.city }
.from(nil).to('Paris')
expect(point.reload.country_id).to be_nil
2025-06-30 16:08:34 -04:00
end
end
2024-07-12 15:59:03 -04:00
context 'when Geocoder returns an error' do
before do
2025-06-26 13:24:40 -04:00
allow(Geocoder).to receive(:search).and_return([double(city: nil, country: nil, data: { 'error' => 'Error' })])
2024-07-12 15:59:03 -04:00
end
it 'does not update point' do
expect { fetch_data }.not_to(change { point.reload.city })
end
end
end