-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.py
167 lines (122 loc) · 5.09 KB
/
tests.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import json
import string
import unittest
from unittest.mock import patch
from simplejson import dumps
from SPARQLTransformer import sparqlTransformer, pre_process
import SPARQLTransformer
OUTPUT = './examples/json_transformed/'
JSONLD_QUERIES = './examples/json_queries/'
SPARQL_OUTPUT = './examples/sparql_output/'
SPARQL_QUERIES = './examples/sparql_queries/'
opt = {
'debug': True
}
def mock(filename):
with open(os.path.join(SPARQL_OUTPUT, filename)) as data:
obj = json.load(data)
def f(self):
class x:
@staticmethod
def convert():
return obj
return x
return f
def load(filename):
with open(os.path.join(JSONLD_QUERIES, filename)) as data:
q = json.load(data)
with open(os.path.join(OUTPUT, filename)) as data:
expected = json.load(data)
with open(os.path.join(SPARQL_QUERIES, filename.replace('.json', '.rq'))) as data:
rq = data.read().strip()
return q, expected, rq
def get_sparql_query(q):
query, proto, opt = pre_process(q, {'debug': False})
return query
def cleans(s):
return s.translate({ord(c): None for c in string.whitespace})
class TestStringMethods(unittest.TestCase):
@patch.object(SPARQLTransformer.SPARQLWrapper, 'query', mock('city.list.json'))
def test_proto(self):
q, expected, rq = load('city.list.json')
outSparql = get_sparql_query(q)
self.assertEqual(cleans(outSparql), cleans(rq))
out = sparqlTransformer(q, opt)
# with open('a.json', 'w') as o:
# json.dump(out, o)
self.assertEqual(dumps(out), dumps(expected))
@patch.object(SPARQLTransformer.SPARQLWrapper, 'query', mock('city.list.ld.json'))
def test_jsonld(self):
q, expected, rq = load('city.list.ld.json')
outSparql = get_sparql_query(q)
self.assertEqual(cleans(outSparql), cleans(rq))
out = sparqlTransformer(q, opt)
# with open('a.json', 'w') as o:
# json.dump(out, o)
self.assertEqual(dumps(out), dumps(expected))
@patch.object(SPARQLTransformer.SPARQLWrapper, 'query', mock('city.region.list.ld.json'))
def test_nested(self):
q, expected, rq = load('city.region.list.ld.json')
outSparql = get_sparql_query(q)
self.assertEqual(cleans(outSparql), cleans(rq))
out = sparqlTransformer(q, opt)
# with open('a.json', 'w') as o:
# json.dump(out, o)
self.assertEqual(dumps(out), dumps(expected))
@patch.object(SPARQLTransformer.SPARQLWrapper, 'query', mock('band.json'))
def test_anchor(self):
q, expected, rq = load('band.json')
outSparql = get_sparql_query(q)
self.assertEqual(cleans(outSparql), cleans(rq))
out = sparqlTransformer(q, opt)
# with open('a.json', 'w') as o:
# json.dump(out, o)
self.assertEqual(dumps(out), dumps(expected))
@patch.object(SPARQLTransformer.SPARQLWrapper, 'query', mock('band_reversed.json'))
def test_reversed(self):
q, expected, rq = load('band_reversed.json')
outSparql = get_sparql_query(q)
self.assertEqual(cleans(outSparql), cleans(rq))
out = sparqlTransformer(q, opt)
# with open('a.json', 'w') as o:
# json.dump(out, o)
self.assertEqual(dumps(out), dumps(expected))
@patch.object(SPARQLTransformer.SPARQLWrapper, 'query', mock('issue_10_duplicate_vars.json'))
def test_reversed(self):
q, expected, rq = load('issue_10_duplicate_vars.json')
outSparql = get_sparql_query(q)
self.assertEqual(cleans(outSparql), cleans(rq))
out = sparqlTransformer(q, opt)
# with open('a.json', 'w') as o:
# json.dump(out, o)
self.assertEqual(dumps(out), dumps(expected))
@patch.object(SPARQLTransformer.SPARQLWrapper, 'query', mock('aggregates.json'))
def test_aggregates(self):
q, expected, rq = load('aggregates.json')
outSparql = get_sparql_query(q)
self.assertEqual(cleans(outSparql), cleans(rq))
out = sparqlTransformer(q, opt)
# with open('a.json', 'w') as o:
# json.dump(out, o)
self.assertEqual(dumps(out), dumps(expected))
@patch.object(SPARQLTransformer.SPARQLWrapper, 'query', mock('band_forcelist.json'))
def test_forcelist(self):
q, expected, rq = load('band_forcelist.json')
outSparql = get_sparql_query(q)
self.assertEqual(cleans(outSparql), cleans(rq))
out = sparqlTransformer(q, opt)
# with open('a.json', 'w') as o:
# json.dump(out, o)
self.assertEqual(dumps(out), dumps(expected))
@patch.object(SPARQLTransformer.SPARQLWrapper, 'query', mock('band.liblimit.json'))
def test_library_limit(self):
q, expected, rq = load('band.liblimit.json')
outSparql = get_sparql_query(q)
self.assertEqual(cleans(outSparql), cleans(rq))
out = sparqlTransformer(q, opt)
# with open('a.json', 'w') as o:
# json.dump(out, o)
self.assertEqual(dumps(out), dumps(expected))
if __name__ == '__main__':
unittest.main()