From f018f0c64c36b53671f585803f2fcc9ec288e250 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Thu, 28 Nov 2024 17:41:43 +0100 Subject: [PATCH] Add specs for trips model --- app/models/trip.rb | 2 +- spec/factories/points.rb | 5 ++ spec/models/trip_spec.rb | 81 +++++++++++++++++++++++++++++ spec/requests/api/v1/photos_spec.rb | 4 +- 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/app/models/trip.rb b/app/models/trip.rb index 22688770..fb67019c 100644 --- a/app/models/trip.rb +++ b/app/models/trip.rb @@ -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 diff --git a/spec/factories/points.rb b/spec/factories/points.rb index 968e6f80..86e76319 100644 --- a/spec/factories/points.rb +++ b/spec/factories/points.rb @@ -54,6 +54,11 @@ FactoryBot.define do } } end + + trait :reverse_geocoded do + country { FFaker::Address.country } + city { FFaker::Address.city } + end end end end diff --git a/spec/models/trip_spec.rb b/spec/models/trip_spec.rb index 55660a6a..65255b90 100644 --- a/spec/models/trip_spec.rb +++ b/spec/models/trip_spec.rb @@ -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 diff --git a/spec/requests/api/v1/photos_spec.rb b/spec/requests/api/v1/photos_spec.rb index e3f3d32c..d15a5342 100644 --- a/spec/requests/api/v1/photos_spec.rb +++ b/spec/requests/api/v1/photos_spec.rb @@ -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' } ]