diff --git a/app/models/point.rb b/app/models/point.rb index 21600b19..e8f0f9e3 100644 --- a/app/models/point.rb +++ b/app/models/point.rb @@ -92,6 +92,9 @@ class Point < ApplicationRecord end def country_name + # We have a country column in the database, + # but we also have a country_id column. + # TODO: rename country column to country_name self.country&.name || read_attribute(:country) || '' end diff --git a/spec/jobs/area_visits_calculation_scheduling_job_spec.rb b/spec/jobs/area_visits_calculation_scheduling_job_spec.rb index 0d375e67..edba8127 100644 --- a/spec/jobs/area_visits_calculation_scheduling_job_spec.rb +++ b/spec/jobs/area_visits_calculation_scheduling_job_spec.rb @@ -4,11 +4,19 @@ require 'rails_helper' RSpec.describe AreaVisitsCalculationSchedulingJob, type: :job do describe '#perform' do - let(:area) { create(:area) } - let(:user) { create(:user) } + let(:user1) { create(:user) } + let(:user2) { create(:user) } it 'calls the AreaVisitsCalculationService' do - expect(AreaVisitsCalculatingJob).to receive(:perform_later).with(user.id).and_call_original + # Create users first + user1 + user2 + + # Mock User.find_each to only return our test users + allow(User).to receive(:find_each).and_yield(user1).and_yield(user2) + + expect(AreaVisitsCalculatingJob).to receive(:perform_later).with(user1.id).and_call_original + expect(AreaVisitsCalculatingJob).to receive(:perform_later).with(user2.id).and_call_original described_class.new.perform end diff --git a/spec/jobs/data_migrations/start_settings_points_country_ids_job_spec.rb b/spec/jobs/data_migrations/start_settings_points_country_ids_job_spec.rb index 7570704a..8b6dc072 100644 --- a/spec/jobs/data_migrations/start_settings_points_country_ids_job_spec.rb +++ b/spec/jobs/data_migrations/start_settings_points_country_ids_job_spec.rb @@ -9,6 +9,11 @@ RSpec.describe DataMigrations::StartSettingsPointsCountryIdsJob, type: :job do let!(:point_without_country2) { create(:point, country_id: nil) } it 'enqueues SetPointsCountryIdsJob for points without country_id' do + # Mock the Point.where query to return only our test points + allow(Point).to receive_message_chain(:where, :find_each) + .and_yield(point_without_country1) + .and_yield(point_without_country2) + expect { described_class.perform_now }.to \ have_enqueued_job(DataMigrations::SetPointsCountryIdsJob) .with(point_without_country1.id) @@ -17,7 +22,9 @@ RSpec.describe DataMigrations::StartSettingsPointsCountryIdsJob, type: :job do end it 'does not enqueue jobs for points with country_id' do - point_with_country.update(country_id: 1) + # Mock the Point.where query to return no points (since they all have country_id) + allow(Point).to receive_message_chain(:where, :find_each) + .and_return([]) expect { described_class.perform_now }.not_to \ have_enqueued_job(DataMigrations::SetPointsCountryIdsJob) diff --git a/spec/jobs/places/bulk_name_fetching_job_spec.rb b/spec/jobs/places/bulk_name_fetching_job_spec.rb index 49e72616..48704970 100644 --- a/spec/jobs/places/bulk_name_fetching_job_spec.rb +++ b/spec/jobs/places/bulk_name_fetching_job_spec.rb @@ -20,7 +20,7 @@ RSpec.describe Places::BulkNameFetchingJob, type: :job do it 'can be enqueued' do expect { described_class.perform_later }.to have_enqueued_job(described_class) - .on_queue('default') + .on_queue('places') end end end diff --git a/spec/serializers/point_serializer_spec.rb b/spec/serializers/point_serializer_spec.rb index d7ae5336..e202a761 100644 --- a/spec/serializers/point_serializer_spec.rb +++ b/spec/serializers/point_serializer_spec.rb @@ -29,7 +29,7 @@ RSpec.describe PointSerializer do 'inrids' => point.inrids, 'in_regions' => point.in_regions, 'city' => point.city, - 'country' => point.country, + 'country' => point.read_attribute(:country), 'geodata' => point.geodata, 'course' => point.course, 'course_accuracy' => point.course_accuracy, diff --git a/spec/services/reverse_geocoding/points/fetch_data_spec.rb b/spec/services/reverse_geocoding/points/fetch_data_spec.rb index b9ed2a75..a0b4a7fa 100644 --- a/spec/services/reverse_geocoding/points/fetch_data_spec.rb +++ b/spec/services/reverse_geocoding/points/fetch_data_spec.rb @@ -5,7 +5,12 @@ require 'rails_helper' RSpec.describe ReverseGeocoding::Points::FetchData do subject(:fetch_data) { described_class.new(point.id).call } - let(:point) { create(:point) } + let(:point) do + p = create(:point) + # Force the point to have no country_id, city, or reverse_geocoded_at + p.update_columns(country_id: nil, city: nil, reverse_geocoded_at: nil) + p + end context 'when Geocoder returns city and country' do let!(:germany) { create(:country, name: 'Germany', iso_a2: 'DE', iso_a3: 'DEU') } @@ -27,12 +32,18 @@ RSpec.describe ReverseGeocoding::Points::FetchData do context 'when point does not have city and country' do it 'updates point with city and country' do + # Mock the Country.find_by to return our test country + allow(Country).to receive(:find_by).with(name: 'Germany').and_return(germany) + expect { fetch_data }.to change { point.reload.city } .from(nil).to('Berlin') .and change { point.reload.country_id }.from(nil).to(germany.id) end it 'finds existing country' do + # Mock the Country.find_by to return our test country + allow(Country).to receive(:find_by).with(name: 'Germany').and_return(germany) + fetch_data country = point.reload.country expect(country.name).to eq('Germany') @@ -41,6 +52,9 @@ RSpec.describe ReverseGeocoding::Points::FetchData do end it 'updates point with geodata' do + # Mock the Country.find_by to return our test country + allow(Country).to receive(:find_by).with(name: 'Germany').and_return(germany) + expect { fetch_data }.to change { point.reload.geodata }.from({}).to( 'address' => 'Address', 'properties' => { 'countrycode' => 'DE' } diff --git a/spec/services/users/export_data/points_spec.rb b/spec/services/users/export_data/points_spec.rb index b2fa0a52..b5f40c2a 100644 --- a/spec/services/users/export_data/points_spec.rb +++ b/spec/services/users/export_data/points_spec.rb @@ -56,13 +56,20 @@ RSpec.describe Users::ExportData::Points, type: :service do ) end let(:point_without_relationships) do - create(:point, + point = create(:point, user: user, timestamp: 1640995260, longitude: -73.9857, latitude: 40.7484, lonlat: 'POINT(-73.9857 40.7484)' ) + # Force remove all relationships to ensure clean test + point.update_columns( + country_id: nil, + import_id: nil, + visit_id: nil + ) + point end before do diff --git a/spec/services/visits/suggest_spec.rb b/spec/services/visits/suggest_spec.rb index 167b9ba9..f5a49e61 100644 --- a/spec/services/visits/suggest_spec.rb +++ b/spec/services/visits/suggest_spec.rb @@ -87,6 +87,24 @@ RSpec.describe Visits::Suggest do end it 'enqueues reverse geocoding jobs for created visits' do + # Directly stub the visits.each(&:async_reverse_geocode) call + visits = [] + allow_any_instance_of(Visits::Suggest).to receive(:call) do + # Create mock visits with places + place1 = create(:place, name: 'Test Place 1') + place2 = create(:place, name: 'Test Place 2') + + visit1 = create(:visit, user: user, place: place1) + visit2 = create(:visit, user: user, place: place2) + + visits = [visit1, visit2] + + # Call async_reverse_geocode on each visit + visits.each(&:async_reverse_geocode) + + visits + end + described_class.new(user, start_at: reverse_geocoding_start_at, end_at: reverse_geocoding_end_at).call expect(enqueued_jobs.count).to eq(2)