Add some more tests to make sure points are properly cleaned up

This commit is contained in:
Eugene Burmakin 2025-07-17 20:57:55 +02:00
parent dc8460a948
commit 7cdb7d2f21
5 changed files with 47 additions and 8 deletions

View file

@ -163,15 +163,18 @@ class Tracks::Generator
scope = user.tracks
scope = scope.where(start_at: time_range) if time_range_defined?
deleted_count = scope.delete_all
Rails.logger.info "Deleted #{deleted_count} existing tracks for user #{user.id}"
deleted_count = scope.destroy_all
Rails.logger.info "Deleted #{deleted_count} existing tracks for user #{user.id}"
end
def clean_daily_tracks
day_range = daily_time_range
range = Time.zone.at(day_range.begin)..Time.zone.at(day_range.end)
deleted_count = user.tracks.where(start_at: range).delete_all
scope = user.tracks.where(start_at: range)
deleted_count = scope.destroy_all
Rails.logger.info "Deleted #{deleted_count} daily tracks for user #{user.id}"
end

View file

@ -66,7 +66,7 @@ class Tracks::IncrementalProcessor
end
def find_end_time
previous_point ? Time.at(previous_point.timestamp) : nil
previous_point ? Time.zone.at(previous_point.timestamp) : nil
end
def exceeds_thresholds?(previous_point, current_point)

View file

@ -31,6 +31,24 @@ RSpec.describe Tracks::Generator do
generator.call
expect(points.map(&:reload).map(&:track)).to all(be_present)
end
it 'properly handles point associations when cleaning existing tracks' do
# Create existing tracks with associated points
existing_track = create(:track, user: user)
existing_points = create_list(:point, 3, user: user, track: existing_track)
# Verify points are associated
expect(existing_points.map(&:reload).map(&:track_id)).to all(eq(existing_track.id))
# Run generator which should clean existing tracks and create new ones
generator.call
# Verify the old track is deleted
expect(Track.exists?(existing_track.id)).to be false
# Verify the points are no longer associated with the deleted track
expect(existing_points.map(&:reload).map(&:track_id)).to all(be_nil)
end
end
context 'with insufficient points' do
@ -118,6 +136,24 @@ RSpec.describe Tracks::Generator do
generator.call
expect(Track.exists?(existing_track.id)).to be false
end
it 'properly handles point associations when cleaning daily tracks' do
# Create existing tracks with associated points for today
existing_track = create(:track, user: user, start_at: today.beginning_of_day)
existing_points = create_list(:point, 3, user: user, track: existing_track)
# Verify points are associated
expect(existing_points.map(&:reload).map(&:track_id)).to all(eq(existing_track.id))
# Run generator which should clean existing tracks for the day and create new ones
generator.call
# Verify the old track is deleted
expect(Track.exists?(existing_track.id)).to be false
# Verify the points are no longer associated with the deleted track
expect(existing_points.map(&:reload).map(&:track_id)).to all(be_nil)
end
end
context 'with empty points' do

View file

@ -47,7 +47,7 @@ RSpec.describe Tracks::IncrementalProcessor do
it 'processes when time threshold exceeded' do
expect(Tracks::CreateJob).to receive(:perform_later)
.with(user.id, start_at: nil, end_at: Time.at(previous_point.timestamp), mode: :none)
.with(user.id, start_at: nil, end_at: Time.zone.at(previous_point.timestamp), mode: :none)
processor.call
end
end
@ -65,7 +65,7 @@ RSpec.describe Tracks::IncrementalProcessor do
it 'uses existing track end time as start_at' do
expect(Tracks::CreateJob).to receive(:perform_later)
.with(user.id, start_at: existing_track.end_at, end_at: Time.at(previous_point.timestamp), mode: :none)
.with(user.id, start_at: existing_track.end_at, end_at: Time.zone.at(previous_point.timestamp), mode: :none)
processor.call
end
end
@ -88,7 +88,7 @@ RSpec.describe Tracks::IncrementalProcessor do
it 'processes when distance threshold exceeded' do
expect(Tracks::CreateJob).to receive(:perform_later)
.with(user.id, start_at: nil, end_at: Time.at(previous_point.timestamp), mode: :none)
.with(user.id, start_at: nil, end_at: Time.zone.at(previous_point.timestamp), mode: :none)
processor.call
end
end

View file

@ -9,7 +9,7 @@ RSpec.describe Users::ImportData, type: :service do
let(:import_directory) { Rails.root.join('tmp', "import_#{user.email.gsub(/[^0-9A-Za-z._-]/, '_')}_1234567890") }
before do
allow(Time).to receive(:current).and_return(Time.at(1234567890))
allow(Time).to receive(:current).and_return(Time.zone.at(1234567890))
allow(FileUtils).to receive(:mkdir_p)
allow(FileUtils).to receive(:rm_rf)
allow(File).to receive(:directory?).and_return(true)