Skip to content

Commit

Permalink
feat: include chemicals with import and export of collections (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambasha0 authored Nov 16, 2023
1 parent d6c6c96 commit a31a0ff
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/export/export_collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
Expand Down
17 changes: 17 additions & 0 deletions lib/import/import_collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -241,6 +257,7 @@ def import_samples
'decoupled',
'molecular_mass',
'sum_formula',
'inventory_sample',
).merge(
created_by: @current_user_id,
collections: fetch_many(
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/attachments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file added spec/fixtures/import/collection_chemicals.zip
Binary file not shown.
26 changes: 26 additions & 0 deletions spec/lib/export/export_collections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/import/import_collections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a31a0ff

Please sign in to comment.