Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 97 additions & 4 deletions tests/test_struct/test_obo/test_struct_obo.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"""Test for OBO header."""

import tempfile
import unittest
from collections.abc import Iterable
from pathlib import Path
from textwrap import dedent

import bioontologies.robot

from pyobo import default_reference
from pyobo.struct.reference import OBOLiteral
from pyobo.struct.struct import Obo, make_ad_hoc_ontology
from pyobo.struct.struct import Obo, build_ontology, make_ad_hoc_ontology
from pyobo.struct.struct_utils import Annotation
from pyobo.struct.typedef import has_license

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

def assert_ofn_lines(self, text: str, ontology: Obo) -> None:
"""Assert OFN header has the right lines."""
with tempfile.TemporaryDirectory() as directory:
in_path = Path(directory).joinpath("tmp.ofn")
ontology.write_ofn(in_path)
self.assert_lines(text, in_path.read_text().splitlines())

def assert_owl_lines(self, text: str, ontology: Obo) -> None:
"""Assert OWL header has the right lines."""
with tempfile.TemporaryDirectory() as directory:
in_path = Path(directory).joinpath("tmp.ofn")
ontology.write_ofn(in_path)
out_path = Path(directory).joinpath("tmp.owl")
bioontologies.robot.convert(in_path, out_path, check=False)
lines = out_path.read_text().splitlines()
lines = [
"" if not line.strip() else line.rstrip()
for line in lines
if line.strip() and not line.strip().startswith("<!-- Generated by the OWL API")
]
self.assert_lines(text, lines)

def test_2_data_version(self) -> None:
"""Test ontology definition."""
ontology = make_ad_hoc_ontology(
_ontology="xxx",
_data_version="1.0",
ontology = build_ontology(
prefix="xxx",
version="1.0",
)
self.assert_obo_lines(
"""\
Expand Down Expand Up @@ -174,6 +200,73 @@ def test_18_properties_bioregistry(self) -> None:
ontology,
)

def test_18_properties_escapes(self) -> None:
"""Test escapes in property values, like for parentheses."""
ontology = build_ontology(
prefix="xxx",
description="MeSH (Medical Subject Headings)",
)
self.assert_obo_lines(
r"""
format-version: 1.4
idspace: dcterms http://purl.org/dc/terms/ "Dublin Core Metadata Initiative Terms"
ontology: xxx
property_value: dcterms:description "MeSH \(Medical Subject Headings\)" xsd:string

[Typedef]
id: dcterms:description
name: description
is_metadata_tag: true
""",
ontology,
)
self.assert_ofn_lines(
"""
Prefix(dcterms:=<http://purl.org/dc/terms/>)
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)

Ontology(<https://w3id.org/biopragmatics/resources/xxx/xxx.ofn>
Annotation(dcterms:description "MeSH (Medical Subject Headings)"^^xsd:string)

Declaration(AnnotationProperty(dcterms:description))
AnnotationAssertion(rdfs:label dcterms:description "description")
)
""",
ontology,
)
self.assert_owl_lines(
"""
<?xml version="1.0"?>
<rdf:RDF xmlns="https://w3id.org/biopragmatics/resources/xxx/xxx.ofn#"
xml:base="https://w3id.org/biopragmatics/resources/xxx/xxx.ofn"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:dcterms="http://purl.org/dc/terms/">
<owl:Ontology rdf:about="https://w3id.org/biopragmatics/resources/xxx/xxx.ofn">
<dcterms:description>MeSH (Medical Subject Headings)</dcterms:description>
</owl:Ontology>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Annotation properties
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://purl.org/dc/terms/description -->
<owl:AnnotationProperty rdf:about="http://purl.org/dc/terms/description">
<rdfs:label>description</rdfs:label>
</owl:AnnotationProperty>
</rdf:RDF>
""",
ontology,
)

def test_18_properties_external(self) -> None:
"""Test properties."""
ontology = make_ad_hoc_ontology(
Expand Down
Loading