Skip to content

Commit d33c535

Browse files
authored
Add tests for exporting valid OWL (#417)
Motivated by EBISPOT/ols4#936 and EBISPOT/ols4#939 This pull request makes sure there are no strange escapes in properties output by the ontology.
1 parent af48e6a commit d33c535

1 file changed

Lines changed: 97 additions & 4 deletions

File tree

tests/test_struct/test_obo/test_struct_obo.py

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
"""Test for OBO header."""
22

3+
import tempfile
34
import unittest
45
from collections.abc import Iterable
6+
from pathlib import Path
57
from textwrap import dedent
68

9+
import bioontologies.robot
10+
711
from pyobo import default_reference
812
from pyobo.struct.reference import OBOLiteral
9-
from pyobo.struct.struct import Obo, make_ad_hoc_ontology
13+
from pyobo.struct.struct import Obo, build_ontology, make_ad_hoc_ontology
1014
from pyobo.struct.struct_utils import Annotation
1115
from pyobo.struct.typedef import has_license
1216

@@ -22,11 +26,33 @@ def assert_obo_lines(self, text, ontology: Obo) -> None:
2226
"""Assert OBO header has the right lines."""
2327
self.assert_lines(text, ontology.iterate_obo_lines())
2428

29+
def assert_ofn_lines(self, text: str, ontology: Obo) -> None:
30+
"""Assert OFN header has the right lines."""
31+
with tempfile.TemporaryDirectory() as directory:
32+
in_path = Path(directory).joinpath("tmp.ofn")
33+
ontology.write_ofn(in_path)
34+
self.assert_lines(text, in_path.read_text().splitlines())
35+
36+
def assert_owl_lines(self, text: str, ontology: Obo) -> None:
37+
"""Assert OWL header has the right lines."""
38+
with tempfile.TemporaryDirectory() as directory:
39+
in_path = Path(directory).joinpath("tmp.ofn")
40+
ontology.write_ofn(in_path)
41+
out_path = Path(directory).joinpath("tmp.owl")
42+
bioontologies.robot.convert(in_path, out_path, check=False)
43+
lines = out_path.read_text().splitlines()
44+
lines = [
45+
"" if not line.strip() else line.rstrip()
46+
for line in lines
47+
if line.strip() and not line.strip().startswith("<!-- Generated by the OWL API")
48+
]
49+
self.assert_lines(text, lines)
50+
2551
def test_2_data_version(self) -> None:
2652
"""Test ontology definition."""
27-
ontology = make_ad_hoc_ontology(
28-
_ontology="xxx",
29-
_data_version="1.0",
53+
ontology = build_ontology(
54+
prefix="xxx",
55+
version="1.0",
3056
)
3157
self.assert_obo_lines(
3258
"""\
@@ -174,6 +200,73 @@ def test_18_properties_bioregistry(self) -> None:
174200
ontology,
175201
)
176202

203+
def test_18_properties_escapes(self) -> None:
204+
"""Test escapes in property values, like for parentheses."""
205+
ontology = build_ontology(
206+
prefix="xxx",
207+
description="MeSH (Medical Subject Headings)",
208+
)
209+
self.assert_obo_lines(
210+
r"""
211+
format-version: 1.4
212+
idspace: dcterms http://purl.org/dc/terms/ "Dublin Core Metadata Initiative Terms"
213+
ontology: xxx
214+
property_value: dcterms:description "MeSH \(Medical Subject Headings\)" xsd:string
215+
216+
[Typedef]
217+
id: dcterms:description
218+
name: description
219+
is_metadata_tag: true
220+
""",
221+
ontology,
222+
)
223+
self.assert_ofn_lines(
224+
"""
225+
Prefix(dcterms:=<http://purl.org/dc/terms/>)
226+
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
227+
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
228+
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)
229+
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
230+
231+
Ontology(<https://w3id.org/biopragmatics/resources/xxx/xxx.ofn>
232+
Annotation(dcterms:description "MeSH (Medical Subject Headings)"^^xsd:string)
233+
234+
Declaration(AnnotationProperty(dcterms:description))
235+
AnnotationAssertion(rdfs:label dcterms:description "description")
236+
)
237+
""",
238+
ontology,
239+
)
240+
self.assert_owl_lines(
241+
"""
242+
<?xml version="1.0"?>
243+
<rdf:RDF xmlns="https://w3id.org/biopragmatics/resources/xxx/xxx.ofn#"
244+
xml:base="https://w3id.org/biopragmatics/resources/xxx/xxx.ofn"
245+
xmlns:owl="http://www.w3.org/2002/07/owl#"
246+
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
247+
xmlns:xml="http://www.w3.org/XML/1998/namespace"
248+
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
249+
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
250+
xmlns:dcterms="http://purl.org/dc/terms/">
251+
<owl:Ontology rdf:about="https://w3id.org/biopragmatics/resources/xxx/xxx.ofn">
252+
<dcterms:description>MeSH (Medical Subject Headings)</dcterms:description>
253+
</owl:Ontology>
254+
<!--
255+
///////////////////////////////////////////////////////////////////////////////////////
256+
//
257+
// Annotation properties
258+
//
259+
///////////////////////////////////////////////////////////////////////////////////////
260+
-->
261+
<!-- http://purl.org/dc/terms/description -->
262+
<owl:AnnotationProperty rdf:about="http://purl.org/dc/terms/description">
263+
<rdfs:label>description</rdfs:label>
264+
</owl:AnnotationProperty>
265+
</rdf:RDF>
266+
""",
267+
ontology,
268+
)
269+
177270
def test_18_properties_external(self) -> None:
178271
"""Test properties."""
179272
ontology = make_ad_hoc_ontology(

0 commit comments

Comments
 (0)