Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kdp-cloud committed Oct 30, 2024
1 parent 6421eed commit 9bcbd7e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 28 deletions.
1 change: 1 addition & 0 deletions app/controllers/sample_types_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def update_sample_json_metadata

def check_if_locked
@sample_type ||= SampleType.find(params[:id])
@sample_type.reload
return unless @sample_type&.locked?

error_message = 'This sample type is locked and cannot be edited right now.'
Expand Down
6 changes: 3 additions & 3 deletions test/functional/sample_types_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SampleTypesControllerTest < ActionController::TestCase
assert_enqueued_with(job: SampleTypeUpdateJob) do
assert_difference('ActivityLog.count') do
assert_difference('SampleType.count') do
assert_difference('Task.count') do
assert_difference('Task.where(key: "template_generation").count') do
post :create, params: { sample_type: { title: 'Hello!',
project_ids: @project_ids,
description: 'The description!!',
Expand Down Expand Up @@ -787,8 +787,8 @@ class SampleTypesControllerTest < ActionController::TestCase
assert_response :success
end

# Locking the sample type
@sample_type.set_lock
# lock the sample type by adding a fake update task
UpdateSampleMetadataJob.perform_later(@sample_type, @person.user, [])
assert @sample_type.locked?

%i[edit manage].each do |action|
Expand Down
5 changes: 2 additions & 3 deletions test/functional/samples_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1591,10 +1591,9 @@ def rdf_test_object

sample_type = FactoryBot.create(:simple_sample_type, contributor: person, project_ids: [project.id])

get :new, params: { sample_type_id: sample_type.id }
assert_response :success
# lock the sample type by adding a fake update task
UpdateSampleMetadataJob.perform_later(sample_type, person.user, [])

sample_type.set_lock
get :new, params: { sample_type_id: sample_type.id }
assert_redirected_to sample_types_path(sample_type)
assert_equal flash[:error], 'This sample type is locked. You cannot edit the sample.'
Expand Down
7 changes: 5 additions & 2 deletions test/unit/sample_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1456,8 +1456,11 @@ class SampleTest < ActiveSupport::TestCase
end

test 'add sample to a locked sample type' do
sample_type = FactoryBot.create(:simple_sample_type, project_ids: [FactoryBot.create(:project).id], contributor: FactoryBot.create(:person))
sample_type.set_lock
person = FactoryBot.create(:person)
sample_type = FactoryBot.create(:simple_sample_type, project_ids: [FactoryBot.create(:project).id], contributor: person)

# lock the sample type by adding a fake update task
UpdateSampleMetadataJob.perform_later(sample_type, person.user, [])
assert sample_type.locked?

assert_no_difference('Sample.count') do
Expand Down
25 changes: 5 additions & 20 deletions test/unit/sample_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ def setup
@person = FactoryBot.create(:person)
@project = @person.projects.first
@project_ids = [@project.id]
Rails.cache.clear
end

def teardown
Rails.cache.clear
end

test 'validation' do
Expand Down Expand Up @@ -695,7 +690,7 @@ def teardown
assert sample_type.new_record?
refute sample_type.template_generation_task.pending?

assert_difference('Task.count', 1) do
assert_difference('Task.where(key: "template_generation").count', 1) do
assert_enqueued_with(job: SampleTemplateGeneratorJob, args: [sample_type]) do
disable_authorization_checks { sample_type.save! }
assert sample_type.reload.template_generation_task.pending?
Expand Down Expand Up @@ -1063,25 +1058,15 @@ def teardown
test 'sample type is locked?' do
sample_type = FactoryBot.create(:simple_sample_type, project_ids: @project_ids, contributor: @person)
refute sample_type.locked?
sample_type.set_lock

# lock the sample type by adding a fake update task
UpdateSampleMetadataJob.perform_later(sample_type, @person.user, [])

assert sample_type.locked?
refute sample_type.valid?
assert sample_type.errors.added?(:base, 'This sample type is locked and cannot be edited right now.')
end

test 'update locked sample type' do
sample_type = FactoryBot.create(:simple_sample_type, project_ids: @project_ids, contributor: @person)
(1..10).each do |i|
FactoryBot.create(:sample, sample_type: sample_type, project_ids: @project_ids, title: "Sample #{i}")
end
sample_type.with_lock do
# sample_type.samples << FactoryBot.create(:sample, sample_type: sample_type, project_ids: @project_ids, title: 'Sample 11')
# sample_type.title = 'Updated title'
sample_type.sample_attributes.first.title = 'Updated title'
sample_type.save!
end
end

private

# sample type with 3 samples
Expand Down

0 comments on commit 9bcbd7e

Please sign in to comment.