2019-07-26 18:40:36 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-05 22:54:04 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
require 'zip'
|
|
|
|
|
2023-11-13 16:34:38 +08:00
|
|
|
describe RepositoryZipExportJob, type: :job do
|
2018-03-05 22:54:04 +08:00
|
|
|
let(:user) { create :user }
|
|
|
|
let(:team) { create :team, created_by: user }
|
2022-06-07 00:21:57 +08:00
|
|
|
let!(:owner_role) { UserRole.find_by(name: I18n.t('user_roles.predefined.owner')) }
|
|
|
|
let!(:team_assignment) { create_user_assignment(team, owner_role, user) }
|
2018-03-05 22:54:04 +08:00
|
|
|
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
|
2018-03-05 22:54:04 +08:00
|
|
|
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
|
2023-11-13 16:34:38 +08:00
|
|
|
|
|
|
|
ZipExport.skip_callback(:create, :after, :self_destruct)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
ZipExport.set_callback(:create, :after, :self_destruct)
|
2018-03-05 22:54:04 +08:00
|
|
|
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'],
|
2023-11-13 16:34:38 +08:00
|
|
|
repository_id: repository.id,
|
2018-03-13 18:24:16 +08:00
|
|
|
row_ids: @row_ids }
|
2018-03-05 22:54:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates a new zip export object' do
|
2023-11-13 16:34:38 +08:00
|
|
|
described_class.perform_now(user_id: user.id, params: params)
|
2018-03-05 22:54:04 +08:00
|
|
|
expect(ZipExport.count).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates a zip with csv file with exported rows' do
|
2023-11-13 16:34:38 +08:00
|
|
|
described_class.perform_now(user_id: user.id, params: params)
|
2018-11-07 13:14:40 +08:00
|
|
|
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|
|
2018-11-07 12:04:29 +08:00
|
|
|
csv_file = zip_file.glob('*.csv').first
|
|
|
|
csv_content = csv_file.get_input_stream.read
|
|
|
|
CSV.parse(csv_content, headers: true)
|
|
|
|
end
|
2018-03-05 22:54:04 +08:00
|
|
|
index = 0
|
2018-11-07 12:04:29 +08:00
|
|
|
|
|
|
|
parsed_csv_content.each do |row|
|
2018-03-05 22:54:04 +08:00
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|