Skip to content

Commit

Permalink
tidy up unit tests
Browse files Browse the repository at this point in the history
- comply with minitest style guide
- fix the use of assigment operator (=) instead of equality operator (==)
- add unit test failure message for debugging #40
  • Loading branch information
alexskr committed Nov 26, 2024
1 parent 2abcd54 commit 0e469ab
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 71 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ end

# NCBO gems (can be from a local dev path or from rubygems/git)
gem 'goo', github: 'ncbo/goo', branch: 'develop'
gem 'ontologies_linked_data', github: 'ncbo/ontologies_linked_data', branch: 'develop'
# gem 'ontologies_linked_data', github: 'ncbo/ontologies_linked_data', branch: 'develop'
gem 'ontologies_linked_data', github: 'ncbo/ontologies_linked_data', tag: 'v5.33.0'
gem 'sparql-client', github: 'ncbo/sparql-client', branch: 'develop'
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ GIT

GIT
remote: https://github.com/ncbo/ontologies_linked_data.git
revision: db05a3b22c4187fb73dd83312a038fb7f7741271
branch: develop
revision: 9ab4be437ed84f5a480e7f0d8799824fcea310ae
tag: v5.33.0
specs:
ontologies_linked_data (0.0.1)
activesupport
Expand Down
139 changes: 71 additions & 68 deletions test/test_annotator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,17 @@ def test_annotate
with_synonyms: true
})
direct = annotations
assert direct.length >= size && direct.length > 0
assert_operator 0, :<, direct.length
assert_operator size, :<, direct.length
found = 0
class_page.each do |cls|
if cls.prefLabel.length > 2
#TODO: This assertion may fail if the dictionary file on mgrep server does not contain the terms from the test ontologies
assert (direct.select { |x| x.annotatedClass.id.to_s == cls.id.to_s }).length > 0
assert_operator 0, :<, (direct.select { |x| x.annotatedClass.id.to_s == cls.id.to_s }).length, "expected to find #{cls.id}"
found += 1
end
end
assert found >= size
assert_operator size, :<=, found
end

# test for a specific class annotation
Expand All @@ -279,12 +280,12 @@ def test_annotate
with_synonyms: true
})

assert annotations.length == 1
assert annotations.first.annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Data_Storage"
assert annotations.first.annotations.length == 1
assert annotations.first.annotations.first[:from] == 1
assert annotations.first.annotations.first[:to] == term_text.length
assert text[annotations.first.annotations.first[:from] - 1, annotations.first.annotations.first[:to]] == term_text
assert_equal 1, annotations.length
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Data_Storage", annotations.first.annotatedClass.id.to_s
assert_equal 1, annotations.first.annotations.length
assert_equal 1, annotations.first.annotations.first[:from]
assert_equal term_text.length, annotations.first.annotations.first[:to]
assert_equal term_text, text[annotations.first.annotations.first[:from] - 1, annotations.first.annotations.first[:to]]

# check for a non-existent ontology
non_existent_ont = ["DOESNOTEXIST"]
Expand Down Expand Up @@ -370,14 +371,15 @@ def test_annotate_minsize_term
})
direct = annotations

assert direct.length >= size && direct.length > 0
assert_operator 0, :<, direct.length
assert_operator size, :<, direct.length
found = 0
filter_out_next = []
must_be_next = []
class_page.each do |cls|
if cls.prefLabel.length > 2
#TODO: This assertion may fail if the dictionary file on mgrep server does not contain the terms from the test ontologies
assert (direct.select { |x| x.annotatedClass.id.to_s == cls.id.to_s }).length > 0
assert_operator 0, :<, (direct.select { |x| x.annotatedClass.id.to_s == cls.id.to_s }).length
found += 1
if cls.prefLabel.length < 10
filter_out_next << cls
Expand All @@ -403,12 +405,13 @@ def test_annotate_minsize_term

direct = annotations
filter_out_next.each do |cls|
assert (direct.select { |x| x.annotatedClass.id.to_s == cls.id.to_s }).length == 0
assert_equal 0, (direct.select { |x| x.annotatedClass.id.to_s == cls.id.to_s }).length
end
must_be_next.each do |cls|
assert (direct.select { |x| x.annotatedClass.id.to_s == cls.id.to_s }).length > 0
assert_operator 0, :<, (direct.select { |x| x.annotatedClass.id.to_s == cls.id.to_s }).length
end
assert must_be_next.length > 0 && filter_out_next.length > 0
assert_operator 0, :<, must_be_next.length
assert_operator 0, :<, filter_out_next.length
end

def test_annotate_stop_words
Expand All @@ -430,7 +433,7 @@ def test_annotate_stop_words
"http://purl.obolibrary.org/obo/MCBCC_0000296#Deletion"]

not_show.each do |cls|
assert (annotations.select { |x| x.annotatedClass.id.to_s == cls }).length > 0
assert_operator 0, :<, (annotations.select { |x| x.annotatedClass.id.to_s == cls }).length
end

#annotation should not show up
Expand All @@ -449,7 +452,7 @@ def test_annotate_stop_words
})

not_show.each do |cls|
assert (annotations.select { |x| x.annotatedClass.id.to_s == cls }).length == 0
assert_equal 0, (annotations.select { |x| x.annotatedClass.id.to_s == cls }).length
end

#empty array must annotate all
Expand All @@ -468,7 +471,7 @@ def test_annotate_stop_words
})

not_show.each do |cls|
assert (annotations.select { |x| x.annotatedClass.id.to_s == cls }).length > 0
assert_operator 0, :<, (annotations.select { |x| x.annotatedClass.id.to_s == cls }).length
end
end

Expand All @@ -477,16 +480,16 @@ def test_annotate_hierarchy
text = "Aggregate Human Data Aggregate Human Data"
annotator = Annotator::Models::NcboAnnotator.new
annotations = annotator.annotate(text)
assert annotations.length == 1
assert annotations.first.annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Aggregate_Human_Data"
assert annotations.first.annotatedClass.submission.ontology.acronym == "BROTEST-0"
assert annotations.first.annotations.length == 2
assert annotations.first.annotations.first[:from] = 1
assert annotations.first.annotations.first[:to] = 1+("Aggregate Human Data".length)
assert annotations.first.annotations[1][:from] = 2 + ("Aggregate Human Data".length)
assert text[annotations.first.annotations.first[:from]-1,annotations.first.annotations.first[:to]-1] == "Aggregate Human Data"
assert annotations.first.annotations[1][:to] == (1 + ("Aggregate Human Data".length)) + ("Aggregate Human Data".length)
assert text[annotations.first.annotations[1][:from]-1,annotations.first.annotations[1][:to]-1] == "Aggregate Human Data"
assert_equal 1, annotations.length
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Aggregate_Human_Data", annotations.first.annotatedClass.id.to_s
assert_equal "BROTEST-0", annotations.first.annotatedClass.submission.ontology.acronym
assert_equal 2, annotations.first.annotations.length
assert_equal 1, annotations.first.annotations.first[:from]
assert_equal "Aggregate Human Data".length, annotations.first.annotations.first[:to]
assert_equal 2 + ("Aggregate Human Data".length), annotations.first.annotations[1][:from]
assert_equal "Aggregate Human Data", text[annotations.first.annotations.first[:from]-1,annotations.first.annotations.first[:to]]
assert_equal (1 + ("Aggregate Human Data".length)) + ("Aggregate Human Data".length), annotations.first.annotations[1][:to]
assert_equal "Aggregate Human Data", text[annotations.first.annotations[1][:from]-1,annotations.first.annotations[1][:to]-1]
annotations = annotator.annotate(text, {
ontologies: [],
semantic_types: [],
Expand All @@ -499,16 +502,16 @@ def test_annotate_hierarchy
with_synonyms: true
})

assert annotations.length == 1
assert annotations.first.annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Aggregate_Human_Data"
assert annotations.first.annotatedClass.submission.ontology.acronym == "BROTEST-0"
assert annotations.first.annotations.length == 2
assert annotations.first.annotations.first[:from] = 1
assert annotations.first.annotations.first[:to] = 1+("Aggregate Human Data".length)
assert_equal 1, annotations.length
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Aggregate_Human_Data", annotations.first.annotatedClass.id.to_s
assert_equal "BROTEST-0", annotations.first.annotatedClass.submission.ontology.acronym
assert_equal 2, annotations.first.annotations.length
assert_equal 1, annotations.first.annotations.first[:from]
assert_equal "Aggregate Human Data".length, annotations.first.annotations.first[:to]

assert annotations.first.hierarchy.length == 1
assert annotations.first.hierarchy.first.annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Clinical_Care_Data"
assert annotations.first.hierarchy.first.distance == 1
assert_equal 1, annotations.first.hierarchy.length
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Clinical_Care_Data", annotations.first.hierarchy.first.annotatedClass.id.to_s
assert_equal 1, annotations.first.hierarchy.first.distance
annotations = annotator.annotate(text, {
ontologies: [],
semantic_types: [],
Expand All @@ -521,13 +524,13 @@ def test_annotate_hierarchy
with_synonyms: true
})

assert annotations.first.hierarchy.length == 3
assert annotations.first.hierarchy.first.annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Clinical_Care_Data"
assert annotations.first.hierarchy.first.distance == 1
assert annotations.first.hierarchy[1].annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Data_Resource"
assert annotations.first.hierarchy[1].distance == 2
assert annotations.first.hierarchy[2].annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Information_Resource"
assert annotations.first.hierarchy[2].distance == 3
assert_equal 3, annotations.first.hierarchy.length
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Clinical_Care_Data", annotations.first.hierarchy.first.annotatedClass.id.to_s
assert_equal 1, annotations.first.hierarchy.first.distance
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Data_Resource", annotations.first.hierarchy[1].annotatedClass.id.to_s
assert_equal 2, annotations.first.hierarchy[1].distance
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Information_Resource", annotations.first.hierarchy[2].annotatedClass.id.to_s
assert_equal 3, annotations.first.hierarchy[2].distance
end

def test_annotate_hierachy_terms_multiple
Expand All @@ -546,56 +549,56 @@ def test_annotate_hierachy_terms_multiple
with_synonyms: true
})

assert annotations[0].annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Aggregate_Human_Data"
assert annotations[0].annotations.length == 3
assert annotations[0].hierarchy.length == 4
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Aggregate_Human_Data", annotations[0].annotatedClass.id.to_s
assert_equal 3, annotations[0].annotations.length
assert_equal 4, annotations[0].hierarchy.length
hhh = annotations[0].hierarchy.sort {|x| x.distance }.map { |x| x.annotatedClass.id.to_s }
hhh.sort!

assert hhh == [
assert_equal [
"http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Resource",
"http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Information_Resource",
"http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Clinical_Care_Data",
"http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Data_Resource"
].sort
].sort, hhh

assert annotations[1].annotatedClass.id.to_s == "http://purl.obolibrary.org/obo/MCBCC_0000288#ChromosomalMutation"
assert annotations[1].annotations.length == 2
assert_equal "http://purl.obolibrary.org/obo/MCBCC_0000288#ChromosomalMutation", annotations[1].annotatedClass.id.to_s
assert_equal 2, annotations[1].annotations.length
hhh = annotations[1].hierarchy.sort {|x| x.distance }.map { |x| x.annotatedClass.id.to_s }
hhh == ["http://purl.obolibrary.org/obo/MCBCC_0000287#GeneticVariation"]
assert_equal ["http://purl.obolibrary.org/obo/MCBCC_0000287#GeneticVariation"], hhh

assert annotations[2].annotatedClass.id.to_s == "http://purl.obolibrary.org/obo/MCBCC_0000289#ChromosomalDeletion"
assert annotations[2].annotations.length == 1
assert_equal "http://purl.obolibrary.org/obo/MCBCC_0000289#ChromosomalDeletion", annotations[2].annotatedClass.id.to_s
assert_equal 1, annotations[2].annotations.length
hhh = annotations[2].hierarchy.sort {|x| x.distance }.map { |x| x.annotatedClass.id.to_s }
assert hhh == ["http://purl.obolibrary.org/obo/MCBCC_0000287#GeneticVariation",
"http://purl.obolibrary.org/obo/MCBCC_0000288#ChromosomalMutation"]

assert annotations[3].annotatedClass.id.to_s == "http://purl.obolibrary.org/obo/MCBCC_0000296#Deletion"
assert_equal "http://purl.obolibrary.org/obo/MCBCC_0000296#Deletion", annotations[3].annotatedClass.id.to_s
hhh = annotations[3].hierarchy.sort {|x| x.distance }.map { |x| x.annotatedClass.id.to_s }
assert hhh = ["http://purl.obolibrary.org/obo/MCBCC_0000287#GeneticVariation",
"http://purl.obolibrary.org/obo/MCBCC_0000295#GeneMutation"]
assert_equal ["http://purl.obolibrary.org/obo/MCBCC_0000287#GeneticVariation",
"http://purl.obolibrary.org/obo/MCBCC_0000295#GeneMutation"], hhh

assert annotations[4].annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Data_Resource"
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Data_Resource", annotations[4].annotatedClass.id.to_s
hhh = annotations[4].hierarchy.sort {|x| x.distance }.map { |x| x.annotatedClass.id.to_s }
assert hhh == ["http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Resource",
"http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Information_Resource"]
assert_equal ["http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Resource",
"http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Information_Resource"], hhh

assert annotations[5].annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Resource"
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Resource", annotations[5].annotatedClass.id.to_s
hhh = annotations[5].hierarchy.sort {|x| x.distance }.map { |x| x.annotatedClass.id.to_s }
assert hhh == [] #root
assert_empty hhh

assert annotations[6].annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Federal_Funding_Resource"
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Federal_Funding_Resource", annotations[6].annotatedClass.id.to_s
hhh = annotations[6].hierarchy.sort {|x| x.distance }.map { |x| x.annotatedClass.id.to_s }
assert hhh == ["http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Resource",
"http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Funding_Resource"]
assert_equal ["http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Resource",
"http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Funding_Resource"], hhh

assert annotations[7].annotatedClass.id.to_s == "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Funding_Resource"
assert_equal "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Funding_Resource", annotations[7].annotatedClass.id.to_s
hhh = annotations[7].hierarchy.sort {|x| x.distance }.map { |x| x.annotatedClass.id.to_s }
assert hhh == ["http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Resource" ]
assert_equal ["http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Resource" ], hhh

assert annotations[8].annotatedClass.id.to_s == "http://purl.obolibrary.org/obo/MCBCC_0000275#ReceptorAntagonists"
assert_equal "http://purl.obolibrary.org/obo/MCBCC_0000275#ReceptorAntagonists", annotations[8].annotatedClass.id.to_s
hhh = annotations[8].hierarchy.sort {|x| x.distance }.map { |x| x.annotatedClass.id.to_s }
assert hhh == ["http://purl.obolibrary.org/obo/MCBCC_0000256#ChemicalsAndDrugs"]
assert_equal ["http://purl.obolibrary.org/obo/MCBCC_0000256#ChemicalsAndDrugs"], hhh

end

Expand Down

0 comments on commit 0e469ab

Please sign in to comment.