1"""Runs the tests for the W3C RDF Working Group's TriG test suite.
2
3"""
4
5from rdflib import ConjunctiveGraph
6from rdflib.namespace import split_uri
7from rdflib.compare import graph_diff, isomorphic
8
9from .manifest import nose_tests, RDFT
10from .testutils import nose_tst_earl_report
11
12verbose = False
13
14
15def trig(test):
16    g = ConjunctiveGraph()
17
18    try:
19        base = 'http://www.w3.org/2013/TriGTests/' + split_uri(test.action)[1]
20
21        g.parse(test.action, publicID=base, format='trig')
22        if not test.syntax:
23            raise AssertionError("Input shouldn't have parsed!")
24
25        if test.result:  # eval test
26            res = ConjunctiveGraph()
27            res.parse(test.result, format='nquads')
28
29            if verbose:
30
31                both, first, second = graph_diff(g, res)
32                if not first and not second:
33                    return
34
35                print('===============================')
36                print('TriG')
37                print(g.serialize(format='nquads'))
38                print('===============================')
39                print('NQuads')
40                print(res.serialize(format='nquads'))
41                print('===============================')
42
43                print("Diff:")
44                # print "%d triples in both"%len(both)
45                print("TriG Only:")
46                for t in first:
47                    print(t)
48
49                print("--------------------")
50                print("NQuads Only")
51                for t in second:
52                    print(t)
53                raise Exception('Graphs do not match!')
54
55            assert isomorphic(g, res), 'graphs must be the same'
56
57    except:
58        if test.syntax:
59            raise
60
61
62testers = {
63    RDFT.TestTrigPositiveSyntax: trig,
64    RDFT.TestTrigNegativeSyntax: trig,
65    RDFT.TestTrigEval: trig,
66    RDFT.TestTrigNegativeEval: trig
67}
68
69
70def test_trig(tests=None):
71    for t in nose_tests(testers, 'test/w3c/trig/manifest.ttl'):
72        if tests:
73            for test in tests:
74                if test in t[1].uri:
75                    break
76            else:
77                continue
78
79        yield t
80
81
82if __name__ == '__main__':
83    verbose = True
84
85    nose_tst_earl_report(test_trig, 'rdflib_trig')
86