<s> from rdflib . term import URIRef , BNode <EOL> from rdflib . namespace import RDFS <EOL> from rdflib . py3compat import b <EOL> from rdflib . plugins . serializers . rdfxml import XMLSerializer <EOL> from rdflib . graph import ConjunctiveGraph <EOL> try : <EOL> from io import BytesIO <EOL> except ImportError : <EOL> from StringIO import StringIO as BytesIO <EOL> class SerializerTestBase ( object ) : <EOL> repeats = <NUM_LIT:8> <EOL> def setup ( self ) : <EOL> graph = ConjunctiveGraph ( ) <EOL> graph . parse ( data = self . testContent , format = self . testContentFormat ) <EOL> self . sourceGraph = graph <EOL> def test_serialize_and_reparse ( self ) : <EOL> reparsedGraph = serialize_and_load ( self . sourceGraph , self . serializer ) <EOL> _assert_equal_graphs ( self . sourceGraph , reparsedGraph ) <EOL> def test_multiple ( self ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( self . repeats ) : <EOL> self . test_serialize_and_reparse ( ) <EOL> def _assert_equal_graphs ( g1 , g2 ) : <EOL> assert len ( g1 ) == len ( g2 ) , "<STR_LIT>" <EOL> g1copy = _mangled_copy ( g1 ) <EOL> g2copy = _mangled_copy ( g2 ) <EOL> g1copy -= _mangled_copy ( g2 ) <EOL> g2copy -= _mangled_copy ( g1 ) <EOL> assert len ( g1copy ) == <NUM_LIT:0> , "<STR_LIT>" <EOL> assert len ( g2copy ) == <NUM_LIT:0> , "<STR_LIT>" <EOL> _blank = BNode ( ) <EOL> def _mangled_copy ( g ) : <EOL> "<STR_LIT>" <EOL> gcopy = ConjunctiveGraph ( ) <EOL> isbnode = lambda v : isinstance ( v , BNode ) <EOL> for s , p , o in g : <EOL> if isbnode ( s ) : s = _blank <EOL> if isbnode ( p ) : p = _blank <EOL> if isbnode ( o ) : o = _blank <EOL> gcopy . add ( ( s , p , o ) ) <EOL> return gcopy <EOL> def serialize ( sourceGraph , makeSerializer , getValue = True , extra_args = { } ) : <EOL> serializer = makeSerializer ( sourceGraph ) <EOL> stream = BytesIO ( ) <EOL> serializer . serialize ( stream , ** extra_args ) <EOL> return getValue and stream . getvalue ( ) or stream <EOL> def serialize_and_load ( sourceGraph , makeSerializer ) : <EOL> stream = serialize ( sourceGraph , makeSerializer , False ) <EOL> stream . seek ( <NUM_LIT:0> ) <EOL> reparsedGraph = ConjunctiveGraph ( ) <EOL> reparsedGraph . load ( stream ) <EOL> return reparsedGraph <EOL> class TestXMLSerializer ( SerializerTestBase ) : <EOL> serializer = XMLSerializer <EOL> testContent = """<STR_LIT>""" <EOL> testContentFormat = '<STR_LIT>' <EOL> def test_result_fragments ( self ) : <EOL> rdfXml = serialize ( self . sourceGraph , self . serializer ) <EOL> assert b ( '<STR_LIT>' ) in rdfXml <EOL> assert b ( '<STR_LIT>' ) in rdfXml <EOL> assert b ( '<STR_LIT>' ) in rdfXml <EOL> assert b ( '<STR_LIT>' ) in rdfXml <EOL> assert b ( '<STR_LIT>' ) in rdfXml <EOL> assert b ( '<STR_LIT>' ) in rdfXml , "<STR_LIT>" <EOL> def test_result_fragments_with_base ( self ) : <EOL> rdfXml = serialize ( self . sourceGraph , self . serializer , <EOL> extra_args = { '<STR_LIT>' : "<STR_LIT>" , '<STR_LIT>' : "<STR_LIT>" } ) <EOL> assert b ( '<STR_LIT>' ) in rdfXml <EOL> assert b ( '<STR_LIT>' ) in rdfXml <EOL> assert b ( '<STR_LIT>' ) in rdfXml <EOL> assert b ( '<STR_LIT>' ) in rdfXml <EOL> assert b ( '<STR_LIT>' ) in rdfXml <EOL> assert b ( '<STR_LIT>' ) in rdfXml , "<STR_LIT>" <EOL> def test_subClassOf_objects ( self ) : <EOL> reparsedGraph = serialize_and_load ( self . sourceGraph , self . serializer ) <EOL> _assert_expected_object_types_for_predicates ( reparsedGraph , <EOL> [ RDFS . seeAlso , RDFS . subClassOf ] , <EOL> [ URIRef , BNode ] ) <EOL> def _assert_expected_object_types_for_predicates ( graph , predicates , types ) : <EOL> for s , p , o in graph : <EOL> if p in predicates : <EOL> someTrue = [ isinstance ( o , t ) for t in types ] <EOL> assert True in someTrue , "<STR_LIT>" % ( type ( o ) , p ) </s>
