Fix few more specs

This commit is contained in:
Eugene Burmakin 2025-11-22 18:38:20 +01:00
parent b18fc392cc
commit ba6314231a
3 changed files with 4 additions and 45 deletions

View file

@ -15,8 +15,6 @@ class Users::ImportData::Places
def call
return 0 unless places_data.respond_to?(:each)
logger.info "Importing #{collection_description(places_data)} places for user: #{user.email}"
enumerate(places_data) do |place_data|
add(place_data)
end
@ -69,12 +67,9 @@ class Users::ImportData::Places
longitude = place_data['longitude']&.to_f
unless name.present? && latitude.present? && longitude.present?
logger.debug "Skipping place with missing required data: #{place_data.inspect}"
return nil
end
logger.debug "Processing place for import: #{name} at (#{latitude}, #{longitude})"
existing_place = Place.where(
name: name,
latitude: latitude,
@ -83,29 +78,22 @@ class Users::ImportData::Places
).first
if existing_place
logger.debug "Found exact place match: #{name} at (#{latitude}, #{longitude}) -> existing place ID #{existing_place.id}"
existing_place.define_singleton_method(:previously_new_record?) { false }
return existing_place
end
logger.debug "No exact match found for #{name} at (#{latitude}, #{longitude}). Creating new place."
place_attributes = place_data.except('created_at', 'updated_at', 'latitude', 'longitude')
place_attributes['lonlat'] = "POINT(#{longitude} #{latitude})"
place_attributes['latitude'] = latitude
place_attributes['longitude'] = longitude
place_attributes.delete('user')
logger.debug "Creating place with attributes: #{place_attributes.inspect}"
begin
place = Place.create!(place_attributes)
place.define_singleton_method(:previously_new_record?) { true }
logger.debug "Created place during import: #{place.name} (ID: #{place.id})"
place
rescue ActiveRecord::RecordInvalid => e
logger.error "Failed to create place: #{place_data.inspect}, error: #{e.message}"
nil
end
end

View file

@ -60,13 +60,6 @@ RSpec.describe Users::ImportData::Places, type: :service do
result = service.call
expect(result).to eq(2)
end
it 'logs the import process' do
expect(Rails.logger).to receive(:info).with("Importing 2 places for user: #{user.email}")
expect(Rails.logger).to receive(:info).with("Places import completed. Created: 2")
service.call
end
end
context 'with duplicate places (same name)' do
@ -103,13 +96,6 @@ RSpec.describe Users::ImportData::Places, type: :service do
expect { service.call }.to change { Place.count }.by(1)
end
it 'logs when finding exact duplicates' do
allow(Rails.logger).to receive(:debug) # Allow any debug logs
expect(Rails.logger).to receive(:debug).with(/Found exact place match: Home at \(40\.7128, -74\.006\) -> existing place ID \d+/)
service.call
end
it 'returns only the count of newly created places' do
result = service.call
expect(result).to eq(1)
@ -125,12 +111,12 @@ RSpec.describe Users::ImportData::Places, type: :service do
end
it 'creates the place since name is different' do
expect { service.call }.to change { Place.count }.by(2)
expect { service.call }.to change { Place.where(user_id: nil).count }.by(2)
end
it 'creates both places with different names' do
service.call
places_at_location = Place.where(latitude: 40.7128, longitude: -74.0060)
places_at_location = Place.where(latitude: 40.7128, longitude: -74.0060, user_id: nil)
expect(places_at_location.count).to eq(2)
expect(places_at_location.pluck(:name)).to contain_exactly('Home', 'Different Name')
end
@ -180,13 +166,6 @@ RSpec.describe Users::ImportData::Places, type: :service do
it 'only creates places with all required fields' do
expect { service.call }.to change { Place.count }.by(1)
end
it 'logs skipped records with missing data' do
allow(Rails.logger).to receive(:debug) # Allow all debug logs
expect(Rails.logger).to receive(:debug).with(/Skipping place with missing required data/).at_least(:once)
service.call
end
end
context 'with nil places data' do
@ -222,13 +201,6 @@ RSpec.describe Users::ImportData::Places, type: :service do
expect { service.call }.not_to change { Place.count }
end
it 'logs the import process with 0 count' do
expect(Rails.logger).to receive(:info).with("Importing 0 places for user: #{user.email}")
expect(Rails.logger).to receive(:info).with("Places import completed. Created: 0")
service.call
end
it 'returns 0' do
result = service.call
expect(result).to eq(0)

View file

@ -32,10 +32,10 @@ RSpec.describe Users::ImportData::Places do
buffered_service = described_class.new(user, nil, batch_size: 2, logger: logger_double)
buffered_service.add('name' => 'First', 'latitude' => 1, 'longitude' => 2)
expect(Place.count).to eq(0)
expect(Place.where(user_id: nil).count).to eq(0)
buffered_service.add('name' => 'Second', 'latitude' => 3, 'longitude' => 4)
expect(Place.count).to eq(2)
expect(Place.where(user_id: nil).count).to eq(2)
expect(buffered_service.finalize).to eq(2)
expect { buffered_service.finalize }.not_to change(Place, :count)
@ -48,7 +48,6 @@ RSpec.describe Users::ImportData::Places do
service.add('name' => 'Missing coords')
expect(service.finalize).to eq(1)
expect(logger).to have_received(:debug).with(/Skipping place with missing required data/)
end
end
end