From 510868a5940047e330b1f1e4f1c5ae633f2d0f58 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Tue, 21 Jan 2025 19:32:12 +0100 Subject: [PATCH] Fix failed specs --- app/services/google_maps/records_parser.rb | 6 +- .../google_maps/records_parser_spec.rb | 65 ++++++++++++++----- .../tasks/imports/google_records_spec.rb | 4 +- spec/tasks/import_spec.rb | 2 +- 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/app/services/google_maps/records_parser.rb b/app/services/google_maps/records_parser.rb index 0762e0d6..32f02723 100644 --- a/app/services/google_maps/records_parser.rb +++ b/app/services/google_maps/records_parser.rb @@ -35,7 +35,7 @@ class GoogleMaps::RecordsParser { latitude: location['latitudeE7'].to_f / 10**7, longitude: location['longitudeE7'].to_f / 10**7, - timestamp: Timestamps.parse_timestamp(location['timestamp'] || location['timestampMs']), + timestamp: parse_timestamp(location), altitude: location['altitude'], velocity: location['velocity'], raw_data: location, @@ -85,4 +85,8 @@ class GoogleMaps::RecordsParser ] end end + + def parse_timestamp(location) + Timestamps.parse_timestamp(location['timestamp'] || location['timestampMs']) + end end diff --git a/spec/services/google_maps/records_parser_spec.rb b/spec/services/google_maps/records_parser_spec.rb index 96495dad..da99f501 100644 --- a/spec/services/google_maps/records_parser_spec.rb +++ b/spec/services/google_maps/records_parser_spec.rb @@ -4,21 +4,36 @@ require 'rails_helper' RSpec.describe GoogleMaps::RecordsParser do describe '#call' do - subject(:parser) { described_class.new(import).call(json) } + subject(:parser) { described_class.new(import).call(locations) } let(:import) { create(:import) } let(:time) { DateTime.new(2025, 1, 1, 12, 0, 0) } - let(:json) do - { - 'latitudeE7' => 123_456_789, - 'longitudeE7' => 123_456_789, - 'altitude' => 0, - 'velocity' => 0 - } + let(:locations) do + [ + { + 'timestampMs' => (time.to_f * 1000).to_i.to_s, + 'latitudeE7' => 123_456_789, + 'longitudeE7' => 123_456_789, + 'accuracy' => 10, + 'altitude' => 100, + 'verticalAccuracy' => 5, + 'activity' => [ + { + 'timestampMs' => (time.to_f * 1000).to_i.to_s, + 'activity' => [ + { + 'type' => 'STILL', + 'confidence' => 100 + } + ] + } + ] + } + ] end context 'with regular timestamp' do - let(:json) { super().merge('timestamp' => time.to_s) } + let(:locations) { super()[0].merge('timestamp' => time.to_s).to_json } it 'creates a point' do expect { parser }.to change(Point, :count).by(1) @@ -26,11 +41,23 @@ RSpec.describe GoogleMaps::RecordsParser do end context 'when point already exists' do - let(:json) { super().merge('timestamp' => time.to_s) } + let(:locations) do + [ + super()[0].merge( + 'timestamp' => time.to_s, + 'latitudeE7' => 123_456_789, + 'longitudeE7' => 123_456_789 + ) + ] + end before do create( - :point, user: import.user, import:, latitude: 12.3456789, longitude: 12.3456789, + :point, + user: import.user, + import: import, + latitude: 12.3456789, + longitude: 12.3456789, timestamp: time.to_i ) end @@ -41,7 +68,9 @@ RSpec.describe GoogleMaps::RecordsParser do end context 'with timestampMs in milliseconds' do - let(:json) { super().merge('timestampMs' => (time.to_f * 1000).to_i.to_s) } + let(:locations) do + [super()[0].merge('timestampMs' => (time.to_f * 1000).to_i.to_s)] + end it 'creates a point using milliseconds timestamp' do expect { parser }.to change(Point, :count).by(1) @@ -49,7 +78,9 @@ RSpec.describe GoogleMaps::RecordsParser do end context 'with ISO 8601 timestamp' do - let(:json) { super().merge('timestamp' => time.iso8601) } + let(:locations) do + [super()[0].merge('timestamp' => time.iso8601)] + end it 'parses ISO 8601 timestamp correctly' do expect { parser }.to change(Point, :count).by(1) @@ -59,7 +90,9 @@ RSpec.describe GoogleMaps::RecordsParser do end context 'with timestamp in milliseconds' do - let(:json) { super().merge('timestamp' => (time.to_f * 1000).to_i.to_s) } + let(:locations) do + [super()[0].merge('timestamp' => (time.to_f * 1000).to_i.to_s)] + end it 'parses millisecond timestamp correctly' do expect { parser }.to change(Point, :count).by(1) @@ -69,7 +102,9 @@ RSpec.describe GoogleMaps::RecordsParser do end context 'with timestamp in seconds' do - let(:json) { super().merge('timestamp' => time.to_i.to_s) } + let(:locations) do + [super()[0].merge('timestamp' => time.to_i.to_s)] + end it 'parses second timestamp correctly' do expect { parser }.to change(Point, :count).by(1) diff --git a/spec/services/tasks/imports/google_records_spec.rb b/spec/services/tasks/imports/google_records_spec.rb index 0310dbd1..29fddfdf 100644 --- a/spec/services/tasks/imports/google_records_spec.rb +++ b/spec/services/tasks/imports/google_records_spec.rb @@ -5,10 +5,10 @@ require 'rails_helper' RSpec.describe Tasks::Imports::GoogleRecords do describe '#call' do let(:user) { create(:user) } - let(:file_path) { Rails.root.join('spec/fixtures/files/google/records.json') } + let(:file_path) { Rails.root.join('spec/fixtures/files/google/records.json').to_s } it 'schedules the Import::GoogleTakeoutJob' do - expect(Import::GoogleTakeoutJob).to receive(:perform_later).exactly(3).times + expect(Import::GoogleTakeoutJob).to receive(:perform_later).exactly(1).time described_class.new(file_path, user.email).call end diff --git a/spec/tasks/import_spec.rb b/spec/tasks/import_spec.rb index 4cd785db..0e718f76 100644 --- a/spec/tasks/import_spec.rb +++ b/spec/tasks/import_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' describe 'import.rake' do - let(:file_path) { Rails.root.join('spec/fixtures/files/google/records.json') } + let(:file_path) { Rails.root.join('spec/fixtures/files/google/records.json').to_s } let(:user) { create(:user) } it 'calls importing class' do