Skip to content

Commit a01d0bf

Browse files
committed
use sparql in tests
1 parent 4d08010 commit a01d0bf

2 files changed

Lines changed: 32 additions & 24 deletions

File tree

tests/integration/profiles/five-safes-crate/test_5src_funding.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from rocrate_validator.models import Severity
1919
from tests.ro_crates import ValidROC
20-
from tests.shared import do_entity_test
20+
from tests.shared import do_entity_test, SPARQL_PREFIXES
2121

2222
# set up logging
2323
logger = logging.getLogger(__name__)
@@ -27,18 +27,14 @@ def test_5src_funding_project_no_name():
2727
"""\
2828
Test a Five Safes Crate where the funding Project does not have a name.
2929
"""
30-
def funding_project_no_name(graph):
31-
SCHEMA = rdflib.Namespace("http://schema.org/")
32-
target_subject = rdflib.URIRef("#project-be6ffb55-4f5a-4c14-b60e-47e0951090c70")
33-
target_predicate = SCHEMA.name
34-
target_object = None
35-
36-
for s, p, o in graph.triples((target_subject, target_predicate, target_object)):
37-
print(f"Removing: {s}, {p}, {o}")
38-
39-
graph.remove((target_subject, target_predicate, target_object))
40-
41-
return graph
30+
sparql = SPARQL_PREFIXES + """DELETE {
31+
?subject schema:name "Investigation of cancer (TRE72 project 81)"
32+
}
33+
WHERE {
34+
?subject a schema:Project .
35+
?subject schema:name "Investigation of cancer (TRE72 project 81)" .
36+
}
37+
"""
4238

4339
do_entity_test(
4440
rocrate_path=ValidROC().five_safes_crate_result,
@@ -49,5 +45,6 @@ def funding_project_no_name(graph):
4945
"The Project Entity MUST have a `name` property (as specified by schema.org)"
5046
],
5147
profile_identifier="five-safes-crate",
52-
rocrate_entity_mod_function=funding_project_no_name,
48+
rocrate_entity_mod_sparql=sparql,
49+
5350
)

tests/shared.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@
3434

3535
T = TypeVar("T")
3636

37+
SPARQL_PREFIXES = """PREFIX schema: <http://schema.org/>
38+
"""
39+
3740

3841
def first(c: Collection[T]) -> T:
3942
return next(iter(c))
4043

4144

42-
def load_graph_and_preserve_relative_ids(json_data, dummy_base="http://example.org/"):
45+
def load_graph_and_preserve_relative_ids(json_data, base="http://example.org/"):
4346

4447
rel_ids = set()
4548

@@ -58,11 +61,11 @@ def collect_ids(obj):
5861
collect_ids(json_data)
5962

6063
g = rdflib.Graph()
61-
g.parse(data=json_data, format="json-ld", publicID=dummy_base)
64+
g.parse(data=json_data, format="json-ld", publicID=base)
6265

6366
mapping = {}
6467
for rid in rel_ids:
65-
expanded = urljoin(dummy_base, rid)
68+
expanded = urljoin(base, rid)
6669
mapping[expanded] = rid
6770

6871
def replace_uri_in_graph(graph, old_uri_str, new_uri_str):
@@ -95,7 +98,7 @@ def do_entity_test(
9598
abort_on_first: bool = False,
9699
profile_identifier: str = DEFAULT_PROFILE_IDENTIFIER,
97100
rocrate_entity_patch: Optional[dict] = None,
98-
rocrate_entity_mod_function: Optional[Callable] = None,
101+
rocrate_entity_mod_sparql: Optional[str] = None,
99102
skip_checks: Optional[list[str]] = ()
100103
):
101104
"""
@@ -109,7 +112,7 @@ def do_entity_test(
109112
rocrate_path = Path(rocrate_path)
110113

111114
temp_rocrate_path = None
112-
if any([rocrate_entity_patch, rocrate_entity_mod_function]) and rocrate_path.is_dir():
115+
if any([rocrate_entity_patch, rocrate_entity_mod_sparql]) and rocrate_path.is_dir():
113116
# create a temporary copy of the RO-Crate
114117
temp_rocrate_path = Path(tempfile.TemporaryDirectory().name)
115118
# copy the RO-Crate to the temporary path using shutil
@@ -127,19 +130,27 @@ def do_entity_test(
127130
# save the updated RO-Crate metadata
128131
with open(temp_rocrate_path / "ro-crate-metadata.json", "w") as f:
129132
json.dump(rocrate, f)
130-
# update the RO-Crate metadata using RDF triples, if required
131-
if rocrate_entity_mod_function is not None:
132-
rocrate_graph = load_graph_and_preserve_relative_ids(rocrate)
133-
rocrate_graph = rocrate_entity_mod_function(rocrate_graph)
133+
# update the RO-Crate metadata using SPARQL, if required
134+
if rocrate_entity_mod_sparql is not None:
135+
base = "https://example.org/"
136+
rocrate_graph = rdflib.Graph()
137+
rocrate_graph.parse(data=rocrate, format="json-ld", publicID=base)
138+
139+
query = f"""BASE <{base}>
140+
{rocrate_entity_mod_sparql}
141+
"""
142+
rocrate_graph.update(query)
134143
# save the updated RO-Crate metadata
135144
context = "https://w3id.org/ro/crate/1.1/context"
136145
rocrate_graph.serialize(
137146
Path(temp_rocrate_path, "ro-crate-metadata.json"),
138147
format="json-ld",
139148
context=context,
140149
indent=2,
141-
use_native_types=True
150+
use_native_types=True,
151+
base=base,
142152
)
153+
# TODO: tweak the generated crate to strip leading slash from relative URIs, and ensure RDE ID is ./
143154
# set the new rocrate path
144155
rocrate_path = temp_rocrate_path
145156

0 commit comments

Comments
 (0)