2025-03-09 11:29:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2025-03-03 14:50:49 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
|
|
RSpec.describe DataMigrations::MigratePlacesLonlatJob, type: :job do
|
2025-03-09 11:29:16 -04:00
|
|
|
describe '#perform' do
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
|
|
context 'when places exist for the user' do
|
2025-11-19 15:19:59 -05:00
|
|
|
let!(:place1) { create(:place, user: user, longitude: 10.0, latitude: 20.0) }
|
|
|
|
|
let!(:place2) { create(:place, user: user, longitude: -73.935242, latitude: 40.730610) }
|
|
|
|
|
let!(:other_place) { create(:place, longitude: 15.0, latitude: 25.0) }
|
2025-03-09 11:29:16 -04:00
|
|
|
|
|
|
|
|
# Create visits to associate places with users
|
|
|
|
|
let!(:visit1) { create(:visit, user: user, place: place1) }
|
|
|
|
|
let!(:visit2) { create(:visit, user: user, place: place2) }
|
|
|
|
|
let!(:other_visit) { create(:visit, place: other_place) } # associated with a different user
|
|
|
|
|
|
2025-11-19 15:19:59 -05:00
|
|
|
# Simulate old data by clearing lonlat after creation (to test migration)
|
|
|
|
|
before do
|
|
|
|
|
place1.update_column(:lonlat, nil)
|
|
|
|
|
place2.update_column(:lonlat, nil)
|
|
|
|
|
other_place.update_column(:lonlat, nil)
|
|
|
|
|
end
|
|
|
|
|
|
2025-03-09 11:29:16 -04:00
|
|
|
it 'updates lonlat field for all places belonging to the user' do
|
|
|
|
|
# Force a reload to ensure we have the initial state
|
|
|
|
|
place1.reload
|
|
|
|
|
place2.reload
|
|
|
|
|
|
|
|
|
|
# Both places should have nil lonlat initially
|
|
|
|
|
expect(place1.lonlat).to be_nil
|
|
|
|
|
expect(place2.lonlat).to be_nil
|
|
|
|
|
|
|
|
|
|
# Run the job
|
|
|
|
|
described_class.perform_now(user.id)
|
|
|
|
|
|
|
|
|
|
# Reload to get updated state
|
|
|
|
|
place1.reload
|
|
|
|
|
place2.reload
|
|
|
|
|
other_place.reload
|
|
|
|
|
|
|
|
|
|
# Check that lonlat is now set correctly
|
|
|
|
|
expect(place1.lonlat).not_to be_nil
|
|
|
|
|
expect(place2.lonlat).not_to be_nil
|
|
|
|
|
|
|
|
|
|
# The other user's place should still have nil lonlat
|
|
|
|
|
expect(other_place.lonlat).to be_nil
|
|
|
|
|
|
|
|
|
|
# Verify the coordinates
|
|
|
|
|
expect(place1.lonlat.x).to eq(10.0) # longitude
|
|
|
|
|
expect(place1.lonlat.y).to eq(20.0) # latitude
|
|
|
|
|
|
|
|
|
|
expect(place2.lonlat.x).to eq(-73.935242) # longitude
|
|
|
|
|
expect(place2.lonlat.y).to eq(40.730610) # latitude
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'sets the correct SRID (4326) on the geometry' do
|
|
|
|
|
described_class.perform_now(user.id)
|
|
|
|
|
place1.reload
|
|
|
|
|
|
|
|
|
|
expect(place1.lonlat.srid).to eq(4326)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when no places exist for the user' do
|
|
|
|
|
it 'completes successfully without errors' do
|
|
|
|
|
expect do
|
|
|
|
|
described_class.perform_now(user.id)
|
|
|
|
|
end.not_to raise_error
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'queue' do
|
2025-07-26 19:08:29 -04:00
|
|
|
it 'uses the data_migrations queue' do
|
|
|
|
|
expect(described_class.queue_name).to eq('data_migrations')
|
2025-03-09 11:29:16 -04:00
|
|
|
end
|
|
|
|
|
end
|
2025-03-03 14:50:49 -05:00
|
|
|
end
|