Fix failed specs

This commit is contained in:
Eugene Burmakin 2025-01-21 19:32:12 +01:00
parent b43810b1fb
commit 510868a594
4 changed files with 58 additions and 19 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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