Skip to content

Commit

Permalink
test: reuse example namespaces (#2485)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aucampia authored Jul 14, 2023
1 parent 53aaf02 commit 394215f
Show file tree
Hide file tree
Showing 23 changed files with 261 additions and 272 deletions.
7 changes: 2 additions & 5 deletions test/test_dataset/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import shutil
import tempfile
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.namespace import Namespace
from rdflib.store import Store

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


EGSCHEMA = Namespace("example:")


def test_subgraph_without_identifier() -> None:
"""
Graphs with no identifies assigned are identified by Skolem IRIs with a
Expand All @@ -257,7 +254,7 @@ def test_subgraph_without_identifier() -> None:
)

subgraph: Graph = dataset.graph()
subgraph.add((EGSCHEMA["subject"], EGSCHEMA["predicate"], EGSCHEMA["object"]))
subgraph.add((EGSCHEME["subject"], EGSCHEME["predicate"], EGSCHEME["object"]))

namespaces = set(nman.namespaces())
assert next(
Expand Down
5 changes: 1 addition & 4 deletions test/test_graph/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from rdflib import Graph
from rdflib.compare import graph_diff
from rdflib.graph import ConjunctiveGraph, Dataset
from rdflib.namespace import FOAF, RDF, Namespace
from rdflib.namespace import FOAF, RDF
from rdflib.term import BNode, Literal

if TYPE_CHECKING:
Expand Down Expand Up @@ -128,9 +128,6 @@ def as_params(self) -> ParameterSet:
return pytest.param(self, marks=self.marks)


EGSCHEME = Namespace("example:")


@pytest.mark.parametrize(
"test_case",
[
Expand Down
17 changes: 8 additions & 9 deletions test/test_graph/test_graph_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
ctx_http_handler,
)
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, Namespace
from rdflib import Graph

"""
Test that correct content negotiation headers are passed
Expand Down Expand Up @@ -61,8 +62,6 @@
]
"""

EG = Namespace("http://example.org/")


class ContentNegotiationHandler(BaseHTTPRequestHandler):
def do_GET(self): # noqa: N802
Expand Down Expand Up @@ -106,7 +105,7 @@ def log_message(self, *args):
class TestGraphHTTP:
def test_content_negotiation(self) -> None:
expected = Graph()
expected.add((EG.a, EG.b, EG.c))
expected.add((EGDO.a, EGDO.b, EGDO.c))
expected_triples = GraphHelper.triple_set(expected)

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

def test_content_negotiation_no_format(self) -> None:
expected = Graph()
expected.add((EG.a, EG.b, EG.c))
expected.add((EGDO.a, EGDO.b, EGDO.c))
expected_triples = GraphHelper.triple_set(expected)

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

def test_source(self) -> None:
expected = Graph()
expected.add((EG["a"], EG["b"], EG["c"]))
expected.add((EGDO["a"], EGDO["b"], EGDO["c"]))
expected_triples = GraphHelper.triple_set(expected)

with ServedBaseHTTPServerMock() as httpmock:
Expand All @@ -145,7 +144,7 @@ def test_source(self) -> None:
MockHTTPResponse(
200,
"OK",
f"<{EG['a']}> <{EG['b']}> <{EG['c']}>.".encode(),
f"<{EGDO['a']}> <{EGDO['b']}> <{EGDO['c']}>.".encode(),
{"Content-Type": ["text/turtle"]},
)
)
Expand All @@ -155,7 +154,7 @@ def test_source(self) -> None:

def test_3xx(self) -> None:
expected = Graph()
expected.add((EG["a"], EG["b"], EG["c"]))
expected.add((EGDO["a"], EGDO["b"], EGDO["c"]))
expected_triples = GraphHelper.triple_set(expected)

with ServedBaseHTTPServerMock() as httpmock:
Expand Down Expand Up @@ -193,7 +192,7 @@ def test_3xx(self) -> None:
MockHTTPResponse(
200,
"OK",
f"<{EG['a']}> <{EG['b']}> <{EG['c']}>.".encode(),
f"<{EGDO['a']}> <{EGDO['b']}> <{EGDO['c']}>.".encode(),
{"Content-Type": ["text/turtle"]},
)
)
Expand Down
16 changes: 7 additions & 9 deletions test/test_graph/test_skolemization.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
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.namespace import Namespace
from rdflib.term import BNode, Literal, URIRef

EG = Namespace("http://example.com/")

base_triples = {
(EG.subject, EG.predicate, EG.object0),
(EG.subject, EG.predicate, EG.object1),
(EGDC.subject, EGDC.predicate, EGDC.object0),
(EGDC.subject, EGDC.predicate, EGDC.object1),
}


Expand All @@ -40,7 +38,7 @@ def test_skolemization(
g = Graph()
for triple in base_triples:
g.add(triple)
g.add((EG.scheck, EG.pcheck, node))
g.add((EGDC.scheck, EGDC.pcheck, node))
assert len(g) == 3
dsg = g.skolemize()
if expected_uri is None:
Expand All @@ -50,7 +48,7 @@ def test_skolemization(
iset = GraphHelper.triple_or_quad_set(dsg)
logging.debug("iset = %s", iset)
assert iset.issuperset(base_triples)
check_triples = list(dsg.triples((EG.scheck, EG.pcheck, None)))
check_triples = list(dsg.triples((EGDC.scheck, EGDC.pcheck, None)))
assert len(check_triples) == 1
sbnode = check_triples[0][2]
logging.debug("sbnode = %s, sbnode_value = %s", sbnode, f"{sbnode}")
Expand All @@ -77,7 +75,7 @@ def test_deskolemization(
g = Graph()
for triple in base_triples:
g.add(triple)
g.add((EG.scheck, EG.pcheck, URIRef(iri)))
g.add((EGDC.scheck, EGDC.pcheck, URIRef(iri)))
assert len(g) == 3
dsg = g.de_skolemize()
if expected_bnode_value is None:
Expand All @@ -87,7 +85,7 @@ def test_deskolemization(
iset = GraphHelper.triple_or_quad_set(dsg)
logging.debug("iset = %s", iset)
assert iset.issuperset(base_triples)
check_triples = list(dsg.triples((EG.scheck, EG.pcheck, None)))
check_triples = list(dsg.triples((EGDC.scheck, EGDC.pcheck, None)))
assert len(check_triples) == 1
bnode = check_triples[0][2]
logging.debug("bnode = %s, bnode_value = %s", bnode, f"{bnode}")
Expand Down
6 changes: 3 additions & 3 deletions test/test_issues/test_issue1043.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import io
import sys
import unittest
from test.utils.namespace import EGDO

from rdflib import RDFS, XSD, Graph, Literal, Namespace
from rdflib import RDFS, XSD, Graph, Literal


class TestIssue1043(unittest.TestCase):
Expand All @@ -19,8 +20,7 @@ def test_issue_1043(self):
g = Graph()
g.bind("xsd", XSD)
g.bind("rdfs", RDFS)
n = Namespace("http://example.org/")
g.add((n.number, RDFS.label, Literal(0.00000004, datatype=XSD.decimal)))
g.add((EGDO.number, RDFS.label, Literal(0.00000004, datatype=XSD.decimal)))
g.print()
sys.stdout = sys.__stdout__
self.assertEqual(capturedOutput.getvalue(), expected)
Expand Down
19 changes: 9 additions & 10 deletions test/test_issues/test_issue1484.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import io
import json
import unittest
from test.utils.namespace import EGDO

from rdflib import RDF, RDFS, Graph, Namespace
from rdflib import RDF, RDFS, Graph


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

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

b = n.base
b = EGDO.base
g = Graph()
g.bind("rdf", RDF)
g.bind("rdfs", RDFS)
g.parse(source=s, publicID=b, format="json-ld")

assert (n.s, RDF.type, n.t) in g
assert (n.s, n.p, n.o) in g
assert (EGDO.s, RDF.type, EGDO.t) in g
assert (EGDO.s, EGDO.p, EGDO.o) in g


class TestIssue1484_str(unittest.TestCase):
Expand All @@ -39,7 +39,6 @@ def test_issue_1484_str(self):
(Previously passes, but broken by earlier fix for above.)
"""
n = Namespace("http://example.org/")
jsonstr = """
{
"@id": "http://example.org/s",
Expand All @@ -52,14 +51,14 @@ def test_issue_1484_str(self):
}
"""

b = n.base
b = EGDO.base
g = Graph()
g.bind("rdf", RDF)
g.bind("rdfs", RDFS)
g.parse(data=jsonstr, publicID=b, format="json-ld")

assert (n.s, RDF.type, n.t) in g
assert (n.s, n.p, n.o) in g
assert (EGDO.s, RDF.type, EGDO.t) in g
assert (EGDO.s, EGDO.p, EGDO.o) in g


if __name__ == "__main__":
Expand Down
16 changes: 8 additions & 8 deletions test/test_issues/test_issue274.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from test.utils import eq_
from test.utils.namespace import EGDO
from unittest import TestCase

import pytest

from rdflib import RDFS, XSD, BNode, Graph, Literal, Namespace
from rdflib import RDFS, XSD, BNode, Graph, Literal
from rdflib.plugins.sparql.operators import (
register_custom_function,
unregister_custom_function,
)

EX = Namespace("http://example.org/")
G = Graph()
G.add((BNode(), RDFS.label, Literal("bnode")))
NS = {
"ex": EX,
"ex": EGDO,
"rdfs": RDFS,
"xsd": XSD,
}
Expand Down Expand Up @@ -176,21 +176,21 @@ def f(x, y):
return Literal("%s %s" % (x, y), datatype=XSD.string)

def setUp(self):
register_custom_function(EX.f, self.f)
register_custom_function(EGDO.f, self.f)

def tearDown(self):
unregister_custom_function(EX.f, self.f)
unregister_custom_function(EGDO.f, self.f)

def test_register_twice_fail(self):
with self.assertRaises(ValueError):
register_custom_function(EX.f, self.f)
register_custom_function(EGDO.f, self.f)

def test_register_override(self):
register_custom_function(EX.f, self.f, override=True)
register_custom_function(EGDO.f, self.f, override=True)

def test_wrong_unregister_warns(self):
with pytest.warns(UserWarning):
unregister_custom_function(EX.notexist)
unregister_custom_function(EGDO.notexist)

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

0 comments on commit 394215f

Please sign in to comment.