1# -*- coding: utf-8 -*-
2#!/usr/bin/python
3
4import time
5
6import inspect
7import os
8import sys
9import unittest
10
11# prefer local copy to the one which is installed
12# hack from http://stackoverflow.com/a/6098238/280539
13_top_level_path = os.path.realpath(os.path.abspath(os.path.join(
14    os.path.split(inspect.getfile(inspect.currentframe()))[0],
15    ".."
16)))
17if _top_level_path not in sys.path:
18    sys.path.insert(0, _top_level_path)
19# end of hack
20
21import warnings
22warnings.simplefilter("always")
23
24try:
25    from rdflib.graph import ConjunctiveGraph
26except ImportError:
27    from rdflib import ConjunctiveGraph
28
29from SPARQLWrapper import SPARQLWrapper, XML, RDFXML, RDF, N3, TURTLE, JSONLD, JSON, CSV, TSV, POST, GET
30from SPARQLWrapper.Wrapper import _SPARQL_XML, _SPARQL_JSON, _XML, _RDF_XML, _RDF_N3, _RDF_TURTLE, _RDF_JSONLD, _CSV, _TSV
31from SPARQLWrapper.SPARQLExceptions import QueryBadFormed
32
33_SPARQL_SELECT_ASK_POSSIBLE = _SPARQL_XML + _SPARQL_JSON + _CSV + _TSV + _XML # only used in test
34_SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE = _RDF_XML + _RDF_N3 + _XML + _RDF_JSONLD # only used in test. Same as Wrapper._RDF_POSSIBLE
35
36try:
37    from urllib.error import HTTPError   # Python 3
38except ImportError:
39    from urllib2 import HTTPError        # Python 2
40
41try:
42    bytes   # Python 2.6 and above
43except NameError:
44    bytes = str
45
46import logging
47logging.basicConfig()
48
49endpoint = "https://query.wikidata.org/sparql"
50
51prefixes = """
52    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
53    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
54    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
55    PREFIX entity: <http://www.wikidata.org/entity/>
56"""
57
58selectQuery = """
59    SELECT ?predicate ?object WHERE {
60        entity:Q3934 ?predicate ?object .
61    } LIMIT 10
62"""
63
64selectQueryCSV_TSV = """
65    SELECT ?predicate ?object WHERE {
66        entity:Q3934 ?predicate ?object .
67    } LIMIT 10
68 """
69askQuery = """
70    ASK { <http://www.wikidata.org/entity/Q3934> rdfs:label "Asturias"@es }
71"""
72
73constructQuery = """
74    CONSTRUCT {
75        _:v skos:prefLabel ?label .
76        _:v rdfs:comment "this is only a mock node to test library"
77    }
78    WHERE {
79        <http://www.wikidata.org/entity/Q3934> rdfs:label ?label .
80        FILTER langMatches( lang(?label), "es" )
81    }
82"""
83
84describeQuery = """
85    DESCRIBE <http://www.wikidata.org/entity/Q3934>
86"""
87
88queryBadFormed = """
89    PREFIX prop: <http://dbpedia.org/property/>
90    PREFIX res: <http://dbpedia.org/resource/>
91    FROM <http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&should-sponge=&query=%0D%0ACONSTRUCT+%7B%0D%0A++++%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FBudapest%3E+%3Fp+%3Fo.%0D%0A%7D%0D%0AWHERE+%7B%0D%0A++++%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FBudapest%3E+%3Fp+%3Fo.%0D%0A%7D%0D%0A&format=application%2Frdf%2Bxml>
92    SELECT ?lat ?long
93    WHERE {
94        res:Budapest prop:latitude ?lat;
95        prop:longitude ?long.
96    }
97"""
98queryDuplicatedPrefix = """
99    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
100    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
101    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
102    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
103    PREFIX entity: <http://www.wikidata.org/entity/>
104
105    SELECT ?predicate ?object WHERE {
106        entity:Q3934 ?predicate ?object .
107    } LIMIT 10
108"""
109
110
111queryManyPrefixes = """
112    PREFIX conf: <http://richard.cyganiak.de/2007/pubby/config.rdf#>
113    PREFIX meta: <http://example.org/metadata#>
114    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
115    PREFIX owl: <http://www.w3.org/2002/07/owl#>
116    PREFIX dc: <http://purl.org/dc/elements/1.1/>
117    PREFIX dcterms: <http://purl.org/dc/terms/>
118    PREFIX foaf: <http://xmlns.com/foaf/0.1/>
119    PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
120    PREFIX dbpedia: <http://dbpedia.org/resource/>
121    PREFIX o: <http://dbpedia.org/ontology/>
122    PREFIX p: <http://dbpedia.org/property/>
123    PREFIX yago: <http://dbpedia.org/class/yago/>
124    PREFIX units: <http://dbpedia.org/units/>
125    PREFIX geonames: <http://www.geonames.org/ontology#>
126    PREFIX prv: <http://purl.org/net/provenance/ns#>
127    PREFIX prvTypes: <http://purl.org/net/provenance/types#>
128    PREFIX foo: <http://purl.org/foo>
129
130    SELECT ?predicate ?object WHERE {
131        entity:Q3934 ?predicate ?object .
132    } LIMIT 10
133"""
134
135queryDuplicatedPrefix = """
136    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
137    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
138
139    SELECT ?s ?p ?o WHERE {
140        ?s ?p ?o .
141    } LIMIT 10
142"""
143
144queryWithCommaInCurie_1 = """
145    PREFIX dbpedia: <http://dbpedia.org/resource/>
146    SELECT ?article ?title WHERE {
147        ?article ?relation dbpedia:Victoria\\,\\_British\\_Columbia .
148        ?article <http://xmlns.com/foaf/0.1/isPrimaryTopicOf> ?title
149    }
150"""
151
152queryWithCommaInCurie_2 = """
153    PREFIX dbpedia: <http://dbpedia.org/resource/>
154    SELECT ?article ?title WHERE {
155        ?article ?relation dbpedia:Category\:Victoria\,\_British\_Columbia .
156        ?article <http://xmlns.com/foaf/0.1/isPrimaryTopicOf> ?title
157    }
158"""
159
160queryWithCommaInUri = """
161    SELECT ?article ?title WHERE {
162        ?article ?relation <http://dbpedia.org/resource/Category:Victoria,_British_Columbia> .
163        ?article <http://xmlns.com/foaf/0.1/isPrimaryTopicOf> ?title
164    }
165"""
166
167class SPARQLWrapperTests(unittest.TestCase):
168
169    def __generic(self, query, returnFormat, method, onlyConneg=False): # Blazegraph uses URL parameters or content negotiation.
170        sparql = SPARQLWrapper(endpoint)
171        sparql.setQuery(prefixes + query)
172        sparql.setReturnFormat(returnFormat)
173        sparql.setMethod(method)
174        sparql.setOnlyConneg(onlyConneg)
175        try:
176            time.sleep(2.5) # sleeps for 2.5 seconds, in order to avoid disruptions in the server
177            result = sparql.query()
178        except HTTPError:
179            # An ugly way to get the exception, but the only one that works
180            # both on Python 2.5 and Python 3.
181            e = sys.exc_info()[1]
182            if e.code == 400:
183                sys.stdout.write("400 Bad Request, probably query is not well formed")
184            elif e.code == 406:
185                sys.stdout.write("406 Not Acceptable, maybe query is not well formed")
186            else:
187                sys.stdout.write(str(e))
188            sys.stdout.write("\n")
189            return False
190        else:
191            return result
192
193
194################################################################################
195################################################################################
196
197################
198#### SELECT ####
199################
200
201    def testSelectByGETinXML(self):
202        result = self.__generic(selectQuery, XML, GET)
203        ct = result.info()["content-type"]
204        assert True in [one in ct for one in _SPARQL_XML], ct
205        results = result.convert()
206        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
207        self.assertEqual(results.__class__.__name__, "Document")
208
209    def testSelectByGETinXML_Conneg(self):
210        result = self.__generic(selectQuery, XML, GET, onlyConneg=True)
211        ct = result.info()["content-type"]
212        assert True in [one in ct for one in _SPARQL_XML], ct
213        results = result.convert()
214        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
215        self.assertEqual(results.__class__.__name__, "Document")
216
217    def testSelectByPOSTinXML(self):
218        result = self.__generic(selectQuery, XML, POST)
219        ct = result.info()["content-type"]
220        assert True in [one in ct for one in _SPARQL_XML], ct
221        results = result.convert()
222        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
223        self.assertEqual(results.__class__.__name__, "Document")
224
225    def testSelectByPOSTinXML_Conneg(self):
226        result = self.__generic(selectQuery, XML, POST, onlyConneg=True)
227        ct = result.info()["content-type"]
228        assert True in [one in ct for one in _SPARQL_XML], ct
229        results = result.convert()
230        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
231        self.assertEqual(results.__class__.__name__, "Document")
232
233    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. csv is not a valid alias. Use content negotiation instead")
234    def testSelectByGETinCSV(self):
235        result = self.__generic(selectQueryCSV_TSV, CSV, GET)
236        ct = result.info()["content-type"]
237        assert True in [one in ct for one in _CSV], ct
238        results = result.convert()
239        self.assertEqual(type(results), bytes)
240
241    def testSelectByGETinCSV_Conneg(self):
242        result = self.__generic(selectQueryCSV_TSV, CSV, GET, onlyConneg=True)
243        ct = result.info()["content-type"]
244        assert True in [one in ct for one in _CSV], ct
245        results = result.convert()
246        self.assertEqual(type(results), bytes)
247
248    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. csv is not a valid alias. Use content negotiation instead")
249    def testSelectByPOSTinCSV(self):
250        result = self.__generic(selectQueryCSV_TSV, CSV, POST)
251        ct = result.info()["content-type"]
252        assert True in [one in ct for one in _CSV], ct
253        results = result.convert()
254        self.assertEqual(type(results), bytes)
255
256    def testSelectByPOSTinCSV_Conneg(self):
257        result = self.__generic(selectQueryCSV_TSV, CSV, POST, onlyConneg=True)
258        ct = result.info()["content-type"]
259        assert True in [one in ct for one in _CSV], ct
260        results = result.convert()
261        self.assertEqual(type(results), bytes)
262
263    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. tsv is not a valid alias. Use content negotiation instead")
264    def testSelectByGETinTSV(self):
265        result = self.__generic(selectQueryCSV_TSV, TSV, GET)
266        ct = result.info()["content-type"]
267        assert True in [one in ct for one in _TSV], ct
268        results = result.convert()
269        self.assertEqual(type(results), bytes)
270
271    def testSelectByGETinTSV_Conneg(self):
272        result = self.__generic(selectQueryCSV_TSV, TSV, GET, onlyConneg=True)
273        ct = result.info()["content-type"]
274        assert True in [one in ct for one in _TSV], ct
275        results = result.convert()
276        self.assertEqual(type(results), bytes)
277
278    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. tsv is not a valid alias. Use content negotiation instead")
279    def testSelectByPOSTinTSV(self):
280        result = self.__generic(selectQueryCSV_TSV, TSV, POST)
281        ct = result.info()["content-type"]
282        assert True in [one in ct for one in _TSV], ct
283        results = result.convert()
284        self.assertEqual(type(results), bytes)
285
286    def testSelectByPOSTinTSV_Conneg(self):
287        result = self.__generic(selectQueryCSV_TSV, TSV, POST, onlyConneg=True)
288        ct = result.info()["content-type"]
289        assert True in [one in ct for one in _TSV], ct
290        results = result.convert()
291        self.assertEqual(type(results), bytes)
292
293    def testSelectByGETinJSON(self):
294        result = self.__generic(selectQuery, JSON, GET)
295        ct = result.info()["content-type"]
296        assert True in [one in ct for one in _SPARQL_JSON], ct
297        results = result.convert()
298        self.assertEqual(type(results), dict)
299
300    def testSelectByGETinJSON_Conneg(self):
301        result = self.__generic(selectQuery, JSON, GET, onlyConneg=True)
302        ct = result.info()["content-type"]
303        assert True in [one in ct for one in _SPARQL_JSON], ct
304        results = result.convert()
305        self.assertEqual(type(results), dict)
306
307    def testSelectByPOSTinJSON(self):
308        result = self.__generic(selectQuery, JSON, POST)
309        ct = result.info()["content-type"]
310        assert True in [one in ct for one in _SPARQL_JSON], ct
311        results = result.convert()
312        self.assertEqual(type(results), dict)
313
314    def testSelectByPOSTinJSON_Conneg(self):
315        result = self.__generic(selectQuery, JSON, POST, onlyConneg=True)
316        ct = result.info()["content-type"]
317        assert True in [one in ct for one in _SPARQL_JSON], ct
318        results = result.convert()
319        self.assertEqual(type(results), dict)
320
321    # Asking for an unexpected return format for SELECT queryType (n3 is not supported, and it is not a valid alias).
322    # Set by default None (and sending */*).
323    # For a SELECT query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
324    def testSelectByGETinN3_Unexpected(self):
325        result = self.__generic(selectQuery, N3, GET)
326        ct = result.info()["content-type"]
327        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
328        results = result.convert()
329        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
330        self.assertEqual(results.__class__.__name__, "Document")
331
332    # Asking for an unexpected return format for SELECT queryType (n3 is not supported, and it is not a valid alias).
333    # Set by default None (and sending */*).
334    # For a SELECT query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
335    def testSelectByGETinN3_Unexpected_Conneg(self):
336        result = self.__generic(selectQuery, N3, GET, onlyConneg=True)
337        ct = result.info()["content-type"]
338        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
339        results = result.convert()
340        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
341        self.assertEqual(results.__class__.__name__, "Document")
342
343    # Asking for an unexpected return format for SELECT queryType (n3 is not supported, and it is not a valid alias).
344    # Set by default None (and sending */*).
345    # For a SELECT query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
346    def testSelectByPOSTinN3_Unexpected(self):
347        result = self.__generic(selectQuery, N3, POST)
348        ct = result.info()["content-type"]
349        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
350        results = result.convert()
351        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
352        self.assertEqual(results.__class__.__name__, "Document")
353
354    # Asking for an unexpected return format for SELECT queryType (n3 is not supported, and it is not a valid alias).
355    # Set by default None (and sending */*).
356    # For a SELECT query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
357    def testSelectByPOSTinN3_Unexpected_Conneg(self):
358        result = self.__generic(selectQuery, N3, POST, onlyConneg=True)
359        ct = result.info()["content-type"]
360        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
361        results = result.convert()
362        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
363        self.assertEqual(results.__class__.__name__, "Document")
364
365
366    # Asking for an unexpected return format for SELECT queryType (json-ld is not supported, and it is not a valid alias).
367    # Set by default None (and sending */*).
368    # For a SELECT query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
369    def testSelectByGETinJSONLD_Unexpected(self):
370        result = self.__generic(selectQuery, JSONLD, GET)
371        ct = result.info()["content-type"]
372        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
373        results = result.convert()
374        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
375        self.assertEqual(results.__class__.__name__, "Document")
376
377    # Asking for an unexpected return format for SELECT queryType (json-ld is not supported, and it is not a valid alias).
378    # Set by default None (and sending */*).
379    # For a SELECT query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
380    def testSelectByGETinJSONLD_Unexpected_Conneg(self):
381        result = self.__generic(selectQuery, JSONLD, GET, onlyConneg=True)
382        ct = result.info()["content-type"]
383        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
384        results = result.convert()
385        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
386        self.assertEqual(results.__class__.__name__, "Document")
387
388    # Asking for an unexpected return format for SELECT queryType (json-ld is not supported, and it is not a valid alias).
389    # Set by default None (and sending */*).
390    # For a SELECT query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
391    def testSelectByPOSTinJSONLD_Unexpected(self):
392        result = self.__generic(selectQuery, JSONLD, POST)
393        ct = result.info()["content-type"]
394        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
395        results = result.convert()
396        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
397        self.assertEqual(results.__class__.__name__, "Document")
398
399    # Asking for an unexpected return format for SELECT queryType (json-ld is not supported, and it is not a valid alias).
400    # Set by default None (and sending */*).
401    # For a SELECT query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
402    def testSelectByPOSTinJSONLD_Unexpected_Conneg(self):
403        result = self.__generic(selectQuery, JSONLD, POST, onlyConneg=True)
404        ct = result.info()["content-type"]
405        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
406        results = result.convert()
407        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
408        self.assertEqual(results.__class__.__name__, "Document")
409
410    # Asking for an unknown return format for SELECT queryType (XML is sent)
411    def testSelectByGETinUnknow(self):
412        result = self.__generic(selectQuery, "foo", GET)
413        ct = result.info()["content-type"]
414        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
415        results = result.convert()
416        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
417        self.assertEqual(results.__class__.__name__, "Document")
418
419    # Asking for an unknown return format for SELECT queryType (XML is sent)
420    def testSelectByGETinUnknow_Conneg(self):
421        result = self.__generic(selectQuery, "foo", GET, onlyConneg=True)
422        ct = result.info()["content-type"]
423        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
424        results = result.convert()
425        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
426        self.assertEqual(results.__class__.__name__, "Document")
427
428    # Asking for an unknown return format for SELECT queryType (XML is sent)
429    def testSelectByPOSTinUnknow(self):
430        result = self.__generic(selectQuery, "bar", POST)
431        ct = result.info()["content-type"]
432        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
433        results = result.convert()
434        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
435        self.assertEqual(results.__class__.__name__, "Document")
436
437    # Asking for an unknown return format for SELECT queryType (XML is sent)
438    def testSelectByPOSTinUnknow_Conneg(self):
439        result = self.__generic(selectQuery, "bar", POST, onlyConneg=True)
440        ct = result.info()["content-type"]
441        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
442        results = result.convert()
443        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
444        self.assertEqual(results.__class__.__name__, "Document")
445
446################################################################################
447################################################################################
448
449#############
450#### ASK ####
451#############
452
453    def testAskByGETinXML(self):
454        result = self.__generic(askQuery, XML, GET)
455        ct = result.info()["content-type"]
456        assert True in [one in ct for one in _SPARQL_XML], ct
457        results = result.convert()
458        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
459        self.assertEqual(results.__class__.__name__, "Document")
460
461    def testAskByGETinXML_Conneg(self):
462        result = self.__generic(askQuery, XML, GET, onlyConneg=True)
463        ct = result.info()["content-type"]
464        assert True in [one in ct for one in _SPARQL_XML], ct
465        results = result.convert()
466        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
467        self.assertEqual(results.__class__.__name__, "Document")
468
469    def testAskByPOSTinXML(self):
470        result = self.__generic(askQuery, XML, POST)
471        ct = result.info()["content-type"]
472        assert True in [one in ct for one in _SPARQL_XML], ct
473        results = result.convert()
474        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
475        self.assertEqual(results.__class__.__name__, "Document")
476
477    def testAskByPOSTinXML_Conneg(self):
478        result = self.__generic(askQuery, XML, POST, onlyConneg=True)
479        ct = result.info()["content-type"]
480        assert True in [one in ct for one in _SPARQL_XML], ct
481        results = result.convert()
482        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
483        self.assertEqual(results.__class__.__name__, "Document")
484
485    @unittest.skip("CSV is not supported for ASK queries in Blazegraph")
486    def testAskByGETinCSV(self):
487        result = self.__generic(askQuery, CSV, GET)
488        ct = result.info()["content-type"]
489        assert True in [one in ct for one in _CSV], ct
490        results = result.convert()
491        self.assertEqual(type(results), bytes)
492
493    @unittest.skip("CSV is not supported for ASK queries in Blazegraph")
494    def testAskByGETinCSV_Conneg(self):
495        result = self.__generic(askQuery, CSV, GET, onlyConneg=True)
496        ct = result.info()["content-type"]
497        assert True in [one in ct for one in _CSV], ct
498        results = result.convert()
499        self.assertEqual(type(results), bytes)
500
501    @unittest.skip("CSV is not supported for ASK queries in Blazegraph")
502    def testAskByPOSTinCSV(self):
503        result = self.__generic(askQuery, CSV, POST)
504        ct = result.info()["content-type"]
505        assert True in [one in ct for one in _CSV], ct
506        results = result.convert()
507        self.assertEqual(type(results), bytes)
508
509    @unittest.skip("CSV is not supported for ASK queries in Blazegraph")
510    def testAskByPOSTinCSV_Conneg(self):
511        result = self.__generic(askQuery, CSV, POST, onlyConneg=True)
512        ct = result.info()["content-type"]
513        assert True in [one in ct for one in _CSV], ct
514        results = result.convert()
515        self.assertEqual(type(results), bytes)
516
517    @unittest.skip("TSV is not supported for ASK queries in Blazegraph")
518    def testAskByGETinTSV(self):
519        result = self.__generic(askQuery, TSV, GET)
520        ct = result.info()["content-type"]
521        assert True in [one in ct for one in _TSV], ct
522        results = result.convert()
523        self.assertEqual(type(results), bytes)
524
525    @unittest.skip("TSV is not supported for ASK queries in Blazegraph")
526    def testAskByGETinTSV_Conneg(self):
527        result = self.__generic(askQuery, TSV, GET, onlyConneg=True)
528        ct = result.info()["content-type"]
529        assert True in [one in ct for one in _TSV], ct
530        results = result.convert()
531        self.assertEqual(type(results), bytes)
532
533    @unittest.skip("TSV is not supported for ASK queries in Blazegraph")
534    def testAskByPOSTinTSV(self):
535        result = self.__generic(askQuery, TSV, POST)
536        ct = result.info()["content-type"]
537        assert True in [one in ct for one in _TSV], ct
538        results = result.convert()
539        self.assertEqual(type(results), bytes)
540
541    @unittest.skip("TSV is not supported for ASK queries in Blazegraph")
542    def testAskByPOSTinTSV_Conneg(self):
543        result = self.__generic(askQuery, TSV, POST, onlyConneg=True)
544        ct = result.info()["content-type"]
545        assert True in [one in ct for one in _TSV], ct
546        results = result.convert()
547        self.assertEqual(type(results), bytes)
548
549    def testAskByGETinJSON(self):
550        result = self.__generic(askQuery, JSON, GET)
551        ct = result.info()["content-type"]
552        assert True in [one in ct for one in _SPARQL_JSON], ct
553        results = result.convert()
554        self.assertEqual(type(results), dict)
555
556    def testAskByGETinJSON_Conneg(self):
557        result = self.__generic(askQuery, JSON, GET, onlyConneg=True)
558        ct = result.info()["content-type"]
559        assert True in [one in ct for one in _SPARQL_JSON], ct
560        results = result.convert()
561        self.assertEqual(type(results), dict)
562
563    def testAskByPOSTinJSON(self):
564        result = self.__generic(askQuery, JSON, POST)
565        ct = result.info()["content-type"]
566        assert True in [one in ct for one in _SPARQL_JSON], ct
567        results = result.convert()
568        self.assertEqual(type(results), dict)
569
570    def testAskByPOSTinJSON_Conneg(self):
571        result = self.__generic(askQuery, JSON, POST, onlyConneg=True)
572        ct = result.info()["content-type"]
573        assert True in [one in ct for one in _SPARQL_JSON], ct
574        results = result.convert()
575        self.assertEqual(type(results), dict)
576
577    # Asking for an unexpected return format for ASK queryType (n3 is not supported, it is not a valid alias).
578    # Set by default None (and sending */*).
579    # For an ASK query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
580    def testAskByGETinN3_Unexpected(self):
581        result = self.__generic(askQuery, N3, GET)
582        ct = result.info()["content-type"]
583        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
584        results = result.convert()
585        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
586        self.assertEqual(results.__class__.__name__, "Document")
587
588    # Asking for an unexpected return format for ASK queryType (n3 is not supported, it is not a valid alias).
589    # Set by default None (and sending */*).
590    # For an ASK query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
591    def testAskByGETinN3_Unexpected_Conneg(self):
592        result = self.__generic(askQuery, N3, GET, onlyConneg=True)
593        ct = result.info()["content-type"]
594        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
595        results = result.convert()
596        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
597        self.assertEqual(results.__class__.__name__, "Document")
598
599    # Asking for an unexpected return format for ASK queryType (n3 is not supported, it is not a valid alias).
600    # Set by default None (and sending */*).
601    # For an ASK query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
602    def testAskByPOSTinN3_Unexpected(self):
603        result = self.__generic(askQuery, N3, POST)
604        ct = result.info()["content-type"]
605        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
606        results = result.convert()
607        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
608        self.assertEqual(results.__class__.__name__, "Document")
609
610    # Asking for an unexpected return format for ASK queryType (n3 is not supported, it is not a valid alias).
611    # Set by default None (and sending */*).
612    # For an ASK query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
613    def testAskByPOSTinN3_Unexpected_Conneg(self):
614        result = self.__generic(askQuery, N3, POST, onlyConneg=True)
615        ct = result.info()["content-type"]
616        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
617        results = result.convert()
618        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
619        self.assertEqual(results.__class__.__name__, "Document")
620
621    # Asking for an unexpected return format for ASK queryType (json-ld is not supported, it is not a valid alias).
622    # Set by default None (and sending */*).
623    # For an ASK query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
624    def testAskByGETinJSONLD_Unexpected(self):
625        result = self.__generic(askQuery, JSONLD, GET)
626        ct = result.info()["content-type"]
627        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
628        results = result.convert()
629        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
630        self.assertEqual(results.__class__.__name__, "Document")
631
632    # Asking for an unexpected return format for ASK queryType (json-ld is not supported, it is not a valid alias).
633    # Set by default None (and sending */*).
634    # For an ASK query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
635    def testAskByGETinJSONLD_Unexpected_Conneg(self):
636        result = self.__generic(askQuery, JSONLD, GET, onlyConneg=True)
637        ct = result.info()["content-type"]
638        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
639        results = result.convert()
640        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
641        self.assertEqual(results.__class__.__name__, "Document")
642
643    # Asking for an unexpected return format for ASK queryType (json-ld is not supported, it is not a valid alias).
644    # Set by default None (and sending */*).
645    # For an ASK query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
646    def testAskByPOSTinJSONLD_Unexpected(self):
647        result = self.__generic(askQuery, JSONLD, POST)
648        ct = result.info()["content-type"]
649        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
650        results = result.convert()
651        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
652        self.assertEqual(results.__class__.__name__, "Document")
653
654    # Asking for an unexpected return format for ASK queryType (json-ld is not supported, it is not a valid alias).
655    # Set by default None (and sending */*).
656    # For an ASK query type, the default return mimetype (if Accept: */* is sent) is application/sparql-results+xml
657    def testAskByPOSTinJSONLD_Unexpected_Conneg(self):
658        result = self.__generic(askQuery, JSONLD, POST, onlyConneg=True)
659        ct = result.info()["content-type"]
660        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
661        results = result.convert()
662        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
663        self.assertEqual(results.__class__.__name__, "Document")
664
665    # Asking for an unknown return format for ASK queryType (XML is sent)
666    def testAskByGETinUnknow(self):
667        result = self.__generic(askQuery, "foo", GET)
668        ct = result.info()["content-type"]
669        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
670        results = result.convert()
671        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
672        self.assertEqual(results.__class__.__name__, "Document")
673
674    # Asking for an unknown return format for ASK queryType (XML is sent)
675    def testAskByGETinUnknow_Conneg(self):
676        result = self.__generic(askQuery, "foo", GET, onlyConneg=True)
677        ct = result.info()["content-type"]
678        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
679        results = result.convert()
680        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
681        self.assertEqual(results.__class__.__name__, "Document")
682
683    # Asking for an unknown return format for ASK queryType (XML is sent)
684    def testAskByPOSTinUnknow(self):
685        result = self.__generic(askQuery, "bar", POST)
686        ct = result.info()["content-type"]
687        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
688        results = result.convert()
689        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
690        self.assertEqual(results.__class__.__name__, "Document")
691
692    # Asking for an unknown return format for ASK queryType (XML is sent)
693    def testAskByPOSTinUnknow_Conneg(self):
694        result = self.__generic(askQuery, "bar", POST, onlyConneg=True)
695        ct = result.info()["content-type"]
696        assert True in [one in ct for one in _SPARQL_SELECT_ASK_POSSIBLE], ct
697        results = result.convert()
698        self.assertEqual(results.__class__.__module__, "xml.dom.minidom")
699        self.assertEqual(results.__class__.__name__, "Document")
700
701################################################################################
702################################################################################
703
704###################
705#### CONSTRUCT ####
706###################
707
708    def testConstructByGETinXML(self):
709        result = self.__generic(constructQuery, XML, GET)
710        ct = result.info()["content-type"]
711        assert True in [one in ct for one in _RDF_XML], ct
712        results = result.convert()
713        self.assertEqual(type(results), ConjunctiveGraph)
714
715    def testConstructByGETinXML_Conneg(self):
716        result = self.__generic(constructQuery, XML, GET, onlyConneg=True)
717        ct = result.info()["content-type"]
718        assert True in [one in ct for one in _RDF_XML], ct
719        results = result.convert()
720        self.assertEqual(type(results), ConjunctiveGraph)
721
722    def testConstructByPOSTinXML(self):
723        result = self.__generic(constructQuery, XML, POST)
724        ct = result.info()["content-type"]
725        assert True in [one in ct for one in _RDF_XML], ct
726        results = result.convert()
727        self.assertEqual(type(results), ConjunctiveGraph)
728
729    def testConstructByPOSTinXML_Conneg(self):
730        result = self.__generic(constructQuery, XML, POST, onlyConneg=True)
731        ct = result.info()["content-type"]
732        assert True in [one in ct for one in _RDF_XML], ct
733        results = result.convert()
734        self.assertEqual(type(results), ConjunctiveGraph)
735
736    # rdf+xml is not a valid alias
737    def testConstructByGETinRDFXML(self):
738        result = self.__generic(constructQuery, RDFXML, GET)
739        ct = result.info()["content-type"]
740        assert True in [one in ct for one in _RDF_XML], ct
741        results = result.convert()
742        self.assertEqual(type(results), ConjunctiveGraph)
743
744    # rdf+xml is not a valid alias
745    def testConstructByGETinRDFXML_Conneg(self):
746        result = self.__generic(constructQuery, RDFXML, GET, onlyConneg=True)
747        ct = result.info()["content-type"]
748        assert True in [one in ct for one in _RDF_XML], ct
749        results = result.convert()
750        self.assertEqual(type(results), ConjunctiveGraph)
751
752    # rdf+xml is not a valid alias
753    def testConstructByPOSTinRDFXML(self):
754        result = self.__generic(constructQuery, RDFXML, POST)
755        ct = result.info()["content-type"]
756        assert True in [one in ct for one in _RDF_XML], ct
757        results = result.convert()
758        self.assertEqual(type(results), ConjunctiveGraph)
759
760    # rdf+xml is not a valid alias
761    def testConstructByPOSTinRDFXML_Conneg(self):
762        result = self.__generic(constructQuery, RDFXML, POST, onlyConneg=True)
763        ct = result.info()["content-type"]
764        assert True in [one in ct for one in _RDF_XML], ct
765        results = result.convert()
766        self.assertEqual(type(results), ConjunctiveGraph)
767
768    # turtle is not a valid alias
769    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. turtle is not a valid alias. Use content negotiation instead")
770    def testConstructByGETinTURTLE(self):
771        result = self.__generic(constructQuery, TURTLE, GET)
772        ct = result.info()["content-type"]
773        assert True in [one in ct for one in _RDF_TURTLE], ct
774        results = result.convert()
775        self.assertEqual(type(results), bytes)
776
777    # turtle is not a valid alias
778    def testConstructByGETinTURTLE_Conneg(self):
779        result = self.__generic(constructQuery, TURTLE, GET, onlyConneg=True)
780        ct = result.info()["content-type"]
781        assert True in [one in ct for one in _RDF_TURTLE], ct
782        results = result.convert()
783        self.assertEqual(type(results), bytes)
784
785    # turtle is not a valid alias
786    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. turtle is not a valid alias. Use content negotiation instead")
787    def testConstructByPOSTinTURTLE(self):
788        result = self.__generic(constructQuery, TURTLE, POST)
789        ct = result.info()["content-type"]
790        assert True in [one in ct for one in _RDF_TURTLE], ct
791        results = result.convert()
792        self.assertEqual(type(results), bytes)
793
794    # turtle is not a valid alias
795    def testConstructByPOSTinTURTLE_Conneg(self):
796        result = self.__generic(constructQuery, TURTLE, POST, onlyConneg=True)
797        ct = result.info()["content-type"]
798        assert True in [one in ct for one in _RDF_TURTLE], ct
799        results = result.convert()
800        self.assertEqual(type(results), bytes)
801
802    # n3 is not a valid alias
803    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. n3 is not a valid alias. Use content negotiation instead")
804    def testConstructByGETinN3(self):
805        result = self.__generic(constructQuery, N3, GET)
806        ct = result.info()["content-type"]
807        assert True in [one in ct for one in _RDF_N3], ct
808        results = result.convert()
809        self.assertEqual(type(results), bytes)
810
811    # n3 is not a valid alias
812    def testConstructByGETinN3_Conneg(self):
813        result = self.__generic(constructQuery, N3, GET, onlyConneg=True)
814        ct = result.info()["content-type"]
815        assert True in [one in ct for one in _RDF_N3], ct
816        results = result.convert()
817        self.assertEqual(type(results), bytes)
818
819    # n3 is not a valid alias
820    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. n3 is not a valid alias. Use content negotiation instead")
821    def testConstructByPOSTinN3(self):
822        result = self.__generic(constructQuery, N3, POST)
823        ct = result.info()["content-type"]
824        assert True in [one in ct for one in _RDF_N3], ct
825        results = result.convert()
826        self.assertEqual(type(results), bytes)
827
828    # n3 is not a valid alias
829    def testConstructByPOSTinN3_Conneg(self):
830        result = self.__generic(constructQuery, N3, POST, onlyConneg=True)
831        ct = result.info()["content-type"]
832        assert True in [one in ct for one in _RDF_N3], ct
833        results = result.convert()
834        self.assertEqual(type(results), bytes)
835
836    # json-ld is not a valid alias. Use content negotiation instead
837    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. json-ld is not a valid alias. Use content negotiation instead")
838    def testConstructByGETinJSONLD(self):
839        result = self.__generic(constructQuery, JSONLD, GET)
840        ct = result.info()["content-type"]
841        assert True in [one in ct for one in _RDF_JSONLD], ct
842        results = result.convert()
843        self.assertEqual(type(results), ConjunctiveGraph)
844
845    # json-ld is not a valid alias. Use content negotiation instead
846    def testConstructByGETinJSONLD_Conneg(self):
847        result = self.__generic(constructQuery, JSONLD, GET, onlyConneg=True)
848        ct = result.info()["content-type"]
849        assert True in [one in ct for one in _RDF_JSONLD], ct
850        results = result.convert()
851        self.assertEqual(type(results), ConjunctiveGraph)
852
853    # json-ld is not a valid alias. Use content negotiation instead
854    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. json-ld is not a valid alias. Use content negotiation instead")
855    def testConstructByPOSTinJSONLD(self):
856        result = self.__generic(constructQuery, JSONLD, POST)
857        ct = result.info()["content-type"]
858        assert True in [one in ct for one in _RDF_JSONLD], ct
859        results = result.convert()
860        self.assertEqual(type(results), ConjunctiveGraph)
861
862    # json-ld is not a valid alias. Use content negotiation instead
863    def testConstructByPOSTinJSONLD_Conneg(self):
864        result = self.__generic(constructQuery, JSONLD, POST, onlyConneg=True)
865        ct = result.info()["content-type"]
866        assert True in [one in ct for one in _RDF_JSONLD], ct
867        results = result.convert()
868        self.assertEqual(type(results), ConjunctiveGraph)
869
870    # Asking for an unexpected return format for CONSTRUCT queryType.
871    # For a CONSTRUCT query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
872    def testConstructByGETinCSV_Unexpected(self):
873        result = self.__generic(constructQuery, CSV, GET)
874        ct = result.info()["content-type"]
875        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
876        results = result.convert()
877        self.assertEqual(type(results), ConjunctiveGraph)
878
879    # Asking for an unexpected return format for CONSTRUCT queryType.
880    # For a CONSTRUCT query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
881    def testConstructByGETinCSV_Unexpected_Conneg(self):
882        result = self.__generic(constructQuery, CSV, GET, onlyConneg=True)
883        ct = result.info()["content-type"]
884        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
885        results = result.convert()
886        self.assertEqual(type(results), ConjunctiveGraph)
887
888    # Asking for an unexpected return format for CONSTRUCT queryType.
889    # For a CONSTRUCT query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
890    def testConstructByPOSTinCSV_Unexpected(self):
891        result = self.__generic(constructQuery, CSV, POST)
892        ct = result.info()["content-type"]
893        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
894        results = result.convert()
895        self.assertEqual(type(results), ConjunctiveGraph)
896
897    # Asking for an unexpected return format for CONSTRUCT queryType.
898    # For a CONSTRUCT query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
899    def testConstructByPOSTinCSV_Unexpected_Conneg(self):
900        result = self.__generic(constructQuery, CSV, POST, onlyConneg=True)
901        ct = result.info()["content-type"]
902        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
903        results = result.convert()
904        self.assertEqual(type(results), ConjunctiveGraph)
905
906    # json is valid alias but returns application/sparql-results+json
907    @unittest.skip("Blazegraph returns application/sparql-results+json. It MUST return an RDF graph [RDF-CONCEPTS] serialized, for example, in the RDF/XML syntax [RDF-XML], or an equivalent RDF graph serialization, for SPARQL Query forms DESCRIBE and CONSTRUCT). See http://www.w3.org/TR/sparql11-protocol/#query-success")
908    def testConstructByGETinJSON_Unexpected(self):
909        result = self.__generic(constructQuery, JSON, GET)
910        ct = result.info()["content-type"]
911        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
912        results = result.convert()
913        self.assertEqual(type(results), ConjunctiveGraph)
914
915    # Asking for an unexpected return format for CONSTRUCT queryType.
916    # For a CONSTRUCT query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
917    def testConstructByGETinJSON_Unexpected_Conneg(self):
918        result = self.__generic(constructQuery, JSON, GET , onlyConneg=True)
919        ct = result.info()["content-type"]
920        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
921        results = result.convert()
922        self.assertEqual(type(results), ConjunctiveGraph)
923
924    # json is valid alias but returns application/sparql-results+json
925    @unittest.skip("Blazegraph returns application/sparql-results+json. It MUST return an RDF graph [RDF-CONCEPTS] serialized, for example, in the RDF/XML syntax [RDF-XML], or an equivalent RDF graph serialization, for SPARQL Query forms DESCRIBE and CONSTRUCT). See http://www.w3.org/TR/sparql11-protocol/#query-success")
926    def testConstructByPOSTinJSON_Unexpected(self):
927        result = self.__generic(constructQuery, JSON, POST)
928        ct = result.info()["content-type"]
929        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE], ct
930        results = result.convert()
931        self.assertEqual(type(results), ConjunctiveGraph)
932
933    # Asking for an unexpected return format for CONSTRUCT queryType.
934    # For a CONSTRUCT query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
935    def testConstructByPOSTinJSON_Unexpected_Conneg(self):
936        result = self.__generic(constructQuery, JSON, POST, onlyConneg=True)
937        ct = result.info()["content-type"]
938        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE], ct
939        results = result.convert()
940        self.assertEqual(type(results), ConjunctiveGraph)
941
942    # Asking for an unknown return format for CONSTRUCT queryType (XML is sent)
943    def testConstructByGETinUnknow(self):
944        result = self.__generic(constructQuery, "foo", GET)
945        ct = result.info()["content-type"]
946        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE], ct
947        results = result.convert()
948        self.assertEqual(type(results), ConjunctiveGraph)
949
950    # Asking for an unknown return format for CONSTRUCT queryType (XML is sent)
951    def testConstructByGETinUnknow_Conneg(self):
952        result = self.__generic(constructQuery, "foo", GET, onlyConneg=True)
953        ct = result.info()["content-type"]
954        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE], ct
955        results = result.convert()
956        self.assertEqual(type(results), ConjunctiveGraph)
957
958    # Asking for an unknown return format for CONSTRUCT queryType (XML is sent)
959    def testConstructByPOSTinUnknow(self):
960        result = self.__generic(constructQuery, "bar", POST)
961        ct = result.info()["content-type"]
962        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE], ct
963        results = result.convert()
964        self.assertEqual(type(results), ConjunctiveGraph)
965
966    # Asking for an unknown return format for CONSTRUCT queryType (XML is sent)
967    def testConstructByPOSTinUnknow_Conneg(self):
968        result = self.__generic(constructQuery, "bar", POST, onlyConneg=True)
969        ct = result.info()["content-type"]
970        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE], ct
971        results = result.convert()
972        self.assertEqual(type(results), ConjunctiveGraph)
973
974################################################################################
975################################################################################
976
977##################
978#### DESCRIBE ####
979##################
980
981    def testDescribeByGETinXML(self):
982        result = self.__generic(describeQuery, XML, GET)
983        ct = result.info()["content-type"]
984        assert True in [one in ct for one in _RDF_XML], ct
985        results = result.convert()
986        self.assertEqual(type(results), ConjunctiveGraph)
987
988    def testDescribeByGETinXML_Conneg(self):
989        result = self.__generic(describeQuery, XML, GET, onlyConneg=True)
990        ct = result.info()["content-type"]
991        assert True in [one in ct for one in _RDF_XML], ct
992        results = result.convert()
993        self.assertEqual(type(results), ConjunctiveGraph)
994
995    def testDescribeByPOSTinXML(self):
996        result = self.__generic(describeQuery, XML, POST)
997        ct = result.info()["content-type"]
998        assert True in [one in ct for one in _RDF_XML], ct
999        results = result.convert()
1000        self.assertEqual(type(results), ConjunctiveGraph)
1001
1002    def testDescribeByPOSTinXML_Conneg(self):
1003        result = self.__generic(describeQuery, XML, POST, onlyConneg=True)
1004        ct = result.info()["content-type"]
1005        assert True in [one in ct for one in _RDF_XML], ct
1006        results = result.convert()
1007        self.assertEqual(type(results), ConjunctiveGraph)
1008
1009    # rdf+xml is not a valid alias
1010    def testDescribeByGETinRDFXML(self):
1011        result = self.__generic(describeQuery, RDFXML, GET)
1012        ct = result.info()["content-type"]
1013        assert True in [one in ct for one in _RDF_XML], ct
1014        results = result.convert()
1015        self.assertEqual(type(results), ConjunctiveGraph)
1016
1017    # rdf+xml is not a valid alias
1018    def testDescribeByGETinRDFXML_Conneg(self):
1019        result = self.__generic(describeQuery, RDFXML, GET, onlyConneg=True)
1020        ct = result.info()["content-type"]
1021        assert True in [one in ct for one in _RDF_XML], ct
1022        results = result.convert()
1023        self.assertEqual(type(results), ConjunctiveGraph)
1024
1025    # rdf+xml is not a valid alias
1026    def testDescribeByPOSTinRDFXML(self):
1027        result = self.__generic(describeQuery, RDFXML, POST)
1028        ct = result.info()["content-type"]
1029        assert True in [one in ct for one in _RDF_XML], ct
1030        results = result.convert()
1031        self.assertEqual(type(results), ConjunctiveGraph)
1032
1033    # rdf+xml is not a valid alias
1034    def testDescribeByPOSTinRDFXML_Conneg(self):
1035        result = self.__generic(describeQuery, RDFXML, POST, onlyConneg=True)
1036        ct = result.info()["content-type"]
1037        assert True in [one in ct for one in _RDF_XML], ct
1038        results = result.convert()
1039        self.assertEqual(type(results), ConjunctiveGraph)
1040
1041    # turtle is not a valid alias
1042    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. turtle is not a valid alias. Use content negotiation instead")
1043    def testDescribeByGETinTURTLE(self):
1044        result = self.__generic(describeQuery, TURTLE, GET)
1045        ct = result.info()["content-type"]
1046        assert True in [one in ct for one in _RDF_TURTLE], ct
1047        results = result.convert()
1048        self.assertEqual(type(results), bytes)
1049
1050    # turtle is not a valid alias
1051    def testDescribeByGETinTURTLE_Conneg(self):
1052        result = self.__generic(describeQuery, TURTLE, GET, onlyConneg=True)
1053        ct = result.info()["content-type"]
1054        assert True in [one in ct for one in _RDF_TURTLE], ct
1055        results = result.convert()
1056        self.assertEqual(type(results), bytes)
1057
1058    # turtle is not a valid alias
1059    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. turtle is not a valid alias. Use content negotiation instead")
1060    def testDescribeByPOSTinTURTLE(self):
1061        result = self.__generic(describeQuery, TURTLE, POST)
1062        ct = result.info()["content-type"]
1063        assert True in [one in ct for one in _RDF_TURTLE], ct
1064        results = result.convert()
1065        self.assertEqual(type(results), bytes)
1066
1067    # turtle is not a valid alias
1068    def testDescribeByPOSTinTURTLE_Conneg(self):
1069        result = self.__generic(describeQuery, TURTLE, POST, onlyConneg=True)
1070        ct = result.info()["content-type"]
1071        assert True in [one in ct for one in _RDF_TURTLE], ct
1072        results = result.convert()
1073        self.assertEqual(type(results), bytes)
1074
1075    # n3 is not a valid alias
1076    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. n3 is not a valid alias. Use content negotiation instead")
1077    def testDescribeByGETinN3(self):
1078        result = self.__generic(describeQuery, N3, GET)
1079        ct = result.info()["content-type"]
1080        assert True in [one in ct for one in _RDF_N3], ct
1081        results = result.convert()
1082        self.assertEqual(type(results), bytes)
1083
1084    # n3 is not a valid alias
1085    def testDescribeByGETinN3_Conneg(self):
1086        result = self.__generic(describeQuery, N3, GET, onlyConneg=True)
1087        ct = result.info()["content-type"]
1088        assert True in [one in ct for one in _RDF_N3], ct
1089        results = result.convert()
1090        self.assertEqual(type(results), bytes)
1091
1092    # n3 is not a valid alias
1093    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. n3 is not a valid alias. Use content negotiation instead")
1094    def testDescribeByPOSTinN3(self):
1095        result = self.__generic(describeQuery, N3, POST)
1096        ct = result.info()["content-type"]
1097        assert True in [one in ct for one in _RDF_N3], ct
1098        results = result.convert()
1099        self.assertEqual(type(results), bytes)
1100
1101    # n3 is not a valid alias
1102    def testDescribeByPOSTinN3_Conneg(self):
1103        result = self.__generic(describeQuery, N3, POST, onlyConneg=True)
1104        ct = result.info()["content-type"]
1105        assert True in [one in ct for one in _RDF_N3], ct
1106        results = result.convert()
1107        self.assertEqual(type(results), bytes)
1108
1109    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. json-ld is not a valid alias. Use content negotiation instead")
1110    def testDescribeByGETinJSONLD(self):
1111        result = self.__generic(describeQuery, JSONLD, GET)
1112        ct = result.info()["content-type"]
1113        assert True in [one in ct for one in _RDF_JSONLD], ct
1114        results = result.convert()
1115        self.assertEqual(type(results), ConjunctiveGraph)
1116
1117    def testDescribeByGETinJSONLD_Conneg(self):
1118        result = self.__generic(describeQuery, JSONLD, GET, onlyConneg=True)
1119        ct = result.info()["content-type"]
1120        assert True in [one in ct for one in _RDF_JSONLD], ct
1121        results = result.convert()
1122        self.assertEqual(type(results), ConjunctiveGraph)
1123
1124    @unittest.skip("Blazegraph does not support receiving unexpected 'format' values. json-ld is not a valid alias. Use content negotiation instead")
1125    def testDescribeByPOSTinJSONLD(self):
1126        result = self.__generic(describeQuery, JSONLD, POST)
1127        ct = result.info()["content-type"]
1128        assert True in [one in ct for one in _RDF_JSONLD], ct
1129        results = result.convert()
1130        self.assertEqual(type(results), ConjunctiveGraph)
1131
1132    def testDescribeByPOSTinJSONLD_Conneg(self):
1133        result = self.__generic(describeQuery, JSONLD, POST, onlyConneg=True)
1134        ct = result.info()["content-type"]
1135        assert True in [one in ct for one in _RDF_JSONLD], ct
1136        results = result.convert()
1137        self.assertEqual(type(results), ConjunctiveGraph)
1138
1139    # Asking for an unexpected return format for DESCRIBE queryType.
1140    # For a DESCRIBE query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
1141    def testDescribeByGETinCSV_Unexpected(self):
1142        result = self.__generic(describeQuery, CSV, GET)
1143        ct = result.info()["content-type"]
1144        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
1145        results = result.convert()
1146        self.assertEqual(type(results), ConjunctiveGraph)
1147
1148    # Asking for an unexpected return format for DESCRIBE queryType.
1149    # For a DESCRIBE query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
1150    # Returned application/rdf+xml
1151    def testDescribeByGETinCSV_Unexpected_Conneg(self):
1152        result = self.__generic(describeQuery, CSV, GET, onlyConneg=True)
1153        ct = result.info()["content-type"]
1154        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
1155        results = result.convert()
1156        self.assertEqual(type(results), ConjunctiveGraph)
1157
1158    # Asking for an unexpected return format for DESCRIBE queryType.
1159    # For a DESCRIBE query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
1160    def testDescribeByPOSTinCSV_Unexpected(self):
1161        result = self.__generic(describeQuery, CSV, POST)
1162        ct = result.info()["content-type"]
1163        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
1164        results = result.convert()
1165        self.assertEqual(type(results), ConjunctiveGraph)
1166
1167    # Asking for an unexpected return format for DESCRIBE queryType.
1168    # For a DESCRIBE query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
1169    def testDescribeByPOSTinCSV_Unexpected_Conneg(self):
1170        result = self.__generic(describeQuery, CSV, POST, onlyConneg=True)
1171        ct = result.info()["content-type"]
1172        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
1173        results = result.convert()
1174        self.assertEqual(type(results), ConjunctiveGraph)
1175
1176    # json is valid alias but returns application/sparql-results+json
1177    @unittest.skip("Blazegraph returns application/sparql-results+json. It MUST return an RDF graph [RDF-CONCEPTS] serialized, for example, in the RDF/XML syntax [RDF-XML], or an equivalent RDF graph serialization, for SPARQL Query forms DESCRIBE and CONSTRUCT). See http://www.w3.org/TR/sparql11-protocol/#query-success")
1178    def testDescribeByGETinJSON_Unexpected(self):
1179        result = self.__generic(describeQuery, JSON, GET)
1180        ct = result.info()["content-type"]
1181        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
1182        results = result.convert()
1183        self.assertEqual(type(results), ConjunctiveGraph)
1184
1185    # Asking for an unexpected return format for DESCRIBE queryType.
1186    # For a DESCRIBE query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
1187    def testDescribeByGETinJSON_Unexpected_Conneg(self):
1188        result = self.__generic(describeQuery, JSON, GET, onlyConneg=True)
1189        ct = result.info()["content-type"]
1190        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
1191        results = result.convert()
1192        self.assertEqual(type(results), ConjunctiveGraph)
1193
1194    # json is valid alias but returns application/sparql-results+json
1195    @unittest.skip("Blazegraph returns application/sparql-results+json. It MUST return an RDF graph [RDF-CONCEPTS] serialized, for example, in the RDF/XML syntax [RDF-XML], or an equivalent RDF graph serialization, for SPARQL Query forms DESCRIBE and CONSTRUCT). See http://www.w3.org/TR/sparql11-protocol/#query-success")
1196    def testDescribeByPOSTinJSON_Unexpected(self):
1197        result = self.__generic(describeQuery, JSON, POST)
1198        ct = result.info()["content-type"]
1199        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
1200        results = result.convert()
1201        self.assertEqual(type(results), ConjunctiveGraph)
1202
1203    # Asking for an unexpected return format for DESCRIBE queryType.
1204    # For a DESCRIBE query type, the default return mimetype (if Accept: */* is sent) is application/rdf+xml
1205    def testDescribeByPOSTinJSON_Unexpected_Conneg(self):
1206        result = self.__generic(describeQuery, JSON, POST, onlyConneg=True)
1207        ct = result.info()["content-type"]
1208        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE]
1209        results = result.convert()
1210        self.assertEqual(type(results), ConjunctiveGraph)
1211
1212    # Asking for an unknown return format for DESCRIBE queryType (XML is sent)
1213    def testDescribeByGETinUnknow(self):
1214        result = self.__generic(describeQuery, "foo", GET)
1215        ct = result.info()["content-type"]
1216        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE], ct
1217        results = result.convert()
1218        self.assertEqual(type(results), ConjunctiveGraph)
1219
1220    # Asking for an unknown return format for DESCRIBE queryType (XML is sent)
1221    def testDescribeByGETinUnknow_Conneg(self):
1222        result = self.__generic(describeQuery, "foo", GET, onlyConneg=True)
1223        ct = result.info()["content-type"]
1224        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE], ct
1225        results = result.convert()
1226        self.assertEqual(type(results), ConjunctiveGraph)
1227
1228    # Asking for an unknown return format for DESCRIBE queryType (XML is sent)
1229    def testDescribeByPOSTinUnknow(self):
1230        result = self.__generic(describeQuery, "bar", POST)
1231        ct = result.info()["content-type"]
1232        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE], ct
1233        results = result.convert()
1234        self.assertEqual(type(results), ConjunctiveGraph)
1235
1236    # Asking for an unknown return format for DESCRIBE queryType (XML is sent)
1237    def testDescribeByPOSTinUnknow_Conneg(self):
1238        result = self.__generic(describeQuery, "bar", POST, onlyConneg=True)
1239        ct = result.info()["content-type"]
1240        assert True in [one in ct for one in _SPARQL_DESCRIBE_CONSTRUCT_POSSIBLE], ct
1241        results = result.convert()
1242        self.assertEqual(type(results), ConjunctiveGraph)
1243
1244################################################################################
1245################################################################################
1246
1247    def testQueryBadFormed(self):
1248        self.assertRaises(QueryBadFormed, self.__generic, queryBadFormed, XML, GET)
1249
1250    def testQueryManyPrefixes(self):
1251        result = self.__generic(queryManyPrefixes, XML, GET)
1252
1253    @unittest.skip('Blazegraph. org.openrdf.query.MalformedQueryException: Multiple prefix declarations for prefix xxx')
1254    def testQueryDuplicatedPrefix(self):
1255        result = self.__generic(queryDuplicatedPrefix, XML, GET)
1256
1257    def testKeepAlive(self):
1258        sparql = SPARQLWrapper(endpoint)
1259        sparql.setQuery('SELECT * WHERE {?s ?p ?o} LIMIT 10')
1260        sparql.setReturnFormat(JSON)
1261        sparql.setMethod(GET)
1262        sparql.setUseKeepAlive()
1263
1264        sparql.query()
1265        sparql.query()
1266
1267    def testQueryWithComma_1(self):
1268        result = self.__generic(queryWithCommaInCurie_1, XML, GET)
1269
1270    @unittest.skip('Blazegraph. parser error: Failed to decode SPARQL ID "dbpedia:Category\". See #94')
1271    def testQueryWithComma_2(self):
1272        result = self.__generic(queryWithCommaInCurie_2, XML, POST)
1273
1274    def testQueryWithComma_3(self):
1275        result = self.__generic(queryWithCommaInUri, XML, GET)
1276
1277if __name__ == "__main__":
1278    unittest.main()
1279