Skip to content

Commit

Permalink
test: migrate some tests from unittest to pytest (#2562)
Browse files Browse the repository at this point in the history
All tests should be using pytest.
  • Loading branch information
aucampia authored Aug 31, 2023
1 parent d09c3af commit bbb9880
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 196 deletions.
28 changes: 11 additions & 17 deletions test/test_issues/test_issue1043.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import io
import sys
import unittest
from test.utils.namespace import EGDO

from rdflib import RDFS, XSD, Graph, Literal


class TestIssue1043(unittest.TestCase):
def test_issue_1043(self):
expected = """@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
def test_issue_1043():
expected = """@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<http://example.org/number> rdfs:label 4e-08 .
"""
capturedOutput = io.StringIO() # noqa: N806
sys.stdout = capturedOutput
g = Graph()
g.bind("xsd", XSD)
g.bind("rdfs", RDFS)
g.add((EGDO.number, RDFS.label, Literal(0.00000004, datatype=XSD.decimal)))
g.print()
sys.stdout = sys.__stdout__
self.assertEqual(capturedOutput.getvalue(), expected)


if __name__ == "__main__":
unittest.main()
capturedOutput = io.StringIO() # noqa: N806
sys.stdout = capturedOutput
g = Graph()
g.bind("xsd", XSD)
g.bind("rdfs", RDFS)
g.add((EGDO.number, RDFS.label, Literal(0.00000004, datatype=XSD.decimal)))
g.print()
sys.stdout = sys.__stdout__
assert capturedOutput.getvalue() == expected
97 changes: 45 additions & 52 deletions test/test_issues/test_issue1484.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,58 @@
import io
import json
import unittest
from test.utils.namespace import EGDO

from rdflib import RDF, RDFS, Graph


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

s = io.StringIO()
json.dump(jsondata, s, indent=2, separators=(",", ": "))
s.seek(0)

DEBUG = False # noqa: N806
if DEBUG:
print("S: ", s.read())
s.seek(0)

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

assert (EGDO.s, RDF.type, EGDO.t) in g
assert (EGDO.s, EGDO.p, EGDO.o) in g
s = io.StringIO()
json.dump(jsondata, s, indent=2, separators=(",", ": "))
s.seek(0)

DEBUG = False # noqa: N806
if DEBUG:
print("S: ", s.read())
s.seek(0)

class TestIssue1484_str(unittest.TestCase): # noqa: N801
def test_issue_1484_str(self):
"""
Test JSON-LD parsing of result from string (used by round tripping tests)
(Previously passes, but broken by earlier fix for above.)
"""
jsonstr = """
{
"@id": "http://example.org/s",
"@type": [
"http://example.org/t"
],
"http://example.org/p": {
"@id": "http://example.org/o"
}
b = EGDO.base
g = Graph()
g.bind("rdf", RDF)
g.bind("rdfs", RDFS)
g.parse(source=s, publicID=b, format="json-ld")

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


def test_issue_1484_str():
"""
Test JSON-LD parsing of result from string (used by round tripping tests)
(Previously passes, but broken by earlier fix for above.)
"""
jsonstr = """
{
"@id": "http://example.org/s",
"@type": [
"http://example.org/t"
],
"http://example.org/p": {
"@id": "http://example.org/o"
}
"""

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

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

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

if __name__ == "__main__":
unittest.main()
assert (EGDO.s, RDF.type, EGDO.t) in g
assert (EGDO.s, EGDO.p, EGDO.o) in g
41 changes: 19 additions & 22 deletions test/test_issues/test_issue161.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
from unittest import TestCase

from rdflib.graph import ConjunctiveGraph


class EntityTest(TestCase):
def test_turtle_namespace_prefixes(self):
g = ConjunctiveGraph()
n3 = """
@prefix _9: <http://data.linkedmdb.org/resource/movie/> .
@prefix p_9: <urn:test:> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
def test_turtle_namespace_prefixes():
g = ConjunctiveGraph()
n3 = """
@prefix _9: <http://data.linkedmdb.org/resource/movie/> .
@prefix p_9: <urn:test:> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
p_9:a p_9:b p_9:c .
p_9:a p_9:b p_9:c .
<http://data.linkedmdb.org/resource/director/1> a
<http://data.linkedmdb.org/resource/movie/director>;
rdfs:label "Cecil B. DeMille (Director)";
_9:director_name "Cecil B. DeMille" ."""
<http://data.linkedmdb.org/resource/director/1> a
<http://data.linkedmdb.org/resource/movie/director>;
rdfs:label "Cecil B. DeMille (Director)";
_9:director_name "Cecil B. DeMille" ."""

g.parse(data=n3, format="n3")
turtle = g.serialize(format="turtle")
g.parse(data=n3, format="n3")
turtle = g.serialize(format="turtle")

# Check round-tripping, just for kicks.
g = ConjunctiveGraph()
g.parse(data=turtle, format="turtle")
# Shouldn't have got to here
s = g.serialize(format="turtle", encoding="latin-1")
# Check round-tripping, just for kicks.
g = ConjunctiveGraph()
g.parse(data=turtle, format="turtle")
# Shouldn't have got to here
s = g.serialize(format="turtle", encoding="latin-1")

self.assertTrue(b"@prefix _9" not in s)
assert b"@prefix _9" not in s
22 changes: 9 additions & 13 deletions test/test_issues/test_issue209.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import threading
import unittest

import rdflib


def makeNode(): # noqa: N802
def make_node():
i = 0
while i < 9999:
i += 1
rdflib.term.BNode()


class TestRandomSeedInThread(unittest.TestCase):
def test_bnode_id_gen_in_thread(self):
""" """
th = threading.Thread(target=makeNode)
th.daemon = True
th.start()
makeNode()


if __name__ == "__main__":
unittest.main()
def test_bnode_id_gen_in_thread():
"""
Test a random seed in a thread.
"""
th = threading.Thread(target=make_node)
th.daemon = True
th.start()
make_node()
113 changes: 53 additions & 60 deletions test/test_issues/test_issue248.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,78 @@
import unittest

import rdflib


class TestSerialization(unittest.TestCase):
def test_issue_248(self):
"""
Ed Summers Thu, 24 May 2007 12:21:17 -0700
As discussed with eikeon in #redfoot it appears that the n3 serializer
is ignoring the base option to Graph.serialize...example follows:
--
def test_issue_248():
"""
Ed Summers Thu, 24 May 2007 12:21:17 -0700
#!/usr/bin/env python
As discussed with eikeon in #redfoot it appears that the n3 serializer
is ignoring the base option to Graph.serialize...example follows:
from rdflib.Graph import Graph
from rdflib.URIRef import URIRef
from rdflib import Literal, Namespace, RDF
--
graph = Graph()
DC = Namespace('http://purl.org/dc/terms/')
SKOS = Namespace('http://www.w3.org/2004/02/skos/core#')
LCCO = Namespace('http://loc.gov/catdir/cpso/lcco/')
#!/usr/bin/env python
graph.bind('dc', DC)
graph.bind('skos', SKOS)
graph.bind('lcco', LCCO)
from rdflib.Graph import Graph
from rdflib.URIRef import URIRef
from rdflib import Literal, Namespace, RDF
concept = URIRef(LCCO['1'])
graph.add((concept, RDF.type, SKOS['Concept']))
graph.add((concept, SKOS['prefLabel'], Literal('Scrapbooks')))
graph.add((concept, DC['LCC'], Literal('AC999.0999 - AC999999.Z9999')))
graph = Graph()
DC = Namespace('http://purl.org/dc/terms/')
SKOS = Namespace('http://www.w3.org/2004/02/skos/core#')
LCCO = Namespace('http://loc.gov/catdir/cpso/lcco/')
print graph.serialize(format='n3', base=LCCO)
graph.bind('dc', DC)
graph.bind('skos', SKOS)
graph.bind('lcco', LCCO)
--
concept = URIRef(LCCO['1'])
graph.add((concept, RDF.type, SKOS['Concept']))
graph.add((concept, SKOS['prefLabel'], Literal('Scrapbooks')))
graph.add((concept, DC['LCC'], Literal('AC999.0999 - AC999999.Z9999')))
Which generates:
print graph.serialize(format='n3', base=LCCO)
--
--
@prefix dc: <http://purl.org/dc/terms/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix skos: <http://www.w3.org/2004/02/skos/core#>.
Which generates:
<http://loc.gov/catdir/cpso/lcco/1> a skos:Concept;
dc:LCC "AC999.0999 - AC999999.Z9999";
skos:prefLabel "Scrapbooks".
--
--
@prefix dc: <http://purl.org/dc/terms/>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix skos: <http://www.w3.org/2004/02/skos/core#>.
Notice
<http://loc.gov/catdir/cpso/lcco/1> a skos:Concept;
dc:LCC "AC999.0999 - AC999999.Z9999";
skos:prefLabel "Scrapbooks".
<http://loc.gov/catdir/cpso/lcco/1> a skos:Concept;
--
instead of:
Notice
<1> a skos:Concept;
<http://loc.gov/catdir/cpso/lcco/1> a skos:Concept;
//Ed
instead of:
"""
graph = rdflib.Graph()
DC = rdflib.Namespace("http://purl.org/dc/terms/") # noqa: N806
SKOS = rdflib.Namespace("http://www.w3.org/2004/02/skos/core#") # noqa: N806
LCCO = rdflib.Namespace("http://loc.gov/catdir/cpso/lcco/") # noqa: N806
<1> a skos:Concept;
graph.bind("dc", DC)
graph.bind("skos", SKOS)
graph.bind("lcco", LCCO)
//Ed
concept = rdflib.URIRef(LCCO["1"])
graph.add((concept, rdflib.RDF.type, SKOS["Concept"]))
graph.add((concept, SKOS["prefLabel"], rdflib.Literal("Scrapbooks")))
graph.add((concept, DC["LCC"], rdflib.Literal("AC999.0999 - AC999999.Z9999")))
sg = graph.serialize(format="n3", base=LCCO)
# See issue 248
# Actual test should be the inverse of the below ...
self.assertTrue("<1> a skos:Concept ;" in sg, sg)
"""
graph = rdflib.Graph()
DC = rdflib.Namespace("http://purl.org/dc/terms/") # noqa: N806
SKOS = rdflib.Namespace("http://www.w3.org/2004/02/skos/core#") # noqa: N806
LCCO = rdflib.Namespace("http://loc.gov/catdir/cpso/lcco/") # noqa: N806

graph.bind("dc", DC)
graph.bind("skos", SKOS)
graph.bind("lcco", LCCO)

if __name__ == "__main__":
unittest.main()
concept = rdflib.URIRef(LCCO["1"])
graph.add((concept, rdflib.RDF.type, SKOS["Concept"]))
graph.add((concept, SKOS["prefLabel"], rdflib.Literal("Scrapbooks")))
graph.add((concept, DC["LCC"], rdflib.Literal("AC999.0999 - AC999999.Z9999")))
sg = graph.serialize(format="n3", base=LCCO)
# See issue 248
# Actual test should be the inverse of the below ...
assert "<1> a skos:Concept ;" in sg, sg
Loading

0 comments on commit bbb9880

Please sign in to comment.