#!/usr/bin/env python

import unittest

from rdflib.graph import ConjunctiveGraph
from rdflib.term import URIRef, Literal
from rdflib.graph import Graph


class TestTrixSerialize(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testSerialize(self):

      s1 = URIRef('store:1')
      r1 = URIRef('resource:1')
      r2 = URIRef('resource:2')

      label = URIRef('predicate:label')

      g1 = Graph(identifier = s1)
      g1.add((r1, label, Literal("label 1", lang="en")))
      g1.add((r1, label, Literal("label 2")))

      s2 = URIRef('store:2')
      g2 = Graph(identifier = s2)
      g2.add((r2, label, Literal("label 3")))

      g = ConjunctiveGraph()
      for s,p,o in g1.triples((None, None, None)):
        g.addN([(s,p,o,g1)])
      for s,p,o in g2.triples((None, None, None)):
        g.addN([(s,p,o,g2)])
      r3 = URIRef('resource:3')
      g.add((r3, label, Literal(4)))
      
      
      r = g.serialize(format='trix')
      g3 = ConjunctiveGraph()
      from StringIO import StringIO

      g3.parse(StringIO(r), format='trix')

      for q in g3.quads((None,None,None)):
        # TODO: Fix once getGraph/getContext is in conjunctive graph
        if isinstance(q[3].identifier, URIRef): 
          tg=Graph(store=g.store, identifier=q[3].identifier)
        else:
          # BNode, this is a bit ugly
          # we cannot match the bnode to the right graph automagically
          # here I know there is only one anonymous graph, 
          # and that is the default one, but this is not always the case
          tg=g.default_context
        self.assertTrue(q[0:3] in tg)

if __name__=='__main__':
    unittest.main()

[((1684, 1699), 'unittest.main', 'unittest.main', ([], {}), '()\n', (1697, 1699), False, 'import unittest\n'), ((314, 331), 'rdflib.term.URIRef', 'URIRef', (['"""store:1"""'], {}), "('store:1')\n", (320, 331), False, 'from rdflib.term import URIRef, Literal\n'), ((343, 363), 'rdflib.term.URIRef', 'URIRef', (['"""resource:1"""'], {}), "('resource:1')\n", (349, 363), False, 'from rdflib.term import URIRef, Literal\n'), ((375, 395), 'rdflib.term.URIRef', 'URIRef', (['"""resource:2"""'], {}), "('resource:2')\n", (381, 395), False, 'from rdflib.term import URIRef, Literal\n'), ((411, 436), 'rdflib.term.URIRef', 'URIRef', (['"""predicate:label"""'], {}), "('predicate:label')\n", (417, 436), False, 'from rdflib.term import URIRef, Literal\n'), ((449, 469), 'rdflib.graph.Graph', 'Graph', ([], {'identifier': 's1'}), '(identifier=s1)\n', (454, 469), False, 'from rdflib.graph import Graph\n'), ((587, 604), 'rdflib.term.URIRef', 'URIRef', (['"""store:2"""'], {}), "('store:2')\n", (593, 604), False, 'from rdflib.term import URIRef, Literal\n'), ((616, 636), 'rdflib.graph.Graph', 'Graph', ([], {'identifier': 's2'}), '(identifier=s2)\n', (621, 636), False, 'from rdflib.graph import Graph\n'), ((696, 714), 'rdflib.graph.ConjunctiveGraph', 'ConjunctiveGraph', ([], {}), '()\n', (712, 714), False, 'from rdflib.graph import ConjunctiveGraph\n'), ((886, 906), 'rdflib.term.URIRef', 'URIRef', (['"""resource:3"""'], {}), "('resource:3')\n", (892, 906), False, 'from rdflib.term import URIRef, Literal\n'), ((1006, 1024), 'rdflib.graph.ConjunctiveGraph', 'ConjunctiveGraph', ([], {}), '()\n', (1022, 1024), False, 'from rdflib.graph import ConjunctiveGraph\n'), ((1077, 1088), 'StringIO.StringIO', 'StringIO', (['r'], {}), '(r)\n', (1085, 1088), False, 'from StringIO import StringIO\n'), ((497, 526), 'rdflib.term.Literal', 'Literal', (['"""label 1"""'], {'lang': '"""en"""'}), "('label 1', lang='en')\n", (504, 526), False, 'from rdflib.term import URIRef, Literal\n'), ((554, 572), 'rdflib.term.Literal', 'Literal', (['"""label 2"""'], {}), "('label 2')\n", (561, 572), False, 'from rdflib.term import URIRef, Literal\n'), ((664, 682), 'rdflib.term.Literal', 'Literal', (['"""label 3"""'], {}), "('label 3')\n", (671, 682), False, 'from rdflib.term import URIRef, Literal\n'), ((931, 941), 'rdflib.term.Literal', 'Literal', (['(4)'], {}), '(4)\n', (938, 941), False, 'from rdflib.term import URIRef, Literal\n'), ((1280, 1328), 'rdflib.graph.Graph', 'Graph', ([], {'store': 'g.store', 'identifier': 'q[3].identifier'}), '(store=g.store, identifier=q[3].identifier)\n', (1285, 1328), False, 'from rdflib.graph import Graph\n')]