Fix sometests

This commit is contained in:
Eugene Burmakin 2025-11-17 19:05:58 +01:00
parent c462d34efa
commit 8a36a69987
5 changed files with 32 additions and 34 deletions

File diff suppressed because one or more lines are too long

View file

@ -3,7 +3,7 @@
class PlacePolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.where(user: user)
scope.where(user_id: user.id)
end
end

View file

@ -76,6 +76,7 @@ class Users::ImportData::Places
logger.debug "Processing place for import: #{name} at (#{latitude}, #{longitude})"
existing_place = Place.where(
user_id: user.id,
name: name,
latitude: latitude,
longitude: longitude
@ -90,9 +91,10 @@ class Users::ImportData::Places
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})"
# lonlat will be auto-generated by the Place model's before_validation callback
place_attributes['latitude'] = latitude
place_attributes['longitude'] = longitude
place_attributes['user_id'] = user.id
place_attributes.delete('user')
logger.debug "Creating place with attributes: #{place_attributes.inspect}"

View file

@ -47,15 +47,15 @@ module Visits
# Step 1: Find existing place
def find_existing_place(lat, lon, name)
# Try to find existing place by location first
existing_by_location = Place.near([lat, lon], SIMILARITY_RADIUS, :m).first
existing_by_location = user.places.near([lat, lon], SIMILARITY_RADIUS, :m).first
return existing_by_location if existing_by_location
# Then try by name if available
return nil if name.blank?
Place.where(name: name)
.near([lat, lon], SEARCH_RADIUS, :m)
.first
user.places.where(name: name)
.near([lat, lon], SEARCH_RADIUS, :m)
.first
end
# Step 2: Collect potential places from all sources
@ -113,14 +113,14 @@ module Visits
return nil if name == Place::DEFAULT_NAME
# Look for existing place with this name
existing = Place.where(name: name)
.near([point.lat, point.lon], SIMILARITY_RADIUS, :m)
.first
existing = user.places.where(name: name)
.near([point.lat, point.lon], SIMILARITY_RADIUS, :m)
.first
return existing if existing
# Create new place
place = Place.new(
place = user.places.build(
name: name,
lonlat: "POINT(#{point.lon} #{point.lat})",
latitude: point.lat,
@ -165,14 +165,14 @@ module Visits
return nil if name == Place::DEFAULT_NAME
# Look for existing place with this name
existing = Place.where(name: name)
.near([result.latitude, result.longitude], SIMILARITY_RADIUS, :m)
.first
existing = user.places.where(name: name)
.near([result.latitude, result.longitude], SIMILARITY_RADIUS, :m)
.first
return existing if existing
# Create new place
place = Place.new(
place = user.places.build(
name: name,
lonlat: "POINT(#{result.longitude} #{result.latitude})",
latitude: result.latitude,
@ -205,7 +205,7 @@ module Visits
def create_default_place(lat, lon, suggested_name)
name = suggested_name.presence || Place::DEFAULT_NAME
place = Place.new(
place = user.places.build(
name: name,
lonlat: "POINT(#{lon} #{lat})",
latitude: lat,
@ -219,7 +219,7 @@ module Visits
# Step 9: Find suggested places
def find_suggested_places(lat, lon)
Place.near([lat, lon], SEARCH_RADIUS, :m).with_distance([lat, lon], :m)
user.places.near([lat, lon], SEARCH_RADIUS, :m).with_distance([lat, lon], :m)
end
# Helper methods

View file

@ -71,10 +71,9 @@ RSpec.describe Users::ImportData::Places, type: :service do
context 'with duplicate places (same name)' do
before do
# Create an existing place with same name but different coordinates
create(:place, name: 'Home',
latitude: 41.0000, longitude: -75.0000,
lonlat: 'POINT(-75.0000 41.0000)')
# Create an existing place with same name but different coordinates for the same user
create(:place, user: user, name: 'Home',
latitude: 41.0000, longitude: -75.0000)
end
it 'creates the place since coordinates are different' do
@ -83,7 +82,7 @@ RSpec.describe Users::ImportData::Places, type: :service do
it 'creates both places with different coordinates' do
service.call
home_places = Place.where(name: 'Home')
home_places = user.places.where(name: 'Home')
expect(home_places.count).to eq(2)
imported_home = home_places.find_by(latitude: 40.7128, longitude: -74.0060)
@ -93,10 +92,9 @@ RSpec.describe Users::ImportData::Places, type: :service do
context 'with exact duplicate places (same name and coordinates)' do
before do
# Create an existing place with exact same name and coordinates
create(:place, name: 'Home',
latitude: 40.7128, longitude: -74.0060,
lonlat: 'POINT(-74.0060 40.7128)')
# Create an existing place with exact same name and coordinates for the same user
create(:place, user: user, name: 'Home',
latitude: 40.7128, longitude: -74.0060)
end
it 'skips exact duplicate places' do
@ -118,10 +116,9 @@ RSpec.describe Users::ImportData::Places, type: :service do
context 'with duplicate places (same coordinates)' do
before do
# Create an existing place with same coordinates but different name
create(:place, name: 'Different Name',
latitude: 40.7128, longitude: -74.0060,
lonlat: 'POINT(-74.0060 40.7128)')
# Create an existing place with same coordinates but different name for the same user
create(:place, user: user, name: 'Different Name',
latitude: 40.7128, longitude: -74.0060)
end
it 'creates the place since name is different' do
@ -130,7 +127,7 @@ RSpec.describe Users::ImportData::Places, type: :service do
it 'creates both places with different names' do
service.call
places_at_location = Place.where(latitude: 40.7128, longitude: -74.0060)
places_at_location = user.places.where(latitude: 40.7128, longitude: -74.0060)
expect(places_at_location.count).to eq(2)
expect(places_at_location.pluck(:name)).to contain_exactly('Home', 'Different Name')
end
@ -138,9 +135,8 @@ RSpec.describe Users::ImportData::Places, type: :service do
context 'with places having same name but different coordinates' do
before do
create(:place, name: 'Different Place',
latitude: 41.0000, longitude: -75.0000,
lonlat: 'POINT(-75.0000 41.0000)')
create(:place, user: user, name: 'Different Place',
latitude: 41.0000, longitude: -75.0000)
end
it 'creates both places since coordinates and names differ' do