|
| 1 | +import logging |
| 2 | +import re |
| 3 | +from test.testutils import GraphHelper |
| 4 | +from typing import Pattern, Union |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from rdflib import Graph |
| 9 | +from rdflib.namespace import Namespace |
| 10 | +from rdflib.term import BNode, Literal, URIRef |
| 11 | + |
| 12 | +EG = Namespace("http://example.com/") |
| 13 | + |
| 14 | +base_triples = { |
| 15 | + (EG.subject, EG.predicate, EG.object0), |
| 16 | + (EG.subject, EG.predicate, EG.object1), |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.parametrize( |
| 21 | + ["node", "expected_uri"], |
| 22 | + [ |
| 23 | + (URIRef("http://example.com"), None), |
| 24 | + (Literal("some string in here ..."), None), |
| 25 | + (BNode("GMeng4V7"), "http://rdlib.net/.well-known/genid/rdflib/GMeng4V7"), |
| 26 | + ( |
| 27 | + BNode(), |
| 28 | + re.compile("^" + re.escape("http://rdlib.net/.well-known/genid/rdflib/")), |
| 29 | + ), |
| 30 | + ], |
| 31 | +) |
| 32 | +def test_skolemization( |
| 33 | + node: Union[BNode, URIRef, Literal], expected_uri: Union[Pattern[str], str, None] |
| 34 | +) -> None: |
| 35 | + g = Graph() |
| 36 | + for triple in base_triples: |
| 37 | + g.add(triple) |
| 38 | + g.add((EG.scheck, EG.pcheck, node)) |
| 39 | + assert len(g) == 3 |
| 40 | + dsg = g.skolemize() |
| 41 | + if expected_uri is None: |
| 42 | + GraphHelper.assert_sets_equals(g, dsg) |
| 43 | + else: |
| 44 | + assert len(dsg) == len(g) |
| 45 | + iset = GraphHelper.triple_or_quad_set(dsg) |
| 46 | + logging.debug("iset = %s", iset) |
| 47 | + assert iset.issuperset(base_triples) |
| 48 | + check_triples = list(dsg.triples((EG.scheck, EG.pcheck, None))) |
| 49 | + assert len(check_triples) == 1 |
| 50 | + sbnode = check_triples[0][2] |
| 51 | + logging.debug("sbnode = %s, sbnode_value = %s", sbnode, f"{sbnode}") |
| 52 | + assert isinstance(sbnode, URIRef) |
| 53 | + if isinstance(expected_uri, str): |
| 54 | + assert expected_uri == f"{sbnode}" |
| 55 | + else: |
| 56 | + assert expected_uri.match(f"{sbnode}") is not None |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize( |
| 60 | + ["iri", "expected_bnode_value"], |
| 61 | + [ |
| 62 | + ("http://example.com", None), |
| 63 | + ("http://example.com/not/.well-known/genid/1", None), |
| 64 | + ("http://rdlib.net/not/.well-known/genid/1", None), |
| 65 | + ("http://example.com/.well-known/genid/1", re.compile("^N")), |
| 66 | + ("http://rdlib.net/.well-known/genid/rdflib/GMeng4V7", "GMeng4V7"), |
| 67 | + ], |
| 68 | +) |
| 69 | +def test_deskolemization( |
| 70 | + iri: str, expected_bnode_value: Union[str, None, Pattern[str]] |
| 71 | +) -> None: |
| 72 | + g = Graph() |
| 73 | + for triple in base_triples: |
| 74 | + g.add(triple) |
| 75 | + g.add((EG.scheck, EG.pcheck, URIRef(iri))) |
| 76 | + assert len(g) == 3 |
| 77 | + dsg = g.de_skolemize() |
| 78 | + if expected_bnode_value is None: |
| 79 | + GraphHelper.assert_sets_equals(g, dsg) |
| 80 | + else: |
| 81 | + assert len(dsg) == len(g) |
| 82 | + iset = GraphHelper.triple_or_quad_set(dsg) |
| 83 | + logging.debug("iset = %s", iset) |
| 84 | + assert iset.issuperset(base_triples) |
| 85 | + check_triples = list(dsg.triples((EG.scheck, EG.pcheck, None))) |
| 86 | + assert len(check_triples) == 1 |
| 87 | + bnode = check_triples[0][2] |
| 88 | + logging.debug("bnode = %s, bnode_value = %s", bnode, f"{bnode}") |
| 89 | + assert isinstance(bnode, BNode) |
| 90 | + if isinstance(expected_bnode_value, str): |
| 91 | + assert expected_bnode_value == f"{bnode}" |
| 92 | + else: |
| 93 | + assert expected_bnode_value.match(f"{bnode}") is not None |
0 commit comments