scinote-web/spec/services/repository_zip_export_spec.rb

91 lines
3.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'rails_helper'
require 'zip'
describe RepositoryZipExport, type: :background_job do
let(:user) { create :user }
let(:team) { create :team, created_by: user }
let(:user_team) { create :user_team, user: user, team: team }
let(:repository) { create :repository, team: team, created_by: user }
let!(:sample_group_column) do
create :repository_column, repository: repository,
created_by: user,
name: 'Sample group',
data_type: 'RepositoryListValue'
end
let!(:repository_list_item) do
2020-04-09 23:11:58 +08:00
create :repository_list_item, data: 'item one', repository_column: sample_group_column
end
let!(:custom_column) do
create :repository_column, repository: repository,
created_by: user,
name: 'Custom items',
data_type: 'RepositoryTextValue'
end
before do
@row_ids = []
10.times do |index|
row = create :repository_row, name: "row #{index}",
repository: repository,
created_by: user,
last_modified_by: user
create :repository_list_value, repository_list_item: repository_list_item,
repository_cell_attributes: {
repository_row: row,
repository_column: sample_group_column
}
create :repository_text_value, data: 'custum column value',
repository_cell_attributes: {
repository_row: row,
repository_column: custom_column
}
@row_ids << row.id.to_s
end
end
describe '#generate_zip/2' do
let(:params) do
2018-03-13 18:24:16 +08:00
{ header_ids: ['-1',
'-2',
sample_group_column.id.to_s,
custom_column.id.to_s,
'-3',
'-5',
'-4'],
row_ids: @row_ids }
end
it 'generates a new zip export object' do
2019-05-07 22:42:06 +08:00
ZipExport.skip_callback(:create, :after, :self_destruct)
RepositoryZipExport.generate_zip(params, repository, user)
expect(ZipExport.count).to eq 1
2019-05-07 22:42:06 +08:00
ZipExport.set_callback(:create, :after, :self_destruct)
end
it 'generates a zip with csv file with exported rows' do
2019-05-07 22:42:06 +08:00
ZipExport.skip_callback(:create, :after, :self_destruct)
RepositoryZipExport.generate_zip(params, repository, user)
csv_zip_file = ZipExport.first.zip_file
2019-07-26 20:16:44 +08:00
file_path = ActiveStorage::Blob.service.public_send(:path_for, csv_zip_file.key)
parsed_csv_content = Zip::File.open(file_path) do |zip_file|
csv_file = zip_file.glob('*.csv').first
csv_content = csv_file.get_input_stream.read
CSV.parse(csv_content, headers: true)
end
index = 0
parsed_csv_content.each do |row|
row_hash = row.to_h
expect(row_hash.fetch('Sample group')).to eq 'item one'
expect(row_hash.fetch('Custom items')).to eq 'custum column value'
expect(row_hash.fetch('Name')).to eq "row #{index}"
index += 1
end
2019-05-07 22:42:06 +08:00
ZipExport.set_callback(:create, :after, :self_destruct)
end
end
end