Skip to content

Commit 80b7df7

Browse files
EttoreMdouglowe
authored andcommitted
Added tests for rule-set 6 implemented in SHACL
1 parent f97d3a6 commit 80b7df7

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Copyright (c) 2024-2025 CRS4
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import logging
16+
17+
from rocrate_validator.models import Severity
18+
from tests.ro_crates import ValidROC
19+
from tests.shared import do_entity_test, SPARQL_PREFIXES
20+
21+
# set up logging
22+
logger = logging.getLogger(__name__)
23+
24+
25+
# ----- MUST fails tests
26+
27+
28+
def test_5src_root_data_entity_no_main_entity():
29+
"""
30+
Remove the RootDataEntity's mainEntity so minCount=1 is violated.
31+
"""
32+
sparql = (
33+
SPARQL_PREFIXES
34+
+ """
35+
DELETE {
36+
<./> schema:mainEntity ?m .
37+
}
38+
WHERE {
39+
<./> schema:mainEntity ?m .
40+
}
41+
"""
42+
)
43+
44+
do_entity_test(
45+
rocrate_path=ValidROC().five_safes_crate_request,
46+
requirement_severity=Severity.REQUIRED,
47+
expected_validation_result=False,
48+
expected_triggered_requirements=["RootDataEntity"],
49+
expected_triggered_issues=[
50+
"The RootDataEntity MUST have exactly one schema:mainEntity property that is an IRI."
51+
],
52+
profile_identifier="five-safes-crate",
53+
rocrate_entity_mod_sparql=sparql,
54+
)
55+
56+
57+
def test_5src_root_data_entity_main_entity_not_dataset_iri():
58+
"""
59+
Test a Five Safes Crate where the RootDataEntity's mainEntity is an IRI but not typed as schema:Dataset.
60+
(We point mainEntity to a new crate-local entity typed as something else.)
61+
"""
62+
sparql = (
63+
SPARQL_PREFIXES
64+
+ """
65+
DELETE {
66+
<./> schema:mainEntity ?m .
67+
}
68+
INSERT {
69+
# add an IRI that is NOT typed as schema:Dataset (e.g. a schema:SoftwareSourceCode)
70+
<./> schema:mainEntity <./not-a-dataset> .
71+
<./not-a-dataset> a schema:SoftwareSourceCode .
72+
}
73+
WHERE {
74+
<./> schema:mainEntity ?m .
75+
}
76+
"""
77+
)
78+
79+
do_entity_test(
80+
rocrate_path=ValidROC().five_safes_crate_request,
81+
requirement_severity=Severity.REQUIRED,
82+
expected_validation_result=False,
83+
expected_triggered_requirements=["RootDataEntity"],
84+
expected_triggered_issues=[
85+
"The mainEntity pointed to by the RootDataEntity MUST be of type schema:Dataset"
86+
],
87+
profile_identifier="five-safes-crate",
88+
rocrate_entity_mod_sparql=sparql,
89+
)
90+
91+
92+
def test_5src_main_entity_conformsTo_invalid():
93+
"""
94+
Test a Five Safes Crate where the mainEntity's purl:conformsTo IRI does NOT start with
95+
"https://w3id.org/workflowhub/workflow-ro-crate" (violates the SHACL SPARQL constraint).
96+
"""
97+
sparql = (
98+
SPARQL_PREFIXES
99+
+ """
100+
PREFIX purl: <http://purl.org/dc/terms/>
101+
DELETE {
102+
?dataset purl:conformsTo ?iri .
103+
}
104+
INSERT {
105+
?dataset purl:conformsTo <http://example.org/not-workflow-ro-crate> .
106+
}
107+
WHERE {
108+
<./> schema:mainEntity ?dataset .
109+
?dataset purl:conformsTo ?iri .
110+
FILTER(STRSTARTS(STR(?iri), "https://w3id.org/workflowhub/workflow-ro-crate"))
111+
}
112+
"""
113+
)
114+
115+
do_entity_test(
116+
rocrate_path=ValidROC().five_safes_crate_request,
117+
requirement_severity=Severity.REQUIRED,
118+
expected_validation_result=False,
119+
expected_triggered_requirements=["mainEntity"],
120+
expected_triggered_issues=[
121+
"conformsTo IRI must start with https://w3id.org/workflowhub/workflow-ro-crate"
122+
],
123+
profile_identifier="five-safes-crate",
124+
rocrate_entity_mod_sparql=sparql,
125+
)
126+
127+
128+
# ----- SHOULD fails tests
129+
130+
131+
def test_5src_main_entity_missing_distribution_warning():
132+
"""
133+
Test a Five Safes Crate where a mainEntity has an HTTP(S) IRI but no distribution with an HTTP(S) URL.
134+
This should trigger the SHACL warning about missing or non-HTTP(S) distributions.
135+
"""
136+
sparql = (
137+
SPARQL_PREFIXES
138+
+ """
139+
DELETE {
140+
?dataset schema:distribution ?dist .
141+
}
142+
WHERE {
143+
<./> schema:mainEntity ?dataset .
144+
?dataset schema:distribution ?dist .
145+
FILTER (STRSTARTS(STR(?dataset), "http://") || STRSTARTS(STR(?dataset), "https://")) .
146+
FILTER (STRSTARTS(STR(?dist), "http://") || STRSTARTS(STR(?dist), "https://")) .
147+
}
148+
"""
149+
)
150+
151+
do_entity_test(
152+
rocrate_path=ValidROC().five_safes_crate_request,
153+
requirement_severity=Severity.RECOMMENDED,
154+
expected_validation_result=False,
155+
expected_triggered_requirements=["mainEntity"],
156+
expected_triggered_issues=[
157+
"If mainEntity has an HTTP(S) @id SHOULD have at least one distribution with an HTTP(S) URL."
158+
],
159+
profile_identifier="five-safes-crate",
160+
rocrate_entity_mod_sparql=sparql,
161+
)

0 commit comments

Comments
 (0)