1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
from __future__ import annotations
import logging
from typing import Union
import pytest
from rdflib import RDF, RDFS, Graph, URIRef
from rdflib.namespace import DCAT, DCTERMS
from rdflib.paths import (
AlternativePath,
InvPath,
MulPath,
NegatedPath,
OneOrMore,
Path,
SequencePath,
ZeroOrMore,
ZeroOrOne,
)
g = Graph()
nsm = g.namespace_manager
@pytest.mark.parametrize(
"path, no_nsm, with_nsm",
[
(~RDF.type, f"^<{RDF.type}>", "^rdf:type"),
(
RDF.type / RDFS.subClassOf,
f"<{RDF.type}>/<{RDFS.subClassOf}>",
"rdf:type/rdfs:subClassOf",
),
(
RDF.type | RDFS.subClassOf,
f"<{RDF.type}>|<{RDFS.subClassOf}>",
"rdf:type|rdfs:subClassOf",
),
(-RDF.type, f"!(<{RDF.type}>)", "!(rdf:type)"),
# type errors: Unsupported operand types for * ("URIRef" and "str")
# note on type errors: The operator is defined but in an odd place
(
RDFS.subClassOf * ZeroOrMore, # type: ignore[operator]
f"<{RDFS.subClassOf}>*",
"rdfs:subClassOf*",
),
(
RDFS.subClassOf * OneOrMore, # type: ignore[operator]
f"<{RDFS.subClassOf}>+",
"rdfs:subClassOf+",
),
(
RDFS.subClassOf * ZeroOrOne, # type: ignore[operator]
f"<{RDFS.subClassOf}>?",
"rdfs:subClassOf?",
),
(
RDF.type / MulPath(RDFS.subClassOf, "*"),
f"<{RDF.type}>/<{RDFS.subClassOf}>*",
"rdf:type/rdfs:subClassOf*",
),
(
RDF.type / ((SequencePath(RDFS.subClassOf)) * "*"),
f"<{RDF.type}>/<{RDFS.subClassOf}>*",
"rdf:type/rdfs:subClassOf*",
),
(
RDF.type / RDFS.subClassOf * "*",
f"(<{RDF.type}>/<{RDFS.subClassOf}>)*",
"(rdf:type/rdfs:subClassOf)*",
),
(
-(RDF.type | RDFS.subClassOf),
f"!(<{RDF.type}>|<{RDFS.subClassOf}>)",
"!(rdf:type|rdfs:subClassOf)",
),
(
-(RDF.type | ((SequencePath(RDFS.subClassOf)) * "*")),
f"!(<{RDF.type}>|<{RDFS.subClassOf}>*)",
"!(rdf:type|rdfs:subClassOf*)",
),
(
SequencePath(RDFS.subClassOf),
f"<{RDFS.subClassOf}>",
"rdfs:subClassOf",
),
(
AlternativePath(RDFS.subClassOf),
f"<{RDFS.subClassOf}>",
"rdfs:subClassOf",
),
],
)
def test_paths_n3(
path: Union[InvPath, SequencePath, AlternativePath, MulPath, NegatedPath],
no_nsm: str,
with_nsm: str,
) -> None:
logging.debug("path = %s", path)
assert path.n3() == no_nsm
assert path.n3(nsm) == with_nsm
def test_mulpath_n3():
uri = "http://example.com/foo"
n3 = (URIRef(uri) * ZeroOrMore).n3()
assert n3 == "<" + uri + ">*"
@pytest.mark.parametrize(
["lhs", "rhs"],
[
(DCTERMS.temporal / DCAT.endDate, DCTERMS.temporal / DCAT.endDate),
(SequencePath(DCTERMS.temporal, DCAT.endDate), DCTERMS.temporal / DCAT.endDate),
],
)
def test_eq(lhs: Path, rhs: Path) -> None:
logging.debug("lhs = %s/%r, rhs = %s/%r", type(lhs), lhs, type(rhs), rhs)
assert lhs == rhs
@pytest.mark.parametrize(
["lhs", "rhs"],
[
(DCTERMS.temporal / DCAT.endDate, DCTERMS.temporal / DCAT.endDate),
(SequencePath(DCTERMS.temporal, DCAT.endDate), DCTERMS.temporal / DCAT.endDate),
],
)
def test_hash(lhs: Path, rhs: Path) -> None:
logging.debug("lhs = %s/%r, rhs = %s/%r", type(lhs), lhs, type(rhs), rhs)
assert hash(lhs) == hash(rhs)
@pytest.mark.parametrize(
["insert_path", "check_path"],
[
(DCTERMS.temporal / DCAT.endDate, DCTERMS.temporal / DCAT.endDate),
(SequencePath(DCTERMS.temporal, DCAT.endDate), DCTERMS.temporal / DCAT.endDate),
],
)
def test_dict_key(insert_path: Path, check_path: Path) -> None:
d = {insert_path: "foo"}
assert d[check_path] == "foo"
|