|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Portions of this file contributed by NIST are governed by the |
| 4 | +# following statement: |
| 5 | +# |
| 6 | +# This software was developed at the National Institute of Standards |
| 7 | +# and Technology by employees of the Federal Government in the course |
| 8 | +# of their official duties. Pursuant to Title 17 Section 105 of the |
| 9 | +# United States Code, this software is not subject to copyright |
| 10 | +# protection within the United States. NIST assumes no responsibility |
| 11 | +# whatsoever for its use by other parties, and makes no guarantees, |
| 12 | +# expressed or implied, about its quality, reliability, or any other |
| 13 | +# characteristic. |
| 14 | +# |
| 15 | +# We would appreciate acknowledgement if the software is used. |
| 16 | + |
| 17 | +import logging |
| 18 | +from collections import defaultdict |
| 19 | +from pathlib import Path |
| 20 | +from pprint import pformat |
| 21 | +from typing import DefaultDict, Dict, Set |
| 22 | +from uuid import UUID |
| 23 | + |
| 24 | +from rdflib import Graph, URIRef |
| 25 | + |
| 26 | +srcdir = Path(__file__).parent |
| 27 | + |
| 28 | + |
| 29 | +def test_uuid_unique_usage_casework_github_io() -> None: |
| 30 | + """ |
| 31 | + This test confirms that if two node IRIs end with the same UUID, then the IRI matches. |
| 32 | + This test is likely to be copied and adjusted between several example repositories. |
| 33 | + """ |
| 34 | + uuid_to_urirefs: DefaultDict[UUID, Set[URIRef]] = defaultdict(set) |
| 35 | + graph = Graph() |
| 36 | + graph.parse(srcdir / "kb.ttl") |
| 37 | + |
| 38 | + def _ingest(n_thing: URIRef) -> None: |
| 39 | + thing_iri = str(n_thing) |
| 40 | + if len(thing_iri) < 40: |
| 41 | + # Not long enough to contain scheme, colon, and UUID. |
| 42 | + return |
| 43 | + try: |
| 44 | + thing_uuid = UUID(thing_iri[-36:]) |
| 45 | + except ValueError: |
| 46 | + return |
| 47 | + uuid_to_urirefs[thing_uuid].add(n_thing) |
| 48 | + |
| 49 | + for triple in graph.triples((None, None, None)): |
| 50 | + if isinstance(triple[0], URIRef): |
| 51 | + _ingest(triple[0]) |
| 52 | + if isinstance(triple[2], URIRef): |
| 53 | + _ingest(triple[2]) |
| 54 | + |
| 55 | + computed: Dict[str, Set[str]] = dict() |
| 56 | + for _uuid in uuid_to_urirefs: |
| 57 | + if len(uuid_to_urirefs[_uuid]) > 1: |
| 58 | + computed[str(_uuid)] = {str(x) for x in uuid_to_urirefs[_uuid]} |
| 59 | + |
| 60 | + try: |
| 61 | + assert len(computed) == 0 |
| 62 | + except AssertionError: |
| 63 | + logging.debug(pformat(computed)) |
| 64 | + raise |
0 commit comments