Make sure speed is recorded when importing GPX files

This commit is contained in:
Eugene Burmakin 2024-11-08 14:42:18 +01:00
parent 3e5ddb7183
commit 5c127f913a
2 changed files with 35 additions and 20 deletions

View file

@ -39,6 +39,7 @@ class Gpx::TrackParser
altitude: point['ele'].to_i,
timestamp: Time.parse(point['time']).to_i,
import_id: import.id,
velocity: speed(point),
raw_data: point,
user_id:
)
@ -54,4 +55,10 @@ class Gpx::TrackParser
user_id:
)
end
def speed(point)
return if point['extensions'].blank?
point.dig('extensions', 'speed').to_f || point.dig('extensions', 'TrackPointExtension', 'speed').to_f
end
end

View file

@ -11,31 +11,29 @@ RSpec.describe Gpx::TrackParser do
let(:raw_data) { Hash.from_xml(File.read(file_path)) }
let(:import) { create(:import, user:, name: 'gpx_track.gpx', raw_data:) }
context 'when file exists' do
context 'when file has a single segment' do
it 'creates points' do
expect { parser }.to change { Point.count }.by(301)
end
it 'broadcasts importing progress' do
expect_any_instance_of(Imports::Broadcaster).to receive(:broadcast_import_progress).exactly(301).times
parser
end
context 'when file has a single segment' do
it 'creates points' do
expect { parser }.to change { Point.count }.by(301)
end
context 'when file has multiple segments' do
let(:file_path) { Rails.root.join('spec/fixtures/files/gpx/gpx_track_multiple_segments.gpx') }
it 'broadcasts importing progress' do
expect_any_instance_of(Imports::Broadcaster).to receive(:broadcast_import_progress).exactly(301).times
it 'creates points' do
expect { parser }.to change { Point.count }.by(558)
end
parser
end
end
it 'broadcasts importing progress' do
expect_any_instance_of(Imports::Broadcaster).to receive(:broadcast_import_progress).exactly(558).times
context 'when file has multiple segments' do
let(:file_path) { Rails.root.join('spec/fixtures/files/gpx/gpx_track_multiple_segments.gpx') }
parser
end
it 'creates points' do
expect { parser }.to change { Point.count }.by(558)
end
it 'broadcasts importing progress' do
expect_any_instance_of(Imports::Broadcaster).to receive(:broadcast_import_progress).exactly(558).times
parser
end
end
@ -51,6 +49,16 @@ RSpec.describe Gpx::TrackParser do
parser
end
it 'creates points with correct data' do
parser
expect(Point.first.latitude).to eq(37.17221.to_d)
expect(Point.first.longitude).to eq(-3.55468.to_d)
expect(Point.first.altitude).to eq(1066)
expect(Point.first.timestamp).to eq(Time.zone.parse('2024-04-21T10:19:55Z').to_i)
expect(Point.first.velocity).to eq('2.9')
end
end
end
end