From 533d16c22447cb5ae237b5ae6be7dc9addce37aa Mon Sep 17 00:00:00 2001 From: Natanael Arndt Date: Wed, 12 Jun 2024 16:30:57 +0200 Subject: [PATCH 1/2] Test if multiple prefixes for the same IRI persist in a namespace manager --- test/test_namespace/test_parser_namespace.py | 118 +++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 test/test_namespace/test_parser_namespace.py diff --git a/test/test_namespace/test_parser_namespace.py b/test/test_namespace/test_parser_namespace.py new file mode 100644 index 000000000..278d45068 --- /dev/null +++ b/test/test_namespace/test_parser_namespace.py @@ -0,0 +1,118 @@ +""" +This module contains tests for the parsing a turtle graph and check the namespace +handling. +""" + +from dataclasses import dataclass +from typing import Any, Iterator + +import pytest +from _pytest.mark.structures import ParameterSet + +from rdflib import ConjunctiveGraph, Graph, URIRef +from rdflib.namespace import Namespace, NamespaceManager + + +def make_parser_namespace_tests() -> Iterator[ParameterSet]: + @dataclass + class Case: + format_name: str + data_string: str + graph_class: Any + + cases = [ + Case( + "ntriples", + """ + . + """, + Graph, + ), + Case( + "nquads", + """ + . + """, + ConjunctiveGraph, + ), + Case( + "turtle", + """ + @prefix ns1: . + @prefix ex: . + ex:a ex:b ex:c . + """, + Graph, + ), + Case( + "trig", + """ + @prefix ns1: . + @prefix ex: . + graph ex:g { + ex:a ex:b ex:c . + } + """, + ConjunctiveGraph, + ), + Case( + "n3", + """ + @prefix ns1: . + @prefix ex: . + ex:a ex:b ex:c . + """, + Graph, + ), + ] + + for case in cases: + yield pytest.param(case.format_name, case.data_string, case.graph_class) + + +@pytest.mark.parametrize( + ["format_name", "data_string", "graph_class"], + make_parser_namespace_tests(), +) +def test_literals( + format_name: str, + data_string: str, + graph_class, +) -> None: + """ + Setup a namespace manager, parse a file, assert that the original namespaces are still set. + """ + namespaces = NamespaceManager(graph_class()) + namespaces.bind( + "testexample", Namespace("https://example.org/namespaces/testexample/") + ) + g = graph_class() + g.namespace_manager = namespaces + g.parse(data=data_string, format=format_name) + all_ns = [n for n in g.namespace_manager.namespaces()] + + assert ( + "testexample", + URIRef("https://example.org/namespaces/testexample/"), + ) in all_ns + +def test_namespacemanager() -> None: + """ + Setup a namespace manager add two prefixes for the same IRI. + """ + namespaces = NamespaceManager(Graph()) + namespaces.bind( + "testexample", Namespace("https://example.org/namespaces/testexample/") + ) + namespaces.bind( + "test", Namespace("https://example.org/namespaces/testexample/") + ) + all_ns = [n for n in namespaces.namespaces()] + assert ( + "testexample", + URIRef("https://example.org/namespaces/testexample/"), + ) in all_ns + assert ( + "test", + URIRef("https://example.org/namespaces/testexample/"), + ) in all_ns From 365e2f2023a3fbe3da6e5e700d5fe5c2532f98e1 Mon Sep 17 00:00:00 2001 From: Natanael Arndt Date: Wed, 12 Jun 2024 16:47:01 +0200 Subject: [PATCH 2/2] Reformat code --- test/test_namespace/test_parser_namespace.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/test_namespace/test_parser_namespace.py b/test/test_namespace/test_parser_namespace.py index 278d45068..66d5cc119 100644 --- a/test/test_namespace/test_parser_namespace.py +++ b/test/test_namespace/test_parser_namespace.py @@ -96,6 +96,7 @@ def test_literals( URIRef("https://example.org/namespaces/testexample/"), ) in all_ns + def test_namespacemanager() -> None: """ Setup a namespace manager add two prefixes for the same IRI. @@ -104,9 +105,7 @@ def test_namespacemanager() -> None: namespaces.bind( "testexample", Namespace("https://example.org/namespaces/testexample/") ) - namespaces.bind( - "test", Namespace("https://example.org/namespaces/testexample/") - ) + namespaces.bind("test", Namespace("https://example.org/namespaces/testexample/")) all_ns = [n for n in namespaces.namespaces()] assert ( "testexample",