|
| 1 | +"""Spec: formal-semantics.md "ldh-GenerateOntologyViews - Generate LDH views |
| 2 | +(`ldh:view`) and SPIN `sp:Select` queries for each non-`owl:FunctionalProperty` |
| 3 | +`owl:DatatypeProperty`/`owl:ObjectProperty` in an ontology graph" |
| 4 | +Abstract: Graph × URI × URI → Graph |
| 5 | +Python: def execute(self, ontology: rdflib.Graph, base_uri: URIRef, service_uri: URIRef) -> rdflib.Graph |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import pytest |
| 11 | +from rdflib import Graph, Literal, Namespace, URIRef |
| 12 | +from rdflib.namespace import OWL, RDF |
| 13 | + |
| 14 | +from web_algebra.operation import Operation |
| 15 | + |
| 16 | + |
| 17 | +LDH = Namespace("https://w3id.org/atomgraph/linkeddatahub#") |
| 18 | +EX = Namespace("http://example.org/ns#") |
| 19 | +BASE = URIRef("http://example.org/portal/") |
| 20 | +SERVICE = URIRef("http://example.org/portal/#Service") |
| 21 | + |
| 22 | + |
| 23 | +class TestLDHGenerateOntologyViewsPure: |
| 24 | + def test_wrong_ontology_type_raises(self, settings): |
| 25 | + op = Operation.get("ldh-GenerateOntologyViews")(settings=settings) |
| 26 | + with pytest.raises(TypeError): |
| 27 | + op.execute(Literal("not-a-graph"), BASE, SERVICE) |
| 28 | + |
| 29 | + def test_wrong_base_uri_type_raises(self, settings): |
| 30 | + op = Operation.get("ldh-GenerateOntologyViews")(settings=settings) |
| 31 | + with pytest.raises(TypeError): |
| 32 | + op.execute(Graph(), Literal("not-a-uri"), SERVICE) |
| 33 | + |
| 34 | + def test_wrong_service_uri_type_raises(self, settings): |
| 35 | + op = Operation.get("ldh-GenerateOntologyViews")(settings=settings) |
| 36 | + with pytest.raises(TypeError): |
| 37 | + op.execute(Graph(), BASE, Literal("not-a-uri")) |
| 38 | + |
| 39 | + def test_emits_ldh_view_for_non_functional_property(self, settings): |
| 40 | + """Spec: emits a view per non-`owl:FunctionalProperty` object/datatype property.""" |
| 41 | + ontology = Graph() |
| 42 | + ontology.add((EX.knows, RDF.type, OWL.ObjectProperty)) |
| 43 | + |
| 44 | + op = Operation.get("ldh-GenerateOntologyViews")(settings=settings) |
| 45 | + out = op.execute(ontology, BASE, SERVICE) |
| 46 | + |
| 47 | + assert isinstance(out, Graph) |
| 48 | + views = list(out.triples((EX.knows, LDH.view, None))) |
| 49 | + assert len(views) == 1, f"expected one ldh:view triple for ex:knows, got {len(views)}" |
| 50 | + |
| 51 | + def test_skips_functional_property(self, settings): |
| 52 | + """Spec: properties declared `owl:FunctionalProperty` are excluded.""" |
| 53 | + ontology = Graph() |
| 54 | + ontology.add((EX.ssn, RDF.type, OWL.DatatypeProperty)) |
| 55 | + ontology.add((EX.ssn, RDF.type, OWL.FunctionalProperty)) |
| 56 | + |
| 57 | + op = Operation.get("ldh-GenerateOntologyViews")(settings=settings) |
| 58 | + out = op.execute(ontology, BASE, SERVICE) |
| 59 | + |
| 60 | + views = list(out.triples((EX.ssn, LDH.view, None))) |
| 61 | + assert len(views) == 0, "functional property must not get a view" |
| 62 | + |
| 63 | + def test_no_ldh_template_in_output(self, settings): |
| 64 | + """Spec phrases the output predicate as `ldh:view` — `ldh:template` (the |
| 65 | + previous LDH vocabulary, since removed) must not appear.""" |
| 66 | + ontology = Graph() |
| 67 | + ontology.add((EX.knows, RDF.type, OWL.ObjectProperty)) |
| 68 | + ontology.add((EX.name, RDF.type, OWL.DatatypeProperty)) |
| 69 | + |
| 70 | + op = Operation.get("ldh-GenerateOntologyViews")(settings=settings) |
| 71 | + out = op.execute(ontology, BASE, SERVICE) |
| 72 | + |
| 73 | + legacy = list(out.triples((None, LDH.template, None))) |
| 74 | + assert legacy == [], "output must contain no `ldh:template` triples" |
| 75 | + |
| 76 | + |
| 77 | +class TestLDHGenerateOntologyViewsJson: |
| 78 | + @pytest.mark.skip(reason="UNCLEAR(spec): JSON arg keys for ldh-GenerateOntologyViews not given by spec or existing fixtures") |
| 79 | + def test_json_dispatch(self, settings): |
| 80 | + pass |
0 commit comments