Fix tests [SCI-10883]

This commit is contained in:
Andrej 2024-07-11 16:00:22 +02:00
parent 34e9c9f4e8
commit 47bec33647
4 changed files with 73 additions and 36 deletions

View file

@ -98,25 +98,50 @@ describe RepositoriesController, type: :controller do
repository_row: repository_row, repository_row: repository_row,
repository_column: repository_column repository_column: repository_column
end end
let(:params) do let(:params_csv) do
{ {
id: repository.id, id: repository.id,
header_ids: [repository_column.id], header_ids: [repository_column.id],
row_ids: [repository_row.id] row_ids: [repository_row.id],
file_type: :csv
} }
end end
let(:action) { post :export_repository, params: params, format: :json }
it 'calls create activity for exporting inventory items' do let(:params_xlsx) do
{
id: repository.id,
header_ids: [repository_column.id],
row_ids: [repository_row.id],
file_type: :xlsx
}
end
let(:action_csv) { post :export_repository, params: params_csv, format: :json }
let(:action_xlsx) { post :export_repository, params: params_xlsx, format: :json }
it 'calls create activity for exporting inventory items csv' do
expect(Activities::CreateActivityService) expect(Activities::CreateActivityService)
.to(receive(:call) .to(receive(:call)
.with(hash_including(activity_type: :export_inventory_items))) .with(hash_including(activity_type: :export_inventory_items)))
action action_csv
end end
it 'adds activity in DB' do it 'adds activity in DB for exporting csv' do
expect { action } expect { action_csv }
.to(change { Activity.count })
end
it 'calls create activity for exporting inventory items xlsx' do
expect(Activities::CreateActivityService)
.to(receive(:call)
.with(hash_including(activity_type: :export_inventory_items)))
action_xlsx
end
it 'adds activity in DB for exporting xlsx' do
expect { action_xlsx }
.to(change { Activity.count }) .to(change { Activity.count })
end end
end end
@ -136,17 +161,18 @@ describe RepositoriesController, type: :controller do
it 'calls create activity for importing inventory items' do it 'calls create activity for importing inventory items' do
allow_any_instance_of(ImportRepository::ImportRecords) allow_any_instance_of(ImportRepository::ImportRecords)
.to receive(:import!).and_return(status: :ok) .to receive(:import!).and_return({ status: :ok, created_rows_count: 1, updated_rows_count: 1 })
expect(Activities::CreateActivityService) expect(Activities::CreateActivityService)
.to(receive(:call) .to(receive(:call)
.with(hash_including(activity_type: :import_inventory_items))) .with(hash_including(activity_type: :inventory_items_added_or_updated_with_import)))
action action
end end
it 'adds activity in DB' do it 'adds activity in DB' do
allow_any_instance_of(ImportRepository::ImportRecords).to receive(:import!).and_return(status: :ok) allow_any_instance_of(ImportRepository::ImportRecords).to receive(:import!)
.and_return({ status: :ok, created_rows_count: 1, updated_rows_count: 1 })
expect { action } expect { action }
.to(change { Activity.count }) .to(change { Activity.count })

View file

@ -70,15 +70,15 @@ describe RepositoryTableStateColumnUpdateService do
end end
it 'should calculate correct length' do it 'should calculate correct length' do
expect(initial_state_1.state['columns'].length).to eq 11 expect(initial_state_1.state['columns'].length).to eq 13
expect(initial_state_2.state['columns'].length).to eq 11 expect(initial_state_2.state['columns'].length).to eq 13
service.update_states_with_new_column(repository) service.update_states_with_new_column(repository)
service.update_states_with_new_column(repository) service.update_states_with_new_column(repository)
[user_1, user_2].each do |user| [user_1, user_2].each do |user|
state = RepositoryTableStateService.new(user, repository).load_state state = RepositoryTableStateService.new(user, repository).load_state
expect(state.state['columns'].length).to eq 11 expect(state.state['columns'].length).to eq 13
end end
end end
@ -125,11 +125,11 @@ describe RepositoryTableStateColumnUpdateService do
end end
it 'should keep column order as it was' do it 'should keep column order as it was' do
initial_state_1.state['ColReorder'] = [5, 3, 2, 0, 1, 4, 6, 7, 8, 9, 10] initial_state_1.state['ColReorder'] = [5, 3, 2, 0, 1, 4, 6, 7, 8, 9, 10, 11, 12]
RepositoryTableStateService.new(user_1, repository).update_state( RepositoryTableStateService.new(user_1, repository).update_state(
initial_state_1.state initial_state_1.state
) )
initial_state_2.state['ColReorder'] = [0, 6, 1, 4, 5, 7, 2, 3, 8, 9, 10] initial_state_2.state['ColReorder'] = [0, 6, 1, 4, 5, 7, 2, 3, 8, 9, 10, 11, 12]
RepositoryTableStateService.new(user_2, repository).update_state( RepositoryTableStateService.new(user_2, repository).update_state(
initial_state_2.state initial_state_2.state
) )
@ -138,9 +138,9 @@ describe RepositoryTableStateColumnUpdateService do
create :repository_column, name: 'My column 4', repository: repository, data_type: :RepositoryTextValue create :repository_column, name: 'My column 4', repository: repository, data_type: :RepositoryTextValue
state_1 = RepositoryTableStateService.new(user_1, repository).load_state state_1 = RepositoryTableStateService.new(user_1, repository).load_state
expect(state_1.state['ColReorder']).to eq([5, 3, 2, 0, 1, 4, 6, 7, 8, 9, 10, 11, 12]) expect(state_1.state['ColReorder']).to eq([5, 3, 2, 0, 1, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14])
state_2 = RepositoryTableStateService.new(user_2, repository).load_state state_2 = RepositoryTableStateService.new(user_2, repository).load_state
expect(state_2.state['ColReorder']).to eq([0, 6, 1, 4, 5, 7, 2, 3, 8, 9, 10, 11, 12]) expect(state_2.state['ColReorder']).to eq([0, 6, 1, 4, 5, 7, 2, 3, 8, 9, 10, 11, 12, 13, 14])
end end
end end
@ -160,8 +160,8 @@ describe RepositoryTableStateColumnUpdateService do
expect(initial_state_1).to be_valid_default_repository_table_state(2) expect(initial_state_1).to be_valid_default_repository_table_state(2)
expect(initial_state_2).to be_valid_default_repository_table_state(2) expect(initial_state_2).to be_valid_default_repository_table_state(2)
service.update_states_with_removed_column(repository, 9) service.update_states_with_removed_column(repository, 11)
service.update_states_with_removed_column(repository, 9) service.update_states_with_removed_column(repository, 11)
[user_1, user_2].each do |user| [user_1, user_2].each do |user|
state = RepositoryTableStateService.new(user, repository).load_state state = RepositoryTableStateService.new(user, repository).load_state
@ -170,15 +170,15 @@ describe RepositoryTableStateColumnUpdateService do
end end
it 'should calculate correct length' do it 'should calculate correct length' do
expect(initial_state_1.state['columns'].length).to eq 11 expect(initial_state_1.state['columns'].length).to eq 13
expect(initial_state_2.state['columns'].length).to eq 11 expect(initial_state_2.state['columns'].length).to eq 13
service.update_states_with_removed_column(repository, 8) service.update_states_with_removed_column(repository, 8)
service.update_states_with_removed_column(repository, 8) service.update_states_with_removed_column(repository, 8)
[user_1, user_2].each do |user| [user_1, user_2].each do |user|
state = RepositoryTableStateService.new(user, repository).load_state state = RepositoryTableStateService.new(user, repository).load_state
expect(state.state['columns'].length).to eq 9 expect(state.state['columns'].length).to eq 11
end end
end end
@ -289,12 +289,12 @@ describe RepositoryTableStateColumnUpdateService do
end end
let!(:initial_state) do let!(:initial_state) do
state = RepositoryTableStateService.new(user_1, repository).create_default_state state = RepositoryTableStateService.new(user_1, repository).create_default_state
state.state['order'] = [[11, 'desc']] state.state['order'] = [[13, 'desc']]
(0..9).each do |idx| (0..9).each do |idx|
state.state['columns'][idx]['search']['search'] = "search_#{idx}" state.state['columns'][idx]['search']['search'] = "search_#{idx}"
end end
state.state['ColReorder'] = state.state['ColReorder'] =
[0, 1, 2, 9, 8, 4, 7, 3, 5, 6, 10, 11, 12] [0, 1, 2, 11, 9, 8, 4, 7, 3, 5, 6, 10, 12, 13, 14]
RepositoryTableStateService.new(user_1, repository).update_state( RepositoryTableStateService.new(user_1, repository).update_state(
state.state state.state
) )
@ -309,7 +309,7 @@ describe RepositoryTableStateColumnUpdateService do
state = RepositoryTableStateService.new(user_1, repository).load_state state = RepositoryTableStateService.new(user_1, repository).load_state
expect(state).to be_valid_repository_table_state(5) expect(state).to be_valid_repository_table_state(5)
expect(state.state['ColReorder']).to eq( expect(state.state['ColReorder']).to eq(
[0, 1, 2, 9, 8, 4, 7, 3, 5, 6, 10, 11, 12, 13] [0, 1, 2, 11, 9, 8, 4, 7, 3, 5, 6, 10, 12, 13, 14, 15]
) )
repository.repository_columns.order(id: :asc).first.destroy! repository.repository_columns.order(id: :asc).first.destroy!
@ -317,25 +317,25 @@ describe RepositoryTableStateColumnUpdateService do
state = RepositoryTableStateService.new(user_1, repository).load_state state = RepositoryTableStateService.new(user_1, repository).load_state
expect(state).to be_valid_repository_table_state(4) expect(state).to be_valid_repository_table_state(4)
expect(state.state['ColReorder']).to eq( expect(state.state['ColReorder']).to eq(
[0, 1, 2, 8, 4, 7, 3, 5, 6, 9, 10, 11, 12] [0, 1, 2, 9, 8, 4, 7, 3, 5, 6, 10, 11, 12, 13, 14]
) )
expect(state.state['order']).to eq([[10, 'desc']]) expect(state.state['order']).to eq([[12, 'desc']])
repository.repository_columns.order(id: :asc).first.destroy! repository.repository_columns.order(id: :asc).first.destroy!
state = RepositoryTableStateService.new(user_1, repository).load_state state = RepositoryTableStateService.new(user_1, repository).load_state
expect(state).to be_valid_repository_table_state(3) expect(state).to be_valid_repository_table_state(3)
expect(state.state['ColReorder']).to eq( expect(state.state['ColReorder']).to eq(
[0, 1, 2, 8, 4, 7, 3, 5, 6, 9, 10, 11] [0, 1, 2, 9, 8, 4, 7, 3, 5, 6, 10, 11, 12, 13]
) )
expect(state.state['order']).to eq([[9, 'desc']]) expect(state.state['order']).to eq([[11, 'desc']])
repository.repository_columns.order(id: :asc).first.destroy! repository.repository_columns.order(id: :asc).first.destroy!
state = RepositoryTableStateService.new(user_1, repository).load_state state = RepositoryTableStateService.new(user_1, repository).load_state
expect(state).to be_valid_repository_table_state(2) expect(state).to be_valid_repository_table_state(2)
expect(state.state['ColReorder']).to eq( expect(state.state['ColReorder']).to eq(
[0, 1, 2, 8, 4, 7, 3, 5, 6, 9, 10] [0, 1, 2, 9, 8, 4, 7, 3, 5, 6, 10, 11, 12]
) )
create :repository_column, name: 'My column 1', repository: repository, data_type: :RepositoryTextValue create :repository_column, name: 'My column 1', repository: repository, data_type: :RepositoryTextValue
@ -344,7 +344,7 @@ describe RepositoryTableStateColumnUpdateService do
state = RepositoryTableStateService.new(user_1, repository).load_state state = RepositoryTableStateService.new(user_1, repository).load_state
expect(state).to be_valid_repository_table_state(4) expect(state).to be_valid_repository_table_state(4)
expect(state.state['ColReorder']).to eq( expect(state.state['ColReorder']).to eq(
[0, 1, 2, 8, 4, 7, 3, 5, 6, 9, 10, 11, 12] [0, 1, 2, 9, 8, 4, 7, 3, 5, 6, 10, 11, 12, 13, 14]
) )
end end
end end

View file

@ -7,8 +7,8 @@ RSpec::Matchers.define :be_valid_default_repository_table_state do |nr_of_cols|
state = subject.state state = subject.state
cols_length = 9 + nr_of_cols cols_length = 11 + nr_of_cols
cols_array = [*0..(8 + nr_of_cols)] cols_array = [*0..(10 + nr_of_cols)]
expect(state).to be_an_instance_of Hash expect(state).to be_an_instance_of Hash
expect(state).to include( expect(state).to include(
@ -25,7 +25,7 @@ RSpec::Matchers.define :be_valid_default_repository_table_state do |nr_of_cols|
expect(state['columns'].length).to eq(cols_length) expect(state['columns'].length).to eq(cols_length)
state['columns'].each_with_index do |val, i| state['columns'].each_with_index do |val, i|
expect(val).to include( expect(val).to include(
'visible' => !([4, 7, 8].include? i), 'visible' => !([4, 7, 8, 9, 10].include? i),
'searchable' => (![0, 4].include?(i)), 'searchable' => (![0, 4].include?(i)),
'search' => { 'search' => {
'search' => '', 'smart' => true, 'regex' => false, 'caseInsensitive' => true 'search' => '', 'smart' => true, 'regex' => false, 'caseInsensitive' => true

View file

@ -39,11 +39,22 @@ describe RepositoryImportParser::Importer do
describe '#run/0' do describe '#run/0' do
let(:subject) do let(:subject) do
RepositoryImportParser::Importer.new(sheet, mappings, user, repository) RepositoryImportParser::Importer.new(sheet, mappings, user, repository, true, true, false)
end end
it 'return a message of imported records' do it 'return a message of imported records' do
expect(subject.run).to eq({ status: :ok, created_rows_count: 5, updated_rows_count: 0 }) response = subject.run
expect(response.keys).to include(:status, :total_rows_count, :created_rows_count,
:updated_rows_count, :changes, :import_date)
expect(response).to include(
total_rows_count: 5,
created_rows_count: 5,
updated_rows_count: 0,
status: :ok
)
expect(response[:changes][:data].count).to eq 5
end end
it 'generate 5 new repository rows' do it 'generate 5 new repository rows' do