2024-05-25 07:26:56 -04:00
# frozen_string_literal: true
2024-03-15 18:27:31 -04:00
require 'rails_helper'
RSpec . describe Point , type : :model do
describe 'associations' do
2024-03-15 19:01:00 -04:00
it { is_expected . to belong_to ( :import ) . optional }
2024-07-12 15:59:03 -04:00
it { is_expected . to belong_to ( :user ) }
2025-06-26 13:48:42 -04:00
it { is_expected . to belong_to ( :country ) . optional }
it { is_expected . to belong_to ( :visit ) . optional }
2025-07-03 14:18:18 -04:00
it { is_expected . to belong_to ( :track ) . optional }
2025-08-05 16:06:34 -04:00
it { is_expected . to belong_to ( :device ) . optional }
2024-03-15 18:27:31 -04:00
end
describe 'validations' do
2025-08-05 16:06:34 -04:00
subject { build ( :point , timestamp : Time . current , lonlat : 'POINT(1.0 2.0)' ) }
2024-03-15 18:27:31 -04:00
it { is_expected . to validate_presence_of ( :timestamp ) }
2025-02-21 17:45:36 -05:00
it { is_expected . to validate_presence_of ( :lonlat ) }
2025-08-05 16:06:34 -04:00
it { is_expected . to validate_uniqueness_of ( :lonlat ) . scoped_to ( % i [ timestamp user_id device_id ] ) . with_message ( 'already has a point at this location and time for this user' ) }
2024-03-15 18:27:31 -04:00
end
2024-07-12 15:59:03 -04:00
2025-05-16 12:51:48 -04:00
describe 'callbacks' do
describe '#set_country' do
let ( :point ) { build ( :point , lonlat : 'POINT(-79.85581250721961 15.854775993302411)' ) }
let ( :country ) { create ( :country ) }
it 'sets the country' do
expect ( Country ) . to receive ( :containing_point ) . with ( - 79 . 85581250721961 , 15 . 854775993302411 ) . and_return ( country )
point . save!
expect ( point . country_id ) . to eq ( country . id )
end
end
2025-07-04 14:09:06 -04:00
2025-07-22 14:28:46 -04:00
xdescribe '#recalculate_track' do
2025-07-04 14:09:06 -04:00
let ( :point ) { create ( :point , track : track ) }
let ( :track ) { create ( :track ) }
it 'recalculates the track' do
expect ( track ) . to receive ( :recalculate_path_and_distance! )
point . update ( lonlat : 'POINT(-79.85581250721961 15.854775993302411)' )
end
end
2025-05-16 12:51:48 -04:00
end
2024-07-12 15:59:03 -04:00
describe 'scopes' do
describe '.reverse_geocoded' do
2024-12-02 08:44:22 -05:00
let ( :point ) { create ( :point , :reverse_geocoded ) }
2024-07-12 15:59:03 -04:00
let ( :point_without_address ) { create ( :point , city : nil , country : nil ) }
it 'returns points with reverse geocoded address' do
expect ( described_class . reverse_geocoded ) . to eq ( [ point ] )
end
end
describe '.not_reverse_geocoded' do
let ( :point ) { create ( :point , country : 'Country' , city : 'City' ) }
let ( :point_without_address ) { create ( :point , city : nil , country : nil ) }
it 'returns points without reverse geocoded address' do
expect ( described_class . not_reverse_geocoded ) . to eq ( [ point_without_address ] )
end
end
end
2024-07-31 13:35:35 -04:00
describe 'methods' do
describe '#recorded_at' do
let ( :point ) { create ( :point , timestamp : 1_554_317_696 ) }
it 'returns recorded at time' do
expect ( point . recorded_at ) . to eq ( Time . zone . at ( 1_554_317_696 ) )
end
end
describe '#async_reverse_geocode' do
let ( :point ) { build ( :point ) }
2025-05-12 16:33:47 -04:00
before do
allow ( DawarichSettings ) . to receive ( :reverse_geocoding_enabled? ) . and_return ( true )
allow ( DawarichSettings ) . to receive ( :store_geodata? ) . and_return ( true )
end
2025-01-07 08:07:33 -05:00
2024-08-05 15:23:08 -04:00
it 'enqueues ReverseGeocodeJob with correct arguments' do
point . save
expect { point . async_reverse_geocode } . to have_enqueued_job ( ReverseGeocodingJob )
. with ( 'Point' , point . id )
end
2024-12-25 06:38:32 -05:00
context 'when point is imported' do
let ( :point ) { build ( :point , import_id : 1 ) }
2025-01-04 15:31:21 -05:00
it 'enqueues ReverseGeocodeJob' do
expect { point . async_reverse_geocode } . to have_enqueued_job ( ReverseGeocodingJob )
2024-12-25 06:38:32 -05:00
end
end
2025-05-12 16:33:47 -04:00
context 'when reverse geocoding is disabled' do
before do
allow ( DawarichSettings ) . to receive ( :reverse_geocoding_enabled? ) . and_return ( false )
end
it 'does not enqueue ReverseGeocodeJob' do
expect { point . save } . not_to have_enqueued_job ( ReverseGeocodingJob )
end
end
2024-07-31 13:35:35 -04:00
end
2025-02-22 17:14:23 -05:00
describe '#lon' do
let ( :point ) { create ( :point , lonlat : 'POINT(1 2)' ) }
it 'returns longitude' do
expect ( point . lon ) . to eq ( 1 )
end
end
describe '#lat' do
let ( :point ) { create ( :point , lonlat : 'POINT(1 2)' ) }
it 'returns latitude' do
expect ( point . lat ) . to eq ( 2 )
end
end
2025-07-07 16:23:37 -04:00
2025-07-22 14:28:46 -04:00
xdescribe '#trigger_incremental_track_generation' do
2025-07-08 12:10:10 -04:00
let ( :point ) do
create ( :point , track : track , import_id : nil , timestamp : 1 . hour . ago . to_i , reverse_geocoded_at : 1 . hour . ago )
end
2025-07-07 16:23:37 -04:00
let ( :track ) { create ( :track ) }
2025-07-16 16:22:33 -04:00
it 'enqueues Tracks::IncrementalCheckJob' do
expect { point . send ( :trigger_incremental_track_generation ) } . to have_enqueued_job ( Tracks :: IncrementalCheckJob ) . with ( point . user_id , point . id )
2025-07-07 16:23:37 -04:00
end
end
2024-07-31 13:35:35 -04:00
end
2024-03-15 18:27:31 -04:00
end