diff --git a/app/services/tasks/samples_to_repository_migration_service.rb b/app/services/tasks/samples_to_repository_migration_service.rb index 3697ec5e6..6270bf296 100644 --- a/app/services/tasks/samples_to_repository_migration_service.rb +++ b/app/services/tasks/samples_to_repository_migration_service.rb @@ -1,110 +1,112 @@ -module Tasks::SamplesToRepositoryMigrationService - def self.prepare_repository(team, copy_num = 0) - repository = Repository.new( - name: copy_num > 0 ? "Samples (#{copy_num})" : 'Samples', - team: team, - created_by: team.created_by - ) - return repository if repository.save - prepare_repository(team, copy_num += 1) - end +module Tasks + module SamplesToRepositoryMigrationService + def self.prepare_repository(team, copy_num = 0) + repository = Repository.new( + name: copy_num > 0 ? "Samples (#{copy_num})" : 'Samples', + team: team, + created_by: team.created_by + ) + return repository if repository.save + prepare_repository(team, copy_num += 1) + end - def self.prepare_text_value_custom_columns(team, repository) - custom_columns_sql = <<-SQL - SELECT * FROM custom_fields WHERE team_id = #{team.id} - SQL - # execute query - custom_columns = ActiveRecord::Base.connection.execute(custom_columns_sql) + def self.prepare_text_value_custom_columns(team, repository) + custom_columns_sql = <<-SQL + SELECT * FROM custom_fields WHERE team_id = #{team.id} + SQL + # execute query + custom_columns = ActiveRecord::Base.connection.execute(custom_columns_sql) - repository_columns = [] - custom_columns.each_with_index do |column, index| - repository_column = RepositoryColumn.create( + repository_columns = [] + custom_columns.each_with_index do |column, index| + repository_column = RepositoryColumn.create( + repository: repository, + created_by_id: column.fetch('user_id') { repository.created_by_id }, + data_type: :RepositoryTextValue, + name: column.fetch('name') { "Custom column (#{index})" }, + created_at: column.fetch('created_at') { DateTime.now }, + updated_at: column.fetch('updated_at') { DateTime.now } + ) + repository_columns << repository_column + end + repository_columns + end + + def self.prepare_list_value_custom_columns_with_list_items(team, repository) + sample_types_sql = <<-SQL + SELECT name, created_by_id, last_modified_by_id + FROM sample_types + WHERE team_id = #{team.id} + SQL + sample_groups_sql = <<-SQL + SELECT name, created_by_id, last_modified_by_id + FROM sample_groups + WHERE team_id = #{team.id} + SQL + # execute query + sample_types = ActiveRecord::Base.connection.execute(sample_types_sql) + sample_groups = ActiveRecord::Base.connection.execute(sample_groups_sql) + + repository_columns = [] + + repository_columns << RepositoryColumn.create( repository: repository, - created_by_id: column.fetch('user_id') { repository.created_by_id }, - data_type: :RepositoryTextValue, - name: column.fetch('name') { "Custom column (#{index})" }, - created_at: column.fetch('created_at') { DateTime.now }, - updated_at: column.fetch('updated_at') { DateTime.now } + created_by_id: repository.created_by_id, + data_type: :RepositoryListValue, + name: 'Sample group' ) - repository_columns << repository_column - end - repository_columns - end - def self.prepare_list_value_custom_columns_with_list_items(team, repository) - sample_types_sql = <<-SQL - SELECT name, created_by_id, last_modified_by_id - FROM sample_types - WHERE team_id = #{team.id} - SQL - sample_groups_sql = <<-SQL - SELECT name, created_by_id, last_modified_by_id - FROM sample_groups - WHERE team_id = #{team.id} - SQL - # execute query - sample_types = ActiveRecord::Base.connection.execute(sample_types_sql) - sample_groups = ActiveRecord::Base.connection.execute(sample_groups_sql) - - repository_columns = [] - - repository_columns << RepositoryColumn.create( - repository: repository, - created_by_id: repository.created_by_id, - data_type: :RepositoryListValue, - name: 'Sample group' - ) - - repository_columns << RepositoryColumn.create( - repository: repository, - created_by_id: repository.created_by_id, - data_type: :RepositoryListValue, - name: 'Sample type' - ) - - sample_groups.each_with_index do |item, index| - RepositoryListItem.create( - data: item.fetch('name') { "sample group item (#{index})" }, - created_by_id: item.fetch('created_by_id') { team.created_by_id }, - last_modified_by_id: item.fetch('last_modified_by_id') { team.created_by_id }, - repository_column: repository_columns.first, - repository: repository + repository_columns << RepositoryColumn.create( + repository: repository, + created_by_id: repository.created_by_id, + data_type: :RepositoryListValue, + name: 'Sample type' ) + + sample_groups.each_with_index do |item, index| + RepositoryListItem.create( + data: item.fetch('name') { "sample group item (#{index})" }, + created_by_id: item.fetch('created_by_id') { team.created_by_id }, + last_modified_by_id: item.fetch('last_modified_by_id') { team.created_by_id }, + repository_column: repository_columns.first, + repository: repository + ) + end + + sample_types.each_with_index do |item, index| + RepositoryListItem.create( + data: item.fetch('name') { "sample group item (#{index})" }, + created_by_id: item.fetch('created_by_id') { team.created_by_id }, + last_modified_by_id: item.fetch('last_modified_by_id') { team.created_by_id }, + repository_column: repository_columns.last, + repository: repository + ) + end + + repository_columns end - sample_types.each_with_index do |item, index| - RepositoryListItem.create( - data: item.fetch('name') { "sample group item (#{index})" }, - created_by_id: item.fetch('created_by_id') { team.created_by_id }, - last_modified_by_id: item.fetch('last_modified_by_id') { team.created_by_id }, - repository_column: repository_columns.last, - repository: repository - ) + def self.get_sample_custom_fields(sample_id) + custom_sample_fields_sql = <<-SQL + SELECT custom_fields.name AS column_name_reference, + sample_custom_fields.value, + sample_custom_fields.created_at, + sample_custom_fields.updated_at + FROM custom_fields + INNER JOIN sample_custom_fields + ON custom_fields.id = sample_custom_fields.custom_field_id + WHERE sample_custom_fields.sample_id = #{sample_id} + SQL + ActiveRecord::Base.connection.execute(custom_sample_fields_sql).to_a end - repository_columns - end - - def self.get_sample_fields(sample_id) - custom_sample_fields_sql = <<-SQL - SELECT custom_fields.name AS column_name_reference, - sample_custom_fields.value, - sample_custom_fields.created_at, - sample_custom_fields.updated_at - FROM custom_fields - INNER JOIN sample_custom_fields - ON custom_fields.id = sample_custom_fields.custom_field_id - WHERE sample_custom_fields.sample_id = #{sample_id} - SQL - ActiveRecord::Base.connection.execute(custom_sample_fields_sql) - end - - def self.get_assigned_sample_module(sample_id) - assigned_samples_sql = <<-SQL - SELECT my_module_id, assigned_by_id, assigned_on - FROM sample_my_modules - WHERE sample_my_modules.sample_id = #{sample_id} - SQL - ActiveRecord::Base.connection.execute(assigned_samples_sql) + def self.get_assigned_sample_module(sample_id) + assigned_samples_sql = <<-SQL + SELECT my_module_id, assigned_by_id, assigned_on + FROM sample_my_modules + WHERE sample_my_modules.sample_id = #{sample_id} + SQL + ActiveRecord::Base.connection.execute(assigned_samples_sql).to_a + end end end diff --git a/spec/factories/sample_custom_field.rb b/spec/factories/sample_custom_field.rb new file mode 100644 index 000000000..c0bf9ddfa --- /dev/null +++ b/spec/factories/sample_custom_field.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :sample_custom_field do + value 'Sample 111' + end +end diff --git a/spec/services/tasks/samples_to_repository_migration_service_spec.rb b/spec/services/tasks/samples_to_repository_migration_service_spec.rb index 3718d001e..ad9241e0f 100644 --- a/spec/services/tasks/samples_to_repository_migration_service_spec.rb +++ b/spec/services/tasks/samples_to_repository_migration_service_spec.rb @@ -2,32 +2,240 @@ require 'rails_helper' describe Tasks::SamplesToRepositoryMigrationService do let(:user) { create :user, email: 'happy.user@scinote.net' } - let(:team) { create :team, created_by: :user } + let(:team) { create :team, created_by: user } let(:user_team) { create :user_team, user: user, team: team } - let(:sample) { create :sample, name: 'my sample', user: user, team: team } + let(:sample) { create :sample, name: 'My sample', user: user, team: team } describe '#prepare_repository/2' do - it 'creates and return a new custom repository named "Samples" for team' do - repository = SamplesToRepositoryMigrationService.prepare_repository(team) - expect(repository).to be_an_instance_of(Repository) - expect(repository.team).to eq team - expect(repository.name).to eq 'Samples' + context 'creates and return a new custom repository named' do + it '"Samples" for team' do + repository = Tasks::SamplesToRepositoryMigrationService + .prepare_repository(team) + expect(repository).to be_an_instance_of(Repository) + expect(repository.team).to eq team + expect(repository.name).to eq 'Samples' + end + + it '"Samples (1)" if repository name "Samples" already exists' do + create :repository, name: 'Samples', team: team, created_by: user + repository = Tasks::SamplesToRepositoryMigrationService + .prepare_repository(team) + expect(repository).to be_an_instance_of(Repository) + expect(repository.team).to eq team + expect(repository.name).to eq 'Samples (1)' + end end end describe '#prepare_text_value_custom_columns/2' do + let(:repository) do + create :repository, name: 'Samples', team: team, created_by: user + end + let(:subject) do + Tasks::SamplesToRepositoryMigrationService + .prepare_text_value_custom_columns(team, repository) + end + context 'custom columns exists' do + before do + create :samples_table, user: user, team: team + 10.times do |index| + create :custom_field, name: "My Custom field (#{index})", + user: user, + team: team, + last_modified_by: user + end + end + + it { is_expected.to be_an Array } + it { expect(subject.length).to eq 10 } + it { expect(subject.first.name).to eq 'My Custom field (0)' } + it { expect(subject.last.name).to eq 'My Custom field (9)' } + it { expect(subject.first).to be_an_instance_of RepositoryColumn } + it { expect(subject.first.data_type).to eq 'RepositoryTextValue' } + it { expect(subject.first.repository).to eq repository } + end + + context 'custom columns does not exists' do + it { is_expected.to be_an Array } + it { is_expected.to be_empty } + end end describe '#prepare_list_value_custom_columns_with_list_items/2' do + let(:repository) do + create :repository, name: 'Samples', team: team, created_by: user + end + let(:subject) do + Tasks::SamplesToRepositoryMigrationService + .prepare_list_value_custom_columns_with_list_items(team, repository) + end + context 'with samples types' do + before do + 10.times do |index| + create :sample_type, name: "Sample Type Item (#{index})", + team: team, + created_by: user, + last_modified_by: user + end + end + + it { is_expected.to be_an Array } + it { expect(subject.length).to eq 2 } + it { expect(subject.first).to be_an_instance_of(RepositoryColumn) } + it { expect(subject.first.name).to eq 'Sample group' } + it { expect(subject.first.data_type).to eq 'RepositoryListValue' } + it { expect(subject.last.name).to eq 'Sample type' } + it { expect(subject.last.data_type).to eq 'RepositoryListValue' } + + describe 'generated list items from sample types' do + let!(:generated_list_items) { subject.last.repository_list_items } + it { expect(generated_list_items.count).to eq 10 } + + it 'has generated list_items with similar properties' do + generated_list_items.each_with_index do |item, index| + expect(item.data).to eq "Sample Type Item (#{index})" + expect(item).to be_an_instance_of RepositoryListItem + expect(item.created_by).to eq user + expect(item.last_modified_by).to eq user + end + end + end + end + + context 'with samples groups' do + before do + 10.times do |index| + create :sample_group, name: "Sample Group Item (#{index})", + color: '#000000', + team: team, + created_by: user, + last_modified_by: user + end + end + + it { is_expected.to be_an Array } + it { expect(subject.length).to eq 2 } + it { expect(subject.first).to be_an_instance_of(RepositoryColumn) } + it { expect(subject.last).to be_an_instance_of(RepositoryColumn) } + it { expect(subject.first.name).to eq 'Sample group' } + it { expect(subject.first.data_type).to eq 'RepositoryListValue' } + it { expect(subject.last.name).to eq 'Sample type' } + it { expect(subject.last.data_type).to eq 'RepositoryListValue' } + + describe 'generated list items from sample groups' do + let!(:generated_list_items) { subject.first.repository_list_items } + it { expect(generated_list_items.count).to eq 10 } + + it 'has generated list_items with similar properties' do + generated_list_items.each_with_index do |item, index| + expect(item.data).to eq "Sample Group Item (#{index})" + expect(item).to be_an_instance_of RepositoryListItem + expect(item.created_by).to eq user + expect(item.last_modified_by).to eq user + end + end + end + end end - describe '#get_sample_fields/1' do + describe '#get_sample_custom_fields/1' do + let(:custom_field) do + create :custom_field, name: 'My Custom column', + user: user, + team: team, + last_modified_by: user + end + let(:subject) do + Tasks::SamplesToRepositoryMigrationService + .get_sample_custom_fields(sample.id) + end + context 'sample has custom column assigned' do + before do + create :samples_table, user: user, team: team + create :sample_custom_field, value: 'field value', + custom_field: custom_field, + sample: sample + end + + it 'returns a hash of sample values' do + element = subject.first + is_expected.to be_an Array + expect(subject.length).to eq 1 + expect(element.fetch('column_name_reference')).to eq 'My Custom column' + expect(element.fetch('value')).to eq 'field value' + expect(element.fetch('created_at')).to be_present + expect(element.fetch('updated_at')).to be_present + end + end + + context 'sample does not have custom columns assigned' do + it { is_expected.to be_an Array } + it { is_expected.to be_empty } + end end describe '#get_assigned_sample_module/1' do + let(:my_module) { create :my_module } + let(:subject) do + Tasks::SamplesToRepositoryMigrationService + .get_assigned_sample_module(sample.id) + end + context 'sample is assigned to one module' do + let!(:sample_my_module) do + create :sample_my_module, sample: sample, + my_module: my_module, + assigned_by: user + end + + it { is_expected.to be_an Array } + it { expect(subject.length).to eq 1 } + it 'returnes assigned my_module data' do + my_module_data = subject.first + expect( + my_module_data.fetch('my_module_id') + ).to eq sample_my_module.my_module_id + expect( + my_module_data.fetch('assigned_by_id') + ).to eq sample_my_module.assigned_by_id + expect( + my_module_data.fetch('assigned_on') + ).to eq sample_my_module.assigned_on + end + end + + context 'sample is not assigned to module' do + it { is_expected.to be_an Array } + it { is_expected.to be_empty } + end + + context 'sample is assigned to multiple modules' do + before do + @modules_ids = [] + 10.times do |index| + my_module = create :my_module, name: "My module (#{index})" + @modules_ids << my_module.id + end + 10.times do |index| + create :sample_my_module, + sample: sample, + my_module_id: @modules_ids[index], + assigned_by: user + end + end + + it { is_expected.to be_an Array } + it { expect(subject.length).to eq 10 } + + it 'is expected to return an array of samples_my_modules data' do + subject.each do |element| + expect(@modules_ids).to include(element.fetch('my_module_id').to_i) + expect(element.fetch('assigned_by_id')).to eq user.id + end + end + end end end