|
17 | 17 | from rdflib import BNode, Graph, Namespace, URIRef |
18 | 18 |
|
19 | 19 | from rocrate_validator.constants import SHACL_NS |
| 20 | +from rocrate_validator.models import LevelCollection |
20 | 21 | from rocrate_validator.requirements.shacl.checks import SHACLCheck |
21 | | -from rocrate_validator.requirements.shacl.models import Shape, ShapesRegistry |
| 22 | +from rocrate_validator.requirements.shacl.models import (NodeShape, |
| 23 | + PropertyShape, Shape, |
| 24 | + ShapesRegistry) |
22 | 25 | from rocrate_validator.requirements.shacl.utils import resolve_parent_shape |
23 | 26 |
|
24 | 27 | logger = logging.getLogger(__name__) |
25 | 28 |
|
26 | 29 |
|
27 | 30 | class MockRequirement: |
28 | | - def __init__(self): |
| 31 | + def __init__(self, requirement_level_from_path=None): |
29 | 32 | self.profile = None |
30 | | - self.requirement_level_from_path = None |
| 33 | + self.requirement_level_from_path = requirement_level_from_path |
31 | 34 |
|
32 | 35 |
|
33 | 36 | class MockParentShape: |
@@ -220,3 +223,98 @@ def test_resolve_parent_shape_with_property_bnode(): |
220 | 223 |
|
221 | 224 | assert result is not None, "Should resolve parent shape for property BNode" |
222 | 225 | assert result.key == shape.key |
| 226 | + |
| 227 | + |
| 228 | +def _make_property(graph: Graph, severity_term: str = None) -> PropertyShape: |
| 229 | + """Build a PropertyShape on a fresh BNode, optionally setting sh:severity.""" |
| 230 | + prop = PropertyShape(BNode(), graph) |
| 231 | + if severity_term is not None: |
| 232 | + prop.severity = severity_term |
| 233 | + return prop |
| 234 | + |
| 235 | + |
| 236 | +def test_derive_level_picks_most_stringent_declared_property_severity(): |
| 237 | + """ |
| 238 | + Flat NodeShape with no declared severity inherits the highest severity |
| 239 | + declared by its nested properties. |
| 240 | + """ |
| 241 | + g = Graph() |
| 242 | + shape = NodeShape(URIRef("http://example.org/NodeShape"), g) |
| 243 | + shape.add_property(_make_property(g, f"{SHACL_NS}Info")) |
| 244 | + shape.add_property(_make_property(g, f"{SHACL_NS}Warning")) |
| 245 | + shape.add_property(_make_property(g, f"{SHACL_NS}Info")) |
| 246 | + |
| 247 | + check = SHACLCheck(MockRequirement(), shape) |
| 248 | + |
| 249 | + assert check.level == LevelCollection.RECOMMENDED |
| 250 | + |
| 251 | + |
| 252 | +def test_derive_level_with_uniform_property_severity(): |
| 253 | + """When every property declares the same severity, derive that severity.""" |
| 254 | + g = Graph() |
| 255 | + shape = NodeShape(URIRef("http://example.org/NodeShape"), g) |
| 256 | + shape.add_property(_make_property(g, f"{SHACL_NS}Info")) |
| 257 | + shape.add_property(_make_property(g, f"{SHACL_NS}Info")) |
| 258 | + |
| 259 | + check = SHACLCheck(MockRequirement(), shape) |
| 260 | + |
| 261 | + assert check.level == LevelCollection.OPTIONAL |
| 262 | + |
| 263 | + |
| 264 | +def test_derive_level_ignores_properties_without_declared_severity(): |
| 265 | + """Properties without sh:severity are skipped; only declared ones drive the result.""" |
| 266 | + g = Graph() |
| 267 | + shape = NodeShape(URIRef("http://example.org/NodeShape"), g) |
| 268 | + shape.add_property(_make_property(g)) # no severity declared |
| 269 | + shape.add_property(_make_property(g, f"{SHACL_NS}Warning")) |
| 270 | + |
| 271 | + check = SHACLCheck(MockRequirement(), shape) |
| 272 | + |
| 273 | + assert check.level == LevelCollection.RECOMMENDED |
| 274 | + |
| 275 | + |
| 276 | +def test_derive_level_falls_back_to_required_when_no_property_declares_severity(): |
| 277 | + """If no nested property declares a severity, fall back to REQUIRED.""" |
| 278 | + g = Graph() |
| 279 | + shape = NodeShape(URIRef("http://example.org/NodeShape"), g) |
| 280 | + shape.add_property(_make_property(g)) |
| 281 | + shape.add_property(_make_property(g)) |
| 282 | + |
| 283 | + check = SHACLCheck(MockRequirement(), shape) |
| 284 | + |
| 285 | + assert check.level == LevelCollection.REQUIRED |
| 286 | + |
| 287 | + |
| 288 | +def test_shape_declared_severity_takes_precedence_over_derivation(): |
| 289 | + """An explicit severity on the NodeShape wins over property-based derivation.""" |
| 290 | + g = Graph() |
| 291 | + shape = NodeShape(URIRef("http://example.org/NodeShape"), g) |
| 292 | + shape.severity = f"{SHACL_NS}Warning" |
| 293 | + shape.add_property(_make_property(g, f"{SHACL_NS}Violation")) |
| 294 | + |
| 295 | + check = SHACLCheck(MockRequirement(), shape) |
| 296 | + |
| 297 | + assert check.level == LevelCollection.RECOMMENDED |
| 298 | + |
| 299 | + |
| 300 | +def test_path_based_level_takes_precedence_over_derivation(): |
| 301 | + """When the requirement file is in a must/should/may folder the path level wins.""" |
| 302 | + g = Graph() |
| 303 | + shape = NodeShape(URIRef("http://example.org/NodeShape"), g) |
| 304 | + shape.add_property(_make_property(g, f"{SHACL_NS}Info")) |
| 305 | + |
| 306 | + check = SHACLCheck( |
| 307 | + MockRequirement(requirement_level_from_path=LevelCollection.SHOULD), shape |
| 308 | + ) |
| 309 | + |
| 310 | + assert check.level == LevelCollection.SHOULD |
| 311 | + |
| 312 | + |
| 313 | +def test_derive_level_for_node_shape_without_properties(): |
| 314 | + """A NodeShape with no nested properties falls back to REQUIRED.""" |
| 315 | + g = Graph() |
| 316 | + shape = NodeShape(URIRef("http://example.org/NodeShape"), g) |
| 317 | + |
| 318 | + check = SHACLCheck(MockRequirement(), shape) |
| 319 | + |
| 320 | + assert check.level == LevelCollection.REQUIRED |
0 commit comments