summaryrefslogtreecommitdiff
path: root/test/test_trig.py
blob: 742e3281f2cccb583b4269bd9fbc45854105cfc5 (plain)
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
import unittest
import rdflib
import re

from rdflib.py3compat import b

class TestTrig(unittest.TestCase):

    def testEmpty(self):
        g=rdflib.Graph()
        s=g.serialize(format='trig')
        self.assertTrue(s is not None)

    def testRepeatTriples(self):
        g=rdflib.ConjunctiveGraph()
        g.get_context('urn:a').add(( rdflib.URIRef('urn:1'),
                                     rdflib.URIRef('urn:2'),
                                     rdflib.URIRef('urn:3') ))

        g.get_context('urn:b').add(( rdflib.URIRef('urn:1'),
                                     rdflib.URIRef('urn:2'),
                                     rdflib.URIRef('urn:3') ))

        self.assertEqual(len(g.get_context('urn:a')),1)
        self.assertEqual(len(g.get_context('urn:b')),1)

        s=g.serialize(format='trig')
        self.assert_(b('{}') not in s) # no empty graphs!

    def testSameSubject(self):
        g=rdflib.ConjunctiveGraph()
        g.get_context('urn:a').add(( rdflib.URIRef('urn:1'),
                                     rdflib.URIRef('urn:p1'),
                                     rdflib.URIRef('urn:o1') ))

        g.get_context('urn:b').add(( rdflib.URIRef('urn:1'),
                                     rdflib.URIRef('urn:p2'),
                                     rdflib.URIRef('urn:o2') ))

        self.assertEqual(len(g.get_context('urn:a')),1)
        self.assertEqual(len(g.get_context('urn:b')),1)

        s=g.serialize(format='trig')

        self.assertEqual(len(re.findall(b("p1"), s)), 1)
        self.assertEqual(len(re.findall(b("p2"), s)), 1)

        self.assert_(b('{}') not in s) # no empty graphs!

    def testRememberNamespace(self):
        g = rdflib.ConjunctiveGraph()
        g.add((rdflib.URIRef("http://example.com/s"),
               rdflib.RDFS.label,
               rdflib.Literal("example 1"),
               rdflib.URIRef("http://example.com/graph1")))
        # In 4.2.0 the first serialization would fail to include the
        # prefix for the graph but later serialize() calls would work.
        first_out = g.serialize(format='trig')
        second_out = g.serialize(format='trig')
        self.assertIn(b'@prefix ns1: <http://example.com/> .', second_out)
        self.assertIn(b'@prefix ns1: <http://example.com/> .', first_out)

        print first_out

    def testGraphQnameSyntax(self):
        g = rdflib.ConjunctiveGraph()
        g.add((rdflib.URIRef("http://example.com/s"),
               rdflib.RDFS.label,
               rdflib.Literal("example 1"),
               rdflib.URIRef("http://example.com/graph1")))
        out = g.serialize(format='trig')
        self.assertIn(b'ns1:graph1 {', out)

    def testGraphUriSyntax(self):
        g = rdflib.ConjunctiveGraph()
        g.add((rdflib.URIRef("http://example.com/s"),
               rdflib.RDFS.label,
               rdflib.Literal("example 1"),
               # getQName will not abbreviate this, so it should come
               # out as a '<...>' term.
               rdflib.URIRef("http://example.com/foo.")))
        out = g.serialize(format='trig')
        self.assertIn(b'<http://example.com/foo.> {', out)