from rdflib.term import URIRef, BNode from rdflib.namespace import RDFS from io import BytesIO from rdflib.plugins.serializers.rdfxml import XMLSerializer 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 TestXMLSerializer(SerializerTestBase): serializer = XMLSerializer testContent = """ @prefix rdfs: . @prefix owl: . @prefix : . :value rdfs:domain :Test . :Test rdfs:subClassOf [ a owl:Restriction; owl:onProperty :value ], [ a owl:Restriction; owl:onProperty :name ] . a :Test; rdfs:seeAlso ; :value "A" . :name "Bee"@en, "Be"@sv; :value "B" . a rdfs:Resource; rdfs:seeAlso ; :value 3 . a rdfs:Resource; rdfs:seeAlso ; rdfs:seeAlso ; rdfs:seeAlso . _: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) # print "--------" # print rdfXml # print "--------" assert ( ''.encode("latin-1") in rdfXml ) assert ( ''.encode( "latin-1" ) in rdfXml ) assert ( ''.encode("latin-1") in rdfXml ) assert 'Bee'.encode("latin-1") in rdfXml assert ( '3'.encode( "latin-1" ) in rdfXml ) assert ( ''.encode("latin-1") in rdfXml assert ''.encode("latin-1") in rdfXml assert ''.encode("latin-1") in rdfXml assert ( '3'.encode( "latin-1" ) in rdfXml ) assert ( '." % (type(o), p)