From 7bac631a6b0394eff078f81fc3e1c13a1611a289 Mon Sep 17 00:00:00 2001 From: fherreazcue Date: Wed, 4 Oct 2023 16:25:08 +0100 Subject: [PATCH] replaced samples with two sops forced to reference same policy --- test/unit/policy_test.rb | 60 ++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/test/unit/policy_test.rb b/test/unit/policy_test.rb index 4bc84c4c7a..6db205c927 100644 --- a/test/unit/policy_test.rb +++ b/test/unit/policy_test.rb @@ -194,39 +194,33 @@ class PolicyTest < ActiveSupport::TestCase end end - # Samples no longer reference the policy, they make a copy of it. - # - # test 'policy not destroyed if still referenced by assets' do - # policy = FactoryBot.create(:public_policy) - # sample_type = FactoryBot.create(:strain_sample_type) - # data_file = FactoryBot.create(:strain_sample_data_file, policy: policy) - # samples = data_file.extract_samples(sample_type, true).select(&:persisted?) - # sample = samples.first - # - # assert_equal sample.policy, data_file.policy - # - # assert_no_difference('Policy.count') do - # disable_authorization_checks { data_file.destroy } - # end - # - # assert_not_nil sample.reload.policy - # assert_not_nil Policy.find_by_id(policy.id) - # end - # - # test 'policy destroyed when no longer referenced' do - # policy = FactoryBot.create(:public_policy) - # sample_type = FactoryBot.create(:strain_sample_type) - # data_file = FactoryBot.create(:strain_sample_data_file, policy: policy) - # samples = data_file.extract_samples(sample_type, true).select(&:persisted?) - # - # disable_authorization_checks { data_file.destroy } - # - # assert_difference('Policy.count', -1) do - # disable_authorization_checks { samples.each(&:destroy) } - # end - # - # assert_nil Policy.find_by_id(policy.id) - # end + test 'policy not destroyed if still referenced by assets' do + policy = FactoryBot.create(:public_policy) + sop1 = FactoryBot.create(:sop, policy: policy) + sop2 = FactoryBot.create(:sop, policy: policy) + + assert_equal sop1.policy, sop2.policy + + assert_no_difference('Policy.count') do + disable_authorization_checks { sop1.destroy } + end + + assert_not_nil sop2.reload.policy + assert_not_nil Policy.find_by_id(policy.id) + end + + test 'policy destroyed when no longer referenced' do + policy = FactoryBot.create(:public_policy) + sop1 = FactoryBot.create(:sop, policy: policy) + sop2 = FactoryBot.create(:sop, policy: policy) + sops = [sop1, sop2] + + assert_difference('Policy.count', -1) do + disable_authorization_checks { sops.each(&:destroy) } + end + + assert_nil Policy.find_by_id(policy.id) + end test 'public? false if sharing scope ALL::USERS' do policy = FactoryBot.create(:public_policy,sharing_scope:Policy::ALL_USERS, access_type:Policy::ACCESSIBLE)