Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin De Pelseneer committed Aug 29, 2023
1 parent eed9e1c commit c5c5628
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/unit/assay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,25 @@ class AssayTest < ActiveSupport::TestCase
assert !one_assay_with_publication.can_delete?(User.current_user.person)
end

# Users shouldn't be able to delete assays populated with samples through their linked sample types
test 'can only delete assays with empty sample types' do
assay_sample_type = FactoryBot.create(:simple_sample_type, title: "Assay Sample Type with samples")
empty_sample_type = FactoryBot.create(:simple_sample_type, title: "Empty assay Sample Type")
assay_samples = (0..4).map do |i|
FactoryBot.create(:sample, title: "DNA Extract nr. #{i}", sample_type: assay_sample_type)
end

assay = FactoryBot.create(:assay, title: "First Assay", sample_type: assay_sample_type)
empty_assay = FactoryBot.create(:assay, title: "Empty assay", sample_type:empty_sample_type)

assert_equal(assay.sample_type.samples.size, 5)
assert(empty_assay.sample_type.samples.none?)

assert_equal(assay.state_allows_delete?, false)
assert_equal(empty_assay.state_allows_delete?, true)
end


test 'assets' do
assay = assays(:metabolomics_assay)
assert_equal 3, assay.assets.size, 'should be 2 sops and 1 data file'
Expand Down
18 changes: 18 additions & 0 deletions test/unit/study_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ class StudyTest < ActiveSupport::TestCase
assert !study.can_delete?(study.contributor)
end

# Users shouldn't be able to delete studies populated with samples through their linked sample types
test 'can only delete studies with empty sample types' do
study_source_sample_type = FactoryBot.create(:simple_sample_type, title: "Source Sample Type")
empty_sample_type = FactoryBot.create(:simple_sample_type, title: "Empty study Sample Type")
sources = (0..4).map do |i|
FactoryBot.create(:sample, title: "Source nr. #{i}", sample_type: study_source_sample_type)
end

study = FactoryBot.create(:study, title: "First study", sample_types: [study_source_sample_type])
empty_study = FactoryBot.create(:study, title: "Empty study", sample_types:[empty_sample_type])

assert_equal(study.sample_types.first.samples.size, 5)
assert(empty_study.sample_types.first.samples.none?)

assert_equal(study.state_allows_delete?, false)
assert_equal(empty_study.state_allows_delete?, true)
end

test 'publications through assays' do
assay1 = FactoryBot.create(:assay)
study = assay1.study
Expand Down

0 comments on commit c5c5628

Please sign in to comment.