mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-07 05:34:55 +08:00
adds specs for sample_to_repository migration service
This commit is contained in:
parent
e0ac8e8f91
commit
c53df21946
3 changed files with 321 additions and 106 deletions
|
@ -1,4 +1,5 @@
|
|||
module Tasks::SamplesToRepositoryMigrationService
|
||||
module Tasks
|
||||
module SamplesToRepositoryMigrationService
|
||||
def self.prepare_repository(team, copy_num = 0)
|
||||
repository = Repository.new(
|
||||
name: copy_num > 0 ? "Samples (#{copy_num})" : 'Samples',
|
||||
|
@ -85,7 +86,7 @@ module Tasks::SamplesToRepositoryMigrationService
|
|||
repository_columns
|
||||
end
|
||||
|
||||
def self.get_sample_fields(sample_id)
|
||||
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,
|
||||
|
@ -96,7 +97,7 @@ module Tasks::SamplesToRepositoryMigrationService
|
|||
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)
|
||||
ActiveRecord::Base.connection.execute(custom_sample_fields_sql).to_a
|
||||
end
|
||||
|
||||
def self.get_assigned_sample_module(sample_id)
|
||||
|
@ -105,6 +106,7 @@ module Tasks::SamplesToRepositoryMigrationService
|
|||
FROM sample_my_modules
|
||||
WHERE sample_my_modules.sample_id = #{sample_id}
|
||||
SQL
|
||||
ActiveRecord::Base.connection.execute(assigned_samples_sql)
|
||||
ActiveRecord::Base.connection.execute(assigned_samples_sql).to_a
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
5
spec/factories/sample_custom_field.rb
Normal file
5
spec/factories/sample_custom_field.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
FactoryBot.define do
|
||||
factory :sample_custom_field do
|
||||
value 'Sample 111'
|
||||
end
|
||||
end
|
|
@ -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)
|
||||
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
|
||||
|
||||
describe '#get_sample_fields/1' do
|
||||
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_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
|
||||
|
|
Loading…
Add table
Reference in a new issue