Skip to content

Commit

Permalink
test for ontology_based? and refactor to use delegation #1632
Browse files Browse the repository at this point in the history
  • Loading branch information
stuzart committed Nov 9, 2023
1 parent 77d78d9 commit e33ec14
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
8 changes: 3 additions & 5 deletions app/models/sample_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class SampleAttribute < ApplicationRecord
# sample type exists
attr_reader :deferred_link_to_self

# whether this attribute is tied to a controlled vocab which has a source ontology
delegate :ontology_based?, to: :sample_controlled_vocab, allow_nil: true

def title=(title)
super
store_accessor_name
Expand Down Expand Up @@ -59,11 +62,6 @@ def linked_custom_metadata_type
nil
end

# whether this attribute is tied to a controlled vocab which has a source ontology
def ontology_based?
controlled_vocab? && sample_controlled_vocab&.source_ontology.present?
end

private

def store_accessor_name
Expand Down
16 changes: 16 additions & 0 deletions test/unit/sample_attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,22 @@ class SampleAttributeTest < ActiveSupport::TestCase
assert_equal '', attribute.short_pid
end

test 'ontology_based?' do
attribute = FactoryBot.create(:sample_sample_attribute, sample_type: FactoryBot.create(:simple_sample_type))
refute attribute.ontology_based?

attribute = FactoryBot.create(:simple_string_sample_attribute, sample_type: FactoryBot.create(:simple_sample_type))
refute attribute.ontology_based?

attribute = FactoryBot.create(:apples_controlled_vocab_attribute, sample_type: FactoryBot.create(:simple_sample_type))
refute attribute.sample_controlled_vocab.ontology_based?
refute attribute.ontology_based?

attribute.sample_controlled_vocab = FactoryBot.create(:topics_controlled_vocab)
assert attribute.sample_controlled_vocab.ontology_based?
assert attribute.ontology_based?
end

private

def valid_value?(attribute, value)
Expand Down

0 comments on commit e33ec14

Please sign in to comment.