Skip to content

Commit

Permalink
Fix functional tests about the isa json exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
kdp-cloud committed Jan 18, 2024
1 parent bc6cb12 commit 0dc8b6c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
11 changes: 8 additions & 3 deletions lib/isa_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,14 @@ def convert_assays(assay_stream)
return unless assay_stream.can_view?(@current_user)
return unless child_assays.all? { |a| a.can_view?(@current_user) }

child_assays.map do |ca|
unless ca.sample_type.present?
raise "No Sample type was found in Assay '#{ca.id} - #{ca.title}'," \
" part of Assay Stream '#{assay_stream.id - assay_stream.title}'"
end
end

all_sample_types = child_assays.map(&:sample_type).compact
# first_assay = assays.detect { |s| s.position.zero? }
# raise 'No assay could be found!' unless first_assay

stream_name = "assays_#{child_assays.pluck(:id).join('_')}"
assay_comments = convert_assay_comments(assay_stream)
Expand Down Expand Up @@ -745,7 +750,7 @@ def random_string(len)
end

def get_derived_from_type(sample_type)
raise 'There is no sample!' if sample_type.samples.length == 0
raise "There are no samples in '#{sample_type.title}'!" if sample_type.samples.blank?

prev_sample_type = sample_type.samples[0]&.linked_samples[0]&.sample_type
return nil if prev_sample_type.blank?
Expand Down
4 changes: 2 additions & 2 deletions test/factories/assays.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
end

factory(:assay_stream_class, class: AssayClass) do
title { 'Assay Stream' }
title { I18n.t('assays.assay_stream') }
key { 'ASS' }
description { "An assay stream class description" }
end
Expand Down Expand Up @@ -115,7 +115,7 @@
description { 'A holder assay holding multiple child assays' }
association :assay_class, factory: :assay_stream_class
after(:build) do |assay|
assay.study = FactoryBot.create(:isa_json_compliant_study)
assay.study ||= FactoryBot.create(:isa_json_compliant_study, contributor: assay.contributor)
end
end

Expand Down
29 changes: 20 additions & 9 deletions test/functional/investigations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -911,16 +911,22 @@ def test_title
contributor: other_user.person)

# Create a 'private' assay in an assay stream
assay_1_stream_1_sample_type = FactoryBot.create(:isa_assay_material_sample_type, linked_sample_type: sample_collection_sample_type, template_id: FactoryBot.create(:isa_assay_material_template).id)
assay_1_stream_1 = FactoryBot.create(:assay, position: 0, sample_type: assay_1_stream_1_sample_type, study: accessible_study, contributor: current_user.person)
assay_2_stream_1_sample_type = FactoryBot.create(:isa_assay_data_file_sample_type, linked_sample_type: assay_1_stream_1_sample_type, template_id: FactoryBot.create(:isa_assay_data_file_template).id)
assay_2_stream_1 = FactoryBot.create(:assay, position:1, sample_type: assay_2_stream_1_sample_type, study: accessible_study, contributor: other_user.person)
stream_1 = FactoryBot.create(:assay_stream, title: 'Assay Stream 1', study: accessible_study, contributor: other_user.person)
assert_equal(stream_1.study, accessible_study)
assert(stream_1.is_assay_stream?)
assay_1_stream_1_sample_type = FactoryBot.create(:isa_assay_material_sample_type, contributor: other_user.person, linked_sample_type: sample_collection_sample_type, template_id: FactoryBot.create(:isa_assay_material_template).id)
assay_1_stream_1 = FactoryBot.create(:assay, position: 1, sample_type: assay_1_stream_1_sample_type, study: accessible_study, contributor: other_user.person, assay_stream_id: stream_1.id)
assay_2_stream_1_sample_type = FactoryBot.create(:isa_assay_data_file_sample_type, contributor: other_user.person, linked_sample_type: assay_1_stream_1_sample_type, template_id: FactoryBot.create(:isa_assay_data_file_template).id)
assay_2_stream_1 = FactoryBot.create(:assay, position: 2, sample_type: assay_2_stream_1_sample_type, study: accessible_study, contributor: other_user.person, assay_stream_id: stream_1.id)

# Create an assay stream with all assays visible
assay_1_stream_2_sample_type = FactoryBot.create(:isa_assay_material_sample_type, linked_sample_type: sample_collection_sample_type, template_id: FactoryBot.create(:isa_assay_material_template).id)
assay_1_stream_2 = FactoryBot.create(:assay, position: 0, sample_type: assay_1_stream_2_sample_type, study: accessible_study, contributor: current_user.person)
assay_2_stream_2_sample_type = FactoryBot.create(:isa_assay_data_file_sample_type, linked_sample_type: assay_1_stream_2_sample_type, template_id: FactoryBot.create(:isa_assay_data_file_template).id)
assay_2_stream_2 = FactoryBot.create(:assay, position:1, sample_type: assay_2_stream_2_sample_type, study: accessible_study, contributor: current_user.person)
stream_2 = FactoryBot.create(:assay_stream, title: 'Assay Stream 2', study: accessible_study, contributor: current_user.person)
assert_equal(stream_2.study, accessible_study)
assert(stream_2.is_assay_stream?)
assay_1_stream_2_sample_type = FactoryBot.create(:isa_assay_material_sample_type, contributor: current_user.person, linked_sample_type: sample_collection_sample_type, template_id: FactoryBot.create(:isa_assay_material_template).id)
assay_1_stream_2 = FactoryBot.create(:assay, position: 1, sample_type: assay_1_stream_2_sample_type, study: accessible_study, contributor: current_user.person, assay_stream_id: stream_2.id)
assay_2_stream_2_sample_type = FactoryBot.create(:isa_assay_data_file_sample_type, contributor: current_user.person, linked_sample_type: assay_1_stream_2_sample_type, template_id: FactoryBot.create(:isa_assay_data_file_template).id)
assay_2_stream_2 = FactoryBot.create(:assay, position: 2, sample_type: assay_2_stream_2_sample_type, study: accessible_study, contributor: current_user.person, assay_stream_id: stream_2.id)

# create samples in second assay stream with viewing permission

Expand Down Expand Up @@ -1096,8 +1102,13 @@ def test_title
assert json_investigation['studies'].map { |s| s['title'] }.include? accessible_study.title
study_json = json_investigation['studies'].first

# Total assays
assert_equal accessible_study.assays.count, 6
# Assay_streams
assert_equal accessible_study.assay_streams.count, 2
# Child assays
assert_equal accessible_study.assay_streams.map(&:child_assays).compact.flatten.count, 4
# Only one assay should end up in 1 assay stream in the ISA JSON
assert_equal accessible_study.assays.count, 4
assert_equal study_json['assays'].count, 1

sample_ids = study_json['materials']['samples'].map { |sample| sample['@id'] }
Expand Down

0 comments on commit 0dc8b6c

Please sign in to comment.