Skip to content

Commit

Permalink
Cleaned up test
Browse files Browse the repository at this point in the history
  • Loading branch information
francescalb committed Aug 25, 2023
1 parent eddaf01 commit 60feca7
Showing 1 changed file with 36 additions and 53 deletions.
89 changes: 36 additions & 53 deletions tests/test_get_by_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,14 @@ def test_get_by_label_onto(repo_dir) -> None:
import owlready2
from ontopy.ontology import DEFAULT_LABEL_ANNOTATIONS

# create ontology with one class and check that it is found
testonto = get_ontology("http://domain_ontology/new_ontology")
testonto.new_entity("Class", owlready2.Thing)

assert testonto.label_annotations == DEFAULT_LABEL_ANNOTATIONS
assert testonto.get_by_label("Class") == testonto.Class

imported_onto = testonto.world.get_ontology(
repo_dir / "tests" / "testonto" / "testonto.ttl"
).load()
testonto.imported_ontologies.append(imported_onto)
assert imported_onto.get_by_label("TestClass")
assert imported_onto.get_by_label("models:TestClass")

assert testonto.get_by_label("TestClass")


def test_get_by_label_all_onto() -> None:
"""Test that label annotations are added correctly if they are not added before
using get_by_label_all
"""
import owlready2

testonto = get_ontology("http://domain_ontology/new_ontology")
testonto.new_entity("Class", owlready2.Thing)
assert testonto.get_by_label_all("*") == {testonto.Class}

testonto.new_annotation_property(
"SpecialAnnotation", owlready2.AnnotationProperty
)
Expand All @@ -46,26 +29,51 @@ def test_get_by_label_all_onto() -> None:
with pytest.raises(AttributeError):
assert testonto.Klasse.prefLabel == ["Klasse"]

assert testonto.get_by_label_all("*") == {
testonto.Class,
testonto.SpecialAnnotation,
testonto.Klasse,
}

# Add prefLabel to ontology
# preflabel = testonto.new_annotation_property(
# "prefLabel", parent=[owlready2.AnnotationPropertyClass],
# )
# preflabel.iri = "http://www.w3.org/2004/02/skos/core#prefLabel"
# assert testonto.prefLabel.prefLabel == ["prefLabel"]
#
# assert testonto.Klasse.prefLabel == ["Klasse"]
preflabel = testonto.new_annotation_property(
"prefLabel", parent=[owlready2.AnnotationProperty],
)
preflabel.iri = "http://www.w3.org/2004/02/skos/core#prefLabel"

# After prefLabel was added to the ontology, prefLabels can be accessed
with pytest.raises(AssertionError):
assert testonto.prefLabel.prefLabel == ["prefLabel"]
testonto.prefLabel.prefLabel = "prefLabel"
assert testonto.prefLabel.prefLabel == ["prefLabel"]

with pytest.raises(AssertionError):
assert testonto.Klasse.prefLabel == ["Klasse"]

testonto.new_entity("UnderKlasse", testonto.Klasse)
assert testonto.UnderKlasse.prefLabel == ["UnderKlasse"]

assert testonto.get_by_label_all("*") == {
# testonto.prefLabel,
# testonto.altLabel,
testonto.prefLabel,
testonto.Class,
testonto.SpecialAnnotation,
testonto.Klasse,
testonto.UnderKlasse
}
assert testonto.get_by_label_all("Class*") == {
testonto.Class,
}


# Check that imported ontologies are searched
imported_onto = testonto.world.get_ontology(
repo_dir / "tests" / "testonto" / "testonto.ttl"
).load()
testonto.imported_ontologies.append(imported_onto)
assert imported_onto.get_by_label("TestClass")
assert imported_onto.get_by_label("models:TestClass")

assert testonto.get_by_label("TestClass")

def test_get_by_label_emmo(emmo: "Ontology") -> None:
# Loading emmo-inferred where everything is sqashed into one ontology
Expand All @@ -86,28 +94,3 @@ def test_get_by_label_emmo(emmo: "Ontology") -> None:

assert onto.get_by_label("Element:X", colon_in_label=True) == onto.Atom


import pytest

from ontopy import get_ontology
from ontopy.ontology import NoSuchLabelError


# Loading emmo-inferred where everything is sqashed into one ontology
emmo = get_ontology().load()
assert emmo[emmo.Atom.name] == emmo.Atom
assert emmo[emmo.Atom.iri] == emmo.Atom

# Load an ontology with imported sub-ontologies
onto = get_ontology(
"https://raw.githubusercontent.com/BIG-MAP/BattINFO/master/battinfo.ttl"
).load()
assert onto.Electrolyte.prefLabel.first() == "Electrolyte"


# Check colon_in_name argument
onto.Atom.altLabel.append("Element:X")
with pytest.raises(NoSuchLabelError):
onto.get_by_label("Element:X")

assert onto.get_by_label("Element:X", colon_in_label=True) == onto.Atom

0 comments on commit 60feca7

Please sign in to comment.