dawarich/spec/services/imports/create_spec.rb

138 lines
4.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
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
it 'calls the GoogleMaps::SemanticHistoryParser' do
2024-12-06 10:52:36 -05:00
expect(GoogleMaps::SemanticHistoryParser).to \
receive(:new).with(import, user.id).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') }
it 'calls the GoogleMaps::PhoneTakeoutParser' do
2024-12-06 10:52:36 -05:00
expect(GoogleMaps::PhoneTakeoutParser).to \
receive(:new).with(import, user.id).and_return(double(call: true))
2024-12-04 07:33:15 -05:00
service.call
end
end
context 'when source is owntracks' do
let(:import) { create(:import, source: 'owntracks') }
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 \
2024-12-06 10:52:36 -05:00
receive(:new).with(import, user.id).and_return(double(call: true))
2024-12-04 07:33:15 -05:00
service.call
end
context 'when import is successful' do
it 'creates a finished notification' do
service.call
expect(user.notifications.last.kind).to eq('info')
end
it 'schedules stats creating' do
Sidekiq::Testing.inline! do
2024-12-06 10:52:36 -05:00
expect { service.call }.to \
have_enqueued_job(Stats::CalculatingJob).with(user.id, 2024, 3)
2024-12-04 07:33:15 -05:00
end
end
it 'schedules visit suggesting' do
2024-12-04 07:33:15 -05:00
Sidekiq::Testing.inline! do
expect { service.call }.to have_enqueued_job(VisitSuggestingJob)
end
end
end
context 'when import fails' do
before do
2025-02-22 17:14:23 -05:00
allow(OwnTracks::Importer).to receive(:new).with(import, user.id).and_raise(StandardError)
2024-12-04 07:33:15 -05:00
end
it 'creates a failed notification' do
service.call
expect(user.notifications.last.kind).to eq('error')
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 \
2024-12-06 10:52:36 -05:00
receive(:new).with(import, user.id).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') }
it 'calls the Geojson::ImportParser' do
2024-12-06 10:52:36 -05:00
expect(Geojson::ImportParser).to \
receive(:new).with(import, user.id).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') }
it 'calls the Photos::ImportParser' do
2024-12-06 10:52:36 -05:00
expect(Photos::ImportParser).to \
receive(:new).with(import, user.id).and_return(double(call: true))
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') }
it 'calls the Photos::ImportParser' do
2024-12-06 10:52:36 -05:00
expect(Photos::ImportParser).to \
receive(:new).with(import, user.id).and_return(double(call: true))
2024-12-04 07:33:15 -05:00
service.call
end
end
end
end