From 7f93cf6220bf09fbdbc526eefbd2aeb553ccdb82 Mon Sep 17 00:00:00 2001 From: zmagod Date: Thu, 12 Apr 2018 13:00:21 +0200 Subject: [PATCH] adds specs for duplicate class --- .../repository_actions/duplicate_spec.rb | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 spec/services/repository_actions/duplicate_spec.rb diff --git a/spec/services/repository_actions/duplicate_spec.rb b/spec/services/repository_actions/duplicate_spec.rb new file mode 100644 index 000000000..daf4287db --- /dev/null +++ b/spec/services/repository_actions/duplicate_spec.rb @@ -0,0 +1,83 @@ +require 'rails_helper' + +describe RepositoryActions::Duplicate do + let!(:user) { create :user } + let!(:repository) { create :repository } + let!(:list_column) do + create(:repository_column, name: 'list', + repository: repository, + created_by: user, + data_type: 'RepositoryListValue') + end + let!(:text_column) do + create(:repository_column, name: 'text', + repository: repository, + created_by: user, + data_type: 'RepositoryTextValue') + end + + describe '#call' do + before do + @rows_ids = [] + + 3.times do |index| + row = create :repository_row, name: "row (#{index})", + repository: repository + create :repository_text_value, data: "text (#{index})", + repository_cell_attributes: { + repository_row: row, + repository_column: text_column + } + create :repository_list_value, + repository_list_item: create(:repository_list_item, + repository: repository, + repository_column: list_column, + data: "list item (#{index})"), + repository_cell_attributes: { + repository_row: row, + repository_column: list_column + } + @rows_ids << row.id.to_s + end + end + + it 'generates a copy of selected items' do + expect(repository.repository_rows.reload.size).to eq 3 + described_class.new(user, repository, @rows_ids).call + expect(repository.repository_rows.reload.size).to eq 6 + end + + it 'generates an exact copy of the row with custom column values' do + described_class.new(user, repository, [@rows_ids.first]).call + duplicated_row = repository.repository_rows.reload.last + expect(duplicated_row.name).to eq 'row (0) (1)' + duplicated_row.repository_cells.each do |cell| + if cell.value_type == 'RepositoryListValue' + expect(cell.value.data).to eq 'list item (0)' + else + expect(cell.value.data).to eq 'text (0)' + end + end + end + + it 'prevents to copy items that do not already belong to repository' do + new_repository = create :repository, name: 'new repo' + new_row = create :repository_row, name: 'other row', + repository: new_repository + described_class.new(user, repository, [new_row.id]).call + expect(repository.repository_rows.reload.size).to eq 3 + end + + it 'returns the number of copied items' do + service_obj = described_class.new(user, repository, @rows_ids) + service_obj.call + expect(service_obj.number_of_duplicated_items).to eq 3 + end + + it 'returns the number of copied items' do + service_obj = described_class.new(user, repository, []) + service_obj.call + expect(service_obj.number_of_duplicated_items).to eq 0 + end + end +end