diff options
| author | Iwan Aucamp <aucampia@gmail.com> | 2023-03-21 22:30:09 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-21 22:30:09 +0100 |
| commit | fe1a8f8a3e3d03c39552fedba44b59b13d23814e (patch) | |
| tree | 58f6f17d7c00e5caac5878f62477979034418302 /test/test_path.py | |
| parent | dd44ae186183b0da8fef63853c9da2be826eb643 (diff) | |
| download | rdflib-fe1a8f8a3e3d03c39552fedba44b59b13d23814e.tar.gz | |
fix: add `__hash__` and `__eq__` back to `rdflib.paths.Path` (#2292)
These methods were removed when `@total_ordering` was added, but
`@total_ordering` does not add them, so removing them essentially
removes functionality.
This change adds the methods back and adds tests to ensure they work
correctly.
All path related tests are also moved into one file.
- Closes <https://github.com/RDFLib/rdflib/issues/2281>.
- Closes <https://github.com/RDFLib/rdflib/issues/2242>.
Diffstat (limited to 'test/test_path.py')
| -rw-r--r-- | test/test_path.py | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/test/test_path.py b/test/test_path.py new file mode 100644 index 00000000..ad967849 --- /dev/null +++ b/test/test_path.py @@ -0,0 +1,117 @@ +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 / RDFS.subClassOf * "*", + f"<{RDF.type}>/<{RDFS.subClassOf}>*", + "rdf:type/rdfs:subClassOf*", + ), + ( + -(RDF.type | RDFS.subClassOf), + f"!(<{RDF.type}>|<{RDFS.subClassOf}>)", + "!(rdf:type|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" |
