|
| 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_createaction_does_not_have_agent(): |
| 29 | + """ |
| 30 | + Test a Five Safes Crate where CreateAction does not have the property agent. |
| 31 | + (We remove the property agent from the CreateAction entity) |
| 32 | + """ |
| 33 | + sparql = ( |
| 34 | + SPARQL_PREFIXES |
| 35 | + + """ |
| 36 | + DELETE { |
| 37 | + ?action schema:agent ?agent . |
| 38 | + } |
| 39 | + WHERE { |
| 40 | + ?action schema:agent ?agent ; |
| 41 | + a schema:CreateAction . |
| 42 | + ?agent a schema:Person . |
| 43 | + } |
| 44 | + """ |
| 45 | + ) |
| 46 | + |
| 47 | + do_entity_test( |
| 48 | + rocrate_path=ValidROC().five_safes_crate_request, |
| 49 | + requirement_severity=Severity.REQUIRED, |
| 50 | + expected_validation_result=False, |
| 51 | + expected_triggered_requirements=["CreateAction"], |
| 52 | + expected_triggered_issues=[ |
| 53 | + "CreateAction MUST have at least one schema:agent that is a contextual entity." |
| 54 | + ], |
| 55 | + profile_identifier="five-safes-crate", |
| 56 | + rocrate_entity_mod_sparql=sparql, |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def test_createaction_agent_is_not_person(): |
| 61 | + """ |
| 62 | + Test a Five Safes Crate where CreateAction has an agent that is not of type schema:Person. |
| 63 | + (We replace the CreateAction's agent with an entity that has no type). |
| 64 | + """ |
| 65 | + sparql = ( |
| 66 | + SPARQL_PREFIXES |
| 67 | + + """ |
| 68 | + DELETE { |
| 69 | + ?action schema:agent ?agent . |
| 70 | + } |
| 71 | + INSERT { |
| 72 | + ?action schema:agent <#not-a-person> . |
| 73 | + } |
| 74 | + WHERE { |
| 75 | + ?action schema:agent ?agent ; |
| 76 | + a schema:CreateAction . |
| 77 | + ?agent a schema:Person . |
| 78 | + } |
| 79 | + """ |
| 80 | + ) |
| 81 | + |
| 82 | + do_entity_test( |
| 83 | + rocrate_path=ValidROC().five_safes_crate_request, |
| 84 | + requirement_severity=Severity.REQUIRED, |
| 85 | + expected_validation_result=False, |
| 86 | + expected_triggered_requirements=["CreateAction"], |
| 87 | + expected_triggered_issues=[ |
| 88 | + "Each CreateAction agent MUST be typed as schema:Person." |
| 89 | + ], |
| 90 | + profile_identifier="five-safes-crate", |
| 91 | + rocrate_entity_mod_sparql=sparql, |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def test_agent_affiliation_not_organization(): |
| 96 | + """ |
| 97 | + Test a Five Safes Crate where the agent of CreateAction has an affiliation |
| 98 | + that is not of type schema:Organization. |
| 99 | + (We rereplace the agent's affiliation with an entity that has no type) |
| 100 | + """ |
| 101 | + sparql = ( |
| 102 | + SPARQL_PREFIXES |
| 103 | + + """ |
| 104 | + DELETE { |
| 105 | + ?agent schema:affiliation ?aff . |
| 106 | + } |
| 107 | + INSERT { |
| 108 | + ?agent schema:affiliation <#not-an-organization> . |
| 109 | + } |
| 110 | + WHERE { |
| 111 | + ?agent schema:affiliation ?aff ; |
| 112 | + a schema:Person . |
| 113 | + ?aff a schema:Organization . |
| 114 | + ?action a schema:CreateAction ; |
| 115 | + schema:agent ?agent . |
| 116 | + } |
| 117 | + """ |
| 118 | + ) |
| 119 | + |
| 120 | + do_entity_test( |
| 121 | + rocrate_path=ValidROC().five_safes_crate_request, |
| 122 | + requirement_severity=Severity.REQUIRED, |
| 123 | + expected_validation_result=False, |
| 124 | + expected_triggered_requirements=["CreateAction"], |
| 125 | + expected_triggered_issues=[ |
| 126 | + "The affiliation of a CreateAction's agent MUST be a contextual entity with type schema:Organization." |
| 127 | + ], |
| 128 | + profile_identifier="five-safes-crate", |
| 129 | + rocrate_entity_mod_sparql=sparql, |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +# ----- SHOULD warning tests |
| 134 | + |
| 135 | + |
| 136 | +def test_5src_agent_does_not_have_affiliation(): |
| 137 | + """ |
| 138 | + Test a Five Safes Crate where the agent of CreteAction does not have an affiliatin. |
| 139 | + (We remove the triplet ?agent schema:affiliation ?org from the RO-Crate graph) |
| 140 | + """ |
| 141 | + sparql = ( |
| 142 | + SPARQL_PREFIXES |
| 143 | + + """ |
| 144 | + DELETE { |
| 145 | + ?agent schema:affiliation ?org . |
| 146 | + } |
| 147 | + WHERE { |
| 148 | + ?agent schema:affiliation ?org ; |
| 149 | + a schema:Person . |
| 150 | + ?action a schema:CreateAction ; |
| 151 | + schema:agent ?agent . |
| 152 | + } |
| 153 | + """ |
| 154 | + ) |
| 155 | + |
| 156 | + do_entity_test( |
| 157 | + rocrate_path=ValidROC().five_safes_crate_request, |
| 158 | + requirement_severity=Severity.RECOMMENDED, |
| 159 | + expected_validation_result=False, |
| 160 | + expected_triggered_requirements=["Agent of CreateAction"], |
| 161 | + expected_triggered_issues=[ |
| 162 | + "The agent of a CreateAction entity SHOULD have an affiliation" |
| 163 | + ], |
| 164 | + profile_identifier="five-safes-crate", |
| 165 | + rocrate_entity_mod_sparql=sparql, |
| 166 | + ) |
0 commit comments