summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rdflib/paths.py6
-rw-r--r--test/test_mulpath_n3.py8
-rw-r--r--test/test_path.py (renamed from test/test_paths_n3.py)46
3 files changed, 51 insertions, 9 deletions
diff --git a/rdflib/paths.py b/rdflib/paths.py
index defd0e75..6ca42d74 100644
--- a/rdflib/paths.py
+++ b/rdflib/paths.py
@@ -229,6 +229,12 @@ class Path(object):
) -> Iterator[Tuple["_SubjectType", "_ObjectType"]]:
raise NotImplementedError()
+ def __hash__(self):
+ return hash(repr(self))
+
+ def __eq__(self, other):
+ return repr(self) == repr(other)
+
def __lt__(self, other: Any) -> bool:
if not isinstance(other, (Path, Node)):
raise TypeError(
diff --git a/test/test_mulpath_n3.py b/test/test_mulpath_n3.py
deleted file mode 100644
index 41885361..00000000
--- a/test/test_mulpath_n3.py
+++ /dev/null
@@ -1,8 +0,0 @@
-from rdflib import URIRef
-from rdflib.paths import ZeroOrMore
-
-
-def test_mulpath_n3():
- uri = "http://example.com/foo"
- n3 = (URIRef(uri) * ZeroOrMore).n3()
- assert n3 == "<" + uri + ">*"
diff --git a/test/test_paths_n3.py b/test/test_path.py
index b7834721..ad967849 100644
--- a/test/test_paths_n3.py
+++ b/test/test_path.py
@@ -3,13 +3,15 @@ from typing import Union
import pytest
-from rdflib import RDF, RDFS, Graph
+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,
@@ -71,3 +73,45 @@ def test_paths_n3(
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"