Add specs for trips model

This commit is contained in:
Eugene Burmakin 2024-11-28 17:41:43 +01:00
parent b56c58f654
commit f018f0c64c
4 changed files with 89 additions and 3 deletions

View file

@ -32,7 +32,7 @@ class Trip < ApplicationRecord
# to show all photos in the same height
photos = vertical_photos.count > horizontal_photos.count ? vertical_photos : horizontal_photos
photos.sample(12).map do |asset|
photos.sample(12).sort_by { _1['localDateTime'] }.map do |asset|
{ url: "/api/v1/photos/#{asset['id']}/thumbnail.jpg?api_key=#{user.api_key}" }
end
end

View file

@ -54,6 +54,11 @@ FactoryBot.define do
}
}
end
trait :reverse_geocoded do
country { FFaker::Address.country }
city { FFaker::Address.city }
end
end
end
end

View file

@ -12,4 +12,85 @@ RSpec.describe Trip, type: :model do
describe 'associations' do
it { is_expected.to belong_to(:user) }
end
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)
end
end
describe '#countries' do
let(:user) { create(:user) }
let(:trip) { create(:trip, user:) }
let(:points) do
create_list(
:point,
25,
:reverse_geocoded,
user:,
timestamp: (trip.started_at.to_i..trip.ended_at.to_i).to_a.sample
)
end
it 'returns the unique countries of the points' do
expect(trip.countries).to eq(trip.points.pluck(:country).uniq.compact)
end
end
describe '#photos' do
let(:photo_data) do
[
{
'id' => '123',
'latitude' => 35.6762,
'longitude' => 139.6503,
'localDateTime' => '2024-01-01T03:00:00.000Z',
'type' => 'photo',
'exifInfo' => {
'orientation' => '3'
}
},
{
'id' => '456',
'latitude' => 40.7128,
'longitude' => -74.0060,
'localDateTime' => '2024-01-02T01:00:00.000Z',
'type' => 'photo',
'exifInfo' => {
'orientation' => '6'
}
},
{
'id' => '789',
'latitude' => 40.7128,
'longitude' => -74.0060,
'localDateTime' => '2024-01-02T02:00:00.000Z',
'type' => 'photo',
'exifInfo' => {
'orientation' => '6'
}
}
]
end
let(:user) { create(:user) }
let(:trip) { create(:trip, user:) }
let(:expected_photos) do
[
{ url: "/api/v1/photos/456/thumbnail.jpg?api_key=#{user.api_key}" },
{ url: "/api/v1/photos/789/thumbnail.jpg?api_key=#{user.api_key}" }
]
end
before do
allow_any_instance_of(Immich::RequestPhotos).to receive(:call).and_return(photo_data)
end
it 'returns the photos' do
expect(trip.photos).to eq(expected_photos)
end
end
end

View file

@ -12,14 +12,14 @@ RSpec.describe 'Api::V1::Photos', type: :request do
'id' => '123',
'latitude' => 35.6762,
'longitude' => 139.6503,
'createdAt' => '2024-01-01T00:00:00.000Z',
'localDateTime' => '2024-01-01T00:00:00.000Z',
'type' => 'photo'
},
{
'id' => '456',
'latitude' => 40.7128,
'longitude' => -74.0060,
'createdAt' => '2024-01-02T00:00:00.000Z',
'localDateTime' => '2024-01-02T00:00:00.000Z',
'type' => 'photo'
}
]