From 96108b12d0f282ad00e0be8b182e144023366131 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Thu, 15 May 2025 22:58:04 +0200 Subject: [PATCH] Update tests a bit --- app/controllers/trips_controller.rb | 1 - .../20250123151849_create_paths_for_trips.rb | 2 +- spec/jobs/trips/create_path_job_spec.rb | 23 --------------- spec/models/trip_spec.rb | 29 ++++++++++++------- 4 files changed, 19 insertions(+), 36 deletions(-) delete mode 100644 spec/jobs/trips/create_path_job_spec.rb diff --git a/app/controllers/trips_controller.rb b/app/controllers/trips_controller.rb index 5ff6adfe..6a4aaf60 100644 --- a/app/controllers/trips_controller.rb +++ b/app/controllers/trips_controller.rb @@ -39,7 +39,6 @@ class TripsController < ApplicationController def update if @trip.update(trip_params) - # Only recalculate if date range changed (handled by model callback) redirect_to @trip, notice: 'Trip was successfully updated.', status: :see_other else render :edit, status: :unprocessable_entity diff --git a/db/data/20250123151849_create_paths_for_trips.rb b/db/data/20250123151849_create_paths_for_trips.rb index c78cffff..5ede2bb6 100644 --- a/db/data/20250123151849_create_paths_for_trips.rb +++ b/db/data/20250123151849_create_paths_for_trips.rb @@ -3,7 +3,7 @@ class CreatePathsForTrips < ActiveRecord::Migration[8.0] def up Trip.find_each do |trip| - Trips::CreatePathJob.perform_later(trip.id) + Trips::CalculatePathJob.perform_later(trip.id) end end diff --git a/spec/jobs/trips/create_path_job_spec.rb b/spec/jobs/trips/create_path_job_spec.rb deleted file mode 100644 index 17a02418..00000000 --- a/spec/jobs/trips/create_path_job_spec.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Trips::CreatePathJob, type: :job do - let!(:trip) { create(:trip, :with_points) } - let(:points) { trip.points } - let(:trip_path) do - "LINESTRING (#{points.map do |point| - "#{point.lon.to_f.round(5)} #{point.lat.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 7d17117d..5001a3a6 100644 --- a/spec/models/trip_spec.rb +++ b/spec/models/trip_spec.rb @@ -20,17 +20,18 @@ RSpec.describe Trip, type: :model do describe 'callbacks' do let(:user) { create(:user) } let(:trip) { create(:trip, :with_points, user:) } - let(:calculated_distance) { trip.send(:calculate_distance) } - it 'sets the distance' do - expect(trip.distance).to eq(calculated_distance) + context 'when the trip is created' do + let(:trip) { build(:trip, :with_points, user:) } + + it 'enqueues the calculation jobs' do + expect(Trips::CalculateAllJob).to receive(:perform_later) + + trip.save + end end - it 'sets the path' do - expect(trip.path).to be_present - end - - context 'when DawarichSettings.store_geodata is enabled' do + context 'when DawarichSettings.store_geodata? is enabled' do before do allow(DawarichSettings).to receive(:store_geodata?).and_return(true) end @@ -40,11 +41,17 @@ RSpec.describe Trip, type: :model do end end - context 'when DawarichSettings.store_geodata is disabled' do + context 'when DawarichSettings.store_geodata? is disabled' do + let(:countries_service) { instance_double(Trips::Countries, call: []) } + let(:trip) { build(:trip, :with_points, user:) } + + before do + allow(DawarichSettings).to receive(:store_geodata?).and_return(false) + end + it 'sets the visited countries' do - countries_service = instance_double(Trips::Countries, call: []) expect(Trips::Countries).to receive(:new).with(trip).and_return(countries_service) - expect(countries_service).to receive(:call) + expect(any_instance_of(Trips::Countries)).to receive(:call) trip.save end