From ef1a22974c99144f4ce8fa3d8396fefcf930ccd2 Mon Sep 17 00:00:00 2001 From: Edmond Chuc Date: Tue, 2 Jul 2024 12:52:42 +1000 Subject: [PATCH 1/6] feat: hextuple handles blank node graph names correctly in parser and serialiser --- rdflib/plugins/parsers/hext.py | 6 ++- rdflib/plugins/serializers/hext.py | 10 +++-- test/test_parsers/test_parser_hext.py | 60 +++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/rdflib/plugins/parsers/hext.py b/rdflib/plugins/parsers/hext.py index 24c97d20e..4a9c00b8d 100644 --- a/rdflib/plugins/parsers/hext.py +++ b/rdflib/plugins/parsers/hext.py @@ -72,7 +72,11 @@ def _parse_hextuple( # 6 - context if tup[5] is not None: - c = URIRef(tup[5]) + c = ( + BNode(tup[5].replace("_:", "")) + if tup[5].startswith("_:") + else URIRef(tup[5]) + ) # type error: Argument 1 to "add" of "ConjunctiveGraph" has incompatible type "Tuple[Union[URIRef, BNode], URIRef, Union[URIRef, BNode, Literal], URIRef]"; expected "Union[Tuple[Node, Node, Node], Tuple[Node, Node, Node, Optional[Graph]]]" cg.add((s, p, o, c)) # type: ignore[arg-type] else: diff --git a/rdflib/plugins/serializers/hext.py b/rdflib/plugins/serializers/hext.py index 5592504b2..00a02c5ce 100644 --- a/rdflib/plugins/serializers/hext.py +++ b/rdflib/plugins/serializers/hext.py @@ -9,7 +9,7 @@ import warnings from typing import IO, Optional, Type, Union -from rdflib.graph import ConjunctiveGraph, Graph +from rdflib.graph import DATASET_DEFAULT_GRAPH_ID, ConjunctiveGraph, Graph from rdflib.namespace import RDF, XSD from rdflib.serializer import Serializer from rdflib.term import BNode, Literal, Node, URIRef @@ -136,10 +136,14 @@ def _iri_or_bn(self, i_): def _context(self, context): if self.graph_type == Graph: return "" - if context.identifier == "urn:x-rdflib:default": + if context.identifier == DATASET_DEFAULT_GRAPH_ID: return "" elif context is not None and self.default_context is not None: # type error: "Node" has no attribute "identifier" if context.identifier == self.default_context.identifier: # type: ignore[attr-defined] return "" - return context.identifier + return ( + context.identifier + if isinstance(context.identifier, URIRef) + else context.identifier.n3() + ) diff --git a/test/test_parsers/test_parser_hext.py b/test/test_parsers/test_parser_hext.py index e558d192e..999b0d4ba 100644 --- a/test/test_parsers/test_parser_hext.py +++ b/test/test_parsers/test_parser_hext.py @@ -1,13 +1,39 @@ from pathlib import Path -from rdflib import ConjunctiveGraph, Dataset, Literal +from rdflib import BNode, ConjunctiveGraph, Dataset, Literal, URIRef +from rdflib.compare import isomorphic +from rdflib.graph import DATASET_DEFAULT_GRAPH_ID from rdflib.namespace import XSD +def test_named_and_anonymous_graph_roundtrip(): + s = """ + ["http://example.com/s01", "http://example.com/a", "http://example.com/Type1", "globalId", "", "https://example.com/graph/1"] + ["http://example.com/s01", "http://example.com/label", "This is a Label", "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", "en", "_:graph-2"] + ["http://example.com/s01", "http://example.com/comment", "This is a comment", "http://www.w3.org/2001/XMLSchema#string", "", ""] + """ + d = Dataset() + d.parse(data=s, format="hext") + + new_s = d.serialize(format="hext") + new_d = Dataset() + new_d.parse(data=new_s, format="hext") + + named_graph = URIRef("https://example.com/graph/1") + assert isomorphic(d.graph(named_graph), new_d.graph(named_graph)) + + anonymous_graph = BNode("graph-2") + assert isomorphic(d.graph(anonymous_graph), new_d.graph(anonymous_graph)) + + assert isomorphic( + d.graph(DATASET_DEFAULT_GRAPH_ID), new_d.graph(DATASET_DEFAULT_GRAPH_ID) + ) + + def test_small_string(): s = """ - ["http://example.com/s01", "http://example.com/a", "http://example.com/Type1", "globalId", "", ""] - ["http://example.com/s01", "http://example.com/label", "This is a Label", "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", "en", ""] + ["http://example.com/s01", "http://example.com/a", "http://example.com/Type1", "globalId", "", "https://example.com/graph/1"] + ["http://example.com/s01", "http://example.com/label", "This is a Label", "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", "en", "_:graph-2"] ["http://example.com/s01", "http://example.com/comment", "This is a comment", "http://www.w3.org/2001/XMLSchema#string", "", ""] ["http://example.com/s01", "http://example.com/creationDate", "2021-12-01", "http://www.w3.org/2001/XMLSchema#date", "", ""] ["http://example.com/s01", "http://example.com/creationTime", "2021-12-01T12:13:00", "http://www.w3.org/2001/XMLSchema#dateTime", "", ""] @@ -17,14 +43,24 @@ def test_small_string(): ["http://example.com/s01", "http://example.com/op1", "http://example.com/o2", "globalId", "", ""] ["http://example.com/s01", "http://example.com/op2", "http://example.com/o3", "globalId", "", ""] """ - d = Dataset().parse(data=s, format="hext") + d = Dataset() + d.parse(data=s, format="hext") + + expected_graph_names = ( + URIRef(DATASET_DEFAULT_GRAPH_ID), + URIRef("https://example.com/graph/1"), + BNode("graph-2"), + ) + for graph in d.contexts(): + assert graph.identifier in expected_graph_names + assert len(d) == 10 def test_small_string_cg(): s = """ - ["http://example.com/s01", "http://example.com/a", "http://example.com/Type1", "globalId", "", ""] - ["http://example.com/s01", "http://example.com/label", "This is a Label", "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", "en", ""] + ["http://example.com/s01", "http://example.com/a", "http://example.com/Type1", "globalId", "", "https://example.com/graph/1"] + ["http://example.com/s01", "http://example.com/label", "This is a Label", "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", "en", "_:graph-2"] ["http://example.com/s01", "http://example.com/comment", "This is a comment", "http://www.w3.org/2001/XMLSchema#string", "", ""] ["http://example.com/s01", "http://example.com/creationDate", "2021-12-01", "http://www.w3.org/2001/XMLSchema#date", "", ""] ["http://example.com/s01", "http://example.com/creationTime", "2021-12-01T12:13:00", "http://www.w3.org/2001/XMLSchema#dateTime", "", ""] @@ -34,7 +70,17 @@ def test_small_string_cg(): ["http://example.com/s01", "http://example.com/op1", "http://example.com/o2", "globalId", "", ""] ["http://example.com/s01", "http://example.com/op2", "http://example.com/o3", "globalId", "", ""] """ - d = ConjunctiveGraph().parse(data=s, format="hext") + d = ConjunctiveGraph(identifier=DATASET_DEFAULT_GRAPH_ID) + d.parse(data=s, format="hext") + + expected_graph_names = ( + URIRef(DATASET_DEFAULT_GRAPH_ID), + URIRef("https://example.com/graph/1"), + BNode("graph-2"), + ) + for graph in d.contexts(): + assert graph.identifier in expected_graph_names + assert len(d) == 10 From 78106bb82d9d62b9cf112155e822b4adafaf6399 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 2 Jul 2024 03:15:25 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test/conftest.py | 6 +++--- test/data.py | 2 +- test/data/suites/trix/test_trix.py | 6 +++--- test/data/variants/diverse_quads.py | 3 +-- test/data/variants/diverse_triples.py | 3 +-- test/data/variants/simple_quad.py | 3 +-- test/data/variants/simple_triple.py | 3 +-- test/jsonld/test_nested_arrays.py | 3 +-- .../test_conjunctivegraph_generators.py | 2 +- ...t_conjunctivegraph_operator_combinations.py | 2 +- test/test_dataset/test_dataset.py | 4 ++-- .../test_dataset/test_dataset_default_graph.py | 2 +- test/test_dataset/test_dataset_generators.py | 2 +- test/test_extras/test_infixowl/test_basic.py | 3 +-- test/test_extras/test_infixowl/test_class.py | 3 +-- test/test_extras/test_infixowl/test_cover.py | 3 +-- .../test_infixowl/test_individual.py | 3 +-- .../test_infixowl/test_manchester_syntax.py | 3 +-- test/test_graph/test_canonicalization.py | 2 +- test/test_graph/test_diff.py | 18 +++++++++--------- test/test_graph/test_graph.py | 8 ++++---- test/test_graph/test_graph_cbd.py | 5 ++--- test/test_graph/test_graph_generators.py | 2 +- test/test_graph/test_graph_http.py | 10 +++++----- test/test_graph/test_graph_redirect.py | 8 ++++---- test/test_graph/test_graph_store.py | 2 +- test/test_graph/test_namespace_rebinding.py | 3 +-- test/test_graph/test_skolemization.py | 4 ++-- test/test_graph/test_slice.py | 3 +-- test/test_graph/test_variants.py | 6 +++--- test/test_issues/test_issue1043.py | 2 +- test/test_issues/test_issue1484.py | 2 +- test/test_issues/test_issue274.py | 4 ++-- test/test_issues/test_issue381.py | 3 +-- test/test_issues/test_issue733.py | 3 +-- test/test_issues/test_issue801.py | 3 +-- test/test_literal/test_literal.py | 3 ++- test/test_literal/test_literal_html5lib.py | 4 ++-- test/test_misc/test_input_source.py | 18 +++++++++--------- test/test_misc/test_networking_redirect.py | 4 ++-- test/test_misc/test_parse_file_guess_format.py | 2 +- test/test_misc/test_security.py | 8 ++++---- test/test_n3.py | 2 +- test/test_namespace/test_definednamespace.py | 2 +- test/test_namespace/test_namespace.py | 2 +- test/test_namespace/test_namespacemanager.py | 2 +- test/test_nt_misc.py | 2 +- .../test_broken_parse_data_from_jena.py | 2 +- test/test_parsers/test_nquads.py | 2 +- test/test_parsers/test_parser_turtlelike.py | 2 +- test/test_parsers/test_swap_n3.py | 2 +- test/test_parsers/test_trix_parse.py | 2 +- test/test_roundtrip.py | 4 ++-- test/test_serializers/test_serializer.py | 6 +++--- .../test_serializers/test_serializer_jsonld.py | 2 +- test/test_serializers/test_serializer_n3.py | 4 ++-- test/test_sparql/test_datetime_processing.py | 2 +- test/test_sparql/test_expressions.py | 2 +- test/test_sparql/test_forward_slash_escapes.py | 4 ++-- test/test_sparql/test_initbindings.py | 3 +-- test/test_sparql/test_prefixed_name.py | 2 +- test/test_sparql/test_result.py | 14 +++++++------- test/test_sparql/test_service.py | 8 ++++---- test/test_sparql/test_sparql.py | 6 +++--- test/test_sparql/test_translate_algebra.py | 2 +- test/test_sparql/test_update.py | 8 ++++---- test/test_store/test_namespace_binding.py | 2 +- test/test_store/test_store_auditable.py | 3 +-- test/test_store/test_store_sparqlstore.py | 6 +++--- .../test_store/test_store_sparqlstore_query.py | 6 +++--- .../test_store_sparqlstore_sparqlconnector.py | 4 ++-- .../test_store/test_store_sparqlupdatestore.py | 2 +- .../test_store_sparqlupdatestore_mock.py | 6 +++--- test/test_tools/test_chunk_serializer.py | 10 +++++----- test/test_tools/test_csv2rdf.py | 1 + test/test_turtle_quoting.py | 2 +- test/test_util.py | 6 +++--- test/test_w3c_spec/test_n3_w3c.py | 6 +++--- test/test_w3c_spec/test_nquads_w3c.py | 10 +++++----- test/test_w3c_spec/test_nt_w3c.py | 10 +++++----- test/test_w3c_spec/test_rdfxml_w3c.py | 10 +++++----- test/test_w3c_spec/test_sparql10_w3c.py | 9 +++++---- test/test_w3c_spec/test_sparql11_w3c.py | 9 +++++---- test/test_w3c_spec/test_sparql_rdflib.py | 9 +++++---- test/test_w3c_spec/test_trig_w3c.py | 10 +++++----- test/test_w3c_spec/test_turtle_w3c.py | 10 +++++----- test/utils/dawg_manifest.py | 8 ++++---- test/utils/earl.py | 6 +++--- test/utils/graph.py | 2 +- test/utils/http.py | 3 ++- test/utils/httpfileserver.py | 3 ++- test/utils/httpservermock.py | 15 ++++++++------- test/utils/iri.py | 4 ++-- test/utils/literal.py | 2 +- test/utils/manifest.py | 2 +- test/utils/sparql_checker.py | 12 ++++++------ test/utils/test/test_httpservermock.py | 5 +++-- test/utils/test/test_iri.py | 3 ++- test/utils/test/test_outcome.py | 3 ++- test/utils/test/test_result.py | 2 +- test/utils/test/test_testutils.py | 12 ++++++------ 101 files changed, 238 insertions(+), 245 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 5bbf5041e..f3431c927 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -10,9 +10,6 @@ pytest.register_assert_rewrite("test.utils") from pathlib import Path -from test.utils.audit import AuditHookDispatcher -from test.utils.http import ctx_http_server -from test.utils.httpfileserver import HTTPFileServer from typing import ( Collection, Dict, @@ -24,6 +21,9 @@ ) from rdflib import Graph +from test.utils.audit import AuditHookDispatcher +from test.utils.http import ctx_http_server +from test.utils.httpfileserver import HTTPFileServer from .data import TEST_DATA_DIR from .utils.earl import EARLReporter diff --git a/test/data.py b/test/data.py index 6cd449fb4..f5f986782 100644 --- a/test/data.py +++ b/test/data.py @@ -1,7 +1,7 @@ from pathlib import Path -from test.utils.graph import cached_graph from rdflib import URIRef +from test.utils.graph import cached_graph TEST_DIR = Path(__file__).parent TEST_DATA_DIR = TEST_DIR / "data" diff --git a/test/data/suites/trix/test_trix.py b/test/data/suites/trix/test_trix.py index e33832afa..8b7c08621 100644 --- a/test/data/suites/trix/test_trix.py +++ b/test/data/suites/trix/test_trix.py @@ -3,9 +3,6 @@ from __future__ import annotations -from test.data import TEST_DATA_DIR -from test.utils.manifest import RDFTest, read_manifest -from test.utils.namespace import RDFT from typing import Callable, Dict import pytest @@ -14,6 +11,9 @@ from rdflib.compare import graph_diff, isomorphic from rdflib.namespace import split_uri from rdflib.term import Node, URIRef +from test.data import TEST_DATA_DIR +from test.utils.manifest import RDFTest, read_manifest +from test.utils.namespace import RDFT verbose = False diff --git a/test/data/variants/diverse_quads.py b/test/data/variants/diverse_quads.py index 8c6a092c7..4c92851f1 100644 --- a/test/data/variants/diverse_quads.py +++ b/test/data/variants/diverse_quads.py @@ -1,8 +1,7 @@ -from test.utils.namespace import EGDC, EGSCHEME, EGURN - from rdflib.graph import ConjunctiveGraph, Graph from rdflib.namespace import XSD from rdflib.term import Literal +from test.utils.namespace import EGDC, EGSCHEME, EGURN def populate_graph(graph: Graph) -> None: diff --git a/test/data/variants/diverse_triples.py b/test/data/variants/diverse_triples.py index 3167a78a6..c77675278 100644 --- a/test/data/variants/diverse_triples.py +++ b/test/data/variants/diverse_triples.py @@ -1,7 +1,6 @@ -from test.utils.namespace import EGDC, EGSCHEME, EGURN - from rdflib.graph import Graph from rdflib.term import Literal +from test.utils.namespace import EGDC, EGSCHEME, EGURN def populate_graph(graph: Graph) -> None: diff --git a/test/data/variants/simple_quad.py b/test/data/variants/simple_quad.py index 09d7ba45d..8d5cccc77 100644 --- a/test/data/variants/simple_quad.py +++ b/test/data/variants/simple_quad.py @@ -1,6 +1,5 @@ -from test.utils.namespace import EGDO - from rdflib.graph import ConjunctiveGraph, Graph +from test.utils.namespace import EGDO def populate_graph(graph: Graph) -> None: diff --git a/test/data/variants/simple_triple.py b/test/data/variants/simple_triple.py index 429f48065..369aca359 100644 --- a/test/data/variants/simple_triple.py +++ b/test/data/variants/simple_triple.py @@ -1,6 +1,5 @@ -from test.utils.namespace import EGDO - from rdflib.graph import Graph +from test.utils.namespace import EGDO def populate_graph(graph: Graph) -> None: diff --git a/test/jsonld/test_nested_arrays.py b/test/jsonld/test_nested_arrays.py index ae495cca0..1207f0e37 100644 --- a/test/jsonld/test_nested_arrays.py +++ b/test/jsonld/test_nested_arrays.py @@ -1,9 +1,8 @@ from __future__ import annotations -from test.utils.namespace import EGDC - from rdflib import Graph, Literal from rdflib.collection import Collection +from test.utils.namespace import EGDC prop = EGDC["props/a"] res = EGDC["res"] diff --git a/test/test_conjunctivegraph/test_conjunctivegraph_generators.py b/test/test_conjunctivegraph/test_conjunctivegraph_generators.py index 853320dd3..1cdc73358 100644 --- a/test/test_conjunctivegraph/test_conjunctivegraph_generators.py +++ b/test/test_conjunctivegraph/test_conjunctivegraph_generators.py @@ -1,7 +1,7 @@ import os -from test.data import TEST_DATA_DIR from rdflib import ConjunctiveGraph, URIRef +from test.data import TEST_DATA_DIR timblcardn3 = open(os.path.join(TEST_DATA_DIR, "timbl-card.n3")).read() diff --git a/test/test_conjunctivegraph/test_conjunctivegraph_operator_combinations.py b/test/test_conjunctivegraph/test_conjunctivegraph_operator_combinations.py index 983453378..f1914b56d 100644 --- a/test/test_conjunctivegraph/test_conjunctivegraph_operator_combinations.py +++ b/test/test_conjunctivegraph/test_conjunctivegraph_operator_combinations.py @@ -1,7 +1,7 @@ import os -from test.data import CHEESE, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR from rdflib import ConjunctiveGraph, Graph +from test.data import CHEESE, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR sportquadstrig = open(os.path.join(TEST_DATA_DIR, "sportquads.trig")).read() diff --git a/test/test_dataset/test_dataset.py b/test/test_dataset/test_dataset.py index 358230ea7..19b9fe830 100644 --- a/test/test_dataset/test_dataset.py +++ b/test/test_dataset/test_dataset.py @@ -2,14 +2,14 @@ import shutil import tempfile import warnings -from test.data import CONTEXT1, LIKES, PIZZA, TAREK -from test.utils.namespace import EGSCHEME import pytest from rdflib import URIRef, plugin from rdflib.graph import DATASET_DEFAULT_GRAPH_ID, Dataset, Graph from rdflib.store import Store +from test.data import CONTEXT1, LIKES, PIZZA, TAREK +from test.utils.namespace import EGSCHEME # Will also run SPARQLUpdateStore tests against local SPARQL1.1 endpoint if # available. This assumes SPARQL1.1 query/update endpoints running locally at diff --git a/test/test_dataset/test_dataset_default_graph.py b/test/test_dataset/test_dataset_default_graph.py index 2d7ff427d..a1de4ebbe 100644 --- a/test/test_dataset/test_dataset_default_graph.py +++ b/test/test_dataset/test_dataset_default_graph.py @@ -2,7 +2,6 @@ import itertools import logging -from test.data import TEST_DATA_DIR from typing import Iterable, Type, Union import pytest @@ -10,6 +9,7 @@ from rdflib.graph import DATASET_DEFAULT_GRAPH_ID, ConjunctiveGraph, Dataset from rdflib.term import BNode, URIRef +from test.data import TEST_DATA_DIR def make_load_default_and_named() -> Iterable[ParameterSet]: diff --git a/test/test_dataset/test_dataset_generators.py b/test/test_dataset/test_dataset_generators.py index 9a698a22a..1b3bd313c 100644 --- a/test/test_dataset/test_dataset_generators.py +++ b/test/test_dataset/test_dataset_generators.py @@ -1,7 +1,7 @@ import os -from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR from rdflib import Dataset, URIRef +from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR timblcardn3 = open(os.path.join(TEST_DATA_DIR, "timbl-card.n3")).read() diff --git a/test/test_extras/test_infixowl/test_basic.py b/test/test_extras/test_infixowl/test_basic.py index 4f0161e07..805ea62f5 100644 --- a/test/test_extras/test_infixowl/test_basic.py +++ b/test/test_extras/test_infixowl/test_basic.py @@ -1,5 +1,3 @@ -from test.data import CONTEXT0 - import pytest from rdflib import OWL, Graph, Literal, Namespace @@ -12,6 +10,7 @@ max, some, ) +from test.data import CONTEXT0 EXNS = Namespace("http://example.org/vocab/") diff --git a/test/test_extras/test_infixowl/test_class.py b/test/test_extras/test_infixowl/test_class.py index 820c788aa..74deb131b 100644 --- a/test/test_extras/test_infixowl/test_class.py +++ b/test/test_extras/test_infixowl/test_class.py @@ -1,5 +1,3 @@ -from test.data import CONTEXT0, CONTEXT1 - import pytest from rdflib import OWL, RDFS, BNode, Graph, Literal, Namespace, URIRef, Variable @@ -12,6 +10,7 @@ max, ) from rdflib.util import first +from test.data import CONTEXT0, CONTEXT1 EXNS = Namespace("http://example.org/vocab/") PZNS = Namespace( diff --git a/test/test_extras/test_infixowl/test_cover.py b/test/test_extras/test_infixowl/test_cover.py index 87889cea3..53fbb1ec4 100644 --- a/test/test_extras/test_infixowl/test_cover.py +++ b/test/test_extras/test_infixowl/test_cover.py @@ -1,5 +1,3 @@ -from test.data import CONTEXT0, TEST_DATA_DIR - import pytest from rdflib import OWL, RDF, RDFS, XSD, Graph, Literal, Namespace, URIRef, logger @@ -28,6 +26,7 @@ some, value, ) +from test.data import CONTEXT0, TEST_DATA_DIR EXNS = Namespace("http://example.org/vocab/") PZNS = Namespace( diff --git a/test/test_extras/test_infixowl/test_individual.py b/test/test_extras/test_infixowl/test_individual.py index e8b4576a0..b07e16852 100644 --- a/test/test_extras/test_infixowl/test_individual.py +++ b/test/test_extras/test_infixowl/test_individual.py @@ -1,9 +1,8 @@ -from test.data import CONTEXT0, PIZZA - import pytest from rdflib import OWL, RDFS, BNode, Graph, Literal, Namespace, URIRef from rdflib.extras.infixowl import Class, Individual +from test.data import CONTEXT0, PIZZA EXNS = Namespace("http://example.org/vocab/") diff --git a/test/test_extras/test_infixowl/test_manchester_syntax.py b/test/test_extras/test_infixowl/test_manchester_syntax.py index 52669af6e..40f1cef10 100644 --- a/test/test_extras/test_infixowl/test_manchester_syntax.py +++ b/test/test_extras/test_infixowl/test_manchester_syntax.py @@ -1,9 +1,8 @@ -from test.data import TEST_DATA_DIR - import pytest from rdflib import OWL, RDFS, Graph, Literal, Namespace from rdflib.extras.infixowl import Class, Individual, manchesterSyntax +from test.data import TEST_DATA_DIR EXNS = Namespace("http://example.org/vocab/") PZNS = Namespace( diff --git a/test/test_graph/test_canonicalization.py b/test/test_graph/test_canonicalization.py index 37c456b64..58a5802a8 100644 --- a/test/test_graph/test_canonicalization.py +++ b/test/test_graph/test_canonicalization.py @@ -1,6 +1,5 @@ from collections import Counter from io import StringIO -from test.utils import GraphHelper from typing import TYPE_CHECKING, Set import pytest @@ -10,6 +9,7 @@ from rdflib.compare import to_canonical_graph, to_isomorphic from rdflib.namespace import FOAF from rdflib.plugins.stores.memory import Memory +from test.utils import GraphHelper if TYPE_CHECKING: from rdflib.graph import _TripleType diff --git a/test/test_graph/test_diff.py b/test/test_graph/test_diff.py index 21bbc4e8b..696d8be89 100644 --- a/test/test_graph/test_diff.py +++ b/test/test_graph/test_diff.py @@ -1,15 +1,6 @@ from __future__ import annotations from dataclasses import dataclass, field -from test.utils import ( - COLLAPSED_BNODE, - BNodeHandling, - GHQuad, - GHTriple, - GraphHelper, - MarksType, - MarkType, -) from typing import TYPE_CHECKING, Collection, Set, Tuple, Type, Union, cast import pytest @@ -21,6 +12,15 @@ from rdflib.graph import ConjunctiveGraph, Dataset from rdflib.namespace import FOAF, RDF from rdflib.term import BNode, Literal +from test.utils import ( + COLLAPSED_BNODE, + BNodeHandling, + GHQuad, + GHTriple, + GraphHelper, + MarksType, + MarkType, +) if TYPE_CHECKING: from rdflib.graph import _TripleType diff --git a/test/test_graph/test_graph.py b/test/test_graph/test_graph.py index 9d7b8b622..639aa710c 100644 --- a/test/test_graph/test_graph.py +++ b/test/test_graph/test_graph.py @@ -3,10 +3,6 @@ import logging import os from pathlib import Path -from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR -from test.utils import GraphHelper, get_unique_plugin_names -from test.utils.httpfileserver import HTTPFileServer, ProtoFileResource -from test.utils.outcome import ExceptionChecker, OutcomeChecker, OutcomePrimitive from typing import Callable, Optional, Set, Tuple from urllib.error import HTTPError, URLError @@ -18,6 +14,10 @@ from rdflib.plugin import PluginException from rdflib.store import Store from rdflib.term import BNode +from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR +from test.utils import GraphHelper, get_unique_plugin_names +from test.utils.httpfileserver import HTTPFileServer, ProtoFileResource +from test.utils.outcome import ExceptionChecker, OutcomeChecker, OutcomePrimitive def test_property_store() -> None: diff --git a/test/test_graph/test_graph_cbd.py b/test/test_graph/test_graph_cbd.py index 8e5de68af..d871bdd59 100644 --- a/test/test_graph/test_graph_cbd.py +++ b/test/test_graph/test_graph_cbd.py @@ -1,13 +1,12 @@ """Tests the Graph class' cbd() function""" -from test.data import TEST_DATA_DIR -from test.utils import BNodeHandling, GraphHelper - import pytest from rdflib import Graph, Namespace from rdflib.namespace import RDF, RDFS from rdflib.term import Literal, URIRef +from test.data import TEST_DATA_DIR +from test.utils import BNodeHandling, GraphHelper EXAMPLE_GRAPH_FILE_PATH = TEST_DATA_DIR / "spec" / "cbd" / "example_graph.rdf" EXAMPLE_GRAPH_CBD_FILE_PATH = TEST_DATA_DIR / "spec" / "cbd" / "example_graph_cbd.rdf" diff --git a/test/test_graph/test_graph_generators.py b/test/test_graph/test_graph_generators.py index 1bc8e1402..0d89c9b7f 100644 --- a/test/test_graph/test_graph_generators.py +++ b/test/test_graph/test_graph_generators.py @@ -1,7 +1,7 @@ import os -from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR from rdflib import Graph +from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR timblcardn3 = open(os.path.join(TEST_DATA_DIR, "timbl-card.n3")).read() diff --git a/test/test_graph/test_graph_http.py b/test/test_graph/test_graph_http.py index 8391a0238..11eebe38b 100644 --- a/test/test_graph/test_graph_http.py +++ b/test/test_graph/test_graph_http.py @@ -3,6 +3,11 @@ import logging import re from http.server import BaseHTTPRequestHandler +from urllib.error import HTTPError + +import pytest + +from rdflib import Graph from test.data import TEST_DATA_DIR from test.utils import GraphHelper from test.utils.graph import cached_graph @@ -16,11 +21,6 @@ from test.utils.httpservermock import ServedBaseHTTPServerMock from test.utils.namespace import EGDO from test.utils.wildcard import URL_PARSE_RESULT_WILDCARD -from urllib.error import HTTPError - -import pytest - -from rdflib import Graph """ Test that correct content negotiation headers are passed diff --git a/test/test_graph/test_graph_redirect.py b/test/test_graph/test_graph_redirect.py index a26331cc9..b91623baa 100644 --- a/test/test_graph/test_graph_redirect.py +++ b/test/test_graph/test_graph_redirect.py @@ -1,13 +1,13 @@ from __future__ import annotations -from test.data import SIMPLE_TRIPLE_GRAPH, TEST_DATA_DIR -from test.utils import GraphHelper -from test.utils.http import MethodName, MockHTTPResponse -from test.utils.httpservermock import ServedBaseHTTPServerMock from typing import Tuple from urllib.parse import urlparse from rdflib.graph import Graph +from test.data import SIMPLE_TRIPLE_GRAPH, TEST_DATA_DIR +from test.utils import GraphHelper +from test.utils.http import MethodName, MockHTTPResponse +from test.utils.httpservermock import ServedBaseHTTPServerMock def test_graph_redirect_new_host( diff --git a/test/test_graph/test_graph_store.py b/test/test_graph/test_graph_store.py index 24c592dfd..9a3397702 100644 --- a/test/test_graph/test_graph_store.py +++ b/test/test_graph/test_graph_store.py @@ -6,7 +6,6 @@ import itertools import logging -from test.data import SIMPLE_TRIPLE_GRAPH from typing import ( TYPE_CHECKING, Any, @@ -32,6 +31,7 @@ from rdflib.query import Result from rdflib.store import Store from rdflib.term import Identifier, URIRef, Variable +from test.data import SIMPLE_TRIPLE_GRAPH if TYPE_CHECKING: from _pytest.mark.structures import ParameterSet diff --git a/test/test_graph/test_namespace_rebinding.py b/test/test_graph/test_namespace_rebinding.py index 34ce668e7..babac1b4f 100644 --- a/test/test_graph/test_namespace_rebinding.py +++ b/test/test_graph/test_namespace_rebinding.py @@ -1,11 +1,10 @@ -from test.data import CONTEXT1, CONTEXT2, TAREK - import pytest from rdflib import ConjunctiveGraph, Graph, Literal from rdflib.namespace import OWL, Namespace, NamespaceManager from rdflib.plugins.stores.memory import Memory from rdflib.term import URIRef +from test.data import CONTEXT1, CONTEXT2, TAREK foaf1_uri = URIRef("http://xmlns.com/foaf/0.1/") foaf2_uri = URIRef("http://xmlns.com/foaf/2.0/") diff --git a/test/test_graph/test_skolemization.py b/test/test_graph/test_skolemization.py index acc77964e..78da75493 100644 --- a/test/test_graph/test_skolemization.py +++ b/test/test_graph/test_skolemization.py @@ -2,14 +2,14 @@ import logging import re -from test.utils import GraphHelper -from test.utils.namespace import EGDC from typing import Pattern, Union import pytest from rdflib import Graph from rdflib.term import BNode, Literal, URIRef +from test.utils import GraphHelper +from test.utils.namespace import EGDC base_triples = { (EGDC.subject, EGDC.predicate, EGDC.object0), diff --git a/test/test_graph/test_slice.py b/test/test_graph/test_slice.py index 7258e5ca8..7e503f8ed 100644 --- a/test/test_graph/test_slice.py +++ b/test/test_graph/test_slice.py @@ -1,6 +1,5 @@ -from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK - from rdflib import Graph +from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK class TestGraphSlice: diff --git a/test/test_graph/test_variants.py b/test/test_graph/test_variants.py index a65a923c9..1136753a4 100644 --- a/test/test_graph/test_variants.py +++ b/test/test_graph/test_variants.py @@ -9,9 +9,6 @@ from collections import defaultdict from dataclasses import dataclass, field from pathlib import Path, PurePath -from test.data import TEST_DATA_DIR -from test.utils import GraphHelper -from test.utils.graph import GraphSource from typing import ( ClassVar, Collection, @@ -36,6 +33,9 @@ from rdflib.graph import Dataset, _GraphT from rdflib.namespace import XSD from rdflib.term import URIRef +from test.data import TEST_DATA_DIR +from test.utils import GraphHelper +from test.utils.graph import GraphSource MODULE_PATH = Path(__file__).parent diff --git a/test/test_issues/test_issue1043.py b/test/test_issues/test_issue1043.py index c70c9b880..09c54af3f 100644 --- a/test/test_issues/test_issue1043.py +++ b/test/test_issues/test_issue1043.py @@ -1,8 +1,8 @@ import io import sys -from test.utils.namespace import EGDO from rdflib import RDFS, XSD, Graph, Literal +from test.utils.namespace import EGDO def test_issue_1043(): diff --git a/test/test_issues/test_issue1484.py b/test/test_issues/test_issue1484.py index 1c1b9ea10..87fdb2080 100644 --- a/test/test_issues/test_issue1484.py +++ b/test/test_issues/test_issue1484.py @@ -1,8 +1,8 @@ import io import json -from test.utils.namespace import EGDO from rdflib import RDF, RDFS, Graph +from test.utils.namespace import EGDO def test_issue_1484_json(): diff --git a/test/test_issues/test_issue274.py b/test/test_issues/test_issue274.py index 8b5e5b209..bb66f976a 100644 --- a/test/test_issues/test_issue274.py +++ b/test/test_issues/test_issue274.py @@ -1,5 +1,3 @@ -from test.utils import eq_ -from test.utils.namespace import EGDO from unittest import TestCase import pytest @@ -9,6 +7,8 @@ register_custom_function, unregister_custom_function, ) +from test.utils import eq_ +from test.utils.namespace import EGDO G = Graph() G.add((BNode(), RDFS.label, Literal("bnode"))) diff --git a/test/test_issues/test_issue381.py b/test/test_issues/test_issue381.py index 50fb8b7e9..190ee506b 100644 --- a/test/test_issues/test_issue381.py +++ b/test/test_issues/test_issue381.py @@ -1,7 +1,6 @@ -from test.utils.namespace import EGDO - from rdflib import BNode, Graph from rdflib.compare import isomorphic +from test.utils.namespace import EGDO def test_no_spurious_semicolon(): diff --git a/test/test_issues/test_issue733.py b/test/test_issues/test_issue733.py index e32f7a02e..502cd07ef 100644 --- a/test/test_issues/test_issue733.py +++ b/test/test_issues/test_issue733.py @@ -5,9 +5,8 @@ subject or the object. """ -from test.utils.namespace import EGDO - from rdflib import Graph +from test.utils.namespace import EGDO def test_issue_733(): diff --git a/test/test_issues/test_issue801.py b/test/test_issues/test_issue801.py index 338a2648c..564711c73 100644 --- a/test/test_issues/test_issue801.py +++ b/test/test_issues/test_issue801.py @@ -2,9 +2,8 @@ Issue 801 - Problem with prefixes created for URIs containing %20 """ -from test.utils.namespace import EGDO - from rdflib import BNode, Graph, Literal +from test.utils.namespace import EGDO def test_issue_801(): diff --git a/test/test_literal/test_literal.py b/test/test_literal/test_literal.py index 1a0383162..59ebab5dd 100644 --- a/test/test_literal/test_literal.py +++ b/test/test_literal/test_literal.py @@ -4,11 +4,12 @@ import datetime import logging from decimal import Decimal +from typing import Any, Callable, Generator, Optional, Type, Union + from test.utils import affix_tuples from test.utils.literal import LiteralChecker, literal_idfn from test.utils.namespace import EGDC from test.utils.outcome import OutcomeChecker, OutcomePrimitive, OutcomePrimitives -from typing import Any, Callable, Generator, Optional, Type, Union # NOTE: The config below enables strict mode for mypy. # mypy: no_ignore_errors diff --git a/test/test_literal/test_literal_html5lib.py b/test/test_literal/test_literal_html5lib.py index ce39039c0..ce22568db 100644 --- a/test/test_literal/test_literal_html5lib.py +++ b/test/test_literal/test_literal_html5lib.py @@ -1,6 +1,4 @@ import xml.dom.minidom -from test.utils.literal import LiteralChecker -from test.utils.outcome import OutcomeChecker, OutcomePrimitives from typing import Callable import pytest @@ -8,6 +6,8 @@ import rdflib.term from rdflib.namespace import RDF from rdflib.term import Literal +from test.utils.literal import LiteralChecker +from test.utils.outcome import OutcomeChecker, OutcomePrimitives try: import html5lib as _ # noqa: F401 diff --git a/test/test_misc/test_input_source.py b/test/test_misc/test_input_source.py index 2280bcd5e..f9ae8393e 100644 --- a/test/test_misc/test_input_source.py +++ b/test/test_misc/test_input_source.py @@ -8,15 +8,6 @@ from contextlib import ExitStack, contextmanager from dataclasses import dataclass from pathlib import Path -from test.utils import GraphHelper -from test.utils.httpfileserver import ( - HTTPFileInfo, - HTTPFileServer, - LocationType, - ProtoFileResource, - ProtoRedirectResource, -) -from test.utils.outcome import ExceptionChecker from typing import ( # Callable, IO, BinaryIO, @@ -44,6 +35,15 @@ URLInputSource, create_input_source, ) +from test.utils import GraphHelper +from test.utils.httpfileserver import ( + HTTPFileInfo, + HTTPFileServer, + LocationType, + ProtoFileResource, + ProtoRedirectResource, +) +from test.utils.outcome import ExceptionChecker from ..data import TEST_DATA_DIR diff --git a/test/test_misc/test_networking_redirect.py b/test/test_misc/test_networking_redirect.py index 6c405f27a..0412f9bf7 100644 --- a/test/test_misc/test_networking_redirect.py +++ b/test/test_misc/test_networking_redirect.py @@ -2,8 +2,6 @@ from contextlib import ExitStack from copy import deepcopy -from test.utils.http import headers_as_message as headers_as_message -from test.utils.outcome import ExceptionChecker from typing import Any, Dict, Iterable, Optional, Type, TypeVar, Union from urllib.error import HTTPError from urllib.request import HTTPRedirectHandler, Request @@ -12,6 +10,8 @@ from _pytest.mark.structures import ParameterSet from rdflib._networking import _make_redirect_request +from test.utils.http import headers_as_message as headers_as_message +from test.utils.outcome import ExceptionChecker AnyT = TypeVar("AnyT") diff --git a/test/test_misc/test_parse_file_guess_format.py b/test/test_misc/test_parse_file_guess_format.py index 614a3d912..5c190aa56 100644 --- a/test/test_misc/test_parse_file_guess_format.py +++ b/test/test_misc/test_parse_file_guess_format.py @@ -12,13 +12,13 @@ from pathlib import Path from shutil import copyfile from tempfile import TemporaryDirectory -from test.data import TEST_DATA_DIR import pytest from rdflib import Graph from rdflib.exceptions import ParserError from rdflib.util import guess_format +from test.data import TEST_DATA_DIR class TestFileParserGuessFormat: diff --git a/test/test_misc/test_security.py b/test/test_misc/test_security.py index bbd46e1b5..090785a2d 100644 --- a/test/test_misc/test_security.py +++ b/test/test_misc/test_security.py @@ -6,10 +6,6 @@ import logging from contextlib import ExitStack from pathlib import Path -from test.utils.audit import AuditHookDispatcher -from test.utils.httpfileserver import HTTPFileServer, ProtoFileResource -from test.utils.namespace import EGDO -from test.utils.urlopen import context_urlopener from textwrap import dedent from typing import Any, Iterable, Tuple from urllib.request import HTTPHandler, OpenerDirector, Request @@ -18,6 +14,10 @@ from _pytest.mark.structures import ParameterSet from rdflib import Graph +from test.utils.audit import AuditHookDispatcher +from test.utils.httpfileserver import HTTPFileServer, ProtoFileResource +from test.utils.namespace import EGDO +from test.utils.urlopen import context_urlopener from ..utils import GraphHelper from ..utils.path import ctx_chdir diff --git a/test/test_n3.py b/test/test_n3.py index c22278efd..f3d7eeb07 100644 --- a/test/test_n3.py +++ b/test/test_n3.py @@ -1,6 +1,5 @@ import itertools import os -from test import TEST_DIR from urllib.error import URLError import pytest @@ -8,6 +7,7 @@ from rdflib.graph import ConjunctiveGraph, Graph from rdflib.plugins.parsers.notation3 import BadSyntax, exponent_syntax from rdflib.term import Literal, URIRef +from test import TEST_DIR test_data = """ # Definitions of terms describing the n3 model diff --git a/test/test_namespace/test_definednamespace.py b/test/test_namespace/test_definednamespace.py index 91b0729f0..ea8e12969 100644 --- a/test/test_namespace/test_definednamespace.py +++ b/test/test_namespace/test_definednamespace.py @@ -8,7 +8,6 @@ from contextlib import ExitStack from dataclasses import dataclass from pathlib import Path -from test.data import TEST_DATA_DIR from typing import Optional, Type import pytest @@ -16,6 +15,7 @@ from rdflib import RDF, SKOS from rdflib.namespace import DefinedNamespace, Namespace from rdflib.term import URIRef +from test.data import TEST_DATA_DIR def test_definednamespace_creator_qb(): diff --git a/test/test_namespace/test_namespace.py b/test/test_namespace/test_namespace.py index adf0b3fce..fe2e2aa7d 100644 --- a/test/test_namespace/test_namespace.py +++ b/test/test_namespace/test_namespace.py @@ -1,6 +1,5 @@ from __future__ import annotations -from test.utils.outcome import OutcomeChecker, OutcomePrimitive from typing import Any, Optional from warnings import warn @@ -20,6 +19,7 @@ URIPattern, ) from rdflib.term import BNode, Literal, URIRef +from test.utils.outcome import OutcomeChecker, OutcomePrimitive class TestNamespace: diff --git a/test/test_namespace/test_namespacemanager.py b/test/test_namespace/test_namespacemanager.py index 7c678cc7b..4ca182ae2 100644 --- a/test/test_namespace/test_namespacemanager.py +++ b/test/test_namespace/test_namespacemanager.py @@ -2,7 +2,6 @@ import logging from contextlib import ExitStack -from test.utils.outcome import ExceptionChecker, OutcomeChecker, OutcomePrimitive from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Set, Tuple, Type, Union import pytest @@ -18,6 +17,7 @@ NamespaceManager, ) from rdflib.term import URIRef +from test.utils.outcome import ExceptionChecker, OutcomeChecker, OutcomePrimitive if TYPE_CHECKING: from rdflib._type_checking import _NamespaceSetString diff --git a/test/test_nt_misc.py b/test/test_nt_misc.py index 78f2c729e..7f48b61c1 100644 --- a/test/test_nt_misc.py +++ b/test/test_nt_misc.py @@ -2,13 +2,13 @@ import os import re from pathlib import Path -from test.data import TEST_DATA_DIR from urllib.request import urlopen import pytest from rdflib import Graph, Literal, URIRef from rdflib.plugins.parsers import ntriples +from test.data import TEST_DATA_DIR log = logging.getLogger(__name__) diff --git a/test/test_parsers/test_broken_parse_data_from_jena.py b/test/test_parsers/test_broken_parse_data_from_jena.py index ec176a4cc..353593837 100644 --- a/test/test_parsers/test_broken_parse_data_from_jena.py +++ b/test/test_parsers/test_broken_parse_data_from_jena.py @@ -1,9 +1,9 @@ import os -from test.data import TEST_DATA_DIR import pytest import rdflib +from test.data import TEST_DATA_DIR # Recovered from # https://github.com/RDFLib/rdflib/tree/6b4607018ebf589da74aea4c25408999f1acf2e2 diff --git a/test/test_parsers/test_nquads.py b/test/test_parsers/test_nquads.py index 843fe237f..ad17b5aee 100644 --- a/test/test_parsers/test_nquads.py +++ b/test/test_parsers/test_nquads.py @@ -1,7 +1,7 @@ import os -from test.data import TEST_DATA_DIR from rdflib import ConjunctiveGraph, Namespace, URIRef +from test.data import TEST_DATA_DIR TEST_BASE = os.path.join(TEST_DATA_DIR, "nquads.rdflib") diff --git a/test/test_parsers/test_parser_turtlelike.py b/test/test_parsers/test_parser_turtlelike.py index 7568974c7..2f0002b26 100644 --- a/test/test_parsers/test_parser_turtlelike.py +++ b/test/test_parsers/test_parser_turtlelike.py @@ -8,7 +8,6 @@ import enum import itertools from dataclasses import dataclass, field -from test.utils.namespace import EGDC from typing import Callable, Dict, Iterator, List, Set, Tuple, Union import pytest @@ -17,6 +16,7 @@ from rdflib import XSD, Graph, Literal from rdflib.term import Identifier from rdflib.util import from_n3 +from test.utils.namespace import EGDC class FormatTrait(enum.Enum): diff --git a/test/test_parsers/test_swap_n3.py b/test/test_parsers/test_swap_n3.py index e173b8452..5de06f927 100644 --- a/test/test_parsers/test_swap_n3.py +++ b/test/test_parsers/test_swap_n3.py @@ -1,9 +1,9 @@ import os -from test.data import TEST_DATA_DIR import pytest import rdflib +from test.data import TEST_DATA_DIR """ SWAP N3 parser test suite diff --git a/test/test_parsers/test_trix_parse.py b/test/test_parsers/test_trix_parse.py index 70eaac130..e6f2ae91b 100644 --- a/test/test_parsers/test_trix_parse.py +++ b/test/test_parsers/test_trix_parse.py @@ -1,7 +1,7 @@ import os -from test.data import TEST_DATA_DIR from rdflib.graph import ConjunctiveGraph +from test.data import TEST_DATA_DIR class TestTrixParse: diff --git a/test/test_roundtrip.py b/test/test_roundtrip.py index e159ccf1e..db007b96d 100644 --- a/test/test_roundtrip.py +++ b/test/test_roundtrip.py @@ -26,8 +26,6 @@ import logging import os.path from pathlib import Path -from test.data import TEST_DATA_DIR -from test.utils import BNodeHandling, GraphHelper from typing import Callable, Iterable, List, Optional, Set, Tuple, Type, Union from xml.sax import SAXParseException @@ -42,6 +40,8 @@ from rdflib.plugins.parsers.notation3 import BadSyntax from rdflib.serializer import Serializer from rdflib.util import guess_format +from test.data import TEST_DATA_DIR +from test.utils import BNodeHandling, GraphHelper logger = logging.getLogger(__name__) diff --git a/test/test_serializers/test_serializer.py b/test/test_serializers/test_serializer.py index 74701df7e..19c1be936 100644 --- a/test/test_serializers/test_serializer.py +++ b/test/test_serializers/test_serializer.py @@ -9,9 +9,6 @@ from dataclasses import dataclass, field from functools import lru_cache from pathlib import Path, PosixPath, PurePath -from test.utils import GraphHelper, get_unique_plugins -from test.utils.destination import DestinationType, DestParmType, DestRef -from test.utils.namespace import EGDC, EGSCHEME, EGURN from typing import ( IO, Callable, @@ -35,6 +32,9 @@ from rdflib import RDF, XSD, Graph, Literal, Namespace, URIRef from rdflib.graph import DATASET_DEFAULT_GRAPH_ID, ConjunctiveGraph, Dataset from rdflib.serializer import Serializer +from test.utils import GraphHelper, get_unique_plugins +from test.utils.destination import DestinationType, DestParmType, DestRef +from test.utils.namespace import EGDC, EGSCHEME, EGURN @pytest.mark.parametrize( diff --git a/test/test_serializers/test_serializer_jsonld.py b/test/test_serializers/test_serializer_jsonld.py index 7f7118e1b..44b36c9dc 100644 --- a/test/test_serializers/test_serializer_jsonld.py +++ b/test/test_serializers/test_serializer_jsonld.py @@ -3,13 +3,13 @@ import json import logging import pprint -from test.utils.namespace import EGDO from typing import Any, Dict, Union import pytest from rdflib import Graph from rdflib.plugins.shared.jsonld.context import Context +from test.utils.namespace import EGDO @pytest.mark.parametrize( diff --git a/test/test_serializers/test_serializer_n3.py b/test/test_serializers/test_serializer_n3.py index 4c8618a35..f266da715 100644 --- a/test/test_serializers/test_serializer_n3.py +++ b/test/test_serializers/test_serializer_n3.py @@ -1,6 +1,4 @@ import logging -from test.utils import GraphHelper -from test.utils.namespace import EGDC import rdflib import rdflib.term @@ -8,6 +6,8 @@ from rdflib.graph import QuotedGraph from rdflib.plugins.parsers.notation3 import LOG_implies_URI from rdflib.term import BNode, URIRef +from test.utils import GraphHelper +from test.utils.namespace import EGDC logger = logging.getLogger(__name__) diff --git a/test/test_sparql/test_datetime_processing.py b/test/test_sparql/test_datetime_processing.py index 3bfe59bc5..c934e9543 100644 --- a/test/test_sparql/test_datetime_processing.py +++ b/test/test_sparql/test_datetime_processing.py @@ -1,8 +1,8 @@ import io -from test.utils import eq_ import rdflib from rdflib import Graph +from test.utils import eq_ def test_datetime_datetime_subs_issue(): diff --git a/test/test_sparql/test_expressions.py b/test/test_sparql/test_expressions.py index b6c551a06..200c68543 100644 --- a/test/test_sparql/test_expressions.py +++ b/test/test_sparql/test_expressions.py @@ -1,11 +1,11 @@ from functools import partial -from test.utils import eq_ as eq import rdflib.plugins.sparql.parser as p from rdflib import Literal, Variable from rdflib.plugins.sparql.algebra import translatePName, traverse from rdflib.plugins.sparql.operators import simplify from rdflib.plugins.sparql.sparql import Prologue, QueryContext, SPARQLError +from test.utils import eq_ as eq def _eval(e, ctx=None): diff --git a/test/test_sparql/test_forward_slash_escapes.py b/test/test_sparql/test_forward_slash_escapes.py index b2d54b6b8..d7c72d07b 100644 --- a/test/test_sparql/test_forward_slash_escapes.py +++ b/test/test_sparql/test_forward_slash_escapes.py @@ -18,8 +18,6 @@ """ from __future__ import annotations -from test.data import TEST_DATA_DIR -from test.utils.graph import cached_graph from typing import Set import pytest @@ -28,6 +26,8 @@ from rdflib.plugins.sparql.processor import prepareQuery from rdflib.plugins.sparql.sparql import Query from rdflib.query import ResultRow +from test.data import TEST_DATA_DIR +from test.utils.graph import cached_graph query_string_expanded = r""" SELECT ?nIndividual diff --git a/test/test_sparql/test_initbindings.py b/test/test_sparql/test_initbindings.py index 6304ede37..0cb55f344 100644 --- a/test/test_sparql/test_initbindings.py +++ b/test/test_sparql/test_initbindings.py @@ -1,7 +1,6 @@ -from test.utils.namespace import EGDC - from rdflib import ConjunctiveGraph, Literal, URIRef, Variable from rdflib.plugins.sparql import prepareQuery +from test.utils.namespace import EGDC g = ConjunctiveGraph() diff --git a/test/test_sparql/test_prefixed_name.py b/test/test_sparql/test_prefixed_name.py index e1976dac1..ae869bf4a 100644 --- a/test/test_sparql/test_prefixed_name.py +++ b/test/test_sparql/test_prefixed_name.py @@ -2,7 +2,6 @@ import itertools import logging -from test.utils.outcome import OutcomeChecker, OutcomePrimitive import pyparsing import pytest @@ -11,6 +10,7 @@ from rdflib import Graph from rdflib.namespace import Namespace from rdflib.term import Node, URIRef +from test.utils.outcome import OutcomeChecker, OutcomePrimitive RESERVED_PCHARS = [ "%20", diff --git a/test/test_sparql/test_result.py b/test/test_sparql/test_result.py index c08fbdedf..8fbac2e7f 100644 --- a/test/test_sparql/test_result.py +++ b/test/test_sparql/test_result.py @@ -9,13 +9,6 @@ from contextlib import ExitStack from io import BytesIO, StringIO from pathlib import Path, PosixPath, PurePath -from test.utils.destination import DestinationType, DestParmType -from test.utils.result import ( - ResultFormat, - ResultFormatInfo, - ResultFormatTrait, - ResultType, -) from typing import ( IO, BinaryIO, @@ -41,6 +34,13 @@ from rdflib.namespace import Namespace from rdflib.query import Result, ResultRow from rdflib.term import BNode, Identifier, Literal, Node, Variable +from test.utils.destination import DestinationType, DestParmType +from test.utils.result import ( + ResultFormat, + ResultFormatInfo, + ResultFormatTrait, + ResultType, +) BindingsType = Sequence[Mapping[Variable, Identifier]] ParseOutcomeType = Union[BindingsType, Type[Exception]] diff --git a/test/test_sparql/test_service.py b/test/test_sparql/test_service.py index 8defd2ced..83ac8388f 100644 --- a/test/test_sparql/test_service.py +++ b/test/test_sparql/test_service.py @@ -2,10 +2,6 @@ import json from http.client import IncompleteRead, RemoteDisconnected -from test.utils import helper -from test.utils.http import MethodName, MockHTTPResponse -from test.utils.httpservermock import ServedBaseHTTPServerMock -from test.utils.outcome import OutcomeChecker from typing import Dict, FrozenSet, List, Mapping, Sequence, Tuple, Type, Union import pytest @@ -13,6 +9,10 @@ from rdflib import Graph, Literal, URIRef, Variable from rdflib.namespace import XSD from rdflib.term import BNode, Identifier +from test.utils import helper +from test.utils.http import MethodName, MockHTTPResponse +from test.utils.httpservermock import ServedBaseHTTPServerMock +from test.utils.outcome import OutcomeChecker @pytest.mark.webtest diff --git a/test/test_sparql/test_sparql.py b/test/test_sparql/test_sparql.py index e14d57d41..a42f7ffa5 100644 --- a/test/test_sparql/test_sparql.py +++ b/test/test_sparql/test_sparql.py @@ -1,9 +1,6 @@ from __future__ import annotations import logging -from test.utils import eq_ -from test.utils.namespace import EGDC -from test.utils.result import assert_bindings_collections_equal from typing import Any, Callable, Mapping, Sequence, Type import pytest @@ -24,6 +21,9 @@ from rdflib.plugins.sparql.sparql import SPARQLError from rdflib.query import Result, ResultRow from rdflib.term import Identifier, Variable +from test.utils import eq_ +from test.utils.namespace import EGDC +from test.utils.result import assert_bindings_collections_equal def test_graph_prefix(): diff --git a/test/test_sparql/test_translate_algebra.py b/test/test_sparql/test_translate_algebra.py index 1fca0fa1d..59e747f65 100644 --- a/test/test_sparql/test_translate_algebra.py +++ b/test/test_sparql/test_translate_algebra.py @@ -5,7 +5,6 @@ from dataclasses import dataclass, field from io import StringIO from pathlib import Path -from test.data import TEST_DATA_DIR from typing import Collection, Tuple, Union, cast import pytest @@ -15,6 +14,7 @@ import rdflib.plugins.sparql.parser as parser from rdflib import Graph, Literal, URIRef from rdflib.plugins.sparql.algebra import translateAlgebra +from test.data import TEST_DATA_DIR @pytest.fixture diff --git a/test/test_sparql/test_update.py b/test/test_sparql/test_update.py index daf45dc6e..17c7967fa 100644 --- a/test/test_sparql/test_update.py +++ b/test/test_sparql/test_update.py @@ -1,14 +1,14 @@ import itertools import logging -from test.data import TEST_DATA_DIR -from test.utils import GraphHelper -from test.utils.graph import GraphSource -from test.utils.namespace import EGDO from typing import Callable import pytest from rdflib.graph import ConjunctiveGraph, Dataset, Graph +from test.data import TEST_DATA_DIR +from test.utils import GraphHelper +from test.utils.graph import GraphSource +from test.utils.namespace import EGDO @pytest.mark.parametrize( diff --git a/test/test_store/test_namespace_binding.py b/test/test_store/test_namespace_binding.py index 1033a6e5f..ae5ca680a 100644 --- a/test/test_store/test_namespace_binding.py +++ b/test/test_store/test_namespace_binding.py @@ -5,7 +5,6 @@ import logging from dataclasses import dataclass, field from pathlib import Path -from test.utils import pytest_mark_filter from typing import Any, Callable, Dict, Set, Union import pytest @@ -15,6 +14,7 @@ from rdflib.plugins.stores.berkeleydb import has_bsddb from rdflib.store import Store from rdflib.term import IdentifiedNode, URIRef +from test.utils import pytest_mark_filter class StoreTrait(enum.Enum): diff --git a/test/test_store/test_store_auditable.py b/test/test_store/test_store_auditable.py index b9d152dec..dfb6e90ba 100644 --- a/test/test_store/test_store_auditable.py +++ b/test/test_store/test_store_auditable.py @@ -1,9 +1,8 @@ -from test.utils.namespace import EGDO - import pytest from rdflib import Graph from rdflib.plugins.stores.auditable import AuditableStore +from test.utils.namespace import EGDO @pytest.fixture diff --git a/test/test_store/test_store_sparqlstore.py b/test/test_store/test_store_sparqlstore.py index 1aabdc011..1e9233242 100644 --- a/test/test_store/test_store_sparqlstore.py +++ b/test/test_store/test_store_sparqlstore.py @@ -4,9 +4,6 @@ import re import socket from http.server import BaseHTTPRequestHandler, HTTPServer -from test.utils import helper -from test.utils.http import MethodName, MockHTTPResponse -from test.utils.httpservermock import ServedBaseHTTPServerMock from threading import Thread from typing import Callable, ClassVar, Type from unittest.mock import patch @@ -16,6 +13,9 @@ from rdflib import Graph, Literal, URIRef from rdflib.namespace import FOAF, RDF, RDFS, XMLNS, XSD from rdflib.plugins.stores.sparqlconnector import SPARQLConnector +from test.utils import helper +from test.utils.http import MethodName, MockHTTPResponse +from test.utils.httpservermock import ServedBaseHTTPServerMock class TestSPARQLStoreGraph: diff --git a/test/test_store/test_store_sparqlstore_query.py b/test/test_store/test_store_sparqlstore_query.py index b22585921..b8089e4d9 100644 --- a/test/test_store/test_store_sparqlstore_query.py +++ b/test/test_store/test_store_sparqlstore_query.py @@ -2,15 +2,15 @@ import itertools import logging -from test.utils import GraphHelper -from test.utils.http import MethodName, MockHTTPResponse -from test.utils.httpservermock import ServedBaseHTTPServerMock from typing import Dict, Iterable, List, Optional, Set, Tuple import pytest from _pytest.mark.structures import ParameterSet from rdflib.graph import Graph +from test.utils import GraphHelper +from test.utils.http import MethodName, MockHTTPResponse +from test.utils.httpservermock import ServedBaseHTTPServerMock def make_test_query_construct_format_cases() -> Iterable[ParameterSet]: diff --git a/test/test_store/test_store_sparqlstore_sparqlconnector.py b/test/test_store/test_store_sparqlstore_sparqlconnector.py index 992ef2b07..acc14389f 100644 --- a/test/test_store/test_store_sparqlstore_sparqlconnector.py +++ b/test/test_store/test_store_sparqlstore_sparqlconnector.py @@ -2,14 +2,14 @@ import json import logging -from test.utils.http import MethodName, MockHTTPResponse -from test.utils.httpservermock import ServedBaseHTTPServerMock from typing import Optional import pytest from rdflib.graph import Graph from rdflib.plugins.stores.sparqlstore import SPARQLStore +from test.utils.http import MethodName, MockHTTPResponse +from test.utils.httpservermock import ServedBaseHTTPServerMock @pytest.mark.parametrize( diff --git a/test/test_store/test_store_sparqlupdatestore.py b/test/test_store/test_store_sparqlupdatestore.py index 1843a0b86..5c9c38005 100644 --- a/test/test_store/test_store_sparqlupdatestore.py +++ b/test/test_store/test_store_sparqlupdatestore.py @@ -1,10 +1,10 @@ import re -from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK from urllib.request import urlopen import pytest from rdflib import BNode, ConjunctiveGraph, Graph, Literal, URIRef +from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK HOST = "http://localhost:3031" DB = "/db/" diff --git a/test/test_store/test_store_sparqlupdatestore_mock.py b/test/test_store/test_store_sparqlupdatestore_mock.py index 5681dd0e1..5d7e13eb8 100644 --- a/test/test_store/test_store_sparqlupdatestore_mock.py +++ b/test/test_store/test_store_sparqlupdatestore_mock.py @@ -1,10 +1,10 @@ -from test.utils.http import MethodName, MockHTTPResponse -from test.utils.httpservermock import ServedBaseHTTPServerMock -from test.utils.namespace import EGDO from typing import ClassVar from rdflib.graph import ConjunctiveGraph from rdflib.plugins.stores.sparqlstore import SPARQLUpdateStore +from test.utils.http import MethodName, MockHTTPResponse +from test.utils.httpservermock import ServedBaseHTTPServerMock +from test.utils.namespace import EGDO class TestSPARQLConnector: diff --git a/test/test_tools/test_chunk_serializer.py b/test/test_tools/test_chunk_serializer.py index 4f582b192..d683ca606 100644 --- a/test/test_tools/test_chunk_serializer.py +++ b/test/test_tools/test_chunk_serializer.py @@ -4,17 +4,17 @@ import os from contextlib import ExitStack from pathlib import Path -from test.data import TEST_DATA_DIR -from test.utils import GraphHelper -from test.utils.graph import cached_graph -from test.utils.namespace import MF -from test.utils.path import ctx_chdir from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union import pytest from rdflib import Graph from rdflib.tools.chunk_serializer import serialize_in_chunks +from test.data import TEST_DATA_DIR +from test.utils import GraphHelper +from test.utils.graph import cached_graph +from test.utils.namespace import MF +from test.utils.path import ctx_chdir if TYPE_CHECKING: from builtins import ellipsis diff --git a/test/test_tools/test_csv2rdf.py b/test/test_tools/test_csv2rdf.py index 3b7c1642b..450aa31bf 100644 --- a/test/test_tools/test_csv2rdf.py +++ b/test/test_tools/test_csv2rdf.py @@ -3,6 +3,7 @@ import subprocess import sys from tempfile import mkstemp + from test.data import TEST_DATA_DIR REALESTATE_FILE_PATH = os.path.join(TEST_DATA_DIR, "csv", "realestate.csv") diff --git a/test/test_turtle_quoting.py b/test/test_turtle_quoting.py index 048a4f2c3..aa523f57b 100644 --- a/test/test_turtle_quoting.py +++ b/test/test_turtle_quoting.py @@ -8,7 +8,6 @@ import itertools import logging import re -from test.utils.namespace import EGDC from typing import Callable, Dict, Iterable, List, Tuple import pytest @@ -16,6 +15,7 @@ from rdflib.graph import ConjunctiveGraph, Graph from rdflib.plugins.parsers import ntriples from rdflib.term import Literal, URIRef +from test.utils.namespace import EGDC from .utils import GraphHelper diff --git a/test/test_util.py b/test/test_util.py index 7ed486f96..63c085033 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -4,9 +4,6 @@ import time from contextlib import ExitStack from pathlib import Path -from test.data import TEST_DATA_DIR -from test.utils.graph import cached_graph -from test.utils.namespace import RDFT from typing import Any, Collection, List, Optional, Set, Tuple, Type, Union import pytest @@ -16,6 +13,9 @@ from rdflib.namespace import RDF, RDFS from rdflib.term import BNode, IdentifiedNode, Literal, Node, URIRef from rdflib.util import _coalesce, _iri2uri, find_roots, get_tree +from test.data import TEST_DATA_DIR +from test.utils.graph import cached_graph +from test.utils.namespace import RDFT n3source = """\ @prefix : . diff --git a/test/test_w3c_spec/test_n3_w3c.py b/test/test_w3c_spec/test_n3_w3c.py index eb2900f1e..a1558255d 100644 --- a/test/test_w3c_spec/test_n3_w3c.py +++ b/test/test_w3c_spec/test_n3_w3c.py @@ -5,9 +5,6 @@ import itertools import os -from test.data import TEST_DATA_DIR -from test.utils.manifest import RDFTest, read_manifest -from test.utils.namespace import RDFT from typing import Callable, Dict import pytest @@ -16,6 +13,9 @@ from rdflib.compare import graph_diff, isomorphic from rdflib.namespace import split_uri from rdflib.term import Node, URIRef +from test.data import TEST_DATA_DIR +from test.utils.manifest import RDFTest, read_manifest +from test.utils.namespace import RDFT verbose = False diff --git a/test/test_w3c_spec/test_nquads_w3c.py b/test/test_w3c_spec/test_nquads_w3c.py index 932202413..255602b73 100644 --- a/test/test_w3c_spec/test_nquads_w3c.py +++ b/test/test_w3c_spec/test_nquads_w3c.py @@ -5,16 +5,16 @@ import logging from contextlib import ExitStack -from test.data import TEST_DATA_DIR -from test.utils import BNodeHandling, GraphHelper, ensure_suffix -from test.utils.dawg_manifest import ManifestEntry, params_from_sources -from test.utils.iri import URIMapper -from test.utils.namespace import RDFT from typing import Optional import pytest from rdflib.graph import Dataset +from test.data import TEST_DATA_DIR +from test.utils import BNodeHandling, GraphHelper, ensure_suffix +from test.utils.dawg_manifest import ManifestEntry, params_from_sources +from test.utils.iri import URIMapper +from test.utils.namespace import RDFT logger = logging.getLogger(__name__) diff --git a/test/test_w3c_spec/test_nt_w3c.py b/test/test_w3c_spec/test_nt_w3c.py index bc28e76e9..a37114211 100644 --- a/test/test_w3c_spec/test_nt_w3c.py +++ b/test/test_w3c_spec/test_nt_w3c.py @@ -5,16 +5,16 @@ import logging from contextlib import ExitStack -from test.data import TEST_DATA_DIR -from test.utils import BNodeHandling, GraphHelper, ensure_suffix -from test.utils.dawg_manifest import ManifestEntry, params_from_sources -from test.utils.iri import URIMapper -from test.utils.namespace import RDFT from typing import Optional import pytest from rdflib.graph import Graph +from test.data import TEST_DATA_DIR +from test.utils import BNodeHandling, GraphHelper, ensure_suffix +from test.utils.dawg_manifest import ManifestEntry, params_from_sources +from test.utils.iri import URIMapper +from test.utils.namespace import RDFT logger = logging.getLogger(__name__) diff --git a/test/test_w3c_spec/test_rdfxml_w3c.py b/test/test_w3c_spec/test_rdfxml_w3c.py index 57f16eb22..cc3974ce9 100644 --- a/test/test_w3c_spec/test_rdfxml_w3c.py +++ b/test/test_w3c_spec/test_rdfxml_w3c.py @@ -2,16 +2,16 @@ import logging from contextlib import ExitStack -from test.data import TEST_DATA_DIR -from test.utils import BNodeHandling, GraphHelper, ensure_suffix -from test.utils.dawg_manifest import ManifestEntry, params_from_sources -from test.utils.iri import URIMapper -from test.utils.namespace import RDFT from typing import Optional import pytest from rdflib.graph import Graph +from test.data import TEST_DATA_DIR +from test.utils import BNodeHandling, GraphHelper, ensure_suffix +from test.utils.dawg_manifest import ManifestEntry, params_from_sources +from test.utils.iri import URIMapper +from test.utils.namespace import RDFT logger = logging.getLogger(__name__) diff --git a/test/test_w3c_spec/test_sparql10_w3c.py b/test/test_w3c_spec/test_sparql10_w3c.py index 4aa26cee4..3f33ca006 100644 --- a/test/test_w3c_spec/test_sparql10_w3c.py +++ b/test/test_w3c_spec/test_sparql10_w3c.py @@ -3,6 +3,11 @@ """ from contextlib import ExitStack +from typing import Generator + +import pytest +from pytest import MonkeyPatch + from test.data import TEST_DATA_DIR from test.utils import ensure_suffix from test.utils.dawg_manifest import MarksDictType, params_from_sources @@ -13,10 +18,6 @@ check_entry, ctx_configure_rdflib, ) -from typing import Generator - -import pytest -from pytest import MonkeyPatch REMOTE_BASE_IRI = "http://www.w3.org/2001/sw/DataAccess/tests/data-r2/" LOCAL_BASE_DIR = TEST_DATA_DIR / "suites/w3c/dawg-data-r2/" diff --git a/test/test_w3c_spec/test_sparql11_w3c.py b/test/test_w3c_spec/test_sparql11_w3c.py index 876a3efda..f68227470 100644 --- a/test/test_w3c_spec/test_sparql11_w3c.py +++ b/test/test_w3c_spec/test_sparql11_w3c.py @@ -3,6 +3,11 @@ """ from contextlib import ExitStack +from typing import Generator + +import pytest +from pytest import MonkeyPatch + from test.data import TEST_DATA_DIR from test.utils import ensure_suffix from test.utils.dawg_manifest import MarksDictType, params_from_sources @@ -13,10 +18,6 @@ check_entry, ctx_configure_rdflib, ) -from typing import Generator - -import pytest -from pytest import MonkeyPatch REMOTE_BASE_IRI = "http://www.w3.org/2009/sparql/docs/tests/data-sparql11/" LOCAL_BASE_DIR = TEST_DATA_DIR / "suites/w3c/sparql11/" diff --git a/test/test_w3c_spec/test_sparql_rdflib.py b/test/test_w3c_spec/test_sparql_rdflib.py index 28642df25..9e23f6d05 100644 --- a/test/test_w3c_spec/test_sparql_rdflib.py +++ b/test/test_w3c_spec/test_sparql_rdflib.py @@ -3,6 +3,11 @@ """ from contextlib import ExitStack +from typing import Generator + +import pytest +from pytest import MonkeyPatch + from test.data import TEST_DATA_DIR from test.utils import ensure_suffix from test.utils.dawg_manifest import MarksDictType, params_from_sources @@ -13,10 +18,6 @@ check_entry, ctx_configure_rdflib, ) -from typing import Generator - -import pytest -from pytest import MonkeyPatch REMOTE_BASE_IRI = ( "http://raw.github.com/RDFLib/rdflib/main/test/data/suites/rdflib/sparql/" diff --git a/test/test_w3c_spec/test_trig_w3c.py b/test/test_w3c_spec/test_trig_w3c.py index e61828235..f3cf18ad6 100644 --- a/test/test_w3c_spec/test_trig_w3c.py +++ b/test/test_w3c_spec/test_trig_w3c.py @@ -6,16 +6,16 @@ import logging from contextlib import ExitStack -from test.data import TEST_DATA_DIR -from test.utils import BNodeHandling, GraphHelper, ensure_suffix -from test.utils.dawg_manifest import ManifestEntry, params_from_sources -from test.utils.iri import URIMapper -from test.utils.namespace import RDFT from typing import Optional import pytest from rdflib.graph import Dataset +from test.data import TEST_DATA_DIR +from test.utils import BNodeHandling, GraphHelper, ensure_suffix +from test.utils.dawg_manifest import ManifestEntry, params_from_sources +from test.utils.iri import URIMapper +from test.utils.namespace import RDFT logger = logging.getLogger(__name__) diff --git a/test/test_w3c_spec/test_turtle_w3c.py b/test/test_w3c_spec/test_turtle_w3c.py index 1ee129100..bec4754a5 100644 --- a/test/test_w3c_spec/test_turtle_w3c.py +++ b/test/test_w3c_spec/test_turtle_w3c.py @@ -5,16 +5,16 @@ import logging from contextlib import ExitStack -from test.data import TEST_DATA_DIR -from test.utils import BNodeHandling, GraphHelper, ensure_suffix -from test.utils.dawg_manifest import ManifestEntry, params_from_sources -from test.utils.iri import URIMapper -from test.utils.namespace import RDFT from typing import Optional import pytest from rdflib.graph import Graph +from test.data import TEST_DATA_DIR +from test.utils import BNodeHandling, GraphHelper, ensure_suffix +from test.utils.dawg_manifest import ManifestEntry, params_from_sources +from test.utils.iri import URIMapper +from test.utils.namespace import RDFT logger = logging.getLogger(__name__) diff --git a/test/utils/dawg_manifest.py b/test/utils/dawg_manifest.py index c4f0dbfb1..ccb96bd7c 100644 --- a/test/utils/dawg_manifest.py +++ b/test/utils/dawg_manifest.py @@ -2,10 +2,6 @@ import logging from dataclasses import dataclass, field -from test.utils import MarkListType, marks_to_list -from test.utils.graph import GraphSource, GraphSourceType -from test.utils.iri import URIMapper -from test.utils.namespace import MF from typing import ( Callable, Collection, @@ -26,6 +22,10 @@ from rdflib.graph import Graph from rdflib.namespace import RDF from rdflib.term import IdentifiedNode, Identifier, URIRef +from test.utils import MarkListType, marks_to_list +from test.utils.graph import GraphSource, GraphSourceType +from test.utils.iri import URIMapper +from test.utils.namespace import MF POFilterType = Tuple[Optional[URIRef], Optional[URIRef]] POFiltersType = Iterable[POFilterType] diff --git a/test/utils/earl.py b/test/utils/earl.py index f1b62461b..2c9e605be 100644 --- a/test/utils/earl.py +++ b/test/utils/earl.py @@ -9,9 +9,6 @@ from dataclasses import dataclass, field from datetime import datetime from pathlib import Path -from test.utils import GraphHelper -from test.utils.dawg_manifest import ManifestEntry -from test.utils.namespace import EARL, MF, RDFT from typing import ( TYPE_CHECKING, Callable, @@ -33,6 +30,9 @@ from rdflib import RDF, BNode, Graph, Literal, URIRef from rdflib.namespace import DC, DOAP, FOAF from rdflib.plugins.stores.memory import Memory +from test.utils import GraphHelper +from test.utils.dawg_manifest import ManifestEntry +from test.utils.namespace import EARL, MF, RDFT if TYPE_CHECKING: from _pytest.main import Session diff --git a/test/utils/graph.py b/test/utils/graph.py index f9001f3bb..4ea1cfbd4 100644 --- a/test/utils/graph.py +++ b/test/utils/graph.py @@ -1,7 +1,6 @@ from __future__ import annotations import logging -import test.data from dataclasses import dataclass from functools import lru_cache from pathlib import Path @@ -9,6 +8,7 @@ from typing import Any, Optional, Tuple, Type, Union import rdflib.util +import test.data from rdflib.graph import Graph, _GraphT from rdflib.util import guess_format diff --git a/test/utils/http.py b/test/utils/http.py index 38beded0c..a345a672b 100644 --- a/test/utils/http.py +++ b/test/utils/http.py @@ -6,7 +6,6 @@ import random from contextlib import contextmanager from http.server import BaseHTTPRequestHandler, HTTPServer -from test.utils.wildcard import EQ_WILDCARD from threading import Thread from typing import ( Dict, @@ -22,6 +21,8 @@ ) from urllib.parse import ParseResult +from test.utils.wildcard import EQ_WILDCARD + __all__: List[str] = [] HeadersT = Union[Dict[str, List[str]], Iterable[Tuple[str, str]]] diff --git a/test/utils/httpfileserver.py b/test/utils/httpfileserver.py index 49c92e807..7dee493ab 100644 --- a/test/utils/httpfileserver.py +++ b/test/utils/httpfileserver.py @@ -7,11 +7,12 @@ from functools import lru_cache from http.server import BaseHTTPRequestHandler, HTTPServer from pathlib import Path -from test.utils.http import HeadersT, MethodName, MockHTTPRequest, apply_headers_to from typing import Dict, List, Optional, Sequence, Type from urllib.parse import parse_qs, urljoin, urlparse from uuid import uuid4 +from test.utils.http import HeadersT, MethodName, MockHTTPRequest, apply_headers_to + __all__: List[str] = [ "LocationType", "ProtoResource", diff --git a/test/utils/httpservermock.py b/test/utils/httpservermock.py index 44c085746..41f6d5b19 100644 --- a/test/utils/httpservermock.py +++ b/test/utils/httpservermock.py @@ -3,13 +3,6 @@ import logging from collections import defaultdict from http.server import BaseHTTPRequestHandler, HTTPServer -from test.utils.http import ( - MethodName, - MockHTTPRequest, - MockHTTPResponse, - apply_headers_to, - get_random_ip, -) from threading import Thread from types import TracebackType from typing import ( @@ -28,6 +21,14 @@ from unittest.mock import MagicMock, Mock from urllib.parse import parse_qs, urlparse +from test.utils.http import ( + MethodName, + MockHTTPRequest, + MockHTTPResponse, + apply_headers_to, + get_random_ip, +) + __all__: List[str] = ["make_spypair", "BaseHTTPServerMock", "ServedBaseHTTPServerMock"] if TYPE_CHECKING: diff --git a/test/utils/iri.py b/test/utils/iri.py index ce76e27a7..4259e8762 100644 --- a/test/utils/iri.py +++ b/test/utils/iri.py @@ -9,14 +9,14 @@ import logging import mimetypes from dataclasses import dataclass +from nturl2path import url2pathname as nt_url2pathname from pathlib import Path, PurePath, PurePosixPath, PureWindowsPath -from test.utils import ensure_suffix from typing import Callable, Optional, Set, Tuple, Type, TypeVar, Union from urllib.parse import quote, unquote, urljoin, urlparse, urlsplit, urlunsplit from urllib.request import BaseHandler, OpenerDirector, Request from urllib.response import addinfourl -from nturl2path import url2pathname as nt_url2pathname +from test.utils import ensure_suffix PurePathT = TypeVar("PurePathT", bound=PurePath) diff --git a/test/utils/literal.py b/test/utils/literal.py index 702f82f6d..75792f97a 100644 --- a/test/utils/literal.py +++ b/test/utils/literal.py @@ -3,11 +3,11 @@ import builtins import logging from dataclasses import dataclass -from test.utils.outcome import NoExceptionChecker from typing import Any, Optional, Union from xml.dom.minidom import DocumentFragment from rdflib.term import Literal, URIRef +from test.utils.outcome import NoExceptionChecker @dataclass(frozen=True) diff --git a/test/utils/manifest.py b/test/utils/manifest.py index 940f7f8b7..8fc092d2f 100644 --- a/test/utils/manifest.py +++ b/test/utils/manifest.py @@ -1,11 +1,11 @@ from __future__ import annotations import logging -from test.utils.namespace import DAWGT, MF, QT, RDFT, UT from typing import Iterable, List, NamedTuple, Optional, Tuple, Union, cast from rdflib import RDF, RDFS, Graph from rdflib.term import Identifier, Node, URIRef +from test.utils.namespace import DAWGT, MF, QT, RDFT, UT logger = logging.getLogger(__name__) diff --git a/test/utils/sparql_checker.py b/test/utils/sparql_checker.py index a96892e50..79a4e71e9 100644 --- a/test/utils/sparql_checker.py +++ b/test/utils/sparql_checker.py @@ -9,12 +9,6 @@ from contextlib import ExitStack, contextmanager from dataclasses import dataclass, field from io import BytesIO, StringIO -from test.utils import BNodeHandling, GraphHelper -from test.utils.dawg_manifest import Manifest, ManifestEntry -from test.utils.iri import URIMapper -from test.utils.namespace import MF, QT, UT -from test.utils.result import ResultType, assert_bindings_collections_equal -from test.utils.urlopen import context_urlopener from typing import Dict, Generator, Optional, Set, Tuple, Type, Union, cast from urllib.parse import urljoin @@ -31,6 +25,12 @@ from rdflib.query import Result from rdflib.term import BNode, IdentifiedNode, Identifier, Literal, Node, URIRef from rdflib.util import guess_format +from test.utils import BNodeHandling, GraphHelper +from test.utils.dawg_manifest import Manifest, ManifestEntry +from test.utils.iri import URIMapper +from test.utils.namespace import MF, QT, UT +from test.utils.result import ResultType, assert_bindings_collections_equal +from test.utils.urlopen import context_urlopener logger = logging.getLogger(__name__) diff --git a/test/utils/test/test_httpservermock.py b/test/utils/test/test_httpservermock.py index fe147c9ec..3b06de32c 100644 --- a/test/utils/test/test_httpservermock.py +++ b/test/utils/test/test_httpservermock.py @@ -1,10 +1,11 @@ -from test.utils.http import MethodName, MockHTTPResponse, ctx_http_handler -from test.utils.httpservermock import BaseHTTPServerMock, ServedBaseHTTPServerMock from urllib.error import HTTPError from urllib.request import Request, urlopen import pytest +from test.utils.http import MethodName, MockHTTPResponse, ctx_http_handler +from test.utils.httpservermock import BaseHTTPServerMock, ServedBaseHTTPServerMock + def test_base() -> None: httpmock = BaseHTTPServerMock() diff --git a/test/utils/test/test_iri.py b/test/utils/test/test_iri.py index 32b606e5f..3554887b9 100644 --- a/test/utils/test/test_iri.py +++ b/test/utils/test/test_iri.py @@ -3,11 +3,12 @@ import logging from contextlib import ExitStack from pathlib import PurePath, PurePosixPath, PureWindowsPath -from test.utils.iri import file_uri_to_path, rebase_url from typing import Optional, Type, Union import pytest +from test.utils.iri import file_uri_to_path, rebase_url + @pytest.mark.parametrize( ["file_uri", "path_class", "expected_result"], diff --git a/test/utils/test/test_outcome.py b/test/utils/test/test_outcome.py index 6276d0cee..4d26e69b7 100644 --- a/test/utils/test/test_outcome.py +++ b/test/utils/test/test_outcome.py @@ -1,11 +1,12 @@ from __future__ import annotations from contextlib import ExitStack -from test.utils.outcome import ExceptionChecker, OutcomeChecker from typing import Any, Callable, NoReturn, Optional, Type, Union import pytest +from test.utils.outcome import ExceptionChecker, OutcomeChecker + def _raise( what: Union[Type[Exception], Callable[..., Exception]], diff --git a/test/utils/test/test_result.py b/test/utils/test/test_result.py index d30e2d55e..432ea4755 100644 --- a/test/utils/test/test_result.py +++ b/test/utils/test/test_result.py @@ -1,13 +1,13 @@ from __future__ import annotations from contextlib import ExitStack -from test.utils.result import BindingsCollectionType, assert_bindings_collections_equal from typing import Optional, Type, Union import pytest from rdflib.namespace import XSD from rdflib.term import BNode, Literal, URIRef, Variable +from test.utils.result import BindingsCollectionType, assert_bindings_collections_equal @pytest.mark.parametrize( diff --git a/test/utils/test/test_testutils.py b/test/utils/test/test_testutils.py index c9b10592b..4de78bcf4 100644 --- a/test/utils/test/test_testutils.py +++ b/test/utils/test/test_testutils.py @@ -4,6 +4,12 @@ from contextlib import ExitStack from dataclasses import dataclass from pathlib import PurePosixPath, PureWindowsPath +from typing import Any, List, Optional, Tuple, Type, Union + +import pytest + +from rdflib.graph import ConjunctiveGraph, Dataset, Graph +from rdflib.term import URIRef from test.utils import ( COLLAPSED_BNODE, BNodeHandling, @@ -11,12 +17,6 @@ affix_tuples, file_uri_to_path, ) -from typing import Any, List, Optional, Tuple, Type, Union - -import pytest - -from rdflib.graph import ConjunctiveGraph, Dataset, Graph -from rdflib.term import URIRef def check( From 68f8cf323779dee98aff48f49c40df9026306d75 Mon Sep 17 00:00:00 2001 From: Edmond Chuc Date: Tue, 2 Jul 2024 13:17:16 +1000 Subject: [PATCH 3/6] Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks" This reverts commit 78106bb82d9d62b9cf112155e822b4adafaf6399. --- test/conftest.py | 6 +++--- test/data.py | 2 +- test/data/suites/trix/test_trix.py | 6 +++--- test/data/variants/diverse_quads.py | 3 ++- test/data/variants/diverse_triples.py | 3 ++- test/data/variants/simple_quad.py | 3 ++- test/data/variants/simple_triple.py | 3 ++- test/jsonld/test_nested_arrays.py | 3 ++- .../test_conjunctivegraph_generators.py | 2 +- ...t_conjunctivegraph_operator_combinations.py | 2 +- test/test_dataset/test_dataset.py | 4 ++-- .../test_dataset/test_dataset_default_graph.py | 2 +- test/test_dataset/test_dataset_generators.py | 2 +- test/test_extras/test_infixowl/test_basic.py | 3 ++- test/test_extras/test_infixowl/test_class.py | 3 ++- test/test_extras/test_infixowl/test_cover.py | 3 ++- .../test_infixowl/test_individual.py | 3 ++- .../test_infixowl/test_manchester_syntax.py | 3 ++- test/test_graph/test_canonicalization.py | 2 +- test/test_graph/test_diff.py | 18 +++++++++--------- test/test_graph/test_graph.py | 8 ++++---- test/test_graph/test_graph_cbd.py | 5 +++-- test/test_graph/test_graph_generators.py | 2 +- test/test_graph/test_graph_http.py | 10 +++++----- test/test_graph/test_graph_redirect.py | 8 ++++---- test/test_graph/test_graph_store.py | 2 +- test/test_graph/test_namespace_rebinding.py | 3 ++- test/test_graph/test_skolemization.py | 4 ++-- test/test_graph/test_slice.py | 3 ++- test/test_graph/test_variants.py | 6 +++--- test/test_issues/test_issue1043.py | 2 +- test/test_issues/test_issue1484.py | 2 +- test/test_issues/test_issue274.py | 4 ++-- test/test_issues/test_issue381.py | 3 ++- test/test_issues/test_issue733.py | 3 ++- test/test_issues/test_issue801.py | 3 ++- test/test_literal/test_literal.py | 3 +-- test/test_literal/test_literal_html5lib.py | 4 ++-- test/test_misc/test_input_source.py | 18 +++++++++--------- test/test_misc/test_networking_redirect.py | 4 ++-- test/test_misc/test_parse_file_guess_format.py | 2 +- test/test_misc/test_security.py | 8 ++++---- test/test_n3.py | 2 +- test/test_namespace/test_definednamespace.py | 2 +- test/test_namespace/test_namespace.py | 2 +- test/test_namespace/test_namespacemanager.py | 2 +- test/test_nt_misc.py | 2 +- .../test_broken_parse_data_from_jena.py | 2 +- test/test_parsers/test_nquads.py | 2 +- test/test_parsers/test_parser_turtlelike.py | 2 +- test/test_parsers/test_swap_n3.py | 2 +- test/test_parsers/test_trix_parse.py | 2 +- test/test_roundtrip.py | 4 ++-- test/test_serializers/test_serializer.py | 6 +++--- .../test_serializers/test_serializer_jsonld.py | 2 +- test/test_serializers/test_serializer_n3.py | 4 ++-- test/test_sparql/test_datetime_processing.py | 2 +- test/test_sparql/test_expressions.py | 2 +- test/test_sparql/test_forward_slash_escapes.py | 4 ++-- test/test_sparql/test_initbindings.py | 3 ++- test/test_sparql/test_prefixed_name.py | 2 +- test/test_sparql/test_result.py | 14 +++++++------- test/test_sparql/test_service.py | 8 ++++---- test/test_sparql/test_sparql.py | 6 +++--- test/test_sparql/test_translate_algebra.py | 2 +- test/test_sparql/test_update.py | 8 ++++---- test/test_store/test_namespace_binding.py | 2 +- test/test_store/test_store_auditable.py | 3 ++- test/test_store/test_store_sparqlstore.py | 6 +++--- .../test_store/test_store_sparqlstore_query.py | 6 +++--- .../test_store_sparqlstore_sparqlconnector.py | 4 ++-- .../test_store/test_store_sparqlupdatestore.py | 2 +- .../test_store_sparqlupdatestore_mock.py | 6 +++--- test/test_tools/test_chunk_serializer.py | 10 +++++----- test/test_tools/test_csv2rdf.py | 1 - test/test_turtle_quoting.py | 2 +- test/test_util.py | 6 +++--- test/test_w3c_spec/test_n3_w3c.py | 6 +++--- test/test_w3c_spec/test_nquads_w3c.py | 10 +++++----- test/test_w3c_spec/test_nt_w3c.py | 10 +++++----- test/test_w3c_spec/test_rdfxml_w3c.py | 10 +++++----- test/test_w3c_spec/test_sparql10_w3c.py | 9 ++++----- test/test_w3c_spec/test_sparql11_w3c.py | 9 ++++----- test/test_w3c_spec/test_sparql_rdflib.py | 9 ++++----- test/test_w3c_spec/test_trig_w3c.py | 10 +++++----- test/test_w3c_spec/test_turtle_w3c.py | 10 +++++----- test/utils/dawg_manifest.py | 8 ++++---- test/utils/earl.py | 6 +++--- test/utils/graph.py | 2 +- test/utils/http.py | 3 +-- test/utils/httpfileserver.py | 3 +-- test/utils/httpservermock.py | 15 +++++++-------- test/utils/iri.py | 4 ++-- test/utils/literal.py | 2 +- test/utils/manifest.py | 2 +- test/utils/sparql_checker.py | 12 ++++++------ test/utils/test/test_httpservermock.py | 5 ++--- test/utils/test/test_iri.py | 3 +-- test/utils/test/test_outcome.py | 3 +-- test/utils/test/test_result.py | 2 +- test/utils/test/test_testutils.py | 12 ++++++------ 101 files changed, 245 insertions(+), 238 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index f3431c927..5bbf5041e 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -10,6 +10,9 @@ pytest.register_assert_rewrite("test.utils") from pathlib import Path +from test.utils.audit import AuditHookDispatcher +from test.utils.http import ctx_http_server +from test.utils.httpfileserver import HTTPFileServer from typing import ( Collection, Dict, @@ -21,9 +24,6 @@ ) from rdflib import Graph -from test.utils.audit import AuditHookDispatcher -from test.utils.http import ctx_http_server -from test.utils.httpfileserver import HTTPFileServer from .data import TEST_DATA_DIR from .utils.earl import EARLReporter diff --git a/test/data.py b/test/data.py index f5f986782..6cd449fb4 100644 --- a/test/data.py +++ b/test/data.py @@ -1,7 +1,7 @@ from pathlib import Path +from test.utils.graph import cached_graph from rdflib import URIRef -from test.utils.graph import cached_graph TEST_DIR = Path(__file__).parent TEST_DATA_DIR = TEST_DIR / "data" diff --git a/test/data/suites/trix/test_trix.py b/test/data/suites/trix/test_trix.py index 8b7c08621..e33832afa 100644 --- a/test/data/suites/trix/test_trix.py +++ b/test/data/suites/trix/test_trix.py @@ -3,6 +3,9 @@ from __future__ import annotations +from test.data import TEST_DATA_DIR +from test.utils.manifest import RDFTest, read_manifest +from test.utils.namespace import RDFT from typing import Callable, Dict import pytest @@ -11,9 +14,6 @@ from rdflib.compare import graph_diff, isomorphic from rdflib.namespace import split_uri from rdflib.term import Node, URIRef -from test.data import TEST_DATA_DIR -from test.utils.manifest import RDFTest, read_manifest -from test.utils.namespace import RDFT verbose = False diff --git a/test/data/variants/diverse_quads.py b/test/data/variants/diverse_quads.py index 4c92851f1..8c6a092c7 100644 --- a/test/data/variants/diverse_quads.py +++ b/test/data/variants/diverse_quads.py @@ -1,7 +1,8 @@ +from test.utils.namespace import EGDC, EGSCHEME, EGURN + from rdflib.graph import ConjunctiveGraph, Graph from rdflib.namespace import XSD from rdflib.term import Literal -from test.utils.namespace import EGDC, EGSCHEME, EGURN def populate_graph(graph: Graph) -> None: diff --git a/test/data/variants/diverse_triples.py b/test/data/variants/diverse_triples.py index c77675278..3167a78a6 100644 --- a/test/data/variants/diverse_triples.py +++ b/test/data/variants/diverse_triples.py @@ -1,6 +1,7 @@ +from test.utils.namespace import EGDC, EGSCHEME, EGURN + from rdflib.graph import Graph from rdflib.term import Literal -from test.utils.namespace import EGDC, EGSCHEME, EGURN def populate_graph(graph: Graph) -> None: diff --git a/test/data/variants/simple_quad.py b/test/data/variants/simple_quad.py index 8d5cccc77..09d7ba45d 100644 --- a/test/data/variants/simple_quad.py +++ b/test/data/variants/simple_quad.py @@ -1,6 +1,7 @@ -from rdflib.graph import ConjunctiveGraph, Graph from test.utils.namespace import EGDO +from rdflib.graph import ConjunctiveGraph, Graph + def populate_graph(graph: Graph) -> None: assert isinstance(graph, ConjunctiveGraph) diff --git a/test/data/variants/simple_triple.py b/test/data/variants/simple_triple.py index 369aca359..429f48065 100644 --- a/test/data/variants/simple_triple.py +++ b/test/data/variants/simple_triple.py @@ -1,6 +1,7 @@ -from rdflib.graph import Graph from test.utils.namespace import EGDO +from rdflib.graph import Graph + def populate_graph(graph: Graph) -> None: graph.add((EGDO.subject, EGDO.predicate, EGDO.object)) diff --git a/test/jsonld/test_nested_arrays.py b/test/jsonld/test_nested_arrays.py index 1207f0e37..ae495cca0 100644 --- a/test/jsonld/test_nested_arrays.py +++ b/test/jsonld/test_nested_arrays.py @@ -1,8 +1,9 @@ from __future__ import annotations +from test.utils.namespace import EGDC + from rdflib import Graph, Literal from rdflib.collection import Collection -from test.utils.namespace import EGDC prop = EGDC["props/a"] res = EGDC["res"] diff --git a/test/test_conjunctivegraph/test_conjunctivegraph_generators.py b/test/test_conjunctivegraph/test_conjunctivegraph_generators.py index 1cdc73358..853320dd3 100644 --- a/test/test_conjunctivegraph/test_conjunctivegraph_generators.py +++ b/test/test_conjunctivegraph/test_conjunctivegraph_generators.py @@ -1,7 +1,7 @@ import os +from test.data import TEST_DATA_DIR from rdflib import ConjunctiveGraph, URIRef -from test.data import TEST_DATA_DIR timblcardn3 = open(os.path.join(TEST_DATA_DIR, "timbl-card.n3")).read() diff --git a/test/test_conjunctivegraph/test_conjunctivegraph_operator_combinations.py b/test/test_conjunctivegraph/test_conjunctivegraph_operator_combinations.py index f1914b56d..983453378 100644 --- a/test/test_conjunctivegraph/test_conjunctivegraph_operator_combinations.py +++ b/test/test_conjunctivegraph/test_conjunctivegraph_operator_combinations.py @@ -1,7 +1,7 @@ import os +from test.data import CHEESE, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR from rdflib import ConjunctiveGraph, Graph -from test.data import CHEESE, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR sportquadstrig = open(os.path.join(TEST_DATA_DIR, "sportquads.trig")).read() diff --git a/test/test_dataset/test_dataset.py b/test/test_dataset/test_dataset.py index 19b9fe830..358230ea7 100644 --- a/test/test_dataset/test_dataset.py +++ b/test/test_dataset/test_dataset.py @@ -2,14 +2,14 @@ import shutil import tempfile import warnings +from test.data import CONTEXT1, LIKES, PIZZA, TAREK +from test.utils.namespace import EGSCHEME import pytest from rdflib import URIRef, plugin from rdflib.graph import DATASET_DEFAULT_GRAPH_ID, Dataset, Graph from rdflib.store import Store -from test.data import CONTEXT1, LIKES, PIZZA, TAREK -from test.utils.namespace import EGSCHEME # Will also run SPARQLUpdateStore tests against local SPARQL1.1 endpoint if # available. This assumes SPARQL1.1 query/update endpoints running locally at diff --git a/test/test_dataset/test_dataset_default_graph.py b/test/test_dataset/test_dataset_default_graph.py index a1de4ebbe..2d7ff427d 100644 --- a/test/test_dataset/test_dataset_default_graph.py +++ b/test/test_dataset/test_dataset_default_graph.py @@ -2,6 +2,7 @@ import itertools import logging +from test.data import TEST_DATA_DIR from typing import Iterable, Type, Union import pytest @@ -9,7 +10,6 @@ from rdflib.graph import DATASET_DEFAULT_GRAPH_ID, ConjunctiveGraph, Dataset from rdflib.term import BNode, URIRef -from test.data import TEST_DATA_DIR def make_load_default_and_named() -> Iterable[ParameterSet]: diff --git a/test/test_dataset/test_dataset_generators.py b/test/test_dataset/test_dataset_generators.py index 1b3bd313c..9a698a22a 100644 --- a/test/test_dataset/test_dataset_generators.py +++ b/test/test_dataset/test_dataset_generators.py @@ -1,7 +1,7 @@ import os +from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR from rdflib import Dataset, URIRef -from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR timblcardn3 = open(os.path.join(TEST_DATA_DIR, "timbl-card.n3")).read() diff --git a/test/test_extras/test_infixowl/test_basic.py b/test/test_extras/test_infixowl/test_basic.py index 805ea62f5..4f0161e07 100644 --- a/test/test_extras/test_infixowl/test_basic.py +++ b/test/test_extras/test_infixowl/test_basic.py @@ -1,3 +1,5 @@ +from test.data import CONTEXT0 + import pytest from rdflib import OWL, Graph, Literal, Namespace @@ -10,7 +12,6 @@ max, some, ) -from test.data import CONTEXT0 EXNS = Namespace("http://example.org/vocab/") diff --git a/test/test_extras/test_infixowl/test_class.py b/test/test_extras/test_infixowl/test_class.py index 74deb131b..820c788aa 100644 --- a/test/test_extras/test_infixowl/test_class.py +++ b/test/test_extras/test_infixowl/test_class.py @@ -1,3 +1,5 @@ +from test.data import CONTEXT0, CONTEXT1 + import pytest from rdflib import OWL, RDFS, BNode, Graph, Literal, Namespace, URIRef, Variable @@ -10,7 +12,6 @@ max, ) from rdflib.util import first -from test.data import CONTEXT0, CONTEXT1 EXNS = Namespace("http://example.org/vocab/") PZNS = Namespace( diff --git a/test/test_extras/test_infixowl/test_cover.py b/test/test_extras/test_infixowl/test_cover.py index 53fbb1ec4..87889cea3 100644 --- a/test/test_extras/test_infixowl/test_cover.py +++ b/test/test_extras/test_infixowl/test_cover.py @@ -1,3 +1,5 @@ +from test.data import CONTEXT0, TEST_DATA_DIR + import pytest from rdflib import OWL, RDF, RDFS, XSD, Graph, Literal, Namespace, URIRef, logger @@ -26,7 +28,6 @@ some, value, ) -from test.data import CONTEXT0, TEST_DATA_DIR EXNS = Namespace("http://example.org/vocab/") PZNS = Namespace( diff --git a/test/test_extras/test_infixowl/test_individual.py b/test/test_extras/test_infixowl/test_individual.py index b07e16852..e8b4576a0 100644 --- a/test/test_extras/test_infixowl/test_individual.py +++ b/test/test_extras/test_infixowl/test_individual.py @@ -1,8 +1,9 @@ +from test.data import CONTEXT0, PIZZA + import pytest from rdflib import OWL, RDFS, BNode, Graph, Literal, Namespace, URIRef from rdflib.extras.infixowl import Class, Individual -from test.data import CONTEXT0, PIZZA EXNS = Namespace("http://example.org/vocab/") diff --git a/test/test_extras/test_infixowl/test_manchester_syntax.py b/test/test_extras/test_infixowl/test_manchester_syntax.py index 40f1cef10..52669af6e 100644 --- a/test/test_extras/test_infixowl/test_manchester_syntax.py +++ b/test/test_extras/test_infixowl/test_manchester_syntax.py @@ -1,8 +1,9 @@ +from test.data import TEST_DATA_DIR + import pytest from rdflib import OWL, RDFS, Graph, Literal, Namespace from rdflib.extras.infixowl import Class, Individual, manchesterSyntax -from test.data import TEST_DATA_DIR EXNS = Namespace("http://example.org/vocab/") PZNS = Namespace( diff --git a/test/test_graph/test_canonicalization.py b/test/test_graph/test_canonicalization.py index 58a5802a8..37c456b64 100644 --- a/test/test_graph/test_canonicalization.py +++ b/test/test_graph/test_canonicalization.py @@ -1,5 +1,6 @@ from collections import Counter from io import StringIO +from test.utils import GraphHelper from typing import TYPE_CHECKING, Set import pytest @@ -9,7 +10,6 @@ from rdflib.compare import to_canonical_graph, to_isomorphic from rdflib.namespace import FOAF from rdflib.plugins.stores.memory import Memory -from test.utils import GraphHelper if TYPE_CHECKING: from rdflib.graph import _TripleType diff --git a/test/test_graph/test_diff.py b/test/test_graph/test_diff.py index 696d8be89..21bbc4e8b 100644 --- a/test/test_graph/test_diff.py +++ b/test/test_graph/test_diff.py @@ -1,6 +1,15 @@ from __future__ import annotations from dataclasses import dataclass, field +from test.utils import ( + COLLAPSED_BNODE, + BNodeHandling, + GHQuad, + GHTriple, + GraphHelper, + MarksType, + MarkType, +) from typing import TYPE_CHECKING, Collection, Set, Tuple, Type, Union, cast import pytest @@ -12,15 +21,6 @@ from rdflib.graph import ConjunctiveGraph, Dataset from rdflib.namespace import FOAF, RDF from rdflib.term import BNode, Literal -from test.utils import ( - COLLAPSED_BNODE, - BNodeHandling, - GHQuad, - GHTriple, - GraphHelper, - MarksType, - MarkType, -) if TYPE_CHECKING: from rdflib.graph import _TripleType diff --git a/test/test_graph/test_graph.py b/test/test_graph/test_graph.py index 639aa710c..9d7b8b622 100644 --- a/test/test_graph/test_graph.py +++ b/test/test_graph/test_graph.py @@ -3,6 +3,10 @@ import logging import os from pathlib import Path +from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR +from test.utils import GraphHelper, get_unique_plugin_names +from test.utils.httpfileserver import HTTPFileServer, ProtoFileResource +from test.utils.outcome import ExceptionChecker, OutcomeChecker, OutcomePrimitive from typing import Callable, Optional, Set, Tuple from urllib.error import HTTPError, URLError @@ -14,10 +18,6 @@ from rdflib.plugin import PluginException from rdflib.store import Store from rdflib.term import BNode -from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR -from test.utils import GraphHelper, get_unique_plugin_names -from test.utils.httpfileserver import HTTPFileServer, ProtoFileResource -from test.utils.outcome import ExceptionChecker, OutcomeChecker, OutcomePrimitive def test_property_store() -> None: diff --git a/test/test_graph/test_graph_cbd.py b/test/test_graph/test_graph_cbd.py index d871bdd59..8e5de68af 100644 --- a/test/test_graph/test_graph_cbd.py +++ b/test/test_graph/test_graph_cbd.py @@ -1,12 +1,13 @@ """Tests the Graph class' cbd() function""" +from test.data import TEST_DATA_DIR +from test.utils import BNodeHandling, GraphHelper + import pytest from rdflib import Graph, Namespace from rdflib.namespace import RDF, RDFS from rdflib.term import Literal, URIRef -from test.data import TEST_DATA_DIR -from test.utils import BNodeHandling, GraphHelper EXAMPLE_GRAPH_FILE_PATH = TEST_DATA_DIR / "spec" / "cbd" / "example_graph.rdf" EXAMPLE_GRAPH_CBD_FILE_PATH = TEST_DATA_DIR / "spec" / "cbd" / "example_graph_cbd.rdf" diff --git a/test/test_graph/test_graph_generators.py b/test/test_graph/test_graph_generators.py index 0d89c9b7f..1bc8e1402 100644 --- a/test/test_graph/test_graph_generators.py +++ b/test/test_graph/test_graph_generators.py @@ -1,7 +1,7 @@ import os +from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR from rdflib import Graph -from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR timblcardn3 = open(os.path.join(TEST_DATA_DIR, "timbl-card.n3")).read() diff --git a/test/test_graph/test_graph_http.py b/test/test_graph/test_graph_http.py index 11eebe38b..8391a0238 100644 --- a/test/test_graph/test_graph_http.py +++ b/test/test_graph/test_graph_http.py @@ -3,11 +3,6 @@ import logging import re from http.server import BaseHTTPRequestHandler -from urllib.error import HTTPError - -import pytest - -from rdflib import Graph from test.data import TEST_DATA_DIR from test.utils import GraphHelper from test.utils.graph import cached_graph @@ -21,6 +16,11 @@ from test.utils.httpservermock import ServedBaseHTTPServerMock from test.utils.namespace import EGDO from test.utils.wildcard import URL_PARSE_RESULT_WILDCARD +from urllib.error import HTTPError + +import pytest + +from rdflib import Graph """ Test that correct content negotiation headers are passed diff --git a/test/test_graph/test_graph_redirect.py b/test/test_graph/test_graph_redirect.py index b91623baa..a26331cc9 100644 --- a/test/test_graph/test_graph_redirect.py +++ b/test/test_graph/test_graph_redirect.py @@ -1,13 +1,13 @@ from __future__ import annotations -from typing import Tuple -from urllib.parse import urlparse - -from rdflib.graph import Graph from test.data import SIMPLE_TRIPLE_GRAPH, TEST_DATA_DIR from test.utils import GraphHelper from test.utils.http import MethodName, MockHTTPResponse from test.utils.httpservermock import ServedBaseHTTPServerMock +from typing import Tuple +from urllib.parse import urlparse + +from rdflib.graph import Graph def test_graph_redirect_new_host( diff --git a/test/test_graph/test_graph_store.py b/test/test_graph/test_graph_store.py index 9a3397702..24c592dfd 100644 --- a/test/test_graph/test_graph_store.py +++ b/test/test_graph/test_graph_store.py @@ -6,6 +6,7 @@ import itertools import logging +from test.data import SIMPLE_TRIPLE_GRAPH from typing import ( TYPE_CHECKING, Any, @@ -31,7 +32,6 @@ from rdflib.query import Result from rdflib.store import Store from rdflib.term import Identifier, URIRef, Variable -from test.data import SIMPLE_TRIPLE_GRAPH if TYPE_CHECKING: from _pytest.mark.structures import ParameterSet diff --git a/test/test_graph/test_namespace_rebinding.py b/test/test_graph/test_namespace_rebinding.py index babac1b4f..34ce668e7 100644 --- a/test/test_graph/test_namespace_rebinding.py +++ b/test/test_graph/test_namespace_rebinding.py @@ -1,10 +1,11 @@ +from test.data import CONTEXT1, CONTEXT2, TAREK + import pytest from rdflib import ConjunctiveGraph, Graph, Literal from rdflib.namespace import OWL, Namespace, NamespaceManager from rdflib.plugins.stores.memory import Memory from rdflib.term import URIRef -from test.data import CONTEXT1, CONTEXT2, TAREK foaf1_uri = URIRef("http://xmlns.com/foaf/0.1/") foaf2_uri = URIRef("http://xmlns.com/foaf/2.0/") diff --git a/test/test_graph/test_skolemization.py b/test/test_graph/test_skolemization.py index 78da75493..acc77964e 100644 --- a/test/test_graph/test_skolemization.py +++ b/test/test_graph/test_skolemization.py @@ -2,14 +2,14 @@ import logging import re +from test.utils import GraphHelper +from test.utils.namespace import EGDC from typing import Pattern, Union import pytest from rdflib import Graph from rdflib.term import BNode, Literal, URIRef -from test.utils import GraphHelper -from test.utils.namespace import EGDC base_triples = { (EGDC.subject, EGDC.predicate, EGDC.object0), diff --git a/test/test_graph/test_slice.py b/test/test_graph/test_slice.py index 7e503f8ed..7258e5ca8 100644 --- a/test/test_graph/test_slice.py +++ b/test/test_graph/test_slice.py @@ -1,6 +1,7 @@ -from rdflib import Graph from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK +from rdflib import Graph + class TestGraphSlice: def test_slice(self): diff --git a/test/test_graph/test_variants.py b/test/test_graph/test_variants.py index 1136753a4..a65a923c9 100644 --- a/test/test_graph/test_variants.py +++ b/test/test_graph/test_variants.py @@ -9,6 +9,9 @@ from collections import defaultdict from dataclasses import dataclass, field from pathlib import Path, PurePath +from test.data import TEST_DATA_DIR +from test.utils import GraphHelper +from test.utils.graph import GraphSource from typing import ( ClassVar, Collection, @@ -33,9 +36,6 @@ from rdflib.graph import Dataset, _GraphT from rdflib.namespace import XSD from rdflib.term import URIRef -from test.data import TEST_DATA_DIR -from test.utils import GraphHelper -from test.utils.graph import GraphSource MODULE_PATH = Path(__file__).parent diff --git a/test/test_issues/test_issue1043.py b/test/test_issues/test_issue1043.py index 09c54af3f..c70c9b880 100644 --- a/test/test_issues/test_issue1043.py +++ b/test/test_issues/test_issue1043.py @@ -1,8 +1,8 @@ import io import sys +from test.utils.namespace import EGDO from rdflib import RDFS, XSD, Graph, Literal -from test.utils.namespace import EGDO def test_issue_1043(): diff --git a/test/test_issues/test_issue1484.py b/test/test_issues/test_issue1484.py index 87fdb2080..1c1b9ea10 100644 --- a/test/test_issues/test_issue1484.py +++ b/test/test_issues/test_issue1484.py @@ -1,8 +1,8 @@ import io import json +from test.utils.namespace import EGDO from rdflib import RDF, RDFS, Graph -from test.utils.namespace import EGDO def test_issue_1484_json(): diff --git a/test/test_issues/test_issue274.py b/test/test_issues/test_issue274.py index bb66f976a..8b5e5b209 100644 --- a/test/test_issues/test_issue274.py +++ b/test/test_issues/test_issue274.py @@ -1,3 +1,5 @@ +from test.utils import eq_ +from test.utils.namespace import EGDO from unittest import TestCase import pytest @@ -7,8 +9,6 @@ register_custom_function, unregister_custom_function, ) -from test.utils import eq_ -from test.utils.namespace import EGDO G = Graph() G.add((BNode(), RDFS.label, Literal("bnode"))) diff --git a/test/test_issues/test_issue381.py b/test/test_issues/test_issue381.py index 190ee506b..50fb8b7e9 100644 --- a/test/test_issues/test_issue381.py +++ b/test/test_issues/test_issue381.py @@ -1,6 +1,7 @@ +from test.utils.namespace import EGDO + from rdflib import BNode, Graph from rdflib.compare import isomorphic -from test.utils.namespace import EGDO def test_no_spurious_semicolon(): diff --git a/test/test_issues/test_issue733.py b/test/test_issues/test_issue733.py index 502cd07ef..e32f7a02e 100644 --- a/test/test_issues/test_issue733.py +++ b/test/test_issues/test_issue733.py @@ -5,9 +5,10 @@ subject or the object. """ -from rdflib import Graph from test.utils.namespace import EGDO +from rdflib import Graph + def test_issue_733(): g = Graph() diff --git a/test/test_issues/test_issue801.py b/test/test_issues/test_issue801.py index 564711c73..338a2648c 100644 --- a/test/test_issues/test_issue801.py +++ b/test/test_issues/test_issue801.py @@ -2,9 +2,10 @@ Issue 801 - Problem with prefixes created for URIs containing %20 """ -from rdflib import BNode, Graph, Literal from test.utils.namespace import EGDO +from rdflib import BNode, Graph, Literal + def test_issue_801(): g = Graph() diff --git a/test/test_literal/test_literal.py b/test/test_literal/test_literal.py index 59ebab5dd..1a0383162 100644 --- a/test/test_literal/test_literal.py +++ b/test/test_literal/test_literal.py @@ -4,12 +4,11 @@ import datetime import logging from decimal import Decimal -from typing import Any, Callable, Generator, Optional, Type, Union - from test.utils import affix_tuples from test.utils.literal import LiteralChecker, literal_idfn from test.utils.namespace import EGDC from test.utils.outcome import OutcomeChecker, OutcomePrimitive, OutcomePrimitives +from typing import Any, Callable, Generator, Optional, Type, Union # NOTE: The config below enables strict mode for mypy. # mypy: no_ignore_errors diff --git a/test/test_literal/test_literal_html5lib.py b/test/test_literal/test_literal_html5lib.py index ce22568db..ce39039c0 100644 --- a/test/test_literal/test_literal_html5lib.py +++ b/test/test_literal/test_literal_html5lib.py @@ -1,4 +1,6 @@ import xml.dom.minidom +from test.utils.literal import LiteralChecker +from test.utils.outcome import OutcomeChecker, OutcomePrimitives from typing import Callable import pytest @@ -6,8 +8,6 @@ import rdflib.term from rdflib.namespace import RDF from rdflib.term import Literal -from test.utils.literal import LiteralChecker -from test.utils.outcome import OutcomeChecker, OutcomePrimitives try: import html5lib as _ # noqa: F401 diff --git a/test/test_misc/test_input_source.py b/test/test_misc/test_input_source.py index f9ae8393e..2280bcd5e 100644 --- a/test/test_misc/test_input_source.py +++ b/test/test_misc/test_input_source.py @@ -8,6 +8,15 @@ from contextlib import ExitStack, contextmanager from dataclasses import dataclass from pathlib import Path +from test.utils import GraphHelper +from test.utils.httpfileserver import ( + HTTPFileInfo, + HTTPFileServer, + LocationType, + ProtoFileResource, + ProtoRedirectResource, +) +from test.utils.outcome import ExceptionChecker from typing import ( # Callable, IO, BinaryIO, @@ -35,15 +44,6 @@ URLInputSource, create_input_source, ) -from test.utils import GraphHelper -from test.utils.httpfileserver import ( - HTTPFileInfo, - HTTPFileServer, - LocationType, - ProtoFileResource, - ProtoRedirectResource, -) -from test.utils.outcome import ExceptionChecker from ..data import TEST_DATA_DIR diff --git a/test/test_misc/test_networking_redirect.py b/test/test_misc/test_networking_redirect.py index 0412f9bf7..6c405f27a 100644 --- a/test/test_misc/test_networking_redirect.py +++ b/test/test_misc/test_networking_redirect.py @@ -2,6 +2,8 @@ from contextlib import ExitStack from copy import deepcopy +from test.utils.http import headers_as_message as headers_as_message +from test.utils.outcome import ExceptionChecker from typing import Any, Dict, Iterable, Optional, Type, TypeVar, Union from urllib.error import HTTPError from urllib.request import HTTPRedirectHandler, Request @@ -10,8 +12,6 @@ from _pytest.mark.structures import ParameterSet from rdflib._networking import _make_redirect_request -from test.utils.http import headers_as_message as headers_as_message -from test.utils.outcome import ExceptionChecker AnyT = TypeVar("AnyT") diff --git a/test/test_misc/test_parse_file_guess_format.py b/test/test_misc/test_parse_file_guess_format.py index 5c190aa56..614a3d912 100644 --- a/test/test_misc/test_parse_file_guess_format.py +++ b/test/test_misc/test_parse_file_guess_format.py @@ -12,13 +12,13 @@ from pathlib import Path from shutil import copyfile from tempfile import TemporaryDirectory +from test.data import TEST_DATA_DIR import pytest from rdflib import Graph from rdflib.exceptions import ParserError from rdflib.util import guess_format -from test.data import TEST_DATA_DIR class TestFileParserGuessFormat: diff --git a/test/test_misc/test_security.py b/test/test_misc/test_security.py index 090785a2d..bbd46e1b5 100644 --- a/test/test_misc/test_security.py +++ b/test/test_misc/test_security.py @@ -6,6 +6,10 @@ import logging from contextlib import ExitStack from pathlib import Path +from test.utils.audit import AuditHookDispatcher +from test.utils.httpfileserver import HTTPFileServer, ProtoFileResource +from test.utils.namespace import EGDO +from test.utils.urlopen import context_urlopener from textwrap import dedent from typing import Any, Iterable, Tuple from urllib.request import HTTPHandler, OpenerDirector, Request @@ -14,10 +18,6 @@ from _pytest.mark.structures import ParameterSet from rdflib import Graph -from test.utils.audit import AuditHookDispatcher -from test.utils.httpfileserver import HTTPFileServer, ProtoFileResource -from test.utils.namespace import EGDO -from test.utils.urlopen import context_urlopener from ..utils import GraphHelper from ..utils.path import ctx_chdir diff --git a/test/test_n3.py b/test/test_n3.py index f3d7eeb07..c22278efd 100644 --- a/test/test_n3.py +++ b/test/test_n3.py @@ -1,5 +1,6 @@ import itertools import os +from test import TEST_DIR from urllib.error import URLError import pytest @@ -7,7 +8,6 @@ from rdflib.graph import ConjunctiveGraph, Graph from rdflib.plugins.parsers.notation3 import BadSyntax, exponent_syntax from rdflib.term import Literal, URIRef -from test import TEST_DIR test_data = """ # Definitions of terms describing the n3 model diff --git a/test/test_namespace/test_definednamespace.py b/test/test_namespace/test_definednamespace.py index ea8e12969..91b0729f0 100644 --- a/test/test_namespace/test_definednamespace.py +++ b/test/test_namespace/test_definednamespace.py @@ -8,6 +8,7 @@ from contextlib import ExitStack from dataclasses import dataclass from pathlib import Path +from test.data import TEST_DATA_DIR from typing import Optional, Type import pytest @@ -15,7 +16,6 @@ from rdflib import RDF, SKOS from rdflib.namespace import DefinedNamespace, Namespace from rdflib.term import URIRef -from test.data import TEST_DATA_DIR def test_definednamespace_creator_qb(): diff --git a/test/test_namespace/test_namespace.py b/test/test_namespace/test_namespace.py index fe2e2aa7d..adf0b3fce 100644 --- a/test/test_namespace/test_namespace.py +++ b/test/test_namespace/test_namespace.py @@ -1,5 +1,6 @@ from __future__ import annotations +from test.utils.outcome import OutcomeChecker, OutcomePrimitive from typing import Any, Optional from warnings import warn @@ -19,7 +20,6 @@ URIPattern, ) from rdflib.term import BNode, Literal, URIRef -from test.utils.outcome import OutcomeChecker, OutcomePrimitive class TestNamespace: diff --git a/test/test_namespace/test_namespacemanager.py b/test/test_namespace/test_namespacemanager.py index 4ca182ae2..7c678cc7b 100644 --- a/test/test_namespace/test_namespacemanager.py +++ b/test/test_namespace/test_namespacemanager.py @@ -2,6 +2,7 @@ import logging from contextlib import ExitStack +from test.utils.outcome import ExceptionChecker, OutcomeChecker, OutcomePrimitive from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Set, Tuple, Type, Union import pytest @@ -17,7 +18,6 @@ NamespaceManager, ) from rdflib.term import URIRef -from test.utils.outcome import ExceptionChecker, OutcomeChecker, OutcomePrimitive if TYPE_CHECKING: from rdflib._type_checking import _NamespaceSetString diff --git a/test/test_nt_misc.py b/test/test_nt_misc.py index 7f48b61c1..78f2c729e 100644 --- a/test/test_nt_misc.py +++ b/test/test_nt_misc.py @@ -2,13 +2,13 @@ import os import re from pathlib import Path +from test.data import TEST_DATA_DIR from urllib.request import urlopen import pytest from rdflib import Graph, Literal, URIRef from rdflib.plugins.parsers import ntriples -from test.data import TEST_DATA_DIR log = logging.getLogger(__name__) diff --git a/test/test_parsers/test_broken_parse_data_from_jena.py b/test/test_parsers/test_broken_parse_data_from_jena.py index 353593837..ec176a4cc 100644 --- a/test/test_parsers/test_broken_parse_data_from_jena.py +++ b/test/test_parsers/test_broken_parse_data_from_jena.py @@ -1,9 +1,9 @@ import os +from test.data import TEST_DATA_DIR import pytest import rdflib -from test.data import TEST_DATA_DIR # Recovered from # https://github.com/RDFLib/rdflib/tree/6b4607018ebf589da74aea4c25408999f1acf2e2 diff --git a/test/test_parsers/test_nquads.py b/test/test_parsers/test_nquads.py index ad17b5aee..843fe237f 100644 --- a/test/test_parsers/test_nquads.py +++ b/test/test_parsers/test_nquads.py @@ -1,7 +1,7 @@ import os +from test.data import TEST_DATA_DIR from rdflib import ConjunctiveGraph, Namespace, URIRef -from test.data import TEST_DATA_DIR TEST_BASE = os.path.join(TEST_DATA_DIR, "nquads.rdflib") diff --git a/test/test_parsers/test_parser_turtlelike.py b/test/test_parsers/test_parser_turtlelike.py index 2f0002b26..7568974c7 100644 --- a/test/test_parsers/test_parser_turtlelike.py +++ b/test/test_parsers/test_parser_turtlelike.py @@ -8,6 +8,7 @@ import enum import itertools from dataclasses import dataclass, field +from test.utils.namespace import EGDC from typing import Callable, Dict, Iterator, List, Set, Tuple, Union import pytest @@ -16,7 +17,6 @@ from rdflib import XSD, Graph, Literal from rdflib.term import Identifier from rdflib.util import from_n3 -from test.utils.namespace import EGDC class FormatTrait(enum.Enum): diff --git a/test/test_parsers/test_swap_n3.py b/test/test_parsers/test_swap_n3.py index 5de06f927..e173b8452 100644 --- a/test/test_parsers/test_swap_n3.py +++ b/test/test_parsers/test_swap_n3.py @@ -1,9 +1,9 @@ import os +from test.data import TEST_DATA_DIR import pytest import rdflib -from test.data import TEST_DATA_DIR """ SWAP N3 parser test suite diff --git a/test/test_parsers/test_trix_parse.py b/test/test_parsers/test_trix_parse.py index e6f2ae91b..70eaac130 100644 --- a/test/test_parsers/test_trix_parse.py +++ b/test/test_parsers/test_trix_parse.py @@ -1,7 +1,7 @@ import os +from test.data import TEST_DATA_DIR from rdflib.graph import ConjunctiveGraph -from test.data import TEST_DATA_DIR class TestTrixParse: diff --git a/test/test_roundtrip.py b/test/test_roundtrip.py index db007b96d..e159ccf1e 100644 --- a/test/test_roundtrip.py +++ b/test/test_roundtrip.py @@ -26,6 +26,8 @@ import logging import os.path from pathlib import Path +from test.data import TEST_DATA_DIR +from test.utils import BNodeHandling, GraphHelper from typing import Callable, Iterable, List, Optional, Set, Tuple, Type, Union from xml.sax import SAXParseException @@ -40,8 +42,6 @@ from rdflib.plugins.parsers.notation3 import BadSyntax from rdflib.serializer import Serializer from rdflib.util import guess_format -from test.data import TEST_DATA_DIR -from test.utils import BNodeHandling, GraphHelper logger = logging.getLogger(__name__) diff --git a/test/test_serializers/test_serializer.py b/test/test_serializers/test_serializer.py index 19c1be936..74701df7e 100644 --- a/test/test_serializers/test_serializer.py +++ b/test/test_serializers/test_serializer.py @@ -9,6 +9,9 @@ from dataclasses import dataclass, field from functools import lru_cache from pathlib import Path, PosixPath, PurePath +from test.utils import GraphHelper, get_unique_plugins +from test.utils.destination import DestinationType, DestParmType, DestRef +from test.utils.namespace import EGDC, EGSCHEME, EGURN from typing import ( IO, Callable, @@ -32,9 +35,6 @@ from rdflib import RDF, XSD, Graph, Literal, Namespace, URIRef from rdflib.graph import DATASET_DEFAULT_GRAPH_ID, ConjunctiveGraph, Dataset from rdflib.serializer import Serializer -from test.utils import GraphHelper, get_unique_plugins -from test.utils.destination import DestinationType, DestParmType, DestRef -from test.utils.namespace import EGDC, EGSCHEME, EGURN @pytest.mark.parametrize( diff --git a/test/test_serializers/test_serializer_jsonld.py b/test/test_serializers/test_serializer_jsonld.py index 44b36c9dc..7f7118e1b 100644 --- a/test/test_serializers/test_serializer_jsonld.py +++ b/test/test_serializers/test_serializer_jsonld.py @@ -3,13 +3,13 @@ import json import logging import pprint +from test.utils.namespace import EGDO from typing import Any, Dict, Union import pytest from rdflib import Graph from rdflib.plugins.shared.jsonld.context import Context -from test.utils.namespace import EGDO @pytest.mark.parametrize( diff --git a/test/test_serializers/test_serializer_n3.py b/test/test_serializers/test_serializer_n3.py index f266da715..4c8618a35 100644 --- a/test/test_serializers/test_serializer_n3.py +++ b/test/test_serializers/test_serializer_n3.py @@ -1,4 +1,6 @@ import logging +from test.utils import GraphHelper +from test.utils.namespace import EGDC import rdflib import rdflib.term @@ -6,8 +8,6 @@ from rdflib.graph import QuotedGraph from rdflib.plugins.parsers.notation3 import LOG_implies_URI from rdflib.term import BNode, URIRef -from test.utils import GraphHelper -from test.utils.namespace import EGDC logger = logging.getLogger(__name__) diff --git a/test/test_sparql/test_datetime_processing.py b/test/test_sparql/test_datetime_processing.py index c934e9543..3bfe59bc5 100644 --- a/test/test_sparql/test_datetime_processing.py +++ b/test/test_sparql/test_datetime_processing.py @@ -1,8 +1,8 @@ import io +from test.utils import eq_ import rdflib from rdflib import Graph -from test.utils import eq_ def test_datetime_datetime_subs_issue(): diff --git a/test/test_sparql/test_expressions.py b/test/test_sparql/test_expressions.py index 200c68543..b6c551a06 100644 --- a/test/test_sparql/test_expressions.py +++ b/test/test_sparql/test_expressions.py @@ -1,11 +1,11 @@ from functools import partial +from test.utils import eq_ as eq import rdflib.plugins.sparql.parser as p from rdflib import Literal, Variable from rdflib.plugins.sparql.algebra import translatePName, traverse from rdflib.plugins.sparql.operators import simplify from rdflib.plugins.sparql.sparql import Prologue, QueryContext, SPARQLError -from test.utils import eq_ as eq def _eval(e, ctx=None): diff --git a/test/test_sparql/test_forward_slash_escapes.py b/test/test_sparql/test_forward_slash_escapes.py index d7c72d07b..b2d54b6b8 100644 --- a/test/test_sparql/test_forward_slash_escapes.py +++ b/test/test_sparql/test_forward_slash_escapes.py @@ -18,6 +18,8 @@ """ from __future__ import annotations +from test.data import TEST_DATA_DIR +from test.utils.graph import cached_graph from typing import Set import pytest @@ -26,8 +28,6 @@ from rdflib.plugins.sparql.processor import prepareQuery from rdflib.plugins.sparql.sparql import Query from rdflib.query import ResultRow -from test.data import TEST_DATA_DIR -from test.utils.graph import cached_graph query_string_expanded = r""" SELECT ?nIndividual diff --git a/test/test_sparql/test_initbindings.py b/test/test_sparql/test_initbindings.py index 0cb55f344..6304ede37 100644 --- a/test/test_sparql/test_initbindings.py +++ b/test/test_sparql/test_initbindings.py @@ -1,6 +1,7 @@ +from test.utils.namespace import EGDC + from rdflib import ConjunctiveGraph, Literal, URIRef, Variable from rdflib.plugins.sparql import prepareQuery -from test.utils.namespace import EGDC g = ConjunctiveGraph() diff --git a/test/test_sparql/test_prefixed_name.py b/test/test_sparql/test_prefixed_name.py index ae869bf4a..e1976dac1 100644 --- a/test/test_sparql/test_prefixed_name.py +++ b/test/test_sparql/test_prefixed_name.py @@ -2,6 +2,7 @@ import itertools import logging +from test.utils.outcome import OutcomeChecker, OutcomePrimitive import pyparsing import pytest @@ -10,7 +11,6 @@ from rdflib import Graph from rdflib.namespace import Namespace from rdflib.term import Node, URIRef -from test.utils.outcome import OutcomeChecker, OutcomePrimitive RESERVED_PCHARS = [ "%20", diff --git a/test/test_sparql/test_result.py b/test/test_sparql/test_result.py index 8fbac2e7f..c08fbdedf 100644 --- a/test/test_sparql/test_result.py +++ b/test/test_sparql/test_result.py @@ -9,6 +9,13 @@ from contextlib import ExitStack from io import BytesIO, StringIO from pathlib import Path, PosixPath, PurePath +from test.utils.destination import DestinationType, DestParmType +from test.utils.result import ( + ResultFormat, + ResultFormatInfo, + ResultFormatTrait, + ResultType, +) from typing import ( IO, BinaryIO, @@ -34,13 +41,6 @@ from rdflib.namespace import Namespace from rdflib.query import Result, ResultRow from rdflib.term import BNode, Identifier, Literal, Node, Variable -from test.utils.destination import DestinationType, DestParmType -from test.utils.result import ( - ResultFormat, - ResultFormatInfo, - ResultFormatTrait, - ResultType, -) BindingsType = Sequence[Mapping[Variable, Identifier]] ParseOutcomeType = Union[BindingsType, Type[Exception]] diff --git a/test/test_sparql/test_service.py b/test/test_sparql/test_service.py index 83ac8388f..8defd2ced 100644 --- a/test/test_sparql/test_service.py +++ b/test/test_sparql/test_service.py @@ -2,6 +2,10 @@ import json from http.client import IncompleteRead, RemoteDisconnected +from test.utils import helper +from test.utils.http import MethodName, MockHTTPResponse +from test.utils.httpservermock import ServedBaseHTTPServerMock +from test.utils.outcome import OutcomeChecker from typing import Dict, FrozenSet, List, Mapping, Sequence, Tuple, Type, Union import pytest @@ -9,10 +13,6 @@ from rdflib import Graph, Literal, URIRef, Variable from rdflib.namespace import XSD from rdflib.term import BNode, Identifier -from test.utils import helper -from test.utils.http import MethodName, MockHTTPResponse -from test.utils.httpservermock import ServedBaseHTTPServerMock -from test.utils.outcome import OutcomeChecker @pytest.mark.webtest diff --git a/test/test_sparql/test_sparql.py b/test/test_sparql/test_sparql.py index a42f7ffa5..e14d57d41 100644 --- a/test/test_sparql/test_sparql.py +++ b/test/test_sparql/test_sparql.py @@ -1,6 +1,9 @@ from __future__ import annotations import logging +from test.utils import eq_ +from test.utils.namespace import EGDC +from test.utils.result import assert_bindings_collections_equal from typing import Any, Callable, Mapping, Sequence, Type import pytest @@ -21,9 +24,6 @@ from rdflib.plugins.sparql.sparql import SPARQLError from rdflib.query import Result, ResultRow from rdflib.term import Identifier, Variable -from test.utils import eq_ -from test.utils.namespace import EGDC -from test.utils.result import assert_bindings_collections_equal def test_graph_prefix(): diff --git a/test/test_sparql/test_translate_algebra.py b/test/test_sparql/test_translate_algebra.py index 59e747f65..1fca0fa1d 100644 --- a/test/test_sparql/test_translate_algebra.py +++ b/test/test_sparql/test_translate_algebra.py @@ -5,6 +5,7 @@ from dataclasses import dataclass, field from io import StringIO from pathlib import Path +from test.data import TEST_DATA_DIR from typing import Collection, Tuple, Union, cast import pytest @@ -14,7 +15,6 @@ import rdflib.plugins.sparql.parser as parser from rdflib import Graph, Literal, URIRef from rdflib.plugins.sparql.algebra import translateAlgebra -from test.data import TEST_DATA_DIR @pytest.fixture diff --git a/test/test_sparql/test_update.py b/test/test_sparql/test_update.py index 17c7967fa..daf45dc6e 100644 --- a/test/test_sparql/test_update.py +++ b/test/test_sparql/test_update.py @@ -1,14 +1,14 @@ import itertools import logging +from test.data import TEST_DATA_DIR +from test.utils import GraphHelper +from test.utils.graph import GraphSource +from test.utils.namespace import EGDO from typing import Callable import pytest from rdflib.graph import ConjunctiveGraph, Dataset, Graph -from test.data import TEST_DATA_DIR -from test.utils import GraphHelper -from test.utils.graph import GraphSource -from test.utils.namespace import EGDO @pytest.mark.parametrize( diff --git a/test/test_store/test_namespace_binding.py b/test/test_store/test_namespace_binding.py index ae5ca680a..1033a6e5f 100644 --- a/test/test_store/test_namespace_binding.py +++ b/test/test_store/test_namespace_binding.py @@ -5,6 +5,7 @@ import logging from dataclasses import dataclass, field from pathlib import Path +from test.utils import pytest_mark_filter from typing import Any, Callable, Dict, Set, Union import pytest @@ -14,7 +15,6 @@ from rdflib.plugins.stores.berkeleydb import has_bsddb from rdflib.store import Store from rdflib.term import IdentifiedNode, URIRef -from test.utils import pytest_mark_filter class StoreTrait(enum.Enum): diff --git a/test/test_store/test_store_auditable.py b/test/test_store/test_store_auditable.py index dfb6e90ba..b9d152dec 100644 --- a/test/test_store/test_store_auditable.py +++ b/test/test_store/test_store_auditable.py @@ -1,8 +1,9 @@ +from test.utils.namespace import EGDO + import pytest from rdflib import Graph from rdflib.plugins.stores.auditable import AuditableStore -from test.utils.namespace import EGDO @pytest.fixture diff --git a/test/test_store/test_store_sparqlstore.py b/test/test_store/test_store_sparqlstore.py index 1e9233242..1aabdc011 100644 --- a/test/test_store/test_store_sparqlstore.py +++ b/test/test_store/test_store_sparqlstore.py @@ -4,6 +4,9 @@ import re import socket from http.server import BaseHTTPRequestHandler, HTTPServer +from test.utils import helper +from test.utils.http import MethodName, MockHTTPResponse +from test.utils.httpservermock import ServedBaseHTTPServerMock from threading import Thread from typing import Callable, ClassVar, Type from unittest.mock import patch @@ -13,9 +16,6 @@ from rdflib import Graph, Literal, URIRef from rdflib.namespace import FOAF, RDF, RDFS, XMLNS, XSD from rdflib.plugins.stores.sparqlconnector import SPARQLConnector -from test.utils import helper -from test.utils.http import MethodName, MockHTTPResponse -from test.utils.httpservermock import ServedBaseHTTPServerMock class TestSPARQLStoreGraph: diff --git a/test/test_store/test_store_sparqlstore_query.py b/test/test_store/test_store_sparqlstore_query.py index b8089e4d9..b22585921 100644 --- a/test/test_store/test_store_sparqlstore_query.py +++ b/test/test_store/test_store_sparqlstore_query.py @@ -2,15 +2,15 @@ import itertools import logging +from test.utils import GraphHelper +from test.utils.http import MethodName, MockHTTPResponse +from test.utils.httpservermock import ServedBaseHTTPServerMock from typing import Dict, Iterable, List, Optional, Set, Tuple import pytest from _pytest.mark.structures import ParameterSet from rdflib.graph import Graph -from test.utils import GraphHelper -from test.utils.http import MethodName, MockHTTPResponse -from test.utils.httpservermock import ServedBaseHTTPServerMock def make_test_query_construct_format_cases() -> Iterable[ParameterSet]: diff --git a/test/test_store/test_store_sparqlstore_sparqlconnector.py b/test/test_store/test_store_sparqlstore_sparqlconnector.py index acc14389f..992ef2b07 100644 --- a/test/test_store/test_store_sparqlstore_sparqlconnector.py +++ b/test/test_store/test_store_sparqlstore_sparqlconnector.py @@ -2,14 +2,14 @@ import json import logging +from test.utils.http import MethodName, MockHTTPResponse +from test.utils.httpservermock import ServedBaseHTTPServerMock from typing import Optional import pytest from rdflib.graph import Graph from rdflib.plugins.stores.sparqlstore import SPARQLStore -from test.utils.http import MethodName, MockHTTPResponse -from test.utils.httpservermock import ServedBaseHTTPServerMock @pytest.mark.parametrize( diff --git a/test/test_store/test_store_sparqlupdatestore.py b/test/test_store/test_store_sparqlupdatestore.py index 5c9c38005..1843a0b86 100644 --- a/test/test_store/test_store_sparqlupdatestore.py +++ b/test/test_store/test_store_sparqlupdatestore.py @@ -1,10 +1,10 @@ import re +from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK from urllib.request import urlopen import pytest from rdflib import BNode, ConjunctiveGraph, Graph, Literal, URIRef -from test.data import BOB, CHEESE, HATES, LIKES, MICHEL, PIZZA, TAREK HOST = "http://localhost:3031" DB = "/db/" diff --git a/test/test_store/test_store_sparqlupdatestore_mock.py b/test/test_store/test_store_sparqlupdatestore_mock.py index 5d7e13eb8..5681dd0e1 100644 --- a/test/test_store/test_store_sparqlupdatestore_mock.py +++ b/test/test_store/test_store_sparqlupdatestore_mock.py @@ -1,10 +1,10 @@ +from test.utils.http import MethodName, MockHTTPResponse +from test.utils.httpservermock import ServedBaseHTTPServerMock +from test.utils.namespace import EGDO from typing import ClassVar from rdflib.graph import ConjunctiveGraph from rdflib.plugins.stores.sparqlstore import SPARQLUpdateStore -from test.utils.http import MethodName, MockHTTPResponse -from test.utils.httpservermock import ServedBaseHTTPServerMock -from test.utils.namespace import EGDO class TestSPARQLConnector: diff --git a/test/test_tools/test_chunk_serializer.py b/test/test_tools/test_chunk_serializer.py index d683ca606..4f582b192 100644 --- a/test/test_tools/test_chunk_serializer.py +++ b/test/test_tools/test_chunk_serializer.py @@ -4,17 +4,17 @@ import os from contextlib import ExitStack from pathlib import Path +from test.data import TEST_DATA_DIR +from test.utils import GraphHelper +from test.utils.graph import cached_graph +from test.utils.namespace import MF +from test.utils.path import ctx_chdir from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union import pytest from rdflib import Graph from rdflib.tools.chunk_serializer import serialize_in_chunks -from test.data import TEST_DATA_DIR -from test.utils import GraphHelper -from test.utils.graph import cached_graph -from test.utils.namespace import MF -from test.utils.path import ctx_chdir if TYPE_CHECKING: from builtins import ellipsis diff --git a/test/test_tools/test_csv2rdf.py b/test/test_tools/test_csv2rdf.py index 450aa31bf..3b7c1642b 100644 --- a/test/test_tools/test_csv2rdf.py +++ b/test/test_tools/test_csv2rdf.py @@ -3,7 +3,6 @@ import subprocess import sys from tempfile import mkstemp - from test.data import TEST_DATA_DIR REALESTATE_FILE_PATH = os.path.join(TEST_DATA_DIR, "csv", "realestate.csv") diff --git a/test/test_turtle_quoting.py b/test/test_turtle_quoting.py index aa523f57b..048a4f2c3 100644 --- a/test/test_turtle_quoting.py +++ b/test/test_turtle_quoting.py @@ -8,6 +8,7 @@ import itertools import logging import re +from test.utils.namespace import EGDC from typing import Callable, Dict, Iterable, List, Tuple import pytest @@ -15,7 +16,6 @@ from rdflib.graph import ConjunctiveGraph, Graph from rdflib.plugins.parsers import ntriples from rdflib.term import Literal, URIRef -from test.utils.namespace import EGDC from .utils import GraphHelper diff --git a/test/test_util.py b/test/test_util.py index 63c085033..7ed486f96 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -4,6 +4,9 @@ import time from contextlib import ExitStack from pathlib import Path +from test.data import TEST_DATA_DIR +from test.utils.graph import cached_graph +from test.utils.namespace import RDFT from typing import Any, Collection, List, Optional, Set, Tuple, Type, Union import pytest @@ -13,9 +16,6 @@ from rdflib.namespace import RDF, RDFS from rdflib.term import BNode, IdentifiedNode, Literal, Node, URIRef from rdflib.util import _coalesce, _iri2uri, find_roots, get_tree -from test.data import TEST_DATA_DIR -from test.utils.graph import cached_graph -from test.utils.namespace import RDFT n3source = """\ @prefix : . diff --git a/test/test_w3c_spec/test_n3_w3c.py b/test/test_w3c_spec/test_n3_w3c.py index a1558255d..eb2900f1e 100644 --- a/test/test_w3c_spec/test_n3_w3c.py +++ b/test/test_w3c_spec/test_n3_w3c.py @@ -5,6 +5,9 @@ import itertools import os +from test.data import TEST_DATA_DIR +from test.utils.manifest import RDFTest, read_manifest +from test.utils.namespace import RDFT from typing import Callable, Dict import pytest @@ -13,9 +16,6 @@ from rdflib.compare import graph_diff, isomorphic from rdflib.namespace import split_uri from rdflib.term import Node, URIRef -from test.data import TEST_DATA_DIR -from test.utils.manifest import RDFTest, read_manifest -from test.utils.namespace import RDFT verbose = False diff --git a/test/test_w3c_spec/test_nquads_w3c.py b/test/test_w3c_spec/test_nquads_w3c.py index 255602b73..932202413 100644 --- a/test/test_w3c_spec/test_nquads_w3c.py +++ b/test/test_w3c_spec/test_nquads_w3c.py @@ -5,16 +5,16 @@ import logging from contextlib import ExitStack -from typing import Optional - -import pytest - -from rdflib.graph import Dataset from test.data import TEST_DATA_DIR from test.utils import BNodeHandling, GraphHelper, ensure_suffix from test.utils.dawg_manifest import ManifestEntry, params_from_sources from test.utils.iri import URIMapper from test.utils.namespace import RDFT +from typing import Optional + +import pytest + +from rdflib.graph import Dataset logger = logging.getLogger(__name__) diff --git a/test/test_w3c_spec/test_nt_w3c.py b/test/test_w3c_spec/test_nt_w3c.py index a37114211..bc28e76e9 100644 --- a/test/test_w3c_spec/test_nt_w3c.py +++ b/test/test_w3c_spec/test_nt_w3c.py @@ -5,16 +5,16 @@ import logging from contextlib import ExitStack -from typing import Optional - -import pytest - -from rdflib.graph import Graph from test.data import TEST_DATA_DIR from test.utils import BNodeHandling, GraphHelper, ensure_suffix from test.utils.dawg_manifest import ManifestEntry, params_from_sources from test.utils.iri import URIMapper from test.utils.namespace import RDFT +from typing import Optional + +import pytest + +from rdflib.graph import Graph logger = logging.getLogger(__name__) diff --git a/test/test_w3c_spec/test_rdfxml_w3c.py b/test/test_w3c_spec/test_rdfxml_w3c.py index cc3974ce9..57f16eb22 100644 --- a/test/test_w3c_spec/test_rdfxml_w3c.py +++ b/test/test_w3c_spec/test_rdfxml_w3c.py @@ -2,16 +2,16 @@ import logging from contextlib import ExitStack -from typing import Optional - -import pytest - -from rdflib.graph import Graph from test.data import TEST_DATA_DIR from test.utils import BNodeHandling, GraphHelper, ensure_suffix from test.utils.dawg_manifest import ManifestEntry, params_from_sources from test.utils.iri import URIMapper from test.utils.namespace import RDFT +from typing import Optional + +import pytest + +from rdflib.graph import Graph logger = logging.getLogger(__name__) diff --git a/test/test_w3c_spec/test_sparql10_w3c.py b/test/test_w3c_spec/test_sparql10_w3c.py index 3f33ca006..4aa26cee4 100644 --- a/test/test_w3c_spec/test_sparql10_w3c.py +++ b/test/test_w3c_spec/test_sparql10_w3c.py @@ -3,11 +3,6 @@ """ from contextlib import ExitStack -from typing import Generator - -import pytest -from pytest import MonkeyPatch - from test.data import TEST_DATA_DIR from test.utils import ensure_suffix from test.utils.dawg_manifest import MarksDictType, params_from_sources @@ -18,6 +13,10 @@ check_entry, ctx_configure_rdflib, ) +from typing import Generator + +import pytest +from pytest import MonkeyPatch REMOTE_BASE_IRI = "http://www.w3.org/2001/sw/DataAccess/tests/data-r2/" LOCAL_BASE_DIR = TEST_DATA_DIR / "suites/w3c/dawg-data-r2/" diff --git a/test/test_w3c_spec/test_sparql11_w3c.py b/test/test_w3c_spec/test_sparql11_w3c.py index f68227470..876a3efda 100644 --- a/test/test_w3c_spec/test_sparql11_w3c.py +++ b/test/test_w3c_spec/test_sparql11_w3c.py @@ -3,11 +3,6 @@ """ from contextlib import ExitStack -from typing import Generator - -import pytest -from pytest import MonkeyPatch - from test.data import TEST_DATA_DIR from test.utils import ensure_suffix from test.utils.dawg_manifest import MarksDictType, params_from_sources @@ -18,6 +13,10 @@ check_entry, ctx_configure_rdflib, ) +from typing import Generator + +import pytest +from pytest import MonkeyPatch REMOTE_BASE_IRI = "http://www.w3.org/2009/sparql/docs/tests/data-sparql11/" LOCAL_BASE_DIR = TEST_DATA_DIR / "suites/w3c/sparql11/" diff --git a/test/test_w3c_spec/test_sparql_rdflib.py b/test/test_w3c_spec/test_sparql_rdflib.py index 9e23f6d05..28642df25 100644 --- a/test/test_w3c_spec/test_sparql_rdflib.py +++ b/test/test_w3c_spec/test_sparql_rdflib.py @@ -3,11 +3,6 @@ """ from contextlib import ExitStack -from typing import Generator - -import pytest -from pytest import MonkeyPatch - from test.data import TEST_DATA_DIR from test.utils import ensure_suffix from test.utils.dawg_manifest import MarksDictType, params_from_sources @@ -18,6 +13,10 @@ check_entry, ctx_configure_rdflib, ) +from typing import Generator + +import pytest +from pytest import MonkeyPatch REMOTE_BASE_IRI = ( "http://raw.github.com/RDFLib/rdflib/main/test/data/suites/rdflib/sparql/" diff --git a/test/test_w3c_spec/test_trig_w3c.py b/test/test_w3c_spec/test_trig_w3c.py index f3cf18ad6..e61828235 100644 --- a/test/test_w3c_spec/test_trig_w3c.py +++ b/test/test_w3c_spec/test_trig_w3c.py @@ -6,16 +6,16 @@ import logging from contextlib import ExitStack -from typing import Optional - -import pytest - -from rdflib.graph import Dataset from test.data import TEST_DATA_DIR from test.utils import BNodeHandling, GraphHelper, ensure_suffix from test.utils.dawg_manifest import ManifestEntry, params_from_sources from test.utils.iri import URIMapper from test.utils.namespace import RDFT +from typing import Optional + +import pytest + +from rdflib.graph import Dataset logger = logging.getLogger(__name__) diff --git a/test/test_w3c_spec/test_turtle_w3c.py b/test/test_w3c_spec/test_turtle_w3c.py index bec4754a5..1ee129100 100644 --- a/test/test_w3c_spec/test_turtle_w3c.py +++ b/test/test_w3c_spec/test_turtle_w3c.py @@ -5,16 +5,16 @@ import logging from contextlib import ExitStack -from typing import Optional - -import pytest - -from rdflib.graph import Graph from test.data import TEST_DATA_DIR from test.utils import BNodeHandling, GraphHelper, ensure_suffix from test.utils.dawg_manifest import ManifestEntry, params_from_sources from test.utils.iri import URIMapper from test.utils.namespace import RDFT +from typing import Optional + +import pytest + +from rdflib.graph import Graph logger = logging.getLogger(__name__) diff --git a/test/utils/dawg_manifest.py b/test/utils/dawg_manifest.py index ccb96bd7c..c4f0dbfb1 100644 --- a/test/utils/dawg_manifest.py +++ b/test/utils/dawg_manifest.py @@ -2,6 +2,10 @@ import logging from dataclasses import dataclass, field +from test.utils import MarkListType, marks_to_list +from test.utils.graph import GraphSource, GraphSourceType +from test.utils.iri import URIMapper +from test.utils.namespace import MF from typing import ( Callable, Collection, @@ -22,10 +26,6 @@ from rdflib.graph import Graph from rdflib.namespace import RDF from rdflib.term import IdentifiedNode, Identifier, URIRef -from test.utils import MarkListType, marks_to_list -from test.utils.graph import GraphSource, GraphSourceType -from test.utils.iri import URIMapper -from test.utils.namespace import MF POFilterType = Tuple[Optional[URIRef], Optional[URIRef]] POFiltersType = Iterable[POFilterType] diff --git a/test/utils/earl.py b/test/utils/earl.py index 2c9e605be..f1b62461b 100644 --- a/test/utils/earl.py +++ b/test/utils/earl.py @@ -9,6 +9,9 @@ from dataclasses import dataclass, field from datetime import datetime from pathlib import Path +from test.utils import GraphHelper +from test.utils.dawg_manifest import ManifestEntry +from test.utils.namespace import EARL, MF, RDFT from typing import ( TYPE_CHECKING, Callable, @@ -30,9 +33,6 @@ from rdflib import RDF, BNode, Graph, Literal, URIRef from rdflib.namespace import DC, DOAP, FOAF from rdflib.plugins.stores.memory import Memory -from test.utils import GraphHelper -from test.utils.dawg_manifest import ManifestEntry -from test.utils.namespace import EARL, MF, RDFT if TYPE_CHECKING: from _pytest.main import Session diff --git a/test/utils/graph.py b/test/utils/graph.py index 4ea1cfbd4..f9001f3bb 100644 --- a/test/utils/graph.py +++ b/test/utils/graph.py @@ -1,6 +1,7 @@ from __future__ import annotations import logging +import test.data from dataclasses import dataclass from functools import lru_cache from pathlib import Path @@ -8,7 +9,6 @@ from typing import Any, Optional, Tuple, Type, Union import rdflib.util -import test.data from rdflib.graph import Graph, _GraphT from rdflib.util import guess_format diff --git a/test/utils/http.py b/test/utils/http.py index a345a672b..38beded0c 100644 --- a/test/utils/http.py +++ b/test/utils/http.py @@ -6,6 +6,7 @@ import random from contextlib import contextmanager from http.server import BaseHTTPRequestHandler, HTTPServer +from test.utils.wildcard import EQ_WILDCARD from threading import Thread from typing import ( Dict, @@ -21,8 +22,6 @@ ) from urllib.parse import ParseResult -from test.utils.wildcard import EQ_WILDCARD - __all__: List[str] = [] HeadersT = Union[Dict[str, List[str]], Iterable[Tuple[str, str]]] diff --git a/test/utils/httpfileserver.py b/test/utils/httpfileserver.py index 7dee493ab..49c92e807 100644 --- a/test/utils/httpfileserver.py +++ b/test/utils/httpfileserver.py @@ -7,12 +7,11 @@ from functools import lru_cache from http.server import BaseHTTPRequestHandler, HTTPServer from pathlib import Path +from test.utils.http import HeadersT, MethodName, MockHTTPRequest, apply_headers_to from typing import Dict, List, Optional, Sequence, Type from urllib.parse import parse_qs, urljoin, urlparse from uuid import uuid4 -from test.utils.http import HeadersT, MethodName, MockHTTPRequest, apply_headers_to - __all__: List[str] = [ "LocationType", "ProtoResource", diff --git a/test/utils/httpservermock.py b/test/utils/httpservermock.py index 41f6d5b19..44c085746 100644 --- a/test/utils/httpservermock.py +++ b/test/utils/httpservermock.py @@ -3,6 +3,13 @@ import logging from collections import defaultdict from http.server import BaseHTTPRequestHandler, HTTPServer +from test.utils.http import ( + MethodName, + MockHTTPRequest, + MockHTTPResponse, + apply_headers_to, + get_random_ip, +) from threading import Thread from types import TracebackType from typing import ( @@ -21,14 +28,6 @@ from unittest.mock import MagicMock, Mock from urllib.parse import parse_qs, urlparse -from test.utils.http import ( - MethodName, - MockHTTPRequest, - MockHTTPResponse, - apply_headers_to, - get_random_ip, -) - __all__: List[str] = ["make_spypair", "BaseHTTPServerMock", "ServedBaseHTTPServerMock"] if TYPE_CHECKING: diff --git a/test/utils/iri.py b/test/utils/iri.py index 4259e8762..ce76e27a7 100644 --- a/test/utils/iri.py +++ b/test/utils/iri.py @@ -9,14 +9,14 @@ import logging import mimetypes from dataclasses import dataclass -from nturl2path import url2pathname as nt_url2pathname from pathlib import Path, PurePath, PurePosixPath, PureWindowsPath +from test.utils import ensure_suffix from typing import Callable, Optional, Set, Tuple, Type, TypeVar, Union from urllib.parse import quote, unquote, urljoin, urlparse, urlsplit, urlunsplit from urllib.request import BaseHandler, OpenerDirector, Request from urllib.response import addinfourl -from test.utils import ensure_suffix +from nturl2path import url2pathname as nt_url2pathname PurePathT = TypeVar("PurePathT", bound=PurePath) diff --git a/test/utils/literal.py b/test/utils/literal.py index 75792f97a..702f82f6d 100644 --- a/test/utils/literal.py +++ b/test/utils/literal.py @@ -3,11 +3,11 @@ import builtins import logging from dataclasses import dataclass +from test.utils.outcome import NoExceptionChecker from typing import Any, Optional, Union from xml.dom.minidom import DocumentFragment from rdflib.term import Literal, URIRef -from test.utils.outcome import NoExceptionChecker @dataclass(frozen=True) diff --git a/test/utils/manifest.py b/test/utils/manifest.py index 8fc092d2f..940f7f8b7 100644 --- a/test/utils/manifest.py +++ b/test/utils/manifest.py @@ -1,11 +1,11 @@ from __future__ import annotations import logging +from test.utils.namespace import DAWGT, MF, QT, RDFT, UT from typing import Iterable, List, NamedTuple, Optional, Tuple, Union, cast from rdflib import RDF, RDFS, Graph from rdflib.term import Identifier, Node, URIRef -from test.utils.namespace import DAWGT, MF, QT, RDFT, UT logger = logging.getLogger(__name__) diff --git a/test/utils/sparql_checker.py b/test/utils/sparql_checker.py index 79a4e71e9..a96892e50 100644 --- a/test/utils/sparql_checker.py +++ b/test/utils/sparql_checker.py @@ -9,6 +9,12 @@ from contextlib import ExitStack, contextmanager from dataclasses import dataclass, field from io import BytesIO, StringIO +from test.utils import BNodeHandling, GraphHelper +from test.utils.dawg_manifest import Manifest, ManifestEntry +from test.utils.iri import URIMapper +from test.utils.namespace import MF, QT, UT +from test.utils.result import ResultType, assert_bindings_collections_equal +from test.utils.urlopen import context_urlopener from typing import Dict, Generator, Optional, Set, Tuple, Type, Union, cast from urllib.parse import urljoin @@ -25,12 +31,6 @@ from rdflib.query import Result from rdflib.term import BNode, IdentifiedNode, Identifier, Literal, Node, URIRef from rdflib.util import guess_format -from test.utils import BNodeHandling, GraphHelper -from test.utils.dawg_manifest import Manifest, ManifestEntry -from test.utils.iri import URIMapper -from test.utils.namespace import MF, QT, UT -from test.utils.result import ResultType, assert_bindings_collections_equal -from test.utils.urlopen import context_urlopener logger = logging.getLogger(__name__) diff --git a/test/utils/test/test_httpservermock.py b/test/utils/test/test_httpservermock.py index 3b06de32c..fe147c9ec 100644 --- a/test/utils/test/test_httpservermock.py +++ b/test/utils/test/test_httpservermock.py @@ -1,11 +1,10 @@ +from test.utils.http import MethodName, MockHTTPResponse, ctx_http_handler +from test.utils.httpservermock import BaseHTTPServerMock, ServedBaseHTTPServerMock from urllib.error import HTTPError from urllib.request import Request, urlopen import pytest -from test.utils.http import MethodName, MockHTTPResponse, ctx_http_handler -from test.utils.httpservermock import BaseHTTPServerMock, ServedBaseHTTPServerMock - def test_base() -> None: httpmock = BaseHTTPServerMock() diff --git a/test/utils/test/test_iri.py b/test/utils/test/test_iri.py index 3554887b9..32b606e5f 100644 --- a/test/utils/test/test_iri.py +++ b/test/utils/test/test_iri.py @@ -3,12 +3,11 @@ import logging from contextlib import ExitStack from pathlib import PurePath, PurePosixPath, PureWindowsPath +from test.utils.iri import file_uri_to_path, rebase_url from typing import Optional, Type, Union import pytest -from test.utils.iri import file_uri_to_path, rebase_url - @pytest.mark.parametrize( ["file_uri", "path_class", "expected_result"], diff --git a/test/utils/test/test_outcome.py b/test/utils/test/test_outcome.py index 4d26e69b7..6276d0cee 100644 --- a/test/utils/test/test_outcome.py +++ b/test/utils/test/test_outcome.py @@ -1,12 +1,11 @@ from __future__ import annotations from contextlib import ExitStack +from test.utils.outcome import ExceptionChecker, OutcomeChecker from typing import Any, Callable, NoReturn, Optional, Type, Union import pytest -from test.utils.outcome import ExceptionChecker, OutcomeChecker - def _raise( what: Union[Type[Exception], Callable[..., Exception]], diff --git a/test/utils/test/test_result.py b/test/utils/test/test_result.py index 432ea4755..d30e2d55e 100644 --- a/test/utils/test/test_result.py +++ b/test/utils/test/test_result.py @@ -1,13 +1,13 @@ from __future__ import annotations from contextlib import ExitStack +from test.utils.result import BindingsCollectionType, assert_bindings_collections_equal from typing import Optional, Type, Union import pytest from rdflib.namespace import XSD from rdflib.term import BNode, Literal, URIRef, Variable -from test.utils.result import BindingsCollectionType, assert_bindings_collections_equal @pytest.mark.parametrize( diff --git a/test/utils/test/test_testutils.py b/test/utils/test/test_testutils.py index 4de78bcf4..c9b10592b 100644 --- a/test/utils/test/test_testutils.py +++ b/test/utils/test/test_testutils.py @@ -4,12 +4,6 @@ from contextlib import ExitStack from dataclasses import dataclass from pathlib import PurePosixPath, PureWindowsPath -from typing import Any, List, Optional, Tuple, Type, Union - -import pytest - -from rdflib.graph import ConjunctiveGraph, Dataset, Graph -from rdflib.term import URIRef from test.utils import ( COLLAPSED_BNODE, BNodeHandling, @@ -17,6 +11,12 @@ affix_tuples, file_uri_to_path, ) +from typing import Any, List, Optional, Tuple, Type, Union + +import pytest + +from rdflib.graph import ConjunctiveGraph, Dataset, Graph +from rdflib.term import URIRef def check( From 33b63d8c3d6651c212a9827bac036c3d491a94d5 Mon Sep 17 00:00:00 2001 From: Edmond Chuc Date: Tue, 2 Jul 2024 19:47:31 +1000 Subject: [PATCH 4/6] feat: add skolemization support for ntriples, nquads, hextuples and json-ld at parse time --- rdflib/plugins/parsers/hext.py | 11 +- rdflib/plugins/parsers/jsonld.py | 43 +++++- rdflib/plugins/parsers/nquads.py | 2 + rdflib/plugins/parsers/ntriples.py | 41 +++--- .../test_parsers/test_parse_with_skolemize.py | 128 ++++++++++++++++++ 5 files changed, 201 insertions(+), 24 deletions(-) create mode 100644 test/test_parsers/test_parse_with_skolemize.py diff --git a/rdflib/plugins/parsers/hext.py b/rdflib/plugins/parsers/hext.py index 4a9c00b8d..17914b0b5 100644 --- a/rdflib/plugins/parsers/hext.py +++ b/rdflib/plugins/parsers/hext.py @@ -25,7 +25,7 @@ class HextuplesParser(Parser): """ def __init__(self): - pass + self.skolemize = False def _load_json_line(self, line: str) -> List[Optional[Any]]: # this complex handing is because the 'value' component is @@ -52,6 +52,8 @@ def _parse_hextuple( s: Union[URIRef, BNode] if tup[0].startswith("_"): s = BNode(value=tup[0].replace("_:", "")) + if self.skolemize: + s = s.skolemize() else: s = URIRef(tup[0]) @@ -64,6 +66,8 @@ def _parse_hextuple( o = URIRef(tup[2]) elif tup[3] == "localId": o = BNode(value=tup[2].replace("_:", "")) + if self.skolemize: + o = o.skolemize() else: # literal if tup[4] is None: o = Literal(tup[2], datatype=URIRef(tup[3])) @@ -77,13 +81,15 @@ def _parse_hextuple( if tup[5].startswith("_:") else URIRef(tup[5]) ) + if isinstance(c, BNode) and self.skolemize: + c = c.skolemize() # type error: Argument 1 to "add" of "ConjunctiveGraph" has incompatible type "Tuple[Union[URIRef, BNode], URIRef, Union[URIRef, BNode, Literal], URIRef]"; expected "Union[Tuple[Node, Node, Node], Tuple[Node, Node, Node, Optional[Graph]]]" cg.add((s, p, o, c)) # type: ignore[arg-type] else: cg.add((s, p, o)) # type error: Signature of "parse" incompatible with supertype "Parser" - def parse(self, source: InputSource, graph: Graph, **kwargs: Any) -> None: # type: ignore[override] + def parse(self, source: InputSource, graph: Graph, skolemize: bool = False, **kwargs: Any) -> None: # type: ignore[override] if kwargs.get("encoding") not in [None, "utf-8"]: warnings.warn( f"Hextuples files are always utf-8 encoded, " @@ -95,6 +101,7 @@ def parse(self, source: InputSource, graph: Graph, **kwargs: Any) -> None: # ty graph.store.context_aware ), "Hextuples Parser needs a context-aware store!" + self.skolemize = skolemize cg = ConjunctiveGraph(store=graph.store, identifier=graph.identifier) cg.default_context = graph diff --git a/rdflib/plugins/parsers/jsonld.py b/rdflib/plugins/parsers/jsonld.py index 6a5bc1103..1922ee5fc 100644 --- a/rdflib/plugins/parsers/jsonld.py +++ b/rdflib/plugins/parsers/jsonld.py @@ -80,7 +80,12 @@ def __init__(self): super(JsonLDParser, self).__init__() def parse( - self, source: InputSource, sink: Graph, version: float = 1.1, **kwargs: Any + self, + source: InputSource, + sink: Graph, + version: float = 1.1, + skolemize: bool = False, + **kwargs: Any, ) -> None: # TODO: docstring w. args and return value encoding = kwargs.get("encoding") or "utf-8" @@ -118,7 +123,15 @@ def parse( else: conj_sink = sink - to_rdf(data, conj_sink, base, context_data, version, generalized_rdf) + to_rdf( + data, + conj_sink, + base, + context_data, + version, + generalized_rdf, + skolemize=skolemize, + ) def to_rdf( @@ -135,21 +148,28 @@ def to_rdf( version: Optional[float] = None, generalized_rdf: bool = False, allow_lists_of_lists: Optional[bool] = None, + skolemize: bool = False, ): # TODO: docstring w. args and return value context = Context(base=base, version=version) if context_data: context.load(context_data) parser = Parser( - generalized_rdf=generalized_rdf, allow_lists_of_lists=allow_lists_of_lists + generalized_rdf=generalized_rdf, + allow_lists_of_lists=allow_lists_of_lists, + skolemize=skolemize, ) return parser.parse(data, context, dataset) class Parser: def __init__( - self, generalized_rdf: bool = False, allow_lists_of_lists: Optional[bool] = None + self, + generalized_rdf: bool = False, + allow_lists_of_lists: Optional[bool] = None, + skolemize: bool = False, ): + self.skolemize = skolemize self.generalized_rdf = generalized_rdf self.allow_lists_of_lists = ( allow_lists_of_lists @@ -219,6 +239,8 @@ def _add_to_graph( subj = self._to_rdf_id(context, id_val) else: subj = BNode() + if self.skolemize: + subj = subj.skolemize() if subj is None: return None @@ -368,7 +390,9 @@ def flatten(n: Iterable[Any]) -> List[Any]: if bid: if not self.generalized_rdf: return - pred = BNode(bid) + pred: BNode = BNode(bid) + if self.skolemize: + pred = pred.skolemize() else: pred = URIRef(pred_uri) @@ -552,7 +576,10 @@ def _to_object( def _to_rdf_id(self, context: Context, id_val: str) -> Optional[IdentifiedNode]: bid = self._get_bnodeid(id_val) if bid: - return BNode(bid) + b = BNode(bid) + if self.skolemize: + b = b.skolemize() + return b else: uri = context.resolve(id_val) if not self.generalized_rdf and ":" not in uri: @@ -578,6 +605,8 @@ def _add_list( node_list = [node_list] first_subj = BNode() + if self.skolemize: + first_subj = first_subj.skolemize() subj, rest = first_subj, None for node in node_list: @@ -596,6 +625,8 @@ def _add_list( graph.add((subj, RDF.first, obj)) rest = BNode() + if self.skolemize: + rest = rest.skolemize() if rest: graph.add((subj, RDF.rest, RDF.nil)) diff --git a/rdflib/plugins/parsers/nquads.py b/rdflib/plugins/parsers/nquads.py index 51a6c65b1..071ef0592 100644 --- a/rdflib/plugins/parsers/nquads.py +++ b/rdflib/plugins/parsers/nquads.py @@ -48,6 +48,7 @@ def parse( # type: ignore[override] inputsource: InputSource, sink: ConjunctiveGraph, bnode_context: Optional[_BNodeContextType] = None, + skolemize: bool = False, **kwargs: Any, ) -> ConjunctiveGraph: """ @@ -68,6 +69,7 @@ def parse( # type: ignore[override] self.sink: ConjunctiveGraph = ConjunctiveGraph( # type: ignore[assignment] store=sink.store, identifier=sink.identifier ) + self.skolemize = skolemize source = inputsource.getCharacterStream() if not source: diff --git a/rdflib/plugins/parsers/ntriples.py b/rdflib/plugins/parsers/ntriples.py index 06f5c6679..933e99f3f 100644 --- a/rdflib/plugins/parsers/ntriples.py +++ b/rdflib/plugins/parsers/ntriples.py @@ -138,13 +138,15 @@ class W3CNTriplesParser: `W3CNTriplesParser`. """ - __slots__ = ("_bnode_ids", "sink", "buffer", "file", "line") + __slots__ = ("_bnode_ids", "sink", "buffer", "file", "line", "skolemize") def __init__( self, sink: Optional[Union[DummySink, NTGraphSink]] = None, bnode_context: Optional[_BNodeContextType] = None, ): + self.skolemize = False + if bnode_context is not None: self._bnode_ids = bnode_context else: @@ -164,6 +166,7 @@ def parse( self, f: Union[TextIO, IO[bytes], codecs.StreamReader], bnode_context: Optional[_BNodeContextType] = None, + skolemize: bool = False, ) -> Union[DummySink, NTGraphSink]: """ Parse f as an N-Triples file. @@ -184,6 +187,7 @@ def parse( # someone still using a bytestream here? f = codecs.getreader("utf-8")(f) + self.skolemize = skolemize self.file = f # type: ignore[assignment] self.buffer = "" while True: @@ -270,7 +274,7 @@ def subject(self, bnode_context=None) -> Union[bNode, URIRef]: raise ParseError("Subject must be uriref or nodeID") return subj - def predicate(self) -> URIRef: + def predicate(self) -> Union[bNode, URIRef]: pred = self.uriref() if not pred: raise ParseError("Predicate must be uriref") @@ -294,22 +298,27 @@ def uriref(self) -> Union[te.Literal[False], URI]: def nodeid( self, bnode_context: Optional[_BNodeContextType] = None - ) -> Union[te.Literal[False], bNode]: + ) -> Union[te.Literal[False], bNode, URI]: if self.peek("_"): - # Fix for https://github.com/RDFLib/rdflib/issues/204 - if bnode_context is None: - bnode_context = self._bnode_ids - bnode_id = self.eat(r_nodeid).group(1) - new_id = bnode_context.get(bnode_id, None) - if new_id is not None: - # Re-map to id specific to this doc - return bNode(new_id) + if self.skolemize: + bnode_id = self.eat(r_nodeid).group(1) + return bNode(bnode_id).skolemize() + else: - # Replace with freshly-generated document-specific BNode id - bnode = bNode() - # Store the mapping - bnode_context[bnode_id] = bnode - return bnode + # Fix for https://github.com/RDFLib/rdflib/issues/204 + if bnode_context is None: + bnode_context = self._bnode_ids + bnode_id = self.eat(r_nodeid).group(1) + new_id = bnode_context.get(bnode_id, None) + if new_id is not None: + # Re-map to id specific to this doc + return bNode(new_id) + else: + # Replace with freshly-generated document-specific BNode id + bnode = bNode() + # Store the mapping + bnode_context[bnode_id] = bnode + return bnode return False def literal(self) -> Union[te.Literal[False], Literal]: diff --git a/test/test_parsers/test_parse_with_skolemize.py b/test/test_parsers/test_parse_with_skolemize.py new file mode 100644 index 000000000..4b5261abd --- /dev/null +++ b/test/test_parsers/test_parse_with_skolemize.py @@ -0,0 +1,128 @@ +import pytest + +from rdflib import BNode, Dataset, Graph +from rdflib.compare import isomorphic + + +@pytest.mark.parametrize( + "data, data_format, expected_data, expected_data_format", + [ + [ + """ + _:internal-bnode-id-1 . + _:internal-bnode-id-1 "..." . + """, + "ntriples", + """ + . + "..." . + """, + "ntriples", + ] + ], +) +def test_parse_with_skolemize_triples( + data: str, data_format: str, expected_data: str, expected_data_format: str +): + graph = Graph().parse(data=data, format=data_format, skolemize=True) + assert len(graph) + + expected_graph = Graph().parse(data=expected_data, format=expected_data_format) + assert len(expected_graph) + + assert isomorphic(graph, expected_graph) + + de_skolem_graph = graph.de_skolemize() + expected_de_skolem_graph = expected_graph.de_skolemize() + assert isomorphic(de_skolem_graph, expected_de_skolem_graph) + + +@pytest.mark.parametrize( + "data, data_format, expected_data, expected_data_format, anonymous_graph_name", + [ + [ + """ + _:internal-bnode-id-1 _:graph-id . + _:internal-bnode-id-1 "..." _:graph-id . + """, + "nquads", + """ + . + "..." . + """, + "nquads", + "graph-id", + ], + [ + """ + ["urn:object", "urn:hasPart", "_:internal-bnode-id-1", "localId", "", "_:graph-id"] + ["_:internal-bnode-id-1", "urn:value", "...", "http://www.w3.org/2001/XMLSchema#string", "", "_:graph-id"] + """, + "hext", + """ + . + "..."^^ . + """, + "nquads", + "graph-id", + ], + [ + """ + [ + { + "@id": "_:graph-id", + "@graph": [ + { + "@id": "urn:object", + "urn:hasPart": { + "@id": "_:internal-bnode-id-1" + } + }, + { + "@id": "_:internal-bnode-id-1", + "urn:value": "..." + } + ] + } + ] + """, + "json-ld", + """ + . + "..." . + """, + "nquads", + "graph-id", + ], + ], +) +def test_parse_with_skolemize_quads( + data: str, + data_format: str, + expected_data: str, + expected_data_format: str, + anonymous_graph_name, +): + ds = Dataset(default_union=True) + ds.parse(data=data, format=data_format, skolemize=True) + assert len(ds) + + expected_ds = Dataset(default_union=True) + expected_ds.parse(data=expected_data, format=expected_data_format) + assert len(expected_ds) + + graph_name = BNode(anonymous_graph_name) + skolem_graph_name = graph_name.skolemize() + + skolem_graph = ds.graph(skolem_graph_name) + expected_skolem_graph = expected_ds.graph(skolem_graph_name) + assert len(skolem_graph) + assert len(expected_skolem_graph) + assert isomorphic(skolem_graph, expected_skolem_graph) + assert isomorphic(skolem_graph.de_skolemize(), expected_skolem_graph.de_skolemize()) + + # Note: Datasets must have default_union set to True, otherwise calling + # de_skolemize returns an empty graph. + assert isomorphic(ds.de_skolemize(), expected_ds.de_skolemize()) + + # TODO: There's no way to roundtrip datasets with skolemization? From 93c930eb498add4065e571ae6327917ae1d6d90c Mon Sep 17 00:00:00 2001 From: Edmond Chuc Date: Tue, 2 Jul 2024 21:46:06 +1000 Subject: [PATCH 5/6] fix: mypy errors --- rdflib/plugins/parsers/jsonld.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/rdflib/plugins/parsers/jsonld.py b/rdflib/plugins/parsers/jsonld.py index 1922ee5fc..5252bf98c 100644 --- a/rdflib/plugins/parsers/jsonld.py +++ b/rdflib/plugins/parsers/jsonld.py @@ -390,7 +390,7 @@ def flatten(n: Iterable[Any]) -> List[Any]: if bid: if not self.generalized_rdf: return - pred: BNode = BNode(bid) + pred = BNode(bid) if self.skolemize: pred = pred.skolemize() else: @@ -578,7 +578,7 @@ def _to_rdf_id(self, context: Context, id_val: str) -> Optional[IdentifiedNode]: if bid: b = BNode(bid) if self.skolemize: - b = b.skolemize() + return b.skolemize() return b else: uri = context.resolve(id_val) @@ -604,9 +604,11 @@ def _add_list( if not isinstance(node_list, list): node_list = [node_list] - first_subj = BNode() - if self.skolemize: + first_subj: Union[URIRef, BNode] = BNode() + if self.skolemize and isinstance(first_subj, BNode): first_subj = first_subj.skolemize() + + rest: Union[URIRef, BNode, None] subj, rest = first_subj, None for node in node_list: @@ -625,7 +627,7 @@ def _add_list( graph.add((subj, RDF.first, obj)) rest = BNode() - if self.skolemize: + if self.skolemize and isinstance(rest, BNode): rest = rest.skolemize() if rest: From 20431620ffb200052837f6857676e7ac5ebcabfc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 26 Jul 2024 10:51:32 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- rdflib/plugins/parsers/jsonld.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdflib/plugins/parsers/jsonld.py b/rdflib/plugins/parsers/jsonld.py index 317995545..6c9dc9d2b 100644 --- a/rdflib/plugins/parsers/jsonld.py +++ b/rdflib/plugins/parsers/jsonld.py @@ -175,7 +175,7 @@ def parse( bool(generalized_rdf), skolemize=skolemize, ) - + def to_rdf( data: Any,