Fix some tests

This commit is contained in:
Eugene Burmakin 2025-11-19 20:56:46 +01:00
parent 01df22d080
commit ebd0f8d6bc
22 changed files with 120 additions and 75 deletions

View file

@ -15,7 +15,6 @@ class Place < ApplicationRecord
before_validation :build_lonlat, if: -> { latitude.present? && longitude.present? } before_validation :build_lonlat, if: -> { latitude.present? && longitude.present? }
validates :name, presence: true validates :name, presence: true
validates :latitude, :longitude, presence: true
validates :lonlat, presence: true validates :lonlat, presence: true
enum :source, { manual: 0, photon: 1 } enum :source, { manual: 0, photon: 1 }

View file

@ -0,0 +1,27 @@
# frozen_string_literal: true
class Api::PlaceSerializer
def initialize(place)
@place = place
end
def call
{
id: place.id,
name: place.name,
longitude: place.lon,
latitude: place.lat,
city: place.city,
country: place.country,
source: place.source,
geodata: place.geodata,
created_at: place.created_at,
updated_at: place.updated_at,
reverse_geocoded_at: place.reverse_geocoded_at
}
end
private
attr_reader :place
end

View file

@ -174,7 +174,6 @@ module Visits
# Create new place # Create new place
place = user.places.build( place = user.places.build(
name: name, name: name,
lonlat: "POINT(#{result.longitude} #{result.latitude})",
latitude: result.latitude, latitude: result.latitude,
longitude: result.longitude, longitude: result.longitude,
city: properties['city'], city: properties['city'],

View file

@ -5,10 +5,7 @@ require 'rails_helper'
RSpec.describe 'Api::V1::Maps::Hexagons', type: :request do RSpec.describe 'Api::V1::Maps::Hexagons', type: :request do
let(:user) { create(:user) } let(:user) { create(:user) }
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe 'GET /api/v1/maps/hexagons' do describe 'GET /api/v1/maps/hexagons' do
let(:valid_params) do let(:valid_params) do

View file

@ -5,13 +5,6 @@ require 'rails_helper'
RSpec.describe 'Authentication', type: :request do RSpec.describe 'Authentication', type: :request do
let(:user) { create(:user, password: 'password123') } let(:user) { create(:user, password: 'password123') }
before do
stub_request(:get, 'https://api.github.com/repos/Freika/dawarich/tags')
.with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => /.*/,
'Host' => 'api.github.com', 'User-Agent' => /.*/ })
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe 'Route Protection' do describe 'Route Protection' do
it 'redirects to sign in page when accessing protected routes while signed out' do it 'redirects to sign in page when accessing protected routes while signed out' do
get map_path get map_path

View file

@ -6,11 +6,6 @@ RSpec.describe '/exports', type: :request do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:params) { { start_at: 1.day.ago, end_at: Time.zone.now } } let(:params) { { start_at: 1.day.ago, end_at: Time.zone.now } }
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe 'GET /index' do describe 'GET /index' do
context 'when user is not logged in' do context 'when user is not logged in' do
it 'redirects to the login page' do it 'redirects to the login page' do

View file

@ -8,10 +8,7 @@ RSpec.describe 'Family::Invitations', type: :request do
let!(:membership) { create(:family_membership, user: user, family: family, role: :owner) } let!(:membership) { create(:family_membership, user: user, family: family, role: :owner) }
let(:invitation) { create(:family_invitation, family: family, invited_by: user) } let(:invitation) { create(:family_invitation, family: family, invited_by: user) }
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe 'GET /family/invitations' do describe 'GET /family/invitations' do
before { sign_in user } before { sign_in user }

View file

@ -7,10 +7,7 @@ RSpec.describe 'Family Workflows', type: :request do
let(:user2) { create(:user, email: 'bob@example.com') } let(:user2) { create(:user, email: 'bob@example.com') }
let(:user3) { create(:user, email: 'charlie@example.com') } let(:user3) { create(:user, email: 'charlie@example.com') }
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe 'Complete family creation and management workflow' do describe 'Complete family creation and management workflow' do
it 'allows creating a family, inviting members, and managing the family' do it 'allows creating a family, inviting members, and managing the family' do

View file

@ -3,10 +3,7 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe 'Imports', type: :request do RSpec.describe 'Imports', type: :request do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe 'GET /imports' do describe 'GET /imports' do
context 'when user is logged in' do context 'when user is logged in' do

View file

@ -3,10 +3,7 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe 'Map', type: :request do RSpec.describe 'Map', type: :request do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe 'GET /index' do describe 'GET /index' do
context 'when user signed in' do context 'when user signed in' do

View file

@ -3,10 +3,7 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe '/notifications', type: :request do RSpec.describe '/notifications', type: :request do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
context 'when user is not logged in' do context 'when user is not logged in' do
it 'redirects to the login page' do it 'redirects to the login page' do

View file

@ -21,7 +21,7 @@ RSpec.describe '/places', type: :request do
end end
describe 'DELETE /destroy' do describe 'DELETE /destroy' do
let!(:place) { create(:place) } let!(:place) { create(:place, user:) }
let!(:visit) { create(:visit, place:, user:) } let!(:visit) { create(:visit, place:, user:) }
it 'destroys the requested place' do it 'destroys the requested place' do

View file

@ -3,10 +3,7 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe '/settings/background_jobs', type: :request do RSpec.describe '/settings/background_jobs', type: :request do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
context 'when Dawarich is in self-hosted mode' do context 'when Dawarich is in self-hosted mode' do
before do before do

View file

@ -3,10 +3,7 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe 'settings/maps', type: :request do RSpec.describe 'settings/maps', type: :request do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
context 'when user is authenticated' do context 'when user is authenticated' do
let!(:user) { create(:user) } let!(:user) { create(:user) }

View file

@ -3,10 +3,7 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe 'Shared::Stats', type: :request do RSpec.describe 'Shared::Stats', type: :request do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
context 'public sharing' do context 'public sharing' do
let(:user) { create(:user) } let(:user) { create(:user) }

View file

@ -3,10 +3,7 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe '/stats', type: :request do RSpec.describe '/stats', type: :request do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
context 'when user is not signed in' do context 'when user is not signed in' do
describe 'GET /index' do describe 'GET /index' do

View file

@ -10,10 +10,7 @@ RSpec.describe 'Users::Registrations', type: :request do
create(:family_invitation, family: family, invited_by: family_owner, email: 'invited@example.com') create(:family_invitation, family: family, invited_by: family_owner, email: 'invited@example.com')
end end
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe 'Family Invitation Registration Flow' do describe 'Family Invitation Registration Flow' do
context 'when accessing registration with a valid invitation token' do context 'when accessing registration with a valid invitation token' do

View file

@ -3,10 +3,7 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe 'Users', type: :request do RSpec.describe 'Users', type: :request do
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
end
describe 'GET /users/sign_up' do describe 'GET /users/sign_up' do
context 'when self-hosted' do context 'when self-hosted' do

View file

@ -0,0 +1,67 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Api::PlaceSerializer do
describe '#call' do
let(:place) do
create(
:place,
:with_geodata,
name: 'Central Park',
longitude: -73.9665,
latitude: 40.7812,
city: 'New York',
country: 'United States',
source: 'photon',
geodata: { 'amenity' => 'park', 'leisure' => 'park' },
reverse_geocoded_at: Time.zone.parse('2023-01-15T12:00:00Z')
)
end
subject(:serializer) { described_class.new(place) }
it 'serializes a place into a hash with all attributes' do
result = serializer.call
expect(result).to be_a(Hash)
expect(result[:id]).to eq(place.id)
expect(result[:name]).to eq('Central Park')
expect(result[:longitude]).to eq(-73.9665)
expect(result[:latitude]).to eq(40.7812)
expect(result[:city]).to eq('New York')
expect(result[:country]).to eq('United States')
expect(result[:source]).to eq('photon')
expect(result[:geodata]).to eq({ 'amenity' => 'park', 'leisure' => 'park' })
expect(result[:reverse_geocoded_at]).to eq(Time.zone.parse('2023-01-15T12:00:00Z'))
end
context 'with nil values' do
let(:place_with_nils) do
create(
:place,
name: 'Unknown Place',
city: nil,
country: nil,
source: nil,
geodata: {},
reverse_geocoded_at: nil
)
end
subject(:serializer_with_nils) { described_class.new(place_with_nils) }
it 'handles nil values correctly' do
result = serializer_with_nils.call
expect(result[:id]).to eq(place_with_nils.id)
expect(result[:name]).to eq('Unknown Place')
expect(result[:city]).to be_nil
expect(result[:country]).to be_nil
expect(result[:source]).to be_nil
expect(result[:geodata]).to eq({})
expect(result[:reverse_geocoded_at]).to be_nil
end
end
end
end

View file

@ -20,7 +20,7 @@ RSpec.describe Visits::PlaceFinder do
end end
context 'when an existing place is found' do context 'when an existing place is found' do
let!(:existing_place) { create(:place, latitude: latitude, longitude: longitude) } let!(:existing_place) { create(:place, user: user, latitude: latitude, longitude: longitude) }
it 'returns the existing place as main_place' do it 'returns the existing place as main_place' do
result = subject.find_or_create_place(visit_data) result = subject.find_or_create_place(visit_data)
@ -38,6 +38,7 @@ RSpec.describe Visits::PlaceFinder do
it 'finds an existing place by name within search radius' do it 'finds an existing place by name within search radius' do
similar_named_place = create(:place, similar_named_place = create(:place,
user: user,
name: 'Test Place', name: 'Test Place',
latitude: latitude + 0.0001, latitude: latitude + 0.0001,
longitude: longitude + 0.0001) longitude: longitude + 0.0001)
@ -183,9 +184,9 @@ RSpec.describe Visits::PlaceFinder do
end end
context 'with multiple potential places' do context 'with multiple potential places' do
let!(:place1) { create(:place, name: 'Place 1', latitude: latitude, longitude: longitude) } let!(:place1) { create(:place, user: user, name: 'Place 1', latitude: latitude, longitude: longitude) }
let!(:place2) { create(:place, name: 'Place 2', latitude: latitude + 0.0005, longitude: longitude + 0.0005) } let!(:place2) { create(:place, user: user, name: 'Place 2', latitude: latitude + 0.0005, longitude: longitude + 0.0005) }
let!(:place3) { create(:place, name: 'Place 3', latitude: latitude + 0.001, longitude: longitude + 0.001) } let!(:place3) { create(:place, user: user, name: 'Place 3', latitude: latitude + 0.001, longitude: longitude + 0.001) }
it 'selects the closest place as main_place' do it 'selects the closest place as main_place' do
result = subject.find_or_create_place(visit_data) result = subject.find_or_create_place(visit_data)
@ -201,7 +202,7 @@ RSpec.describe Visits::PlaceFinder do
end end
it 'may include places with the same name' do it 'may include places with the same name' do
dup_place = create(:place, name: 'Place 1', latitude: latitude + 0.0002, longitude: longitude + 0.0002) dup_place = create(:place, user: user, name: 'Place 1', latitude: latitude + 0.0002, longitude: longitude + 0.0002)
allow(subject).to receive(:place_name_exists?).and_return(false) allow(subject).to receive(:place_name_exists?).and_return(false)

View file

@ -14,7 +14,9 @@ RSpec.configure do |config|
'state' => 'New York', 'state' => 'New York',
'name' => 'Test Location' 'name' => 'Test Location'
} }
} },
latitude: 40.7128,
longitude: -74.0060
) )
] ]
) )

View file

@ -1,14 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
# Stub GitHub API requests in tests
RSpec.configure do |config| RSpec.configure do |config|
config.before(:each) do config.before(:each) do
# Stub GitHub API version checking
stub_request(:get, "https://api.github.com/repos/Freika/dawarich/tags") stub_request(:get, "https://api.github.com/repos/Freika/dawarich/tags")
.to_return( .to_return(
status: 200, status: 200,
body: [{ name: "v0.1.0" }].to_json, body: '[{"name": "1.0.0"}]',
headers: { 'Content-Type' => 'application/json' } headers: {}
) )
end end
end end