forked from robstewart57/hsparql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryGeneratorTest.hs
324 lines (266 loc) · 8.14 KB
/
QueryGeneratorTest.hs
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
{-# LANGUAGE QuasiQuotes #-}
module Database.HSparql.QueryGeneratorTest ( testSuite ) where
import Test.Framework (testGroup, Test)
import Test.Framework.Providers.HUnit
import Test.HUnit
import Data.Text (Text)
import qualified Data.Text as T
import Data.String.QQ (s)
import Database.HSparql.QueryGenerator
class CreateQuery a where
createQuery :: a -> Text
instance CreateQuery (Query SelectQuery) where
createQuery = T.pack . createSelectQuery
instance CreateQuery (Query ConstructQuery) where
createQuery = T.pack . createConstructQuery
instance CreateQuery (Query AskQuery) where
createQuery = T.pack . createAskQuery
instance CreateQuery (Query UpdateQuery) where
createQuery = T.pack . createUpdateQuery
instance CreateQuery (Query DescribeQuery) where
createQuery = T.pack . createDescribeQuery
normalizeWhitespace :: Text -> Text
normalizeWhitespace = T.strip
. (T.replace " " " ")
. (T.replace " " " ")
. (T.replace " " " ")
. (T.replace "\n" " ")
queryTexts :: [(Text, Text)]
queryTexts =
[ ( [s|
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x1 ?x2
WHERE {
?x0 dbprop:genre dbpedia:Web_browser .
?x0 foaf:name ?x1 .
?x0 foaf:page ?x2 .
}
|]
, createQuery $ do
resource <- prefix "dbpedia" (iriRef "http://dbpedia.org/resource/")
dbpprop <- prefix "dbprop" (iriRef "http://dbpedia.org/property/")
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
x <- var
name <- var
page <- var
triple_ x (dbpprop .:. "genre") (resource .:. "Web_browser")
triple_ x (foaf .:. "name") name
triple_ x (foaf .:. "page") page
selectVars [name, page]
)
, ( [s|
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX example: <http://www.example.com/>
CONSTRUCT {
?x0 example:hasName ?x1 .
}
WHERE {
?x0 dbprop:genre dbpedia:Web_browser .
?x0 foaf:name ?x1 .
?x0 foaf:page ?x2 .
}
|]
, createQuery $ do
resource <- prefix "dbpedia" (iriRef "http://dbpedia.org/resource/")
dbpprop <- prefix "dbprop" (iriRef "http://dbpedia.org/property/")
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
example <- prefix "example" (iriRef "http://www.example.com/")
x <- var
name <- var
page <- var
construct <- constructTriple x (example .:. "hasName") name
triple_ x (dbpprop .:. "genre") (resource .:. "Web_browser")
triple_ x (foaf .:. "name") name
triple_ x (foaf .:. "page") page
return ConstructQuery { queryConstructs = [construct] }
)
, ( [s|
PREFIX dbpedia: <http://dbpedia.org/resource/> DESCRIBE dbpedia:Edinburgh WHERE { }
|]
, createQuery $ do
resource <- prefix "dbpedia" (iriRef "http://dbpedia.org/resource/")
uri <- describeIRI (resource .:. "Edinburgh")
return DescribeQuery { queryDescribe = uri }
)
, ( [s|
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbprop: <http://dbpedia.org/property/>
ASK {
?x0 dbprop:genre dbpedia:Web_browser .
}
|]
, createQuery $ do
resource <- prefix "dbpedia" (iriRef "http://dbpedia.org/resource/")
dbprop <- prefix "dbprop" (iriRef "http://dbpedia.org/property/")
x <- var
ask <- askTriple x (dbprop .:. "genre") (resource .:. "Web_browser")
return AskQuery { queryAsk = [ask] }
)
-- https://github.com/robstewart57/hsparql/pull/31
, ( [s|
PREFIX : <http://example1.com/>
PREFIX ex: <http://example2.com/>
SELECT ?x0 WHERE {
?x0 :property1 :X .
FILTER ((?x1>(10))&&(?x1<(20))) .
{
SELECT ((AVG(?x0_0)) AS ?x1)
WHERE {
?x0 :property2 [ex:property3 ?x0_0] .
}
}
}
|]
, createQuery $ do
p1 <- prefix "" (iriRef "http://example1.com/")
s <- var
v <- var
triple_ s (p1 .:. "property1") (p1 .:. "X")
filterExpr_ $ (v .>. (10::Integer)) .&&. (v .<. (20::Integer))
subQuery_ $ do
p2 <- prefix "ex" (iriRef "http://example2.com/")
v' <- var
triple_ s (p1 .:. "property2") [mkPredicateObject (p2 .:. "property3") v']
select [avg v' `as` v]
selectVars [s]
)
-- https://www.w3.org/TR/sparql11-query/#OptionalMatching
, ( [s|
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x1 ?x2
WHERE {
?x0 foaf:name ?x1 .
OPTIONAL { ?x0 foaf:mbox ?x2 . }
}
|]
, createQuery $ do
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
x <- var
name <- var
mbox <- var
triple_ x (foaf .:. "name") name
optional_ $ do
triple_ x (foaf .:. "mbox") mbox
selectVars [name, mbox]
)
-- https://www.w3.org/TR/sparql11-query/#alternatives
, ( [s|
PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>
SELECT ?x1
WHERE { { ?x0 dc10:title ?x1 . } UNION { ?x0 dc11:title ?x1 . } }
|]
, createQuery $ do
dc10 <- prefix "dc10" (iriRef "http://purl.org/dc/elements/1.0/")
dc11 <- prefix "dc11" (iriRef "http://purl.org/dc/elements/1.1/")
book <- var
title <- var
let tt10 = triple_ book (dc10 .:. "title") title
tt11 = triple_ book (dc11 .:. "title") title
in union_ tt10 tt11
selectVars [title]
)
-- https://www.w3.org/TR/sparql11-query/#neg-notexists
, ( [s|
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x0
WHERE
{
?x0 rdf:type foaf:Person .
FILTER NOT EXISTS { ?x0 foaf:name ?x1 . }
}
|]
, createQuery $ do
rdf <- prefix "rdf" (iriRef "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
person <- var
name <- var
triple_ person (rdf .:. "type") (foaf .:. "Person")
filterNotExists_ $ do
triple_ person (foaf .:. "name") name
selectVars [person]
)
, ( [s|
PREFIX ex: <http://example.com/>
SELECT ?x0 ?x1
WHERE
{
?x0 ex:foo%20bar ?x1 .
}
|]
, createQuery $ do
ex <- prefix "ex" (iriRef "http://example.com/")
s <- var
o <- var
triple_ s (ex .:. "foo bar") o
selectVars [s, o]
)
-- Count distinct subjects and objects query
, ( [s|
SELECT ((COUNT(?x0)) AS ?x4)
WHERE
{
{
SELECT DISTINCT ?x0 WHERE {
{ ?x0 ?x2 ?x3 . } UNION
{ ?x1 ?x2 ?x0 . }
}
}
}
|]
, createQuery $ do
r <- var
s <- var
p <- var
o <- var
subQuery_ $ do
let tt1 = triple_ r p o
tt2 = triple_ s p r
in do
distinct_
union_ tt1 tt2
selectVars [r]
countVar <- var
select [count r `as` countVar]
)
-- Count number of instances of each class in the dataset.
, ( [s|
SELECT ?x1 ((COUNT(?x0)) AS ?x2) WHERE {
?x0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?x1 .
} GROUP BY ?x1
|]
, createQuery $ do
s <- var
o <- var
triple_ s (iriRef "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") o
groupBy_ o
countVar <- var
select [SelectVar o, count s `as` countVar]
)
-- Count number of resources typed with a class from Wikidata.
, ( [s|
SELECT ((COUNT(?x0)) AS ?x2) WHERE {
?x0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?x1 .
FILTER (CONTAINS((STR(?x1)), "http://www.wikidata.org/entity")) .
}
|]
, createQuery $ do
s <- var
o <- var
triple_ s (iriRef "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") o
filterExpr_ $ contains (str o) ("http://www.wikidata.org/entity" :: Text)
countVar <- var
select [count s `as` countVar]
)
]
testSuite :: [Test.Framework.Test]
testSuite = [
testGroup "Database.HSparql.QueryGenerator tests" $ (`map` queryTexts) $
\(expected, actual) -> testCase (T.unpack expected) $ do
assertEqual "" (normalizeWhitespace expected) (normalizeWhitespace actual)
]