dawarich/spec/services/imports/create_spec.rb

224 lines
7.5 KiB
Ruby
Raw Normal View History

2024-12-04 07:33:15 -05:00
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Imports::Create do
let(:user) { create(:user) }
let(:service) { described_class.new(user, import) }
describe '#call' do
2025-06-30 16:08:34 -04:00
describe 'status transitions' do
let(:import) { create(:import, source: 'owntracks', status: 'created') }
let(:file_path) { Rails.root.join('spec/fixtures/files/owntracks/2024-03.rec') }
before do
import.file.attach(io: File.open(file_path), filename: '2024-03.rec', content_type: 'application/octet-stream')
end
it 'sets status to processing at start' do
service.call
2025-08-22 13:10:40 -04:00
2025-06-30 16:08:34 -04:00
expect(import.reload.status).to eq('processing').or eq('completed')
end
2025-08-23 10:37:53 -04:00
it 'updates the import source' do
service.call
expect(import.reload.source).to eq('owntracks')
end
2025-06-30 16:08:34 -04:00
context 'when import succeeds' do
it 'sets status to completed' do
service.call
expect(import.reload.status).to eq('completed')
end
end
context 'when import fails' do
before do
2025-08-22 13:10:40 -04:00
allow(OwnTracks::Importer).to receive(:new).with(import, user.id, kind_of(String)).and_raise(StandardError)
2025-06-30 16:08:34 -04:00
end
it 'sets status to failed' do
service.call
expect(import.reload.status).to eq('failed')
end
end
end
2024-12-04 07:33:15 -05:00
context 'when source is google_semantic_history' do
let(:import) { create(:import, source: 'google_semantic_history') }
let(:file_path) { Rails.root.join('spec/fixtures/files/google/semantic_history.json') }
let(:file) { Rack::Test::UploadedFile.new(file_path, 'application/json') }
before do
import.file.attach(io: File.open(file_path), filename: 'semantic_history.json',
content_type: 'application/json')
end
2024-12-04 07:33:15 -05:00
2025-04-23 17:36:16 -04:00
it 'calls the GoogleMaps::SemanticHistoryImporter' do
expect(GoogleMaps::SemanticHistoryImporter).to \
2025-08-22 13:10:40 -04:00
receive(:new).with(import, user.id, kind_of(String)).and_return(double(call: true))
2024-12-04 07:33:15 -05:00
service.call
end
it 'updates the import points count' do
expect { service.call }.to have_enqueued_job(Import::UpdatePointsCountJob).with(import.id)
end
2024-12-04 07:33:15 -05:00
end
context 'when source is google_phone_takeout' do
let(:import) { create(:import, source: 'google_phone_takeout') }
2025-08-23 10:07:15 -04:00
let(:file_path) { Rails.root.join('spec/fixtures/files/google/phone-takeout_w_3_duplicates.json') }
2025-08-22 13:10:40 -04:00
before do
2025-08-23 10:07:15 -04:00
import.file.attach(io: File.open(file_path), filename: 'phone-takeout_w_3_duplicates.json',
2025-08-22 13:10:40 -04:00
content_type: 'application/json')
end
2024-12-04 07:33:15 -05:00
2025-04-23 17:36:16 -04:00
it 'calls the GoogleMaps::PhoneTakeoutImporter' do
expect(GoogleMaps::PhoneTakeoutImporter).to \
2025-08-22 13:10:40 -04:00
receive(:new).with(import, user.id, kind_of(String)).and_return(double(call: true))
2024-12-04 07:33:15 -05:00
service.call
end
end
context 'when source is owntracks' do
2025-04-19 07:18:39 -04:00
let(:import) { create(:import, source: 'owntracks', name: '2024-03.rec') }
let(:file_path) { Rails.root.join('spec/fixtures/files/owntracks/2024-03.rec') }
let(:file) { Rack::Test::UploadedFile.new(file_path, 'application/octet-stream') }
before do
import.file.attach(io: File.open(file_path), filename: '2024-03.rec', content_type: 'application/octet-stream')
end
2024-12-04 07:33:15 -05:00
2025-02-22 17:14:23 -05:00
it 'calls the OwnTracks::Importer' do
expect(OwnTracks::Importer).to \
2025-08-22 13:10:40 -04:00
receive(:new).with(import, user.id, kind_of(String)).and_return(double(call: true))
2024-12-04 07:33:15 -05:00
service.call
end
context 'when import is successful' do
it 'schedules stats creating' do
2025-06-09 07:50:43 -04:00
Sidekiq::Testing.inline! do
expect { service.call }.to \
have_enqueued_job(Stats::CalculatingJob).with(user.id, 2024, 3)
end
2024-12-04 07:33:15 -05:00
end
it 'schedules visit suggesting' do
2025-06-09 07:50:43 -04:00
Sidekiq::Testing.inline! do
expect { service.call }.to have_enqueued_job(VisitSuggestingJob)
end
2024-12-04 07:33:15 -05:00
end
end
context 'when import fails' do
before do
2025-08-22 13:10:40 -04:00
allow(OwnTracks::Importer).to receive(:new).with(import, user.id, kind_of(String)).and_raise(StandardError)
2024-12-04 07:33:15 -05:00
end
2025-04-19 07:18:39 -04:00
context 'when self-hosted' do
before do
allow(DawarichSettings).to receive(:self_hosted?).and_return(true)
end
after do
allow(DawarichSettings).to receive(:self_hosted?).and_call_original
end
it 'creates a failed notification' do
service.call
expect(user.notifications.last.content).to \
include('Import "2024-03.rec" failed: StandardError, stacktrace: ')
end
end
context 'when not self-hosted' do
before do
allow(DawarichSettings).to receive(:self_hosted?).and_return(false)
end
after do
allow(DawarichSettings).to receive(:self_hosted?).and_call_original
end
it 'does not create a failed notification' do
service.call
2024-12-04 07:33:15 -05:00
2025-04-19 07:18:39 -04:00
expect(user.notifications.last.content).to \
include('Import "2024-03.rec" failed, please contact us at hi@dawarich.com')
end
2024-12-04 07:33:15 -05:00
end
end
end
context 'when source is gpx' do
let(:import) { create(:import, source: 'gpx') }
let(:file_path) { Rails.root.join('spec/fixtures/files/gpx/gpx_track_single_segment.gpx') }
let(:file) { Rack::Test::UploadedFile.new(file_path, 'application/octet-stream') }
before do
import.file.attach(io: File.open(file_path), filename: 'gpx_track_single_segment.gpx',
content_type: 'application/octet-stream')
end
2024-12-04 07:33:15 -05:00
2025-02-22 17:14:23 -05:00
it 'calls the Gpx::TrackImporter' do
expect(Gpx::TrackImporter).to \
2025-08-22 13:10:40 -04:00
receive(:new).with(import, user.id, kind_of(String)).and_return(double(call: true))
2024-12-04 07:33:15 -05:00
service.call
end
end
context 'when source is geojson' do
let(:import) { create(:import, source: 'geojson') }
2025-08-22 13:10:40 -04:00
let(:file_path) { Rails.root.join('spec/fixtures/files/geojson/export.json') }
before do
import.file.attach(io: File.open(file_path), filename: 'export.json',
content_type: 'application/json')
end
2024-12-04 07:33:15 -05:00
2025-04-23 17:36:16 -04:00
it 'calls the Geojson::Importer' do
expect(Geojson::Importer).to \
2025-08-22 13:10:40 -04:00
receive(:new).with(import, user.id, kind_of(String)).and_return(double(call: true))
2024-12-04 07:33:15 -05:00
service.call
end
end
context 'when source is immich_api' do
let(:import) { create(:import, source: 'immich_api') }
2025-08-22 13:10:40 -04:00
let(:file_path) { Rails.root.join('spec/fixtures/files/immich/geodata.json') }
before do
import.file.attach(io: File.open(file_path), filename: 'geodata.json',
content_type: 'application/json')
end
2024-12-04 07:33:15 -05:00
2025-04-23 17:36:16 -04:00
it 'calls the Photos::Importer' do
expect(Photos::Importer).to \
2025-08-22 13:10:40 -04:00
receive(:new).with(import, user.id, kind_of(String)).and_return(double(call: true))
2025-08-23 10:07:15 -04:00
2024-12-04 07:33:15 -05:00
service.call
end
end
context 'when source is photoprism_api' do
let(:import) { create(:import, source: 'photoprism_api') }
2025-08-22 13:10:40 -04:00
let(:file_path) { Rails.root.join('spec/fixtures/files/immich/geodata.json') }
before do
import.file.attach(io: File.open(file_path), filename: 'geodata.json',
content_type: 'application/json')
end
2024-12-04 07:33:15 -05:00
2025-04-23 17:36:16 -04:00
it 'calls the Photos::Importer' do
expect(Photos::Importer).to \
2025-08-22 13:10:40 -04:00
receive(:new).with(import, user.id, kind_of(String)).and_return(double(call: true))
2024-12-04 07:33:15 -05:00
service.call
end
end
end
end