Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kdp-cloud committed May 27, 2024
1 parent 2bf968a commit 1bb74c5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
26 changes: 17 additions & 9 deletions app/controllers/isa_assays_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ class IsaAssaysController < ApplicationController
after_action :rearrange_assay_positions_create_isa_assay, only: :create

def new
study = Study.find(params[:study_id])
new_position =
if params[:is_assay_stream] || params[:source_assay_id].nil? || (params[:source_assay_id] == params[:assay_stream_id])
if params[:is_assay_stream] || params[:source_assay_id].nil? # If first assay is of class assay stream
study.assay_streams.any? ? study.assay_streams.map(&:position).max + 1 : 0
elsif params[:source_assay_id] == params[:assay_stream_id] # If first assay in the stream
0
else
Assay.find(params[:source_assay_id]).position + 1
end

study = Study.find(params[:study_id])
source_assay = Assay.find(params[:source_assay_id]) if params[:source_assay_id]
input_sample_type_id =
if params[:is_assay_stream] || source_assay.is_assay_stream?
if params[:is_assay_stream] || source_assay&.is_assay_stream?
study.sample_types.second.id
else
source_assay&.sample_type&.id
Expand All @@ -29,7 +31,7 @@ def new
if params[:is_assay_stream]
IsaAssay.new({ assay: { assay_class_id: AssayClass.assay_stream.id,
study_id: study.id,
position: 0 },
position: new_position },
input_sample_type_id: })
else
IsaAssay.new({ assay: { assay_class_id: AssayClass.experimental.id,
Expand Down Expand Up @@ -67,7 +69,7 @@ def update
@isa_assay.assay.attributes = isa_assay_params[:assay]

# update the sample_type
unless @isa_assay.assay.is_assay_stream?
unless @isa_assay&.assay&.is_assay_stream?
if requested_item_authorized?(@isa_assay.sample_type)
@isa_assay.sample_type.update(isa_assay_params[:sample_type])
@isa_assay.sample_type.resolve_inconsistencies
Expand Down Expand Up @@ -202,13 +204,19 @@ def find_requested_item
@isa_assay = IsaAssay.new
@isa_assay.populate(params[:id])

@isa_assay.errors.add(:assay, "The #{t('isa_assay')} was not found.") if @isa_assay.assay.nil?
@isa_assay.errors.add(:assay, "You are not authorized to edit this #{t('isa_assay')}.") unless requested_item_authorized?(@isa_assay.assay)
if @isa_assay.assay.nil?
@isa_assay.errors.add(:assay, "The #{t('isa_assay')} was not found.")
else
@isa_assay.errors.add(:assay, "You are not authorized to edit this #{t('isa_assay')}.") unless requested_item_authorized?(@isa_assay.assay)
end

# Should not deal with sample type if assay has assay_class assay stream
unless @isa_assay.assay&.is_assay_stream?
@isa_assay.errors.add(:sample_type, 'Sample type not found.') if @isa_assay.sample_type.nil?
@isa_assay.errors.add(:sample_type, "You are not authorized to edit this assay's #{t('sample_type')}.") unless requested_item_authorized?(@isa_assay.sample_type)
if @isa_assay.sample_type.nil?
@isa_assay.errors.add(:sample_type, 'Sample type not found.')
else
@isa_assay.errors.add(:sample_type, "You are not authorized to edit this assay's #{t('sample_type')}.") unless requested_item_authorized?(@isa_assay.sample_type)
end
end

if @isa_assay.errors.any?
Expand Down
2 changes: 1 addition & 1 deletion app/views/isa_assays/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<%= assay_fields.text_area :description, rows: 5, class: "form-control rich-text-edit" -%>
</div>

# Show extended metadata is assay is of class assay_stream
<%# Show extended metadata is assay is of class assay_stream %>
<% if is_assay_stream %>
<%= render partial: 'extended_metadata/extended_metadata_type_selection', locals:{f:assay_fields, resource:@isa_assay.assay} %>
<%= render partial: 'extended_metadata/extended_metadata_attribute_input', locals:{f:assay_fields,resource:@isa_assay.assay, parent_resource: "isa_assay"} %>
Expand Down
11 changes: 5 additions & 6 deletions test/functional/isa_assays_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def setup
end
isa_assay = assigns(:isa_assay)
assert_redirected_to controller: 'single_pages', action: 'show', id: isa_assay.assay.projects.first.id,
params: { notice: 'The ISA assay was created successfully!',
item_type: 'assay', item_id: Assay.last.id }
params: { item_type: 'assay', item_id: Assay.last.id }

sample_types = SampleType.last(2)
title = sample_types[0].sample_attributes.detect(&:is_title).title
Expand Down Expand Up @@ -139,7 +138,7 @@ def setup
assay = FactoryBot.create(:assay, study:, contributor: person)
put :update, params: { id: assay, isa_assay: { assay: { title: 'assay title' } } }
assert_redirected_to single_page_path(id: project, item_type: 'assay', item_id: assay.id)
assert flash[:error].include?('Resource not found.')
assert flash[:error].include?('Sample type not found.')

assay = FactoryBot.create(:assay, study:, sample_type: assay_type, contributor: person)

Expand Down Expand Up @@ -359,7 +358,7 @@ def setup
end

isa_assay = assigns(:isa_assay)
assert_redirected_to single_page_path(id: project, item_type: 'assay', item_id: isa_assay.assay.id, notice: 'The ISA assay was created successfully!')
assert_redirected_to single_page_path(id: project, item_type: 'assay', item_id: isa_assay.assay.id)

assert_equal isa_assay.assay.sample_type.previous_linked_sample_type, study.sample_types.second
assert_equal isa_assay.assay.next_linked_child_assay, end_assay
Expand Down Expand Up @@ -455,7 +454,7 @@ def setup
end

isa_assay = assigns(:isa_assay)
assert_redirected_to single_page_path(id: project, item_type: 'assay', item_id: isa_assay.assay.id, notice: 'The ISA assay was created successfully!')
assert_redirected_to single_page_path(id: project, item_type: 'assay', item_id: isa_assay.assay.id)

assert_equal begin_assay.previous_linked_sample_type, study.sample_types.second
assert_equal isa_assay.assay.sample_type.previous_linked_sample_type, begin_assay.sample_type
Expand Down Expand Up @@ -619,7 +618,7 @@ def setup
# New assay stream should have position 1 and is of type 'number'
assert_select 'input[type=number][value=1]#isa_assay_assay_position', count: 1

assay_stream2 = FactoryBot.create(:assay_stream, study: , contributor: person, position: 5)
FactoryBot.create(:assay_stream, study: , contributor: person, position: 5)
get :new, params: { study_id: study.id, is_assay_stream: true }
assert_response :success
# New assay stream should have position 6 and is of type 'number'
Expand Down

0 comments on commit 1bb74c5

Please sign in to comment.