File: test_roundtrip.py

package info (click to toggle)
rdflib 6.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 38,248 kB
  • sloc: python: 39,216; sh: 153; makefile: 110
file content (142 lines) | stat: -rw-r--r-- 4,438 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import pytest

import rdflib
import rdflib.compare

try:
    from .test_nt_suite import all_nt_files
    assert all_nt_files

    from .test_n3_suite import all_n3_files
    assert all_n3_files
except:
    from test.test_nt_suite import all_nt_files
    from test.test_n3_suite import all_n3_files

"""
Test round-tripping by all serializers/parser that are registered.
This means, you may test more than just core rdflib!

run with no arguments to test all formats + all files
run with a single argument, to test only that format, i.e. "n3"
run with three arguments to test round-tripping in a given format
and reading a single file in the given format, i.e.:

python test/test_roundtrip.py xml nt test/nt/literals-02.nt

tests roundtripping through rdf/xml with only the literals-02 file

HexTuples format, "hext", cannot be used in all roundtrips due to its
addition of xsd:string to literals of no declared type as this breaks
(rdflib) graph isomorphism, and given that its JSON serialization is
simple (lacking), so hext has been excluded from roundtripping here
but provides some roundtrip test functions of its own (see test_parser_hext.py
& test_serializer_hext.py)

"""


SKIP = [
    ("xml", "test/n3/n3-writer-test-29.n3"),
    # has predicates that cannot be shortened to strict qnames
    ("xml", "test/nt/qname-02.nt"),  # uses a property that cannot be qname'd
    ("trix", "test/n3/strquot.n3"),  # contains characters forbidden by the xml spec
    ("xml", "test/n3/strquot.n3"),  # contains characters forbidden by the xml spec
    ("json-ld", "test/nt/keywords-04.nt"),  # known NT->JSONLD problem
    ("json-ld", "test/n3/example-misc.n3"),  # known N3->JSONLD problem
    ("json-ld", "test/n3/n3-writer-test-16.n3"),  # known N3->JSONLD problem
    ("json-ld", "test/n3/rdf-test-11.n3"),  # known N3->JSONLD problem
    ("json-ld", "test/n3/rdf-test-28.n3"),  # known N3->JSONLD problem
    ("json-ld", "test/n3/n3-writer-test-26.n3"),  # known N3->JSONLD problem
    ("json-ld", "test/n3/n3-writer-test-28.n3"),  # known N3->JSONLD problem
    ("json-ld", "test/n3/n3-writer-test-22.n3"),  # known N3->JSONLD problem
    ("json-ld", "test/n3/rdf-test-21.n3"),  # known N3->JSONLD problem
]


def roundtrip(e, verbose=False):
    infmt, testfmt, source = e

    g1 = rdflib.ConjunctiveGraph()

    g1.parse(source, format=infmt)

    s = g1.serialize(format=testfmt)

    if verbose:
        print("S:")
        print(s, flush=True)

    g2 = rdflib.ConjunctiveGraph()
    g2.parse(data=s, format=testfmt)

    if verbose:
        both, first, second = rdflib.compare.graph_diff(g1, g2)
        print("Diff:")
        print("%d triples in both" % len(both))
        print("G1 Only:")
        for t in sorted(first):
            print(t)

        print("--------------------")
        print("G2 Only")
        for t in sorted(second):
            print(t)

    assert rdflib.compare.isomorphic(g1, g2)

    if verbose:
        print("Ok!")


formats = None


def get_cases():
    global formats
    if not formats:
        serializers = set(
            x.name for x in rdflib.plugin.plugins(None, rdflib.plugin.Serializer)
        )
        parsers = set(x.name for x in rdflib.plugin.plugins(None, rdflib.plugin.Parser))
        formats = parsers.intersection(serializers)

    for testfmt in formats:
        if testfmt != "hext":
            if "/" in testfmt:
                continue  # skip double testing
            for f, infmt in all_nt_files():
                if (testfmt, f) not in SKIP:
                    yield roundtrip, (infmt, testfmt, f)


@pytest.mark.parametrize("checker, args", get_cases())
def test_cases(checker, args):
    checker(args)


def get_n3_test():
    global formats
    if not formats:
        serializers = set(
            x.name for x in rdflib.plugin.plugins(None, rdflib.plugin.Serializer)
        )
        parsers = set(x.name for x in rdflib.plugin.plugins(None, rdflib.plugin.Parser))
        formats = parsers.intersection(serializers)

    for testfmt in formats:
        if testfmt != "hext":
            if "/" in testfmt:
                continue  # skip double testing
            for f, infmt in all_n3_files():
                if (testfmt, f) not in SKIP:
                    yield roundtrip, (infmt, testfmt, f)


@pytest.mark.parametrize("checker, args", get_n3_test())
def test_n3(checker, args):
    checker(args)


if __name__ == "__main__":
    print("hi")