From 01275d0d2e39490f452bd02876ea19589b474065 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Fri, 24 Jan 2025 15:58:44 +0100 Subject: [PATCH] Add some tests --- app/models/trip.rb | 4 +-- .../20250123151849_create_paths_for_trips.rb | 3 ++ spec/factories/trips.rb | 2 ++ spec/jobs/trips/create_path_job_spec.rb | 20 ++++++++++- spec/models/trip_spec.rb | 4 +++ spec/services/tracks/build_path_spec.rb | 35 +++++++++++++++++++ 6 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 spec/services/tracks/build_path_spec.rb diff --git a/app/models/trip.rb b/app/models/trip.rb index 4043e8c7..cd4f7225 100644 --- a/app/models/trip.rb +++ b/app/models/trip.rb @@ -11,9 +11,9 @@ class Trip < ApplicationRecord def create_path! trip_path = Tracks::BuildPath.new(points.pluck(:latitude, :longitude)).call - distance = calculate_distance - update_columns(path: trip_path, distance: distance) # Avoids recursion with `after_save` + self.distance = calculate_distance + self.path = trip_path end def points diff --git a/db/data/20250123151849_create_paths_for_trips.rb b/db/data/20250123151849_create_paths_for_trips.rb index 6abcfff4..c78cffff 100644 --- a/db/data/20250123151849_create_paths_for_trips.rb +++ b/db/data/20250123151849_create_paths_for_trips.rb @@ -2,6 +2,9 @@ class CreatePathsForTrips < ActiveRecord::Migration[8.0] def up + Trip.find_each do |trip| + Trips::CreatePathJob.perform_later(trip.id) + end end def down diff --git a/spec/factories/trips.rb b/spec/factories/trips.rb index 4ef4041a..5986e882 100644 --- a/spec/factories/trips.rb +++ b/spec/factories/trips.rb @@ -7,6 +7,8 @@ FactoryBot.define do started_at { DateTime.new(2024, 11, 27, 17, 16, 21) } ended_at { DateTime.new(2024, 11, 29, 17, 16, 21) } notes { FFaker::Lorem.sentence } + distance { 100 } + path { 'LINESTRING(1 1, 2 2, 3 3)' } trait :with_points do after(:build) do |trip| diff --git a/spec/jobs/trips/create_path_job_spec.rb b/spec/jobs/trips/create_path_job_spec.rb index 1dd711ef..60d288e3 100644 --- a/spec/jobs/trips/create_path_job_spec.rb +++ b/spec/jobs/trips/create_path_job_spec.rb @@ -1,5 +1,23 @@ +# frozen_string_literal: true + require 'rails_helper' RSpec.describe Trips::CreatePathJob, type: :job do - pending "add some examples to (or delete) #{__FILE__}" + let!(:trip) { create(:trip, :with_points) } + let(:points) { trip.points } + let(:trip_path) do + "LINESTRING (#{points.map do |point| + "#{point.longitude.to_f.round(5)} #{point.latitude.to_f.round(5)}" + end.join(', ')})" + end + + before do + trip.update(path: nil, distance: nil) + end + + it 'creates a path for a trip' do + described_class.perform_now(trip.id) + + expect(trip.reload.path.to_s).to eq(trip_path) + end end diff --git a/spec/models/trip_spec.rb b/spec/models/trip_spec.rb index 032185bd..f56daf20 100644 --- a/spec/models/trip_spec.rb +++ b/spec/models/trip_spec.rb @@ -21,6 +21,10 @@ RSpec.describe Trip, type: :model do it 'sets the distance' do expect(trip.distance).to eq(calculated_distance) end + + it 'sets the path' do + expect(trip.path).to be_present + end end describe '#countries' do diff --git a/spec/services/tracks/build_path_spec.rb b/spec/services/tracks/build_path_spec.rb new file mode 100644 index 00000000..1d2db10a --- /dev/null +++ b/spec/services/tracks/build_path_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Tracks::BuildPath do + describe '#call' do + let(:coordinates) do + [ + [45.123456, -122.654321], # [lat, lng] + [45.234567, -122.765432], + [45.345678, -122.876543] + ] + end + + let(:service) { described_class.new(coordinates) } + let(:result) { service.call } + + it 'returns an RGeo::Geographic::SphericalLineString' do + expect(result).to be_a(RGeo::Geographic::SphericalLineStringImpl) + end + + it 'creates a line string with the correct number of points' do + expect(result.num_points).to eq(coordinates.length) + end + + it 'correctly converts coordinates to points with rounded values' do + points = result.points + + coordinates.each_with_index do |(lat, lng), index| + expect(points[index].x).to eq(lng.to_f.round(5)) + expect(points[index].y).to eq(lat.to_f.round(5)) + end + end + end +end