From 1bb74c5e25b1fc030fb3d315b09e9da8e61ef8d3 Mon Sep 17 00:00:00 2001 From: Kevin De Pelseneer Date: Mon, 27 May 2024 09:32:46 +0200 Subject: [PATCH] Fix tests --- app/controllers/isa_assays_controller.rb | 26 ++++++++++++------- app/views/isa_assays/_form.html.erb | 2 +- test/functional/isa_assays_controller_test.rb | 11 ++++---- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/controllers/isa_assays_controller.rb b/app/controllers/isa_assays_controller.rb index b343b2b2d4..7e93c82ab4 100644 --- a/app/controllers/isa_assays_controller.rb +++ b/app/controllers/isa_assays_controller.rb @@ -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 @@ -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, @@ -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 @@ -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? diff --git a/app/views/isa_assays/_form.html.erb b/app/views/isa_assays/_form.html.erb index 15d980b68e..81b4c064d1 100644 --- a/app/views/isa_assays/_form.html.erb +++ b/app/views/isa_assays/_form.html.erb @@ -15,7 +15,7 @@ <%= assay_fields.text_area :description, rows: 5, class: "form-control rich-text-edit" -%> - # 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"} %> diff --git a/test/functional/isa_assays_controller_test.rb b/test/functional/isa_assays_controller_test.rb index ba746b6d70..1ad8afd286 100644 --- a/test/functional/isa_assays_controller_test.rb +++ b/test/functional/isa_assays_controller_test.rb @@ -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 @@ -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) @@ -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 @@ -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 @@ -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'