|
| 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_rocrate_does_not_have_createaction(): |
| 29 | + """ |
| 30 | + Test a Five Safes Crate where no `CreateAction` entity exists. |
| 31 | + (We remove the entire CreateAction entity from the RO-Crate) |
| 32 | + """ |
| 33 | + sparql = ( |
| 34 | + SPARQL_PREFIXES |
| 35 | + + """ |
| 36 | + DELETE { |
| 37 | + ?action ?p ?o . |
| 38 | + ?s ?p2 ?action . |
| 39 | + } |
| 40 | + WHERE { |
| 41 | + ?action a schema:CreateAction . |
| 42 | + ?action ?p ?o . |
| 43 | + OPTIONAL { ?s ?p2 ?action . } |
| 44 | + } |
| 45 | + """ |
| 46 | + ) |
| 47 | + |
| 48 | + do_entity_test( |
| 49 | + rocrate_path=ValidROC().five_safes_crate_request, |
| 50 | + requirement_severity=Severity.REQUIRED, |
| 51 | + expected_validation_result=False, |
| 52 | + expected_triggered_requirements=["RootDataEntity"], |
| 53 | + expected_triggered_issues=[ |
| 54 | + "`RootDataEntity` MUST reference at least one `CreateAction` through `mentions`" |
| 55 | + ], |
| 56 | + profile_identifier="five-safes-crate", |
| 57 | + rocrate_entity_mod_sparql=sparql, |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def test_rootdataentity_does_not_have_mentions_property(): |
| 62 | + """ |
| 63 | + Test a Five Safes Crate where RootDataEntity does not have the property mentions. |
| 64 | + (We remove the property mentions from the RootDataEntity entity) |
| 65 | + """ |
| 66 | + sparql = ( |
| 67 | + SPARQL_PREFIXES |
| 68 | + + """ |
| 69 | + DELETE { |
| 70 | + <./> schema:mentions ?o . |
| 71 | + } |
| 72 | + WHERE { |
| 73 | + <./> schema:mentions ?o . |
| 74 | + } |
| 75 | + """ |
| 76 | + ) |
| 77 | + |
| 78 | + do_entity_test( |
| 79 | + rocrate_path=ValidROC().five_safes_crate_request, |
| 80 | + requirement_severity=Severity.REQUIRED, |
| 81 | + expected_validation_result=False, |
| 82 | + expected_triggered_requirements=["RootDataEntity"], |
| 83 | + expected_triggered_issues=[ |
| 84 | + "`RootDataEntity` MUST reference at least one `CreateAction` through `mentions`" |
| 85 | + ], |
| 86 | + profile_identifier="five-safes-crate", |
| 87 | + rocrate_entity_mod_sparql=sparql, |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +def test_rootdataentity_does_not_mention_create_action(): |
| 92 | + """ |
| 93 | + Test a Five Safes Crate where `RootDataEntity` does not `mention` a `CreateAction` entity. |
| 94 | + (We replace the object of RooDataEntity --> mentions with a string literal). |
| 95 | + """ |
| 96 | + sparql = ( |
| 97 | + SPARQL_PREFIXES |
| 98 | + + """ |
| 99 | + DELETE { |
| 100 | + <./> schema:mentions ?o . |
| 101 | + } |
| 102 | + INSERT { |
| 103 | + <./> schema:mentions "This is not a CreateAction entity" . |
| 104 | + } |
| 105 | + WHERE { |
| 106 | + <./> schema:mentions ?o . |
| 107 | + } |
| 108 | + """ |
| 109 | + ) |
| 110 | + |
| 111 | + do_entity_test( |
| 112 | + rocrate_path=ValidROC().five_safes_crate_request, |
| 113 | + requirement_severity=Severity.REQUIRED, |
| 114 | + expected_validation_result=False, |
| 115 | + expected_triggered_requirements=["RootDataEntity"], |
| 116 | + expected_triggered_issues=[ |
| 117 | + "`RootDataEntity` MUST reference at least one `CreateAction` through `mentions`" |
| 118 | + ], |
| 119 | + profile_identifier="five-safes-crate", |
| 120 | + rocrate_entity_mod_sparql=sparql, |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +def test_createaction_does_not_have_instrument_property(): |
| 125 | + """ |
| 126 | + Test a Five Safes Crate where `CreateAction` does not have the property `instrument`. |
| 127 | + (We remove the property `instrument` from the `CreateAction` entity) |
| 128 | + """ |
| 129 | + sparql = ( |
| 130 | + SPARQL_PREFIXES |
| 131 | + + """ |
| 132 | + DELETE { |
| 133 | + ?action schema:instrument ?o . |
| 134 | + } |
| 135 | + WHERE { |
| 136 | + ?action schema:instrument ?o ; |
| 137 | + a schema:CreateAction . |
| 138 | + } |
| 139 | + """ |
| 140 | + ) |
| 141 | + |
| 142 | + do_entity_test( |
| 143 | + rocrate_path=ValidROC().five_safes_crate_request, |
| 144 | + requirement_severity=Severity.REQUIRED, |
| 145 | + expected_validation_result=False, |
| 146 | + expected_triggered_requirements=["CreateAction"], |
| 147 | + expected_triggered_issues=[ |
| 148 | + "`CreateAction` MUST have the `schema:instrument` property" |
| 149 | + ], |
| 150 | + profile_identifier="five-safes-crate", |
| 151 | + rocrate_entity_mod_sparql=sparql, |
| 152 | + ) |
| 153 | + |
| 154 | + |
| 155 | +def test_createaction_does_not_reference_mainentity_via_instrument(): |
| 156 | + """ |
| 157 | + Test a Five Safes Crate where `CreateAction` --> `instrument` does not |
| 158 | + reference `mainEntity`. |
| 159 | + (We replace `mainEntity` with a literal as the object of `CreateAction` --> `instrument`) |
| 160 | + """ |
| 161 | + sparql = ( |
| 162 | + SPARQL_PREFIXES |
| 163 | + + """ |
| 164 | + DELETE { |
| 165 | + ?action schema:instrument ?o . |
| 166 | + } |
| 167 | + INSERT { |
| 168 | + ?action schema:instrument "This is not the mainEntity" . |
| 169 | + } |
| 170 | + WHERE { |
| 171 | + ?action schema:instrument ?o ; |
| 172 | + a schema:CreateAction . |
| 173 | + } |
| 174 | + """ |
| 175 | + ) |
| 176 | + |
| 177 | + do_entity_test( |
| 178 | + rocrate_path=ValidROC().five_safes_crate_request, |
| 179 | + requirement_severity=Severity.REQUIRED, |
| 180 | + expected_validation_result=False, |
| 181 | + expected_triggered_requirements=["CreateAction"], |
| 182 | + expected_triggered_issues=[ |
| 183 | + "`CreateAction` --> `instrument` MUST reference the same entity as `Root Data Entity` --> `mainEntity`" |
| 184 | + ], |
| 185 | + profile_identifier="five-safes-crate", |
| 186 | + rocrate_entity_mod_sparql=sparql, |
| 187 | + ) |
| 188 | + |
| 189 | + |
| 190 | +def test_createaction_object_does_not_reference_existing_entities(): |
| 191 | + """ |
| 192 | + Test a Five Safes Crate where `CreateAction` --> `object` does not |
| 193 | + reference an existing entity in the RO-Crate. |
| 194 | + (We replace the objects of `CreateAction` --> `object` with a literal.`) |
| 195 | + """ |
| 196 | + sparql = ( |
| 197 | + SPARQL_PREFIXES |
| 198 | + + """ |
| 199 | + DELETE { |
| 200 | + ?action schema:object ?o . |
| 201 | + } |
| 202 | + INSERT { |
| 203 | + ?action schema:object "This is not an entity in the RO-Crate" . |
| 204 | + } |
| 205 | + WHERE { |
| 206 | + ?action schema:object ?o ; |
| 207 | + a schema:CreateAction . |
| 208 | + } |
| 209 | + """ |
| 210 | + ) |
| 211 | + |
| 212 | + do_entity_test( |
| 213 | + rocrate_path=ValidROC().five_safes_crate_request, |
| 214 | + requirement_severity=Severity.REQUIRED, |
| 215 | + expected_validation_result=False, |
| 216 | + expected_triggered_requirements=["CreateAction"], |
| 217 | + expected_triggered_issues=[ |
| 218 | + "Each `object` in `CreateAction` MUST reference an existing entity." |
| 219 | + ], |
| 220 | + profile_identifier="five-safes-crate", |
| 221 | + rocrate_entity_mod_sparql=sparql, |
| 222 | + ) |
| 223 | + |
| 224 | + |
| 225 | +# ----- SHOULD fails tests |
| 226 | + |
| 227 | + |
| 228 | +def test_createaction_does_not_have_object_property(): |
| 229 | + """ |
| 230 | + Test a Five Safes Crate where `CreateAction` does not have the property `object`. |
| 231 | + (We remove the property `object` from `CreateAction`) |
| 232 | + """ |
| 233 | + sparql = ( |
| 234 | + SPARQL_PREFIXES |
| 235 | + + """ |
| 236 | + DELETE { |
| 237 | + ?action schema:object ?o . |
| 238 | + } |
| 239 | + WHERE { |
| 240 | + ?action schema:object ?o ; |
| 241 | + a schema:CreateAction . |
| 242 | + } |
| 243 | + """ |
| 244 | + ) |
| 245 | + |
| 246 | + do_entity_test( |
| 247 | + rocrate_path=ValidROC().five_safes_crate_request, |
| 248 | + requirement_severity=Severity.RECOMMENDED, |
| 249 | + expected_validation_result=False, |
| 250 | + expected_triggered_requirements=["CreateAction"], |
| 251 | + expected_triggered_issues=[ |
| 252 | + "`CreateAction` SHOULD have the property `object` with IRI values." |
| 253 | + ], |
| 254 | + profile_identifier="five-safes-crate", |
| 255 | + rocrate_entity_mod_sparql=sparql, |
| 256 | + ) |
0 commit comments