summaryrefslogtreecommitdiff
path: root/test/test_parsers/test_swap_n3.py
blob: e173b8452491644b0efdba04bbacd563d0460f8c (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
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
import os
from test.data import TEST_DATA_DIR

import pytest

import rdflib

"""
SWAP N3 parser test suite
"""

rdf = rdflib.Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
rdfs = rdflib.Namespace("http://www.w3.org/2000/01/rdf-schema#")
xsd = rdflib.Namespace("http://www.w3.org/2001/XMLSchema#")
owl = rdflib.Namespace("http://www.w3.org/2002/07/owl#")
test = rdflib.Namespace("http://www.w3.org/2000/10/swap/test.n3#")
n3test = rdflib.Namespace("http://www.w3.org/2004/11/n3test#")
rdft = rdflib.Namespace("http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#")
triage = rdflib.Namespace("http://www.w3.org/2000/10/swap/test/triage#")
mf = rdflib.Namespace("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#")
qt = rdflib.Namespace("http://www.w3.org/2001/sw/DataAccess/tests/test-query#")


# class TestSWAPN3(unittest.TestCase):
#     """SWAP 2000/10/n3 tests"""

#     def setUp(self):

#     def test_foo(self):
#         """footest"""
#         self.graph.parse(os.getcwd()+'/test/swap-n3/n3-rdf.tests', format="n3")
#         tfiles = []
#         for tst in self.graph.subjects():
#             files = [str(tfile).replace('http://www.w3.org/2000/10/', 'file://'+os.getcwd()+'/test/swap-n3/')
#                         for tfile in self.graph.objects(tst, rdflib.URIRef("http://www.w3.org/2004/11/n3test#inputDocument")) if tfile.endswith('n3')]
#             tfiles += files
#         for tfile in tfiles:
#             self.graph.parse(tfile, format="n3")


skiptests = [
    "syntax_neg_single_quote",
    "syntax_neg_literal_predicate",
    "syntax_this_quantifiers",
    "syntax_trailing_semicolon",
    "syntax_neg_thisadoc",
    "syntax_equals1",
    "syntax_equals2",
    "syntax_this_rules",
    "syntax_neg_keywords3",
    "syntax_zero_objects",
    "syntax_neg_formula_predicate",
    "syntax_zero_predicates",
    # 'syntax_qvars1',
    # 'syntax_qvars2',
    # 'contexts',
    "syntax_too_nested",
]


class Envelope:
    def __init__(self, n, f):
        self.name = n
        self.file = f

    def __repr__(self):
        return self.name


def generictest(e):
    """Documentation"""
    if e.skip:
        pytest.xfail("%s skipped, known issue" % e.name)
    g = rdflib.Graph()
    for i in [rdf, rdfs, xsd, owl, test, n3test, rdft, triage, mf, qt]:
        g.bind(str(i), i)
    g.parse(e.file, format="n3")


def dir_to_uri(directory, sep=os.path.sep):
    """
    Convert a local path to a File URI.

    >>> dir_to_uri('c:\\\\temp\\\\foo\\\\file.txt', sep='\\\\')
    'file:///c:/temp/foo/file.txt'

    >>> dir_to_uri('/tmp/foo/file.txt', sep='/')
    'file:///tmp/foo/file.txt'
    """
    items = directory.split(sep)
    path = "/".join(items)
    if path.startswith("/"):
        path = path[1:]
    return "file:///%s" % (path,)


def get_cases():
    from copy import deepcopy

    g = rdflib.Graph()
    swap_dir = os.path.join(TEST_DATA_DIR, "suites", "swap-n3")
    g.parse(os.path.join(swap_dir, "n3-rdf.tests"), format="n3")
    g.parse(os.path.join(swap_dir, "n3-full.tests"), format="n3")
    tfiles = []
    swap_dir_uri = dir_to_uri(swap_dir) + "/"
    for tst in g.subjects():
        files = [
            str(tfile).replace("http://www.w3.org/2000/10/", swap_dir_uri)
            for tfile in g.objects(
                tst, rdflib.URIRef("http://www.w3.org/2004/11/n3test#inputDocument")
            )
            if tfile.endswith("n3")
        ]
        tfiles += files
    for tfile in set(tfiles):
        gname = tfile.split("/swap-n3/swap/test/")[1][:-3].translate(
            str.maketrans("-/", "__")
        )
        e = Envelope(gname, tfile)
        if gname in skiptests:
            e.skip = True
        else:
            e.skip = False
        # e.skip = True
        gt = deepcopy(generictest)
        gt.__doc__ = tfile
        yield gt, e


@pytest.mark.parametrize("gt, envelope", get_cases())
def test_cases(gt, envelope):
    gt(envelope)