-
Notifications
You must be signed in to change notification settings - Fork 3
/
test-sparql.lisp
258 lines (251 loc) · 8.81 KB
/
test-sparql.lisp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
;;; CL-AGRAPH symbolic SPARQL translator test
;;; (c) Vsevolod Dyomkin. see LICENSE file for permissions
(in-package :agraph)
(named-readtables:in-readtable rutils-readtable)
(defmethod should-check ((key (eql :match-except-ws)) expected fn &rest _)
(declare (ignore _))
(with ((raw (call fn))
(rez (re:regex-replace-all "\\s+" raw "")))
(or (string= rez (re:regex-replace-all "\\s+" expected ""))
(values nil
raw))))
(deftest simple ()
(should match-except-ws "
SELECT * { ?S ?P ?O . }"
(generate-sparql '(select * (?s ?p ?o))
nil))
(should match-except-ws "
SELECT * { ?S ?P ?O1 , ?O2 . }"
(generate-sparql '(select * (?s ?p (?o1
?o2)))
nil))
(should match-except-ws "
SELECT *
{ ?S1 ?P1 ?O1 ;
?P2 ?O2 ,
?O3 .
?S4 ?P4 ?O4 . }"
(generate-sparql '(select * ((?s1 (?p1 ?o1
?p2 (?o2
?o3)))
(?s4 ?p4 ?o4)))
nil))
(should match-except-ws "
SELECT ?X
WHERE { ?X <http://www.w3.org/2001/vcard-rdf/3.0#FN> \"John Smith\" . }"
(generate-sparql
'(select ?x
where (?x |<http://www.w3.org/2001/vcard-rdf/3.0#FN>| "John Smith"))
nil))
(should match-except-ws "
SELECT ?GIVENNAME
WHERE
{ ?Y <http://www.w3.org/2001/vcard-rdf/3.0#Family> \"Smith\" .
?Y <http://www.w3.org/2001/vcard-rdf/3.0#Given> ?GIVENNAME .
}"
(generate-sparql
'(select ?givenName
where ((?y |<http://www.w3.org/2001/vcard-rdf/3.0#Family>| "Smith")
(?y |<http://www.w3.org/2001/vcard-rdf/3.0#Given>| ?givenName)))
nil))
(should match-except-ws "
SELECT * { ?S ?P ?O . }
ORDER BY DESC(?S)"
(generate-sparql '(select * (?s ?p ?o)
order by (:desc ?s))
nil))
(should match-except-ws "
SELECT * { ?S ?P ?O . }
ORDER BY x:func(?X)"
(generate-sparql '(select * (?s ?p ?o)
order by (:|x:func| ?x))
nil)))
(deftest custom ()
(should match-except-ws "
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?GIVENNAME
WHERE
{ ?Y vcard:Family \"Smith\" .
?Y vcard:Given ?GIVENNAME .
}"
(generate-sparql '((:prefix |vCard| |<http://www.w3.org/2001/vcard-rdf/3.0#>|)
select ?givenName
where ((?y |vcard:Family| "Smith")
(?y |vcard:Given| ?givenName)))
nil))
(should match-except-ws "
SELECT ?G
WHERE
{ ?Y vcard:Given ?G .
FILTER(regex(?G, \"r\", \"i\")) }"
(generate-sparql '(select ?g
where ((?y |vcard:Given| ?g)
(:filter (:regex ?g "r" "i"))))
nil))
(should match-except-ws "
SELECT ?RESOURCE
WHERE
{
?RESOURCE info:age ?AGE .
FILTER (?AGE >= 24)
}"
(generate-sparql '(select ?resource
where ((?resource |info:age| ?age)
(:filter (:>= ?age 24))))
nil))
(should match-except-ws "
SELECT ?NAME ?AGE
WHERE
{
?PERSON vcard:FN ?NAME .
OPTIONAL { ?PERSON info:age ?AGE . }
}"
(generate-sparql '(select ?name ?age
where ((?person |vcard:FN| ?name)
(:optional (?person |info:age| ?age))))
nil))
(should match-except-ws "
SELECT ?NAME ?AGE
WHERE
{
?PERSON vcard:FN ?NAME .
OPTIONAL { ?PERSON info:age ?AGE .
FILTER ( ?AGE > 24 ) }
}"
(generate-sparql '(select ?name ?age
where ((?person |vcard:FN| ?name)
(:optional ((?person |info:age| ?age)
(:filter (:> ?age 24))))))
nil))
(should match-except-ws "
SELECT ?NAME ?AGE
WHERE
{
?PERSON vcard:FN ?NAME .
OPTIONAL { ?PERSON info:age ?AGE . }
FILTER ( !bound(?AGE) || (?AGE > 24) )
}"
(generate-sparql '(select ?name ?age
where ((?person |vcard:FN| ?name)
(:optional (?person |info:age| ?age))
(:filter (:\|\| (:! (:bound ?age))
(:> ?age 24)))))
nil))
(should match-except-ws "
SELECT ?NAME
WHERE
{
?X a foaf:Person .
OPTIONAL { ?X foaf:name ?NAME . }
OPTIONAL { ?X vCard:FN ?NAME . }
}"
(generate-sparql '(select ?name
where ((?x |a| |foaf:Person|)
(:optional (?x |foaf:name| ?name))
(:optional (?x |vCard:FN| ?name))))
nil))
(should match-except-ws "
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?NAME
WHERE
{
{ [] foaf:name ?NAME . } UNION { [] vCard:FN ?NAME . }
}"
(generate-sparql '((:prefix |foaf| |<http://xmlns.com/foaf/0.1/>|)
(:prefix |vCard| |<http://www.w3.org/2001/vcard-rdf/3.0#>|)
select ?name
where (:union ([] |foaf:name| ?name)
([] |vCard:FN| ?name)))
nil))
(should match-except-ws "
SELECT ?NAME
WHERE
{
[] ?P ?NAME .
FILTER ( (?P = foaf:name) || (?P = vCard:FN) )
}"
(generate-sparql '(select ?name
where (([] ?p ?name)
(:filter (:\|\| (:= ?p |foaf:name|)
(:= ?p |vCard:FN|)))))
nil))
(should match-except-ws "
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <.>
SELECT *
{ ?S ?P ?O . }"
(generate-sparql '((:prefix |xsd| |<http://www.w3.org/2001/XMLSchema#>|)
(:prefix |dc| |<http://purl.org/dc/elements/1.1/>|)
(:prefix || |<.>|)
select * (?s ?p ?o))
nil))
(should match-except-ws "
SELECT *
{
{ ?S ?P ?O . }
UNION
{ GRAPH ?G { ?S ?P ?O . } }
}"
(generate-sparql '(select * (:union (?s ?p ?o)
(:graph ?g (?s ?p ?o))))
nil))
(should match-except-ws "
SELECT ?TITLE
{
GRAPH :ds-ng-2.ttl
{ ?B dc:title ?TITLE . }
}"
(generate-sparql '(select ?title (:graph |:ds-ng-2.ttl|
(?b |dc:title| ?title)))
nil))
(should match-except-ws "
SELECT ?DATE ?TITLE
{
?G dc:date ?DATE . FILTER (?DATE > \"2005-08-01T00:00:00Z\"^^xsd:dateTime )
GRAPH ?G { ?B dc:title ?TITLE . }
}"
(generate-sparql '(select ?date ?title
((?g |dc:date| ?date)
(:filter (:> ?date (:|^| "2005-08-01T00:00:00Z"
|xsd:dateTime|)))
(:graph ?g (?b |dc:title| ?title))))
nil))
(should match-except-ws "
SELECT ?G { ?G dc:title \"Foo\"@en . }"
(generate-sparql '(select ?g (?g |dc:title| (:@ "Foo" "en")))
nil))
(should match-except-ws "
SELECT *
FROM <ds-dft.ttl>
FROM NAMED <ds-ng-1.ttl>
FROM NAMED <ds-ng-2.ttl>
{
{ ?S ?P ?O . } UNION { GRAPH ?G { ?S ?P ?O . } }
}"
(generate-sparql '(select *
from |<ds-dft.ttl>|
from named |<ds-ng-1.ttl>|
from named |<ds-ng-2.ttl>|
(:union (?s ?p ?o)
(:graph ?g (?s ?p ?o))))
nil))
(should match-except-ws "
SELECT ?TITLE ?PRICE
FROM <urn:sparql:bind:tests>
{
?X ns:price ?P ;
ns:discount ?DISCOUNT ;
<http://purl.org/dc/elements/1.1/title> ?TITLE .
BIND (?P*(1-?DISCOUNT) AS ?PRICE)
FILTER( ?PRICE < 20 )
}"
(generate-sparql '(select ?title ?price
from |<urn:sparql:bind:tests>|
((?x (|ns:price| ?p
|ns:discount| ?discount
|<http://purl.org/dc/elements/1.1/title>| ?title))
(:bind (:* ?p (:- 1 ?discount)) ?price)
(:filter (:< ?price 20))))
nil)))