Skip to content

Commit

Permalink
style: Fix linting errors in serializer tests (#2559)
Browse files Browse the repository at this point in the history
This makes it possible to remove some ignores from `pyproject.toml` and
makes the code more PEP8 compliant.
  • Loading branch information
aucampia authored Aug 31, 2023
1 parent e42b752 commit d09c3af
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 82 deletions.
6 changes: 0 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,6 @@ line-length = 88
"test/test_store/test_store_sparql*.py" = [
"N802", # Function name should be lowercase
]
"test/test_serializers/test_{prettyxml,serializer_turtle,serializer_xml}.py" = [
"N802", # Function name should be lowercase
"N803", # Function name should be lowercase
"N806", # Variable in function should be lowercase
"N815", # Variable in class scope should not be mixedCase
]
"test/test_literal/test_uriref_literal_comparison.py" = [
"N802", # Function name should be lowercase
]
Expand Down
68 changes: 34 additions & 34 deletions test/test_serializers/test_prettyxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class SerializerTestBase:

def setup_method(self):
graph = ConjunctiveGraph()
graph.parse(data=self.testContent, format=self.testContentFormat)
self.sourceGraph = graph
graph.parse(data=self.test_content, format=self.test_content_format)
self.source_graph = graph

def test_serialize_and_reparse(self):
reparsedGraph = serialize_and_load(self.sourceGraph, self.serializer)
_assert_equal_graphs(self.sourceGraph, reparsedGraph)
reparsed_graph = serialize_and_load(self.source_graph, self.serializer)
_assert_equal_graphs(self.source_graph, reparsed_graph)

def test_multiple(self):
"""Repeats ``test_serialize`` ``self.repeats`` times, to reduce sucess based on in-memory ordering."""
Expand Down Expand Up @@ -58,25 +58,25 @@ def isbnode(v):
return gcopy


def serialize(sourceGraph, makeSerializer, getValue=True, extra_args={}):
serializer = makeSerializer(sourceGraph)
def serialize(source_graph, make_serializer, get_value=True, extra_args={}):
serializer = make_serializer(source_graph)
stream = BytesIO()
serializer.serialize(stream, **extra_args)
return getValue and stream.getvalue() or stream
return get_value and stream.getvalue() or stream


def serialize_and_load(sourceGraph, makeSerializer):
stream = serialize(sourceGraph, makeSerializer, False)
def serialize_and_load(source_graph, make_serializer):
stream = serialize(source_graph, make_serializer, False)
stream.seek(0)
reparsedGraph = ConjunctiveGraph()
reparsedGraph.parse(stream, format="xml")
return reparsedGraph
reparsed_graph = ConjunctiveGraph()
reparsed_graph.parse(stream, format="xml")
return reparsed_graph


class TestPrettyXmlSerializer(SerializerTestBase):
serializer = PrettyXMLSerializer

testContent = """
test_content = """
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix : <http://example.org/model/test#> .
Expand Down Expand Up @@ -116,57 +116,57 @@ class TestPrettyXmlSerializer(SerializerTestBase):
rdfs:seeAlso _:bnode2 .
"""
testContentFormat = "n3"
test_content_format = "n3"

def test_result_fragments(self):
rdfXml = serialize(self.sourceGraph, self.serializer)
rdf_xml = serialize(self.source_graph, self.serializer)
assert (
'<Test rdf:about="http://example.org/data/a">'.encode("latin-1") in rdfXml
'<Test rdf:about="http://example.org/data/a">'.encode("latin-1") in rdf_xml
)
assert (
'<rdf:Description rdf:about="http://example.org/data/b">'.encode("latin-1")
in rdfXml
in rdf_xml
)
assert '<name xml:lang="en">Bee</name>'.encode("latin-1") in rdfXml
assert '<name xml:lang="en">Bee</name>'.encode("latin-1") in rdf_xml
assert (
'<value rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">3</value>'.encode(
"latin-1"
)
in rdfXml
in rdf_xml
)
assert (
'<BNode rdf:nodeID="'.encode("latin-1") in rdfXml
'<BNode rdf:nodeID="'.encode("latin-1") in rdf_xml
), "expected one identified bnode in serialized graph"
# onlyBNodesMsg = "expected only inlined subClassOf-bnodes in serialized graph"
# assert '<rdfs:subClassOf>' in rdfXml, onlyBNodesMsg
# assert not '<rdfs:subClassOf ' in rdfXml, onlyBNodesMsg
# assert '<rdfs:subClassOf>' in rdf_xml, onlyBNodesMsg
# assert not '<rdfs:subClassOf ' in rdf_xml, onlyBNodesMsg

def test_result_fragments_with_base(self):
rdfXml = serialize(
self.sourceGraph,
rdf_xml = serialize(
self.source_graph,
self.serializer,
extra_args={
"base": "http://example.org/",
"xml_base": "http://example.org/",
},
)
assert 'xml:base="http://example.org/"'.encode("latin-1") in rdfXml
assert '<Test rdf:about="data/a">'.encode("latin-1") in rdfXml
assert '<rdf:Description rdf:about="data/b">'.encode("latin-1") in rdfXml
assert 'xml:base="http://example.org/"'.encode("latin-1") in rdf_xml
assert '<Test rdf:about="data/a">'.encode("latin-1") in rdf_xml
assert '<rdf:Description rdf:about="data/b">'.encode("latin-1") in rdf_xml
assert (
'<value rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">3</value>'.encode(
"latin-1"
)
in rdfXml
in rdf_xml
)
assert (
'<BNode rdf:nodeID="'.encode("latin-1") in rdfXml
'<BNode rdf:nodeID="'.encode("latin-1") in rdf_xml
), "expected one identified bnode in serialized graph"

def test_subClassOf_objects(self):
reparsedGraph = serialize_and_load(self.sourceGraph, self.serializer)
def test_subclass_of_objects(self):
reparsed_graph = serialize_and_load(self.source_graph, self.serializer)
_assert_expected_object_types_for_predicates(
reparsedGraph, [RDFS.seeAlso, RDFS.subClassOf], [URIRef, BNode]
reparsed_graph, [RDFS.seeAlso, RDFS.subClassOf], [URIRef, BNode]
)

def test_pretty_xmlliteral(self):
Expand Down Expand Up @@ -206,7 +206,7 @@ def test_pretty_broken_xmlliteral(self):
def _assert_expected_object_types_for_predicates(graph, predicates, types):
for s, p, o in graph:
if p in predicates:
someTrue = [isinstance(o, t) for t in types]
some_true = [isinstance(o, t) for t in types]
assert (
True in someTrue
True in some_true
), "Bad type %s for object when predicate is <%s>." % (type(o), p)
12 changes: 6 additions & 6 deletions test/test_serializers/test_serializer_turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rdflib.plugins.serializers.turtle import TurtleSerializer


def testTurtleFinalDot():
def test_turtle_final_dot():
"""
https://github.com/RDFLib/rdflib/issues/282
"""
Expand All @@ -16,7 +16,7 @@ def testTurtleFinalDot():
assert b"ns:bob." not in s


def testTurtleBoolList():
def test_turtle_bool_list():
subject = URIRef("http://localhost/user")
predicate = URIRef("http://localhost/vocab#hasList")
g1 = Graph()
Expand All @@ -40,7 +40,7 @@ def testTurtleBoolList():
assert bool_list == [True, False, True]


def testUnicodeEscaping():
def test_unicode_escaping():
turtle_string = " <http://example.com/A> <http://example.com/B> <http://example.com/aaa\\u00F3bbbb> . <http://example.com/A> <http://example.com/C> <http://example.com/zzz\\U00100000zzz> . <http://example.com/A> <http://example.com/D> <http://example.com/aaa\\u00f3bbb> ."
g = Graph()

Expand All @@ -56,21 +56,21 @@ def testUnicodeEscaping():


def test_turtle_valid_list():
NS = Namespace("http://example.org/ns/")
ns = Namespace("http://example.org/ns/")
g = Graph()
g.parse(
data="""
@prefix : <{0}> .
:s :p (""), (0), (false) .
""".format(
NS
ns
),
format="turtle",
)

turtle_serializer = TurtleSerializer(g)

for o in g.objects(NS.s, NS.p):
for o in g.objects(ns.s, ns.p):
assert turtle_serializer.isValidList(o)


Expand Down
72 changes: 36 additions & 36 deletions test/test_serializers/test_serializer_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class SerializerTestBase:

def setup_method(self):
graph = ConjunctiveGraph()
graph.parse(data=self.testContent, format=self.testContentFormat)
self.sourceGraph = graph
graph.parse(data=self.test_content, format=self.test_content_format)
self.source_graph = graph

def test_serialize_and_reparse(self):
reparsedGraph = serialize_and_load(self.sourceGraph, self.serializer)
_assert_equal_graphs(self.sourceGraph, reparsedGraph)
reparsed_graph = serialize_and_load(self.source_graph, self.serializer)
_assert_equal_graphs(self.source_graph, reparsed_graph)

def test_multiple(self):
"""Repeats ``test_serialize`` ``self.repeats`` times, to reduce sucess based on in-memory ordering."""
Expand Down Expand Up @@ -57,25 +57,25 @@ def isbnode(v):
return gcopy


def serialize(sourceGraph, makeSerializer, getValue=True, extra_args={}):
serializer = makeSerializer(sourceGraph)
def serialize(source_graph, make_serializer, get_value=True, extra_args={}):
serializer = make_serializer(source_graph)
stream = BytesIO()
serializer.serialize(stream, **extra_args)
return getValue and stream.getvalue() or stream
return get_value and stream.getvalue() or stream


def serialize_and_load(sourceGraph, makeSerializer):
stream = serialize(sourceGraph, makeSerializer, False)
def serialize_and_load(source_graph, make_serializer):
stream = serialize(source_graph, make_serializer, False)
stream.seek(0)
reparsedGraph = ConjunctiveGraph()
reparsedGraph.parse(stream, publicID=None, format="xml")
return reparsedGraph
reparsed_graph = ConjunctiveGraph()
reparsed_graph.parse(stream, publicID=None, format="xml")
return reparsed_graph


class TestXMLSerializer(SerializerTestBase):
serializer = XMLSerializer

testContent = """
test_content = """
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix : <http://example.org/model/test#> .
Expand Down Expand Up @@ -115,75 +115,75 @@ class TestXMLSerializer(SerializerTestBase):
rdfs:seeAlso _:bnode2 .
"""
testContentFormat = "n3"
test_content_format = "n3"

def test_result_fragments(self):
rdfXml = serialize(self.sourceGraph, self.serializer)
rdf_xml = serialize(self.source_graph, self.serializer)
# print "--------"
# print rdfXml
# print rdf_xml
# print "--------"
assert (
'<rdf:Description rdf:about="http://example.org/data/a">'.encode("latin-1")
in rdfXml
in rdf_xml
)
assert (
'<rdf:type rdf:resource="http://example.org/model/test#Test"/>'.encode(
"latin-1"
)
in rdfXml
in rdf_xml
)
assert (
'<rdf:Description rdf:about="http://example.org/data/b">'.encode("latin-1")
in rdfXml
in rdf_xml
)
assert '<name xml:lang="en">Bee</name>'.encode("latin-1") in rdfXml
assert '<name xml:lang="en">Bee</name>'.encode("latin-1") in rdf_xml
assert (
'<value rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">3</value>'.encode(
"latin-1"
)
in rdfXml
in rdf_xml
)
assert (
'<rdf:Description rdf:nodeID="'.encode("latin-1") in rdfXml
'<rdf:Description rdf:nodeID="'.encode("latin-1") in rdf_xml
), "expected one identified bnode in serialized graph"

def test_result_fragments_with_base(self):
rdfXml = serialize(
self.sourceGraph,
rdf_xml = serialize(
self.source_graph,
self.serializer,
extra_args={
"base": "http://example.org/",
"xml_base": "http://example.org/",
},
)
# print "--------"
# print rdfXml
# print rdf_xml
# print "--------"
assert 'xml:base="http://example.org/"'.encode("latin-1") in rdfXml
assert '<rdf:Description rdf:about="data/a">'.encode("latin-1") in rdfXml
assert '<rdf:type rdf:resource="model/test#Test"/>'.encode("latin-1") in rdfXml
assert '<rdf:Description rdf:about="data/b">'.encode("latin-1") in rdfXml
assert 'xml:base="http://example.org/"'.encode("latin-1") in rdf_xml
assert '<rdf:Description rdf:about="data/a">'.encode("latin-1") in rdf_xml
assert '<rdf:type rdf:resource="model/test#Test"/>'.encode("latin-1") in rdf_xml
assert '<rdf:Description rdf:about="data/b">'.encode("latin-1") in rdf_xml
assert (
'<value rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">3</value>'.encode(
"latin-1"
)
in rdfXml
in rdf_xml
)
assert (
'<rdf:Description rdf:nodeID="'.encode("latin-1") in rdfXml
'<rdf:Description rdf:nodeID="'.encode("latin-1") in rdf_xml
), "expected one identified bnode in serialized graph"

def test_subClassOf_objects(self):
reparsedGraph = serialize_and_load(self.sourceGraph, self.serializer)
def test_subslass_of_objects(self):
reparsed_graph = serialize_and_load(self.source_graph, self.serializer)
_assert_expected_object_types_for_predicates(
reparsedGraph, [RDFS.seeAlso, RDFS.subClassOf], [URIRef, BNode]
reparsed_graph, [RDFS.seeAlso, RDFS.subClassOf], [URIRef, BNode]
)


def _assert_expected_object_types_for_predicates(graph, predicates, types):
for s, p, o in graph:
if p in predicates:
someTrue = [isinstance(o, t) for t in types]
some_true = [isinstance(o, t) for t in types]
assert (
True in someTrue
True in some_true
), "Bad type %s for object when predicate is <%s>." % (type(o), p)

0 comments on commit d09c3af

Please sign in to comment.