fix(ProcedureExportService.to_zip): prebatch dossiers only for to_csv,to_xlsx,to_ods. also add spec around to_zip

This commit is contained in:
Martin 2022-04-21 17:02:23 +02:00 committed by mfo
parent a3c5bf58ac
commit 79c473ecf9
2 changed files with 40 additions and 6 deletions

View file

@ -3,16 +3,18 @@ class ProcedureExportService
def initialize(procedure, dossiers)
@procedure = procedure
@dossiers = dossiers.downloadable_sorted_batch
@dossiers = dossiers
@tables = [:dossiers, :etablissements, :avis] + champs_repetables_options
end
def to_csv
@dossiers = @dossiers.downloadable_sorted_batch
io = StringIO.new(SpreadsheetArchitect.to_csv(options_for(:dossiers, :csv)))
create_blob(io, :csv)
end
def to_xlsx
@dossiers = @dossiers.downloadable_sorted_batch
# We recursively build multi page spreadsheet
io = @tables.reduce(nil) do |package, table|
SpreadsheetArchitect.to_axlsx_package(options_for(table, :xlsx), package)
@ -21,6 +23,7 @@ class ProcedureExportService
end
def to_ods
@dossiers = @dossiers.downloadable_sorted_batch
# We recursively build multi page spreadsheet
io = StringIO.new(@tables.reduce(nil) do |spreadsheet, table|
SpreadsheetArchitect.to_rodf_spreadsheet(options_for(table, :ods), spreadsheet)

View file

@ -410,13 +410,44 @@ describe ProcedureExportService do
end
describe 'to_zip' do
subject do
service
.to_zip
subject { service.to_zip }
context 'without files' do
it 'does not raises in_batches' do
expect { subject }.not_to raise_error(NoMethodError)
end
it 'returns an empty blob' do
expect(subject).to be_an_instance_of(ActiveStorage::Blob)
end
end
it 'does not raises in_batches' do
expect(subject).not_to raise_error(NoMethodError)
context 'with files (and http calls)' do
let!(:dossier) { create(:dossier, :accepte, :with_populated_champs, :with_individual, procedure: procedure) }
before do
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
end
it 'returns a blob with valid files' do
VCR.use_cassette('archive/new_file_to_get_200') do
subject
File.write('tmp.zip', subject.download, mode: 'wb')
File.open('tmp.zip') do |fd|
files = ZipTricks::FileReader.read_zip_structure(io: fd)
structure = [
"#{service.send(:base_filename)}/",
"#{service.send(:base_filename)}/dossier-#{dossier.id}/",
"#{service.send(:base_filename)}/dossier-#{dossier.id}/pieces_justificatives/",
"#{service.send(:base_filename)}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(ActiveStorage::Attachment.where(record_type: "Champ").first)}",
"#{service.send(:base_filename)}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(PiecesJustificativesService.generate_dossier_export(dossier))}"
]
expect(files.size).to eq(structure.size)
expect(files.map(&:filename)).to match_array(structure)
end
FileUtils.remove_entry_secure('tmp.zip')
end
end
end
end
end