diff --git a/lib/export/export_collections.rb b/lib/export/export_collections.rb index 2a34487980..7e18b8fc30 100644 --- a/lib/export/export_collections.rb +++ b/lib/export/export_collections.rb @@ -125,6 +125,7 @@ def prepare_data 'user_id' => 'User' }) fetch_samples collection + fetch_chemicals collection fetch_reactions collection # fetch_elements collection if @gt == false fetch_wellplates collection if @gt == false @@ -135,6 +136,14 @@ def prepare_data private + def fetch_chemicals(collection) + chemicals = collection.samples.filter_map(&:chemical) + fetch_many(chemicals, { + 'id' => 'Chemical', + 'sample_id' => 'Sample', + }) + end + def fetch_samples(collection) # get samples in order of ancestry, but with empty ancestry first samples = collection.samples.order(Arel.sql("NULLIF(ancestry, '') ASC NULLS FIRST")) diff --git a/lib/import/import_collections.rb b/lib/import/import_collections.rb index 4813b1f336..d7e9edbc6f 100644 --- a/lib/import/import_collections.rb +++ b/lib/import/import_collections.rb @@ -88,6 +88,7 @@ def import gate_collection if @gt == true import_collections if @gt == false import_samples + import_chemicals if @gt == false import_residues import_reactions import_reactions_samples @@ -186,6 +187,21 @@ def fetch_bound(value) end end + def import_chemicals + @data.fetch('Chemical', {}).each do |_uuid, fields| + sample = @instances.fetch('Sample').fetch(fields.fetch('sample_id')) + next unless sample + + chemical = Chemical.create!(fields.slice( + 'cas', + 'chemical_data', + ).merge( + sample_id: sample&.id, + )) + chemical.save! + end + end + def import_samples @data.fetch('Sample', {}).each do |uuid, fields| # look for the molecule_name @@ -241,6 +257,7 @@ def import_samples 'decoupled', 'molecular_mass', 'sum_formula', + 'inventory_sample', ).merge( created_by: @current_user_id, collections: fetch_many( diff --git a/spec/factories/attachments.rb b/spec/factories/attachments.rb index 8615deb3c3..d6c397b5b9 100644 --- a/spec/factories/attachments.rb +++ b/spec/factories/attachments.rb @@ -99,5 +99,9 @@ trait :with_researchplan_collection_zip do file_path { Rails.root.join('spec/fixtures/import/20230113_research_plan_one_attachment.zip') } end + + trait :with_chemicals_collection_zip do + file_path { Rails.root.join('spec/fixtures/import/collection_chemicals.zip') } + end end end diff --git a/spec/fixtures/import/collection_chemicals.zip b/spec/fixtures/import/collection_chemicals.zip new file mode 100644 index 0000000000..ff7ccf4055 Binary files /dev/null and b/spec/fixtures/import/collection_chemicals.zip differ diff --git a/spec/lib/export/export_collections_spec.rb b/spec/lib/export/export_collections_spec.rb index 0f7275ff7c..04484fa811 100644 --- a/spec/lib/export/export_collections_spec.rb +++ b/spec/lib/export/export_collections_spec.rb @@ -78,6 +78,32 @@ end end + context 'with a chemical' do + let(:chemical) { create(:chemical, sample_id: sample.id) } + + before do + sample.save! + chemical.save! + export = Export::ExportCollections.new(job_id, [collection.id], 'zip', true) + export.prepare_data + export.to_file + end + + it 'exported file exists' do + file_path = File.join('public', 'zip', "#{job_id}.zip") + expect(File.exist?(file_path)).to be true + end + + it 'Chemical key is present in export.json' do + file_path = File.join('public', 'zip', "#{job_id}.zip") + export_json_content = Zip::File.open(file_path) do |files| + json_file = files.detect { |file| file.name == 'export.json' } + JSON.parse(json_file.get_input_stream.read) + end + expect(export_json_content).to have_key('Chemical') + end + end + def update_body_of_researchplan(research_plan, identifier_of_attachment) # rubocop:disable Metrics/MethodLength research_plan.body = [ { diff --git a/spec/lib/import/import_collections_spec.rb b/spec/lib/import/import_collections_spec.rb index 29e99492a4..5ebb816a4c 100644 --- a/spec/lib/import/import_collections_spec.rb +++ b/spec/lib/import/import_collections_spec.rb @@ -171,6 +171,19 @@ expect(attachment.attachment_data).not_to be_nil end end + + describe 'import a collection with a chemical' do + let(:import_id) { 'collection_chemicals' } + let(:attachment) { create(:attachment, :with_chemicals_collection_zip) } + + it 'successfully imported 1 chemical' do + importer.execute + + collection = Collection.find_by(label: 'collection_with_chemical') + expect(collection).to be_present + expect(collection.samples.map(&:chemical).length).to eq(1) + end + end end def stub_rest_request(identifier)