Skip to content

Commit

Permalink
chore: update service tests for clone, destroy, and transfer samples …
Browse files Browse the repository at this point in the history
…to ensure csamples_count is updated correctly.
  • Loading branch information
ericenns committed Dec 18, 2024
1 parent f5fba11 commit 85dcdde
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 69 deletions.
33 changes: 7 additions & 26 deletions test/services/samples/clone_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,33 +204,14 @@ def setup
project31 = projects(:project31)
sample33 = samples(:sample33)

assert_equal(2, subgroup12aa.samples_count)
assert_equal(3, subgroup12a.samples_count)
assert_equal(1, subgroup12b.samples_count)
assert_equal(4, group12.samples_count)

clone_samples_params = { new_project_id: project31.id, sample_ids: [sample33.id] }
cloned_sample_ids = Samples::CloneService.new(project30, @john_doe).execute(clone_samples_params[:new_project_id],
clone_samples_params[:sample_ids])
cloned_sample_ids.each do |sample_id, clone_id|
sample = Sample.find_by(id: sample_id)
clone = Sample.find_by(id: clone_id)
assert_equal project30.id, sample.project_id
assert_equal project31.id, clone.project_id
assert_not_equal sample.puid, clone.puid
assert_equal sample.name, clone.name
assert_equal sample.description, clone.description
assert_equal sample.metadata, clone.metadata
assert_equal sample.metadata_provenance, clone.metadata_provenance
sample_blobs = sample.attachments.map { |attachment| attachment.file.blob }
clone_blobs = clone.attachments.map { |attachment| attachment.file.blob }
assert_equal sample_blobs.sort, clone_blobs.sort
assert_difference -> { project30.reload.samples.size } => 0,
-> { project31.reload.samples.size } => 1,
-> { subgroup12aa.reload.samples_count } => 1,
-> { subgroup12a.reload.samples_count } => 1,
-> { subgroup12b.reload.samples_count } => 0,
-> { group12.reload.samples_count } => 1 do
Samples::CloneService.new(project30, @john_doe).execute(project31.id, [sample33.id])
end

assert_equal(3, subgroup12aa.reload.samples_count)
assert_equal(4, subgroup12a.reload.samples_count)
assert_equal(1, subgroup12b.reload.samples_count)
assert_equal(5, group12.reload.samples_count)
end
end
end
34 changes: 12 additions & 22 deletions test/services/samples/destroy_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,14 @@ def setup
# group12 < subgroup12b (project30 > sample 33)
# |
# ---- < subgroup12a (project29 > sample 32) < subgroup12aa (project31 > sample34 + 35)
assert_equal(2, @subgroup12aa.samples_count)
assert_equal(3, @subgroup12a.samples_count)
assert_equal(1, @subgroup12b.samples_count)
assert_equal(4, @group12.samples_count)

assert_no_changes -> { @subgroup12b.reload.metadata_summary } do
assert_difference -> { @subgroup12b.reload.samples_count } => 0,
-> { @project31.reload.samples.size } => -1,
-> { @subgroup12aa.reload.samples_count } => -1,
-> { @subgroup12a.reload.samples_count } => -1,
-> { @subgroup12b.reload.samples_count } => 0,
-> { @group12.reload.samples_count } => -1 do
Samples::DestroyService.new(@project31, @user, { sample: @sample34 }).execute
end

assert_equal(1, @subgroup12aa.reload.samples_count)
assert_equal(2, @subgroup12a.reload.samples_count)
assert_equal(1, @subgroup12b.reload.samples_count)
assert_equal(3, @group12.reload.samples_count)
end

test 'samples count updated after multiple sample deletion' do
Expand All @@ -148,19 +143,14 @@ def setup
# ---- < subgroup12a (project29 > sample 32) < subgroup12aa (project31 > sample34 + 35)
sample35 = samples(:sample35)

assert_equal(2, @subgroup12aa.samples_count)
assert_equal(3, @subgroup12a.samples_count)
assert_equal(1, @subgroup12b.samples_count)
assert_equal(4, @group12.samples_count)

assert_no_changes -> { @subgroup12b.reload.samples_count } do
assert_difference -> { @subgroup12b.reload.samples_count } => 0,
-> { @project31.reload.samples.size } => -2,
-> { @subgroup12aa.reload.samples_count } => -2,
-> { @subgroup12a.reload.samples_count } => -2,
-> { @subgroup12b.reload.samples_count } => 0,
-> { @group12.reload.samples_count } => -2 do
Samples::DestroyService.new(@project31, @user, { sample_ids: [@sample34.id, sample35.id] }).execute
end

assert_equal(0, @subgroup12aa.reload.samples_count)
assert_equal(1, @subgroup12a.reload.samples_count)
assert_equal(1, @subgroup12b.reload.samples_count)
assert_equal(2, @group12.reload.samples_count)
end
end
end
23 changes: 2 additions & 21 deletions test/services/samples/transfer_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,39 +191,27 @@ def setup # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# ---- < subgroup12a (project29 > sample 32) < subgroup12aa (project31 > sample34 + 35)
sample24 = samples(:sample24)

assert_equal(2, @subgroup12aa.samples_count)
assert_equal(3, @subgroup12a.samples_count)
assert_equal(1, @subgroup12b.samples_count)
assert_equal(4, @group12.samples_count)

assert_difference -> { @subgroup12aa.reload.samples_count } => 1,
-> { @subgroup12a.reload.samples_count } => 1,
-> { @subgroup12b.reload.samples_count } => 0,
-> { @group12.reload.samples_count } => 1,
-> { @john_doe_project2.reload.samples.size } => -1 do
Samples::TransferService.new(@john_doe_project2, @john_doe).execute(@project31.id, [sample24.id])
end

assert_equal(1, @subgroup12b.reload.samples_count)
end

test 'samples count updates after a sample transfer to a user namespace' do
# Reference group/projects descendants tree:
# group12 < subgroup12b (project30 > sample 33)
# |
# ---- < subgroup12a (project29 > sample 32) < subgroup12aa (project31 > sample34 + 35)
assert_equal(2, @subgroup12aa.samples_count)
assert_equal(3, @subgroup12a.samples_count)
assert_equal(1, @subgroup12b.samples_count)
assert_equal(4, @group12.samples_count)

assert_difference -> { @subgroup12aa.reload.samples_count } => -2,
-> { @subgroup12a.reload.samples_count } => -2,
-> { @subgroup12b.reload.samples_count } => 0,
-> { @group12.reload.samples_count } => -2,
-> { @john_doe_project2.reload.samples.size } => 2 do
Samples::TransferService.new(@project31, @john_doe).execute(@john_doe_project2.id, [@sample34.id, @sample35.id])
end

assert_equal(1, @subgroup12b.reload.samples_count)
end

test 'samples count updates after a sample transfer between projects in the same user namespace' do
Expand All @@ -234,17 +222,10 @@ def setup # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
john_doe_project3 = projects(:john_doe_project3)
sample24 = samples(:sample24)

assert_equal(2, @subgroup12aa.samples_count)
assert_equal(3, @subgroup12a.samples_count)
assert_equal(1, @subgroup12b.samples_count)
assert_equal(4, @group12.samples_count)

assert_difference -> { @john_doe_project2.reload.samples.size } => -1,
-> { john_doe_project3.reload.samples.size } => 1 do
Samples::TransferService.new(@john_doe_project2, @john_doe).execute(john_doe_project3.id, [sample24.id])
end

assert_equal(1, @subgroup12b.reload.samples_count)
end
end
end

0 comments on commit 85dcdde

Please sign in to comment.