2024-06-12 14:29:38 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
|
|
RSpec.describe Exports::Create do
|
|
|
|
|
describe '#call' do
|
2024-09-02 15:45:27 -04:00
|
|
|
subject(:create_export) { described_class.new(export:, start_at:, end_at:, file_format:).call }
|
2024-06-12 14:29:38 -04:00
|
|
|
|
2024-09-02 15:45:27 -04:00
|
|
|
let(:file_format) { :json }
|
2024-09-02 15:35:08 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
let(:start_at) { DateTime.new(2021, 1, 1).to_s }
|
|
|
|
|
let(:end_at) { DateTime.new(2021, 1, 2).to_s }
|
2024-12-11 08:42:26 -05:00
|
|
|
let(:export_name) { "#{start_at.to_date}_#{end_at.to_date}.#{file_format}" }
|
2024-09-02 15:35:08 -04:00
|
|
|
let(:export) { create(:export, user:, name: export_name, status: :created) }
|
|
|
|
|
let(:export_content) { Points::GeojsonSerializer.new(points).call }
|
2024-12-02 08:44:22 -05:00
|
|
|
let(:reverse_geocoded_at) { Time.zone.local(2021, 1, 1) }
|
2024-09-28 10:43:36 -04:00
|
|
|
let!(:points) do
|
2024-12-02 08:44:22 -05:00
|
|
|
create_list(:point, 10, :with_known_location, user:, timestamp: start_at.to_datetime.to_i, reverse_geocoded_at:)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
allow_any_instance_of(Point).to receive(:reverse_geocoded_at).and_return(reverse_geocoded_at)
|
2024-09-28 10:43:36 -04:00
|
|
|
end
|
2024-06-12 14:29:38 -04:00
|
|
|
|
|
|
|
|
it 'writes the data to a file' do
|
|
|
|
|
create_export
|
|
|
|
|
|
2024-09-02 16:42:29 -04:00
|
|
|
file_path = Rails.root.join('spec/fixtures/files/geojson/export_same_points.json')
|
2024-06-12 14:29:38 -04:00
|
|
|
|
2024-09-28 10:43:36 -04:00
|
|
|
expect(File.read(file_path).strip).to eq(export_content)
|
2024-06-12 14:29:38 -04:00
|
|
|
end
|
|
|
|
|
|
2024-12-11 08:42:26 -05:00
|
|
|
it 'sets the export url' do
|
2024-06-12 14:29:38 -04:00
|
|
|
create_export
|
|
|
|
|
|
2024-12-11 08:42:26 -05:00
|
|
|
expect(export.reload.url).to eq("exports/#{export.name}")
|
2024-06-12 14:29:38 -04:00
|
|
|
end
|
|
|
|
|
|
2024-07-04 16:20:12 -04:00
|
|
|
it 'updates the export status to completed' do
|
|
|
|
|
create_export
|
|
|
|
|
|
|
|
|
|
expect(export.reload.completed?).to be_truthy
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'creates a notification' do
|
|
|
|
|
expect { create_export }.to change { Notification.count }.by(1)
|
|
|
|
|
end
|
|
|
|
|
|
2024-06-12 14:29:38 -04:00
|
|
|
context 'when an error occurs' do
|
|
|
|
|
before do
|
|
|
|
|
allow(File).to receive(:open).and_raise(StandardError)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'updates the export status to failed' do
|
|
|
|
|
create_export
|
|
|
|
|
|
|
|
|
|
expect(export.reload.failed?).to be_truthy
|
|
|
|
|
end
|
2024-07-04 16:20:12 -04:00
|
|
|
|
|
|
|
|
it 'creates a notification' do
|
|
|
|
|
expect { create_export }.to change { Notification.count }.by(1)
|
|
|
|
|
end
|
2024-06-12 14:29:38 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|