1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
# -*- coding: UTF-8 -*-
from rdflib.term import URIRef, BNode, Literal
from rdflib.namespace import RDF, RDFS
from io import BytesIO
from rdflib.plugins.serializers.rdfxml import PrettyXMLSerializer
from rdflib.graph import ConjunctiveGraph
class SerializerTestBase(object):
repeats = 8
def setup(self):
graph = ConjunctiveGraph()
graph.parse(data=self.testContent, format=self.testContentFormat)
self.sourceGraph = graph
def test_serialize_and_reparse(self):
reparsedGraph = serialize_and_load(self.sourceGraph, self.serializer)
_assert_equal_graphs(self.sourceGraph, reparsedGraph)
def test_multiple(self):
"""Repeats ``test_serialize`` ``self.repeats`` times, to reduce sucess based on in-memory ordering."""
for i in range(self.repeats):
self.test_serialize_and_reparse()
# test_multiple.slowtest=True # not really slow?
def _assert_equal_graphs(g1, g2):
assert len(g1) == len(g2), "Serialized graph not same size as source graph."
g1copy = _mangled_copy(g1)
g2copy = _mangled_copy(g2)
g1copy -= _mangled_copy(g2)
g2copy -= _mangled_copy(g1)
assert len(g1copy) == 0, "Source graph larger than serialized graph."
assert len(g2copy) == 0, "Serialized graph larger than source graph."
_blank = BNode()
def _mangled_copy(g):
"Makes a copy of the graph, replacing all bnodes with the bnode ``_blank``."
gcopy = ConjunctiveGraph()
def isbnode(v):
return isinstance(v, BNode)
for s, p, o in g:
if isbnode(s):
s = _blank
if isbnode(p):
p = _blank
if isbnode(o):
o = _blank
gcopy.add((s, p, o))
return gcopy
def serialize(sourceGraph, makeSerializer, getValue=True, extra_args={}):
serializer = makeSerializer(sourceGraph)
stream = BytesIO()
serializer.serialize(stream, **extra_args)
return getValue and stream.getvalue() or stream
def serialize_and_load(sourceGraph, makeSerializer):
stream = serialize(sourceGraph, makeSerializer, False)
stream.seek(0)
reparsedGraph = ConjunctiveGraph()
reparsedGraph.load(stream)
return reparsedGraph
class TestPrettyXmlSerializer(SerializerTestBase):
serializer = PrettyXMLSerializer
testContent = """
@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#> .
:value rdfs:domain :Test .
:Test rdfs:subClassOf
[ a owl:Restriction;
owl:onProperty :value ],
[ a owl:Restriction;
owl:onProperty :name ] .
<http://example.org/data/a> a :Test;
rdfs:seeAlso <http://example.org/data/b>;
:value "A" .
<http://example.org/data/b>
:name "Bee"@en, "Be"@sv;
:value "B" .
<http://example.org/data/c> a rdfs:Resource;
rdfs:seeAlso <http://example.org/data/c>;
:value 3 .
<http://example.org/data/d> a rdfs:Resource;
rdfs:seeAlso <http://example.org/data/c> ;
rdfs:seeAlso <http://example.org/data/b> ;
rdfs:seeAlso <http://example.org/data/a> .
_:bnode1 a :BNode;
rdfs:seeAlso _:bnode2 .
_:bnode2 a :BNode ;
rdfs:seeAlso _:bnode3 .
_:bnode3 a :BNode ;
rdfs:seeAlso _:bnode2 .
"""
testContentFormat = "n3"
def test_result_fragments(self):
rdfXml = serialize(self.sourceGraph, self.serializer)
assert (
'<Test rdf:about="http://example.org/data/a">'.encode("latin-1") in rdfXml
)
assert (
'<rdf:Description rdf:about="http://example.org/data/b">'.encode("latin-1")
in rdfXml
)
assert '<name xml:lang="en">Bee</name>'.encode("latin-1") in rdfXml
assert (
'<value rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">3</value>'.encode(
"latin-1"
)
in rdfXml
)
assert (
'<BNode rdf:nodeID="'.encode("latin-1") in rdfXml
), "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
def test_result_fragments_with_base(self):
rdfXml = serialize(
self.sourceGraph,
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 (
'<value rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">3</value>'.encode(
"latin-1"
)
in rdfXml
)
assert (
'<BNode rdf:nodeID="'.encode("latin-1") in rdfXml
), "expected one identified bnode in serialized graph"
def test_subClassOf_objects(self):
reparsedGraph = serialize_and_load(self.sourceGraph, self.serializer)
_assert_expected_object_types_for_predicates(
reparsedGraph, [RDFS.seeAlso, RDFS.subClassOf], [URIRef, BNode]
)
def test_pretty_xmlliteral(self):
# given:
g = ConjunctiveGraph()
g.add(
(
BNode(),
RDF.value,
Literal(
"""<p xmlns="http://www.w3.org/1999/xhtml">See also <a href="#aring">Å</a></p>""",
datatype=RDF.XMLLiteral,
),
)
)
# when:
xmlrepr = g.serialize(format="pretty-xml")
# then:
assert (
"""<rdf:value rdf:parseType="Literal"><p xmlns="http://www.w3.org/1999/xhtml">See also <a href="#aring">Å</a></p></rdf:value>"""
in xmlrepr
)
def test_pretty_broken_xmlliteral(self):
# given:
g = ConjunctiveGraph()
g.add((BNode(), RDF.value, Literal("""<p """, datatype=RDF.XMLLiteral)))
# when:
xmlrepr = g.serialize(format="pretty-xml")
# then:
assert (
"""<rdf:value rdf:datatype="http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"><p """
in xmlrepr
)
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]
assert (
True in someTrue
), "Bad type %s for object when predicate is <%s>." % (type(o), p)
|