Skip to content

Commit 394215f

Browse files
authored
test: reuse example namespaces (#2485)
Reuse global definitions for example namespaces, `http://example.com/`, `http://example.org/`, `example:`, and `urn:example:`. This makes tests more consistent in that they use the same symbol name for the same namespace.
1 parent 53aaf02 commit 394215f

23 files changed

+261
-272
lines changed

test/test_dataset/test_dataset.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import shutil
44
import tempfile
55
from test.data import context1, likes, pizza, tarek
6+
from test.utils.namespace import EGSCHEME
67

78
import pytest
89

910
from rdflib import URIRef, plugin
1011
from rdflib.graph import DATASET_DEFAULT_GRAPH_ID, Dataset, Graph
11-
from rdflib.namespace import Namespace
1212
from rdflib.store import Store
1313

1414
# Will also run SPARQLUpdateStore tests against local SPARQL1.1 endpoint if
@@ -232,9 +232,6 @@ def test_iter(get_dataset):
232232
assert i_new == i_trad # both should be 3
233233

234234

235-
EGSCHEMA = Namespace("example:")
236-
237-
238235
def test_subgraph_without_identifier() -> None:
239236
"""
240237
Graphs with no identifies assigned are identified by Skolem IRIs with a
@@ -257,7 +254,7 @@ def test_subgraph_without_identifier() -> None:
257254
)
258255

259256
subgraph: Graph = dataset.graph()
260-
subgraph.add((EGSCHEMA["subject"], EGSCHEMA["predicate"], EGSCHEMA["object"]))
257+
subgraph.add((EGSCHEME["subject"], EGSCHEME["predicate"], EGSCHEME["object"]))
261258

262259
namespaces = set(nman.namespaces())
263260
assert next(

test/test_graph/test_diff.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from rdflib import Graph
1818
from rdflib.compare import graph_diff
1919
from rdflib.graph import ConjunctiveGraph, Dataset
20-
from rdflib.namespace import FOAF, RDF, Namespace
20+
from rdflib.namespace import FOAF, RDF
2121
from rdflib.term import BNode, Literal
2222

2323
if TYPE_CHECKING:
@@ -128,9 +128,6 @@ def as_params(self) -> ParameterSet:
128128
return pytest.param(self, marks=self.marks)
129129

130130

131-
EGSCHEME = Namespace("example:")
132-
133-
134131
@pytest.mark.parametrize(
135132
"test_case",
136133
[

test/test_graph/test_graph_http.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
ctx_http_handler,
1313
)
1414
from test.utils.httpservermock import ServedBaseHTTPServerMock
15+
from test.utils.namespace import EGDO
1516
from test.utils.wildcard import URL_PARSE_RESULT_WILDCARD
1617
from urllib.error import HTTPError
1718

1819
import pytest
1920

20-
from rdflib import Graph, Namespace
21+
from rdflib import Graph
2122

2223
"""
2324
Test that correct content negotiation headers are passed
@@ -61,8 +62,6 @@
6162
]
6263
"""
6364

64-
EG = Namespace("http://example.org/")
65-
6665

6766
class ContentNegotiationHandler(BaseHTTPRequestHandler):
6867
def do_GET(self): # noqa: N802
@@ -106,7 +105,7 @@ def log_message(self, *args):
106105
class TestGraphHTTP:
107106
def test_content_negotiation(self) -> None:
108107
expected = Graph()
109-
expected.add((EG.a, EG.b, EG.c))
108+
expected.add((EGDO.a, EGDO.b, EGDO.c))
110109
expected_triples = GraphHelper.triple_set(expected)
111110

112111
with ctx_http_handler(ContentNegotiationHandler) as server:
@@ -121,7 +120,7 @@ def test_content_negotiation(self) -> None:
121120

122121
def test_content_negotiation_no_format(self) -> None:
123122
expected = Graph()
124-
expected.add((EG.a, EG.b, EG.c))
123+
expected.add((EGDO.a, EGDO.b, EGDO.c))
125124
expected_triples = GraphHelper.triple_set(expected)
126125

127126
with ctx_http_handler(ContentNegotiationHandler) as server:
@@ -135,7 +134,7 @@ def test_content_negotiation_no_format(self) -> None:
135134

136135
def test_source(self) -> None:
137136
expected = Graph()
138-
expected.add((EG["a"], EG["b"], EG["c"]))
137+
expected.add((EGDO["a"], EGDO["b"], EGDO["c"]))
139138
expected_triples = GraphHelper.triple_set(expected)
140139

141140
with ServedBaseHTTPServerMock() as httpmock:
@@ -145,7 +144,7 @@ def test_source(self) -> None:
145144
MockHTTPResponse(
146145
200,
147146
"OK",
148-
f"<{EG['a']}> <{EG['b']}> <{EG['c']}>.".encode(),
147+
f"<{EGDO['a']}> <{EGDO['b']}> <{EGDO['c']}>.".encode(),
149148
{"Content-Type": ["text/turtle"]},
150149
)
151150
)
@@ -155,7 +154,7 @@ def test_source(self) -> None:
155154

156155
def test_3xx(self) -> None:
157156
expected = Graph()
158-
expected.add((EG["a"], EG["b"], EG["c"]))
157+
expected.add((EGDO["a"], EGDO["b"], EGDO["c"]))
159158
expected_triples = GraphHelper.triple_set(expected)
160159

161160
with ServedBaseHTTPServerMock() as httpmock:
@@ -193,7 +192,7 @@ def test_3xx(self) -> None:
193192
MockHTTPResponse(
194193
200,
195194
"OK",
196-
f"<{EG['a']}> <{EG['b']}> <{EG['c']}>.".encode(),
195+
f"<{EGDO['a']}> <{EGDO['b']}> <{EGDO['c']}>.".encode(),
197196
{"Content-Type": ["text/turtle"]},
198197
)
199198
)

test/test_graph/test_skolemization.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import logging
22
import re
33
from test.utils import GraphHelper
4+
from test.utils.namespace import EGDC
45
from typing import Pattern, Union
56

67
import pytest
78

89
from rdflib import Graph
9-
from rdflib.namespace import Namespace
1010
from rdflib.term import BNode, Literal, URIRef
1111

12-
EG = Namespace("http://example.com/")
13-
1412
base_triples = {
15-
(EG.subject, EG.predicate, EG.object0),
16-
(EG.subject, EG.predicate, EG.object1),
13+
(EGDC.subject, EGDC.predicate, EGDC.object0),
14+
(EGDC.subject, EGDC.predicate, EGDC.object1),
1715
}
1816

1917

@@ -40,7 +38,7 @@ def test_skolemization(
4038
g = Graph()
4139
for triple in base_triples:
4240
g.add(triple)
43-
g.add((EG.scheck, EG.pcheck, node))
41+
g.add((EGDC.scheck, EGDC.pcheck, node))
4442
assert len(g) == 3
4543
dsg = g.skolemize()
4644
if expected_uri is None:
@@ -50,7 +48,7 @@ def test_skolemization(
5048
iset = GraphHelper.triple_or_quad_set(dsg)
5149
logging.debug("iset = %s", iset)
5250
assert iset.issuperset(base_triples)
53-
check_triples = list(dsg.triples((EG.scheck, EG.pcheck, None)))
51+
check_triples = list(dsg.triples((EGDC.scheck, EGDC.pcheck, None)))
5452
assert len(check_triples) == 1
5553
sbnode = check_triples[0][2]
5654
logging.debug("sbnode = %s, sbnode_value = %s", sbnode, f"{sbnode}")
@@ -77,7 +75,7 @@ def test_deskolemization(
7775
g = Graph()
7876
for triple in base_triples:
7977
g.add(triple)
80-
g.add((EG.scheck, EG.pcheck, URIRef(iri)))
78+
g.add((EGDC.scheck, EGDC.pcheck, URIRef(iri)))
8179
assert len(g) == 3
8280
dsg = g.de_skolemize()
8381
if expected_bnode_value is None:
@@ -87,7 +85,7 @@ def test_deskolemization(
8785
iset = GraphHelper.triple_or_quad_set(dsg)
8886
logging.debug("iset = %s", iset)
8987
assert iset.issuperset(base_triples)
90-
check_triples = list(dsg.triples((EG.scheck, EG.pcheck, None)))
88+
check_triples = list(dsg.triples((EGDC.scheck, EGDC.pcheck, None)))
9189
assert len(check_triples) == 1
9290
bnode = check_triples[0][2]
9391
logging.debug("bnode = %s, bnode_value = %s", bnode, f"{bnode}")

test/test_issues/test_issue1043.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import io
22
import sys
33
import unittest
4+
from test.utils.namespace import EGDO
45

5-
from rdflib import RDFS, XSD, Graph, Literal, Namespace
6+
from rdflib import RDFS, XSD, Graph, Literal
67

78

89
class TestIssue1043(unittest.TestCase):
@@ -19,8 +20,7 @@ def test_issue_1043(self):
1920
g = Graph()
2021
g.bind("xsd", XSD)
2122
g.bind("rdfs", RDFS)
22-
n = Namespace("http://example.org/")
23-
g.add((n.number, RDFS.label, Literal(0.00000004, datatype=XSD.decimal)))
23+
g.add((EGDO.number, RDFS.label, Literal(0.00000004, datatype=XSD.decimal)))
2424
g.print()
2525
sys.stdout = sys.__stdout__
2626
self.assertEqual(capturedOutput.getvalue(), expected)

test/test_issues/test_issue1484.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import io
22
import json
33
import unittest
4+
from test.utils.namespace import EGDO
45

5-
from rdflib import RDF, RDFS, Graph, Namespace
6+
from rdflib import RDF, RDFS, Graph
67

78

89
class TestIssue1484_json(unittest.TestCase):
910
def test_issue_1484_json(self):
1011
"""
1112
Test JSON-LD parsing of result from json.dump
1213
"""
13-
n = Namespace("http://example.org/")
14-
jsondata = {"@id": n.s, "@type": [n.t], n.p: {"@id": n.o}}
14+
jsondata = {"@id": EGDO.s, "@type": [EGDO.t], EGDO.p: {"@id": EGDO.o}}
1515

1616
s = io.StringIO()
1717
json.dump(jsondata, s, indent=2, separators=(",", ": "))
@@ -22,14 +22,14 @@ def test_issue_1484_json(self):
2222
print("S: ", s.read())
2323
s.seek(0)
2424

25-
b = n.base
25+
b = EGDO.base
2626
g = Graph()
2727
g.bind("rdf", RDF)
2828
g.bind("rdfs", RDFS)
2929
g.parse(source=s, publicID=b, format="json-ld")
3030

31-
assert (n.s, RDF.type, n.t) in g
32-
assert (n.s, n.p, n.o) in g
31+
assert (EGDO.s, RDF.type, EGDO.t) in g
32+
assert (EGDO.s, EGDO.p, EGDO.o) in g
3333

3434

3535
class TestIssue1484_str(unittest.TestCase):
@@ -39,7 +39,6 @@ def test_issue_1484_str(self):
3939
4040
(Previously passes, but broken by earlier fix for above.)
4141
"""
42-
n = Namespace("http://example.org/")
4342
jsonstr = """
4443
{
4544
"@id": "http://example.org/s",
@@ -52,14 +51,14 @@ def test_issue_1484_str(self):
5251
}
5352
"""
5453

55-
b = n.base
54+
b = EGDO.base
5655
g = Graph()
5756
g.bind("rdf", RDF)
5857
g.bind("rdfs", RDFS)
5958
g.parse(data=jsonstr, publicID=b, format="json-ld")
6059

61-
assert (n.s, RDF.type, n.t) in g
62-
assert (n.s, n.p, n.o) in g
60+
assert (EGDO.s, RDF.type, EGDO.t) in g
61+
assert (EGDO.s, EGDO.p, EGDO.o) in g
6362

6463

6564
if __name__ == "__main__":

test/test_issues/test_issue274.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
from test.utils import eq_
2+
from test.utils.namespace import EGDO
23
from unittest import TestCase
34

45
import pytest
56

6-
from rdflib import RDFS, XSD, BNode, Graph, Literal, Namespace
7+
from rdflib import RDFS, XSD, BNode, Graph, Literal
78
from rdflib.plugins.sparql.operators import (
89
register_custom_function,
910
unregister_custom_function,
1011
)
1112

12-
EX = Namespace("http://example.org/")
1313
G = Graph()
1414
G.add((BNode(), RDFS.label, Literal("bnode")))
1515
NS = {
16-
"ex": EX,
16+
"ex": EGDO,
1717
"rdfs": RDFS,
1818
"xsd": XSD,
1919
}
@@ -176,21 +176,21 @@ def f(x, y):
176176
return Literal("%s %s" % (x, y), datatype=XSD.string)
177177

178178
def setUp(self):
179-
register_custom_function(EX.f, self.f)
179+
register_custom_function(EGDO.f, self.f)
180180

181181
def tearDown(self):
182-
unregister_custom_function(EX.f, self.f)
182+
unregister_custom_function(EGDO.f, self.f)
183183

184184
def test_register_twice_fail(self):
185185
with self.assertRaises(ValueError):
186-
register_custom_function(EX.f, self.f)
186+
register_custom_function(EGDO.f, self.f)
187187

188188
def test_register_override(self):
189-
register_custom_function(EX.f, self.f, override=True)
189+
register_custom_function(EGDO.f, self.f, override=True)
190190

191191
def test_wrong_unregister_warns(self):
192192
with pytest.warns(UserWarning):
193-
unregister_custom_function(EX.notexist)
193+
unregister_custom_function(EGDO.notexist)
194194

195195
def test_f(self):
196196
res = query("""SELECT (ex:f(42, "hello") as ?x) {}""")

0 commit comments

Comments
 (0)