Select Git revision
SearchResults.js
test_ontology_submodules.py 8.20 KiB
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# test_ontology_modules: Ontology Modules Testing
#------------------------------------------------------------------------------
# Contains tests for verifying functionality of the ontology submodules.
#==============================================================================
import unittest
from rdflib import URIRef
# Importing necessary modules from the ontoScorer package for testing.
from context import ontoScorer
from ontoScorer.ontology_axioms import *
from ontoScorer.ontology_nontaxonomic_relations import *
from ontoScorer.ontology_taxonomic_relations import *
from rdflib import URIRef, BNode
#--------------------------------------------------------------------------
# Ontology Entities Test
#--------------------------------------------------------------------------
from ontoScorer.ontology_entity import NamedEntity, BlankEntity
class TestOntologyEntities(unittest.TestCase):
def setUp(self):
self.graph = None
def test_named_entity_creation_and_name(self):
uri = URIRef("http://example.org#EntityName")
named_entity = NamedEntity(uri, self.graph)
self.assertEqual(named_entity.name(), "EntityName")
def test_named_entity_properties(self):
uri = URIRef("http://example.org#EntityName")
named_entity = NamedEntity(uri, self.graph)
# Since we have a mock graph with no triples, we expect properties() to return an empty list.
self.assertEqual(named_entity.properties(), [])
def test_blank_entity_creation_and_name(self):
bnode = BNode()
blank_entity = BlankEntity(bnode, self.graph)
self.assertEqual(blank_entity.name(), str(bnode))
def test_blank_entity_properties(self):
bnode = BNode()
blank_entity = BlankEntity(bnode, self.graph)
# Since we have a mock graph with no triples, we expect properties() to return an empty list.
self.assertEqual(blank_entity.properties(), [])
#--------------------------------------------------------------------------
# Ontology Taxonomic Relations Test
#--------------------------------------------------------------------------
from ontoScorer.ontology_taxonomic_relations import (SubclassOfRelation,
SubpropertyOfRelation,
InstanceOfRelation)
class TestOntologyTaxonomicRelations(unittest.TestCase):
def setUp(self):
self.source = URIRef("http://example.org#source")
self.target = URIRef("http://example.org#target")
self.graph = None # This can be set up with a mock RDF graph if needed
def test_subclass_relation_str(self):
relation = SubclassOfRelation(self.source, self.target, self.graph)
self.assertEqual(str(relation), "(http://example.org#source, subClassOf, http://example.org#target)")
def test_subproperty_relation_str(self):
relation = SubpropertyOfRelation(self.source, self.target, self.graph)
self.assertEqual(str(relation), "(http://example.org#source, subPropertyOf, http://example.org#target)")
def test_instance_relation_str(self):
relation = InstanceOfRelation(self.source, self.target, self.graph)
self.assertEqual(str(relation), "(http://example.org#source, instanceOf, http://example.org#target)")
class TestTaxonomicRelationComparison(unittest.TestCase):
def setUp(self):
self.graph = None
self.source1 = URIRef("http://example.org#source1")
self.target1 = URIRef("http://example.org#target1")
self.source2 = URIRef("http://example.org#source2")
self.target2 = URIRef("http://example.org#target2")
def test_subclass_relation_comparison_same(self):
relation1 = SubclassOfRelation(self.source1, self.target1, self.graph)
relation2 = SubclassOfRelation(self.source1, self.target1, self.graph)
self.assertTrue(relation1.compare_relation(relation2))
def test_subclass_relation_comparison_different(self):
relation1 = SubclassOfRelation(self.source1, self.target1, self.graph)
relation2 = SubclassOfRelation(self.source2, self.target2, self.graph)
self.assertFalse(relation1.compare_relation(relation2))
def test_subproperty_relation_comparison_same(self):
relation1 = SubpropertyOfRelation(self.source1, self.target1, self.graph)
relation2 = SubpropertyOfRelation(self.source1, self.target1, self.graph)
self.assertTrue(relation1.compare_relation(relation2))
def test_instance_relation_comparison_same(self):
relation1 = InstanceOfRelation(self.source1, self.target1, self.graph)
relation2 = InstanceOfRelation(self.source1, self.target1, self.graph)
self.assertTrue(relation1.compare_relation(relation2))
def test_instance_relation_comparison_different(self):
relation1 = InstanceOfRelation(self.source1, self.target1, self.graph)
relation2 = InstanceOfRelation(self.source2, self.target2, self.graph)
self.assertFalse(relation1.compare_relation(relation2))
#--------------------------------------------------------------------------
# Ontology Non-Taxonomic Relations Test
#--------------------------------------------------------------------------
from ontoScorer.ontology_nontaxonomic_relations import (ObjectPropertyRelation,
DataPropertyRelation,
DomainRelation,
RangeRelation)
class TestOntologyNonTaxonomicRelations(unittest.TestCase):
def setUp(self):
self.source = URIRef("http://example.org#source")
self.property = URIRef("http://example.org#property") # This was added for non-taxonomic relations
self.target = URIRef("http://example.org#target")
self.graph = None # This can be set up with a mock RDF graph if needed
def test_object_property_relation_str(self):
relation = ObjectPropertyRelation(self.source, self.property, self.target, self.graph)
self.assertEqual(str(relation), "(http://example.org#source, http://example.org#property, http://example.org#target)")
def test_data_property_relation_str(self):
relation = DataPropertyRelation(self.source, self.property, self.target, self.graph)
self.assertEqual(str(relation), "(http://example.org#source, http://example.org#property, http://example.org#target)")
def test_domain_relation_str(self):
relation = DomainRelation(self.source, self.property, self.target, self.graph)
self.assertEqual(str(relation), "(http://example.org#source, http://example.org#property, http://example.org#target)")
def test_range_relation_str(self):
relation = RangeRelation(self.source, self.property, self.target, self.graph)
self.assertEqual(str(relation), "(http://example.org#source, http://example.org#property, http://example.org#target)")
#--------------------------------------------------------------------------
# Ontology Axioms Test
#--------------------------------------------------------------------------
from ontoScorer.ontology_axioms import SomeValuesFromRestrictionAxiom
class TestAxioms(unittest.TestCase):
def test_some_values_from_restriction_axiom(self):
parent_class = URIRef("http://example.org#ParentClass")
restriction_node = URIRef("http://example.org#RestrictionNode")
on_property = URIRef("http://example.org#hasProperty")
some_values_from = URIRef("http://example.org#SomeClass")
axiom = SomeValuesFromRestrictionAxiom(parent_class, restriction_node, on_property, some_values_from)
expected_str = f"Restriction on class: {parent_class} with node: {restriction_node} via property: {on_property} with some values from: {some_values_from}"
self.assertEqual(str(axiom), expected_str, "Unexpected string representation for SomeValuesFromRestrictionAxiom.")
#--------------------------------------------------------------------------
# Main Unit Test Run
#--------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()