Browse Source

add mechanism to download bulk upload (#1072)

pull/1076/head
Phil Lee 2 years ago committed by GitHub
parent
commit
8c23fd4168
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      app/services/bulk_upload/downloader.rb
  2. 10
      spec/factories/bulk_upload.rb
  3. 31
      spec/services/bulk_upload/downloader_spec.rb

33
app/services/bulk_upload/downloader.rb

@ -0,0 +1,33 @@
class BulkUpload::Downloader
attr_reader :bulk_upload
delegate :path, to: :file
def initialize(bulk_upload:)
@bulk_upload = bulk_upload
end
def call
download
end
private
def download
io = storage_service.get_file_io(bulk_upload.identifier)
file.write(io.read)
io.close
file.close
end
def file
@file ||= Tempfile.new
end
def storage_service
@storage_service ||= Storage::S3Service.new(
Configuration::PaasConfigurationService.new,
ENV["CSV_DOWNLOAD_PAAS_INSTANCE"],
)
end
end

10
spec/factories/bulk_upload.rb

@ -0,0 +1,10 @@
require "securerandom"
FactoryBot.define do
factory :bulk_upload do
user
log_type { BulkUpload.log_types.values.sample }
year { 2022 }
identifier { SecureRandom.uuid }
end
end

31
spec/services/bulk_upload/downloader_spec.rb

@ -0,0 +1,31 @@
require "rails_helper"
RSpec.describe BulkUpload::Downloader do
subject(:downloader) { described_class.new(bulk_upload:) }
let(:bulk_upload) { build(:bulk_upload) }
let(:get_file_io) do
io = StringIO.new
io.write("hello")
io.rewind
io
end
# rubocop:disable RSpec/PredicateMatcher
describe "#call" do
let(:mock_storage_service) { instance_double(Storage::S3Service, get_file_io:) }
it "downloads the file as a temporary file" do
allow(Storage::S3Service).to receive(:new).and_return(mock_storage_service)
downloader.call
expect(mock_storage_service).to have_received(:get_file_io).with(bulk_upload.identifier)
expect(File.exist?(downloader.path)).to be_truthy
expect(File.read(downloader.path)).to eql("hello")
end
end
# rubocop:enable RSpec/PredicateMatcher
end
Loading…
Cancel
Save