Fix the rest of the tests

This commit is contained in:
Eugene Burmakin 2025-02-22 22:37:21 +01:00
parent fc01cda5c6
commit 7c766a4d92
7 changed files with 27 additions and 28 deletions

View file

@ -6,21 +6,23 @@ class Overland::BatchCreatingJob < ApplicationJob
def perform(params, user_id) def perform(params, user_id)
data = Overland::Params.new(params).call data = Overland::Params.new(params).call
data.each do |location| records = data.map do |location|
next if point_exists?(location, user_id) {
lonlat: location[:lonlat],
Point.create!(location.merge(user_id:)) timestamp: location[:timestamp],
user_id: user_id,
created_at: Time.current,
updated_at: Time.current
}
end end
end
private # rubocop:disable Rails/SkipsModelValidations
Point.upsert_all(
def point_exists?(params, user_id) records,
Point.exists?( unique_by: %i[lonlat timestamp user_id],
latitude: params[:latitude], returning: false,
longitude: params[:longitude], on_duplicate: :skip
timestamp: params[:timestamp],
user_id:
) )
# rubocop:enable Rails/SkipsModelValidations
end end
end end

View file

@ -9,7 +9,7 @@ class Points::CreateJob < ApplicationJob
data.each_slice(1000) do |location_batch| data.each_slice(1000) do |location_batch|
Point.upsert_all( Point.upsert_all(
location_batch, location_batch,
unique_by: %i[latitude longitude timestamp user_id], unique_by: %i[lonlat timestamp user_id],
returning: false returning: false
) )
end end

View file

@ -46,7 +46,7 @@ class Trip < ApplicationRecord
end end
def calculate_path def calculate_path
trip_path = Tracks::BuildPath.new(points.pluck(:latitude, :longitude)).call trip_path = Tracks::BuildPath.new(points.pluck(:lonlat)).call
self.path = trip_path self.path = trip_path
end end

View file

@ -13,8 +13,7 @@ class Overland::Params
next if point[:geometry].nil? || point.dig(:properties, :timestamp).nil? next if point[:geometry].nil? || point.dig(:properties, :timestamp).nil?
{ {
latitude: point[:geometry][:coordinates][1], lonlat: "POINT(#{point[:geometry][:coordinates][0]} #{point[:geometry][:coordinates][1]})",
longitude: point[:geometry][:coordinates][0],
battery_status: point[:properties][:battery_state], battery_status: point[:properties][:battery_state],
battery: battery_level(point[:properties][:battery_level]), battery: battery_level(point[:properties][:battery_level]),
timestamp: DateTime.parse(point[:properties][:timestamp]), timestamp: DateTime.parse(point[:properties][:timestamp]),

View file

@ -7,7 +7,7 @@ class Tracks::BuildPath
def call def call
factory.line_string( factory.line_string(
coordinates.map { |point| factory.point(point[1].to_f.round(5), point[0].to_f.round(5)) } coordinates.map { |point| factory.point(point.lon.to_f.round(5), point.lat.to_f.round(5)) }
) )
end end

View file

@ -11,8 +11,7 @@ RSpec.describe Overland::Params do
let(:expected_json) do let(:expected_json) do
{ {
latitude: 37.3318, lonlat: 'POINT(-122.030581 37.3318)',
longitude: -122.030581,
battery_status: 'charging', battery_status: 'charging',
battery: 89, battery: 89,
altitude: 0, altitude: 0,
@ -31,8 +30,6 @@ RSpec.describe Overland::Params do
it 'returns a hash with the correct keys' do it 'returns a hash with the correct keys' do
expect(params[0].keys).to match_array( expect(params[0].keys).to match_array(
%i[ %i[
latitude
longitude
battery_status battery_status
battery battery
altitude altitude
@ -43,6 +40,7 @@ RSpec.describe Overland::Params do
tracker_id tracker_id
timestamp timestamp
raw_data raw_data
lonlat
] ]
) )
end end

View file

@ -6,9 +6,9 @@ RSpec.describe Tracks::BuildPath do
describe '#call' do describe '#call' do
let(:coordinates) do let(:coordinates) do
[ [
[45.123456, -122.654321], # [lat, lng] RGeo::Geographic.spherical_factory.point(-122.654321, 45.123456),
[45.234567, -122.765432], RGeo::Geographic.spherical_factory.point(-122.765432, 45.234567),
[45.345678, -122.876543] RGeo::Geographic.spherical_factory.point(-122.876543, 45.345678)
] ]
end end
@ -26,9 +26,9 @@ RSpec.describe Tracks::BuildPath do
it 'correctly converts coordinates to points with rounded values' do it 'correctly converts coordinates to points with rounded values' do
points = result.points points = result.points
coordinates.each_with_index do |(lat, lng), index| coordinates.each_with_index do |coordinate_pair, index|
expect(points[index].x).to eq(lng.to_f.round(5)) expect(points[index].x).to eq(coordinate_pair.lon.to_f.round(5))
expect(points[index].y).to eq(lat.to_f.round(5)) expect(points[index].y).to eq(coordinate_pair.lat.to_f.round(5))
end end
end end
end end