Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gtfierro committed Feb 13, 2024
1 parent 2bd1976 commit fc784c0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
11 changes: 5 additions & 6 deletions buildingmotif/dataclasses/shape_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,13 @@ def gensym():
clauses += " UNION ".join(too_clauses)

# handle targetNode
targetNode = graph.objects(shape, SH.targetNode)
if len(targetNode) > 1:
targetNode = list(graph.objects(shape, SH.targetNode))
if len(targetNode) == 1:
clauses += f"BIND({targetNode[0].n3()} AS ?target) .\n"
elif len(targetNode) > 1:
raise ValueError(
"Cannot currently have more than one sh:targetNode in shacl-to-sparql conversion"
"More than one targetNode found. This is not currently supported"
)
if targetNode:
targetNode = next(targetNode)
clauses += f"BIND({targetNode.n3()} AS ?target) .\n"

# find all of the non-qualified property shapes. All of these will use the same variable
# for all uses of the same sh:path value
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/dataclasses/test_shape_collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random

import pytest
import rdflib
from rdflib import RDF, Graph, URIRef
from rdflib.compare import isomorphic
Expand Down Expand Up @@ -160,3 +161,19 @@ def test_shape_to_query(clean_building_motif):
)
# assert this parses correctly
g.query(query2)

# test that we handle multiple target nodes
with pytest.raises(ValueError):
sc.shape_to_query(URIRef("urn:shapes_to_query/multiple_targets"))

# handle targetSubjectsOf
query3 = sc.shape_to_query(URIRef("urn:shapes_to_query/subjectTarget"))
assert (
"?target <https://brickschema.org/schema/Brick#hasPoint> ?ignore ."
) in query3, query3

# handle targetObjectsOf
query4 = sc.shape_to_query(URIRef("urn:shapes_to_query/objectTarget"))
assert (
"?ignore <https://brickschema.org/schema/Brick#hasPoint> ?target ."
) in query4, query4
33 changes: 33 additions & 0 deletions tests/unit/fixtures/shape_to_query/shapes.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,36 @@
# optional
] ;
.

:multiple_targets a sh:NodeShape, owl:Class ;
sh:targetNode brick:ABC, brick:DEF ;
sh:property [
sh:name "sensor" ;
sh:path brick:hasPoint ;
sh:qualifiedValueShape [ sh:node :sensor ] ;
sh:qualifiedMaxCount 1 ;
sh:qualifiedMinCount 1 ;
] ;
.

:subjectTarget a sh:NodeShape, owl:Class ;
sh:targetSubjectsOf brick:hasPoint ;
sh:property [
sh:name "sensor" ;
sh:path brick:hasPoint ;
sh:qualifiedValueShape [ sh:node :sensor ] ;
sh:qualifiedMaxCount 1 ;
sh:qualifiedMinCount 1 ;
] ;
.

:objectTarget a sh:NodeShape, owl:Class ;
sh:targetObjectsOf brick:hasPoint ;
sh:property [
sh:name "equip" ;
sh:path brick:isPointOf ;
sh:qualifiedValueShape [ sh:node :vav ] ;
sh:qualifiedMaxCount 1 ;
sh:qualifiedMinCount 1 ;
] ;
.

0 comments on commit fc784c0

Please sign in to comment.