#!/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()

[((62, 4, 62, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((20, 11, 20, 28), 'rdflib.term.URIRef', 'URIRef', ({(20, 18, 20, 27): '"""store:1"""'}, {}), "('store:1')", False, 'from rdflib.term import URIRef, Literal\n'), ((21, 11, 21, 31), 'rdflib.term.URIRef', 'URIRef', ({(21, 18, 21, 30): '"""resource:1"""'}, {}), "('resource:1')", False, 'from rdflib.term import URIRef, Literal\n'), ((22, 11, 22, 31), 'rdflib.term.URIRef', 'URIRef', ({(22, 18, 22, 30): '"""resource:2"""'}, {}), "('resource:2')", False, 'from rdflib.term import URIRef, Literal\n'), ((24, 14, 24, 39), 'rdflib.term.URIRef', 'URIRef', ({(24, 21, 24, 38): '"""predicate:label"""'}, {}), "('predicate:label')", False, 'from rdflib.term import URIRef, Literal\n'), ((26, 11, 26, 33), 'rdflib.graph.Graph', 'Graph', (), '', False, 'from rdflib.graph import Graph\n'), ((30, 11, 30, 28), 'rdflib.term.URIRef', 'URIRef', ({(30, 18, 30, 27): '"""store:2"""'}, {}), "('store:2')", False, 'from rdflib.term import URIRef, Literal\n'), ((31, 11, 31, 33), 'rdflib.graph.Graph', 'Graph', (), '', False, 'from rdflib.graph import Graph\n'), ((34, 10, 34, 28), 'rdflib.graph.ConjunctiveGraph', 'ConjunctiveGraph', ({}, {}), '()', False, 'from rdflib.graph import ConjunctiveGraph\n'), ((39, 11, 39, 31), 'rdflib.term.URIRef', 'URIRef', ({(39, 18, 39, 30): '"""resource:3"""'}, {}), "('resource:3')", False, 'from rdflib.term import URIRef, Literal\n'), ((44, 11, 44, 29), 'rdflib.graph.ConjunctiveGraph', 'ConjunctiveGraph', ({}, {}), '()', False, 'from rdflib.graph import ConjunctiveGraph\n'), ((47, 15, 47, 26), 'StringIO.StringIO', 'StringIO', ({(47, 24, 47, 25): 'r'}, {}), '(r)', False, 'from StringIO import StringIO\n'), ((27, 25, 27, 54), 'rdflib.term.Literal', 'Literal', (), '', False, 'from rdflib.term import URIRef, Literal\n'), ((28, 25, 28, 43), 'rdflib.term.Literal', 'Literal', ({(28, 33, 28, 42): '"""label 2"""'}, {}), "('label 2')", False, 'from rdflib.term import URIRef, Literal\n'), ((32, 25, 32, 43), 'rdflib.term.Literal', 'Literal', ({(32, 33, 32, 42): '"""label 3"""'}, {}), "('label 3')", False, 'from rdflib.term import URIRef, Literal\n'), ((40, 24, 40, 34), 'rdflib.term.Literal', 'Literal', ({(40, 32, 40, 33): '(4)'}, {}), '(4)', False, 'from rdflib.term import URIRef, Literal\n'), ((52, 13, 52, 61), 'rdflib.graph.Graph', 'Graph', (), '', False, 'from rdflib.graph import Graph\n')]