From fe6d330a8fe524b7866dccffcadbec511b707d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gehrke?= <5106696+b-gehrke@users.noreply.github.com> Date: Fri, 7 Jun 2024 16:54:52 +0200 Subject: [PATCH] Added first set of automatic tests --- test/resources/simple.ofn | 22 +++++ test/resources/simple.ofn.raw | 22 +++++ test/{ => resources}/simple.omn | 0 test/resources/simple.omn.raw | 4 + test/resources/simple.owl | 40 ++++++++++ test/resources/simple.owl.raw | 40 ++++++++++ test/resources/simple.owx | 28 +++++++ test/resources/simple.owx.raw | 28 +++++++ test/test_io.py | 137 ++++++++++++++++++++++++++++++++ 9 files changed, 321 insertions(+) create mode 100644 test/resources/simple.ofn create mode 100644 test/resources/simple.ofn.raw rename test/{ => resources}/simple.omn (100%) create mode 100644 test/resources/simple.omn.raw create mode 100644 test/resources/simple.owl create mode 100644 test/resources/simple.owl.raw create mode 100644 test/resources/simple.owx create mode 100644 test/resources/simple.owx.raw create mode 100644 test/test_io.py diff --git a/test/resources/simple.ofn b/test/resources/simple.ofn new file mode 100644 index 0000000..1ca73f5 --- /dev/null +++ b/test/resources/simple.ofn @@ -0,0 +1,22 @@ +Prefix(:=) +Prefix(dc:=) +Prefix(owl:=) +Prefix(rdf:=) +Prefix(xml:=) +Prefix(xsd:=) +Prefix(rdfs:=) + + +Ontology( +Declaration(Class(:A)) +Declaration(Class(:B)) +############################ +# Classes +############################ + +# Class: :B (:B) + +SubClassOf(:B :A) + + +) \ No newline at end of file diff --git a/test/resources/simple.ofn.raw b/test/resources/simple.ofn.raw new file mode 100644 index 0000000..1ca73f5 --- /dev/null +++ b/test/resources/simple.ofn.raw @@ -0,0 +1,22 @@ +Prefix(:=) +Prefix(dc:=) +Prefix(owl:=) +Prefix(rdf:=) +Prefix(xml:=) +Prefix(xsd:=) +Prefix(rdfs:=) + + +Ontology( +Declaration(Class(:A)) +Declaration(Class(:B)) +############################ +# Classes +############################ + +# Class: :B (:B) + +SubClassOf(:B :A) + + +) \ No newline at end of file diff --git a/test/simple.omn b/test/resources/simple.omn similarity index 100% rename from test/simple.omn rename to test/resources/simple.omn diff --git a/test/resources/simple.omn.raw b/test/resources/simple.omn.raw new file mode 100644 index 0000000..216c02a --- /dev/null +++ b/test/resources/simple.omn.raw @@ -0,0 +1,4 @@ +Prefix: : +Ontology: + Class: A + Class: B SubClassOf: A \ No newline at end of file diff --git a/test/resources/simple.owl b/test/resources/simple.owl new file mode 100644 index 0000000..774c551 --- /dev/null +++ b/test/resources/simple.owl @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/resources/simple.owl.raw b/test/resources/simple.owl.raw new file mode 100644 index 0000000..774c551 --- /dev/null +++ b/test/resources/simple.owl.raw @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/resources/simple.owx b/test/resources/simple.owx new file mode 100644 index 0000000..c9afc54 --- /dev/null +++ b/test/resources/simple.owx @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/resources/simple.owx.raw b/test/resources/simple.owx.raw new file mode 100644 index 0000000..c9afc54 --- /dev/null +++ b/test/resources/simple.owx.raw @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/test_io.py b/test/test_io.py new file mode 100644 index 0000000..0e04a4b --- /dev/null +++ b/test/test_io.py @@ -0,0 +1,137 @@ +import typing +from tempfile import NamedTemporaryFile + +import os.path +import unittest +import pyhornedowl +from pyhornedowl.model import * + + +def r(*args: str) -> str: + return os.path.abspath(os.path.join(os.path.dirname(__file__), "resources", *args)) + + +def res(resource: str) -> str: + with open(r(resource)) as f: + return f.read() + + +def simple_ontology() -> pyhornedowl.PyIndexedOntology: + onto = pyhornedowl.PyIndexedOntology() + onto.add_axiom(DeclareClass(Class(onto.iri("https://example.com/A")))) + onto.add_axiom(DeclareClass(Class(onto.iri("https://example.com/B")))) + onto.add_axiom(SubClassOf( + Class(onto.iri("https://example.com/A")), + Class(onto.iri("https://example.com/B")))) + + return onto + + +SERIALIZATIONS: typing.List[typing.Literal['ofn', 'owx', 'owl']] = ['ofn', 'owx', 'owl'] + + +class IO(unittest.TestCase): + + def assertOntologiesEqual(self, actual, expected): + self.assertSetEqual(set(actual.get_axioms()), set(expected.get_axioms()), "Axioms do not match!") + self.assertEqual(actual.get_iri(), expected.get_iri(), "Ontology IRIs do not match!") + + @unittest.skip("Functional syntax parser parses others formats without errors others panic") + def test_load_simple_from_string_generic_guess(self): + for s in SERIALIZATIONS: + with self.subTest(serialization=s): + content = res(f"simple.{s}") + + actual = pyhornedowl.open_ontology(content) + expected = simple_ontology() + + self.assertOntologiesEqual(actual, expected) + + def test_load_simple_from_string_generic_explicit(self): + for s in SERIALIZATIONS: + with self.subTest(serialization=s): + content = res(f"simple.{s}") + + actual = pyhornedowl.open_ontology(content, s) + expected = simple_ontology() + + self.assertOntologiesEqual(actual, expected) + + def test_load_simple_from_string_explicit(self): + for s in SERIALIZATIONS: + with self.subTest(serialization=s): + content = res(f"simple.{s}") + + actual = pyhornedowl.open_ontology_from_string(content, s) + expected = simple_ontology() + + self.assertOntologiesEqual(actual, expected) + + @unittest.skip("Functional syntax parser parses others formats without errors others panic") + def test_load_simple_from_string_guess_parser(self): + for s in SERIALIZATIONS: + with self.subTest(serialization=s): + content = res(f"simple.{s}") + + actual = pyhornedowl.open_ontology_from_string(content) + expected = simple_ontology() + + self.assertOntologiesEqual(actual, expected) + + def test_load_simple_from_file_generic_guess(self): + for s in SERIALIZATIONS: + with self.subTest(serialization=s): + actual = pyhornedowl.open_ontology(r(f'simple.{s}')) + expected = simple_ontology() + + self.assertOntologiesEqual(actual, expected) + + def test_load_simple_from_file_generic_explicit(self): + for s in SERIALIZATIONS: + with self.subTest(serialization=s): + actual = pyhornedowl.open_ontology(r(f'simple.{s}'), s) + expected = simple_ontology() + + self.assertOntologiesEqual(actual, expected) + + def test_load_simple_from_file_guess_ext(self): + for s in SERIALIZATIONS: + with self.subTest(serialization=s): + actual = pyhornedowl.open_ontology_from_file(r(f'simple.{s}')) + expected = simple_ontology() + + self.assertOntologiesEqual(actual, expected) + + def test_load_simple_from_file_explicit(self): + for s in SERIALIZATIONS: + with self.subTest(serialization=s): + actual = pyhornedowl.open_ontology_from_file(r(f'simple.{s}.raw'), s) + expected = simple_ontology() + + self.assertOntologiesEqual(actual, expected) + + def test_write_simple_guess_ext(self): + for s in SERIALIZATIONS: + with self.subTest(serialization=s): + with NamedTemporaryFile(suffix=f".{s}") as f: + original = simple_ontology() + original.save_to_file(f.name) + + actual = pyhornedowl.open_ontology_from_file(f.name, s) + + self.assertOntologiesEqual(original, actual) + + def test_write_simple_explicit(self): + for s in SERIALIZATIONS: + with self.subTest(serialization=s): + with NamedTemporaryFile(suffix=f".{s}.raw") as f: + original = simple_ontology() + original.save_to_file(f.name, s) + + actual = pyhornedowl.open_ontology_from_file(f.name, s) + + self.assertOntologiesEqual(original, actual) + + +if __name__ == '__main__': + unittest.main()