|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +class HasOntologyTermsTest < ActiveSupport::TestCase |
| 4 | + # Summary: we create attributes 'test_topics' and 'multi_test_topics' for |
| 5 | + # the fake model DummyMaterial. 'test_topics' uses DummyOntology, |
| 6 | + # 'multi_test_topics' uses both DummyOntology and Edam::Ontology |
| 7 | + |
| 8 | + teardown do |
| 9 | + DummyMaterial.clear_index! |
| 10 | + end |
| 11 | + |
| 12 | + class DummyTerm |
| 13 | + attr_reader :label, :uri |
| 14 | + def initialize(term) |
| 15 | + @label = term |
| 16 | + @uri = "http://dummy/#{term}" |
| 17 | + end |
| 18 | + alias_method :preferred_label, :label |
| 19 | + end |
| 20 | + |
| 21 | + class DummyOntology < ::Ontology |
| 22 | + # A very permissive ontology: it allows any term as long as it doesn't have Chemistry in it |
| 23 | + include Singleton |
| 24 | + |
| 25 | + def initialize |
| 26 | + end |
| 27 | + |
| 28 | + def uri |
| 29 | + 'http://dummy/' |
| 30 | + end |
| 31 | + |
| 32 | + def scoped_lookup_by_name(term, subset = :_) |
| 33 | + return DummyTerm.new(term) unless term =~ /chemistry/i |
| 34 | + end |
| 35 | + alias_method :scoped_lookup_by_name_or_synonym, :scoped_lookup_by_name |
| 36 | + |
| 37 | + def lookup(uri) |
| 38 | + term = uri[/http:\/\/dummy\/(.*)/,1] |
| 39 | + return DummyTerm.new(term) unless term.blank? |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + class DummyMaterial < ::Material |
| 44 | + has_ontology_terms(:test_topics, ontology: DummyOntology.instance) |
| 45 | + has_ontology_terms(:multi_test_topics, |
| 46 | + ontologies: [{ ontology: Edam::Ontology.instance, |
| 47 | + branch: EDAM.topics}, |
| 48 | + { ontology: DummyOntology.instance}]) |
| 49 | + |
| 50 | + # TODO: see similar tests with model subclasses, maybe can be in a module? |
| 51 | + def self.index |
| 52 | + (@index ||= Hash.new).values.flatten.uniq |
| 53 | + end |
| 54 | + |
| 55 | + def self.add_to_index(m) |
| 56 | + index |
| 57 | + @index[m.id] = m.reload.collections.to_a |
| 58 | + end |
| 59 | + |
| 60 | + def self.clear_index! |
| 61 | + @index = Hash.new |
| 62 | + end |
| 63 | + |
| 64 | + def solr_index |
| 65 | + self.class.add_to_index(self) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + test 'can create an attribute with terms from a single ontology' do |
| 70 | + # See the Event/Material model tests for many examples of this ... |
| 71 | + dummy = materials(:good_material).becomes(DummyMaterial) |
| 72 | + |
| 73 | + # This is found in the ontology ... |
| 74 | + dummy.test_topic_names = ['Bioinformatics'] |
| 75 | + dummy.save! |
| 76 | + assert_equal dummy.test_topics.count, 1 |
| 77 | + assert_equal dummy.test_topic_names, ['Bioinformatics'] |
| 78 | + assert_equal dummy.test_topic_uris, ['http://dummy/Bioinformatics'] |
| 79 | + |
| 80 | + # This is not |
| 81 | + dummy.test_topic_names = ['Biochemistry'] |
| 82 | + dummy.save! |
| 83 | + assert_equal dummy.test_topics.count, 0 |
| 84 | + assert_equal dummy.test_topic_names, [] |
| 85 | + assert_equal dummy.test_topic_uris, [] |
| 86 | + end |
| 87 | + |
| 88 | + test 'can create an attribute with terms from multiple ontologies' do |
| 89 | + dummy = materials(:good_material).becomes(DummyMaterial) |
| 90 | + |
| 91 | + # This is found in both ontologies ... |
| 92 | + dummy.multi_test_topic_names = ['Bioinformatics'] |
| 93 | + dummy.save! |
| 94 | + assert_equal dummy.multi_test_topics.count, 2 |
| 95 | + assert_equal Set.new(dummy.multi_test_topic_uris), |
| 96 | + Set.new(['http://edamontology.org/topic_0091', 'http://dummy/Bioinformatics']) |
| 97 | + # The two exact names collapse into one ... |
| 98 | + assert_equal dummy.multi_test_topic_names, ['Bioinformatics'] |
| 99 | + |
| 100 | + # This is found in only in Edam ... |
| 101 | + dummy.multi_test_topic_names = ['Biochemistry'] |
| 102 | + dummy.save! |
| 103 | + assert_equal dummy.multi_test_topics.count, 1 |
| 104 | + assert_equal dummy.multi_test_topic_names, ['Biochemistry'] |
| 105 | + assert_equal dummy.multi_test_topic_uris, ['http://edamontology.org/topic_3292'] |
| 106 | + |
| 107 | + # This is found only in DummyOntology ... |
| 108 | + dummy.multi_test_topic_names = ['Poodles'] |
| 109 | + dummy.save! |
| 110 | + assert_equal dummy.multi_test_topics.count, 1 |
| 111 | + assert_equal dummy.multi_test_topic_names, ['Poodles'] |
| 112 | + assert_equal dummy.multi_test_topic_uris, ['http://dummy/Poodles'] |
| 113 | + |
| 114 | + # This is found in neither ... |
| 115 | + dummy.multi_test_topic_names = ['Poodle Chemistry'] |
| 116 | + dummy.save! |
| 117 | + assert_equal dummy.multi_test_topics.count, 0 |
| 118 | + assert_equal dummy.multi_test_topic_names, [] |
| 119 | + assert_equal dummy.multi_test_topic_uris, [] |
| 120 | + |
| 121 | + # Set via URIs |
| 122 | + dummy.multi_test_topic_uris = ['http://dummy/Poodles', |
| 123 | + 'http://edamontology.org/topic_3292'] |
| 124 | + dummy.save! |
| 125 | + assert_equal dummy.multi_test_topics.count, 2 |
| 126 | + assert_equal Set.new(dummy.multi_test_topic_names), |
| 127 | + Set.new(['Biochemistry', 'Poodles']) |
| 128 | + assert_equal Set.new(dummy.multi_test_topic_uris), |
| 129 | + Set.new(['http://dummy/Poodles', |
| 130 | + 'http://edamontology.org/topic_3292']) |
| 131 | + assert_equal dummy.ontology_term_links.map(&:field), ["multi_test_topics", |
| 132 | + "multi_test_topics"] |
| 133 | + assert_equal Set.new(dummy.ontology_term_links.map(&:term_uri)), |
| 134 | + Set.new(['http://dummy/Poodles', |
| 135 | + 'http://edamontology.org/topic_3292']) |
| 136 | + end |
| 137 | + |
| 138 | + test "Ignores attributes that don't come from any ontology" do |
| 139 | + dummy = materials(:good_material).becomes(DummyMaterial) |
| 140 | + dummy.ontology_term_links.create(field: :test_topics, term_uri: 'http://not-a-term.com') |
| 141 | + dummy.ontology_term_links.create(field: :multi_test_topics, term_uri: 'http://also-not-a-term.com') |
| 142 | + |
| 143 | + assert_equal dummy.ontology_term_links.count, 2 |
| 144 | + |
| 145 | + assert_equal dummy.test_topics, [] |
| 146 | + assert_equal dummy.test_topic_names, [] |
| 147 | + assert_equal dummy.test_topic_uris, [] |
| 148 | + |
| 149 | + assert_equal dummy.multi_test_topics, [] |
| 150 | + assert_equal dummy.multi_test_topic_names, [] |
| 151 | + assert_equal dummy.multi_test_topic_uris, [] |
| 152 | + |
| 153 | + # Setting URI manually wipes out the ontology_term_links |
| 154 | + dummy.test_topic_uris = ['http://not-a-term.com'] |
| 155 | + dummy.multi_test_topic_uris = ['http://also-not-a-term.com'] |
| 156 | + |
| 157 | + assert_equal dummy.ontology_term_links.count, 0 |
| 158 | + |
| 159 | + # What if there is a term in here already, plus a bogus term link? |
| 160 | + # (perhaps bogus because a previous ontology was take out). |
| 161 | + dummy.test_topic_names = ['Bioinformatics'] |
| 162 | + dummy.multi_test_topic_names = ['Biochemistry', 'Bioinformatics', 'Poodles'] |
| 163 | + assert_equal dummy.ontology_term_links.count, 5 |
| 164 | + assert_equal dummy.test_topic_links.count, 1 |
| 165 | + assert_equal dummy.multi_test_topic_links.count, 4 |
| 166 | + |
| 167 | + dummy.ontology_term_links.create(field: :test_topics, term_uri: 'http://not-a-term.com') |
| 168 | + dummy.ontology_term_links.create(field: :multi_test_topics, term_uri: 'http://also-not-a-term.com') |
| 169 | + assert_equal dummy.ontology_term_links.count, 7 |
| 170 | + assert_equal dummy.test_topic_links.count, 2 |
| 171 | + assert_equal dummy.multi_test_topic_links.count, 5 |
| 172 | + |
| 173 | + # Terms with bogus URIs don't appear here |
| 174 | + assert_equal dummy.test_topics.count, 1 |
| 175 | + assert_equal dummy.test_topic_names, ['Bioinformatics'] |
| 176 | + assert_equal dummy.test_topic_uris, ['http://dummy/Bioinformatics'] |
| 177 | + |
| 178 | + assert_equal dummy.multi_test_topics.count, 4 |
| 179 | + # Bioinformatics is in both ontologies |
| 180 | + assert_equal Set.new(dummy.multi_test_topic_names), Set.new(['Biochemistry', 'Bioinformatics', 'Poodles']) |
| 181 | + assert_equal Set.new(dummy.multi_test_topic_uris), |
| 182 | + Set.new(['http://edamontology.org/topic_3292', |
| 183 | + 'http://edamontology.org/topic_0091', |
| 184 | + 'http://dummy/Bioinformatics', |
| 185 | + 'http://dummy/Poodles']) |
| 186 | + end |
| 187 | + |
| 188 | +end |
0 commit comments