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