Example #1
0
 def testQueryRdfDataConstruct(self):
     class testOptions(object):
         verbose        = False
         rdf_data       = ["test1.rdf", "test2.rdf"]
         prefix         = None
         format_rdf_in  = None
     options  = testOptions()
     prefixes = asqc.getPrefixes(options)+"PREFIX ex: <http://example.org/test#>\n"
     bindings = (
             { "head":    { "vars": ["s", "p", "o"] }
             , "results": 
               { "bindings": 
                 [ { 's': rdflib.URIRef("http://example.org/test#s1")
                   }
                 , { 's': rdflib.URIRef("http://example.org/test#s2")
                   , 'o': rdflib.URIRef("http://example.org/test#o4")
                   }
                 , { 's': rdflib.URIRef("http://example.org/test#s3")
                   , 'p': rdflib.URIRef("http://example.org/test#p5")
                   }
                 ]
               }
             })
     query    = "CONSTRUCT {?s ?p ?o } WHERE { ?s ?p ?o }"
     (status,result)   = asqc.queryRdfData("test", options, prefixes, query, bindings)
     assert status == 0, "queryRdfData CONSTRUCT with data status"
     assert len(result) == 4, "queryRdfData triple count"
     assert ( rdflib.URIRef("http://example.org/test#s1"),
              rdflib.URIRef("http://example.org/test#p1"),
              rdflib.URIRef("http://example.org/test#o1") ) in result
     query    = "CONSTRUCT { <http://example.org/nonesuch> ?p ?o } WHERE { <http://example.org/nonesuch> ?p ?o }"
     (status,result)   = asqc.queryRdfData("test", options, prefixes, query, bindings)
     assert status == 1, "queryRdfData CONSTRUCT with no data status"
     assert len(result) == 0, "queryRdfData triple count"
     return
Example #2
0
 def testQueryRdfDataAsk(self):
     class testOptions(object):
         verbose        = False
         rdf_data       = ["test1.rdf", "test2.rdf"]
         prefix         = None
         format_rdf_in  = None
     options  = testOptions()
     prefixes = asqc.getPrefixes(options)+"PREFIX ex: <http://example.org/test#>\n"
     bindings = (
             { "head":    { "vars": ["s", "p", "o"] }
             , "results": 
               { "bindings": 
                 [ { 's': rdflib.URIRef("http://example.org/test#s1")
                   }
                 , { 's': rdflib.URIRef("http://example.org/test#s2")
                   , 'o': rdflib.URIRef("http://example.org/test#o4")
                   }
                 , { 's': rdflib.URIRef("http://example.org/test#s3")
                   , 'p': rdflib.URIRef("http://example.org/test#p5")
                   }
                 ]
               }
             })
     query = "ASK { ?s ?p ?o }"
     (status,result) = asqc.queryRdfData("test", options, prefixes, query, bindings)
     assert status == 0, "queryRdfData ASK with data status"
     assert result == {'head': {}, 'boolean': True}
     query = "ASK { ex:nonesuch ?p ?o }"
     (status,result) = asqc.queryRdfData("test", options, prefixes, query, bindings)
     assert status == 1, "queryRdfData ASK with no data status"
     assert result == {'head': {}, 'boolean': False}
     return
Example #3
0
 def testGetPrefixes(self):
     class testOptions(object):
         verbose = False
         prefix  = None
     options = testOptions()
     # named file or resource
     f = open("test.prefixes", "r")
     p = f.read()
     f.close()
     options.prefix = "test.prefixes"
     assert asqc.getPrefixes(options) == p, asqc.getPrefixes(options)
     # configured defaults
     configprefixfile = os.path.join(os.path.expanduser("~"), ".asqc-prefixes")
     f = open(configprefixfile, "r")
     p = f.read()
     f.close()
     options.prefix = None
     assert asqc.getPrefixes(options) == p, asqc.getPrefixes(options)
     return
Example #4
0
 def testQueryRdfDataSelect(self):
     class testOptions(object):
         verbose        = False
         rdf_data       = ["test1.rdf", "test2.rdf"]
         prefix         = None
         format_rdf_in  = None
     options  = testOptions()
     prefixes = asqc.getPrefixes(options)+"PREFIX ex: <http://example.org/test#>\n"
     bindings = (
             { "head":    { "vars": ["s", "p", "o"] }
             , "results": 
               { "bindings": 
                 [ { 's': rdflib.URIRef("http://example.org/test#s1")
                   }
                 , { 's': rdflib.URIRef("http://example.org/test#s2")
                   , 'o': rdflib.URIRef("http://example.org/test#o4")
                   }
                 , { 's': rdflib.URIRef("http://example.org/test#s3")
                   , 'p': rdflib.URIRef("http://example.org/test#p5")
                   }
                 ]
               }
             })
     query = "SELECT * WHERE { ?s ?p ?o }"
     (status,result) = asqc.queryRdfData("test", options, prefixes, query, bindings)
     assert status == 0, "queryRdfData SELECT with data status"
     assert len(result["results"]["bindings"]) == 4, "queryRdfData result count"
     assert { 's': { 'type': "uri", 'value': "http://example.org/test#s1" }
            , 'p': { 'type': "uri", 'value': "http://example.org/test#p1" }
            , 'o': { 'type': "uri", 'value': "http://example.org/test#o1" }
            } in result["results"]["bindings"], "queryRdfData result 1"
     assert { 's': { 'type': "uri", 'value': "http://example.org/test#s1" }
            , 'p': { 'type': "uri", 'value': "http://example.org/test#p2" }
            , 'o': { 'type': "uri", 'value': "http://example.org/test#o2" }
            } in result["results"]["bindings"], "queryRdfData result 2"
     assert { 's': { 'type': "uri", 'value': "http://example.org/test#s2" }
            , 'p': { 'type': "uri", 'value': "http://example.org/test#p4" }
            , 'o': { 'type': "uri", 'value': "http://example.org/test#o4" }
            } in result["results"]["bindings"], "queryRdfData result 3"
     assert { 's': { 'type': "uri", 'value': "http://example.org/test#s3" }
            , 'p': { 'type': "uri", 'value': "http://example.org/test#p5" }
            , 'o': { 'type': "uri", 'value': "http://example.org/test#o5" }
            } in result["results"]["bindings"], "queryRdfData result 4"
     query = "SELECT * WHERE { <http://example.org/nonesuch> ?p ?o }"
     (status,result) = asqc.queryRdfData("test", options, prefixes, query, bindings)
     assert status == 1, "queryRdfData SELECT with no data status"
     assert len(result["results"]["bindings"]) == 0, "queryRdfData result count"
     return
Example #5
0
 def testQuerySparqlEndpointSelect(self):
     # This test assumes a SPARQL endpoint running at http://localhost:3030/ds/query 
     # containing the contents of files test1.rdf and test2.rdf.
     # (I use Jena Fuseki with default settings for testing.)
     class testOptions(object):
         verbose  = False
         endpoint = "http://localhost:3030/ds/query"
         prefix   = None
     options  = testOptions()
     prefixes = asqc.getPrefixes(options)+"PREFIX ex: <http://example.org/test#>\n"
     query    = "SELECT * WHERE { ?s ?p ?o }"
     bindings = (
             { "head":    { "vars": ["s", "p", "o"] }
             , "results": 
               { "bindings": 
                 [ { 's': rdflib.URIRef("http://example.org/test#s1")
                   }
                 , { 's': rdflib.URIRef("http://example.org/test#s2")
                   , 'o': rdflib.URIRef("http://example.org/test#o4")
                   }
                 , { 's': rdflib.URIRef("http://example.org/test#s3")
                   , 'p': rdflib.URIRef("http://example.org/test#p5")
                   }
                 ]
               }
             })
     (status,result) = asqc.querySparqlEndpoint("test", options, prefixes, query, bindings)
     assert len(result["results"]["bindings"]) == 4, "querySparqlEndpoint result count"
     assert { 's': { 'type': "uri", 'value': "http://example.org/test#s1" }
            , 'p': { 'type': "uri", 'value': "http://example.org/test#p1" }
            , 'o': { 'type': "uri", 'value': "http://example.org/test#o1" }
            } in result["results"]["bindings"], "querySparqlEndpoint result 1"
     assert { 's': { 'type': "uri", 'value': "http://example.org/test#s1" }
            , 'p': { 'type': "uri", 'value': "http://example.org/test#p2" }
            , 'o': { 'type': "uri", 'value': "http://example.org/test#o2" }
            } in result["results"]["bindings"], "querySparqlEndpoint result 2"
     assert { 's': { 'type': "uri", 'value': "http://example.org/test#s2" }
            , 'p': { 'type': "uri", 'value': "http://example.org/test#p4" }
            , 'o': { 'type': "uri", 'value': "http://example.org/test#o4" }
            } in result["results"]["bindings"], "querySparqlEndpoint result 3"
     assert { 's': { 'type': "uri", 'value': "http://example.org/test#s3" }
            , 'p': { 'type': "uri", 'value': "http://example.org/test#p5" }
            , 'o': { 'type': "uri", 'value': "http://example.org/test#o5" }
            } in result["results"]["bindings"], "querySparqlEndpoint result 4"
     return
Example #6
0
 def testQuerySparqlEndpointAsk(self):
     # This test assumes a SPARQL endpoint running at http://localhost:3030/ds/query 
     # containing the contents of files test1.rdf and test2.rdf.
     # (I use Jena Fuseki with default settings for testing.)
     class testOptions(object):
         verbose  = False
         endpoint = "http://localhost:3030/ds/query"
         prefix   = None
     options  = testOptions()
     prefixes = asqc.getPrefixes(options)+"PREFIX ex: <http://example.org/test#>\n"
     query    = "ASK { ex:s1 ?p ?o }"
     (status,result) = asqc.querySparqlEndpoint("test", options, prefixes, query, None)
     assert status == 0, "Ask success status"
     assert result == {'head': {}, 'boolean': True}
     query    = "ASK { ex:notfound ?p ?o }"
     (status,result) = asqc.querySparqlEndpoint("test", options, prefixes, query, None)
     assert status == 1, "Ask not found status"
     assert result == {'head': {}, 'boolean': False}
     return
Example #7
0
 def testQuerySparqlEndpointConstruct(self):
     # This test assumes a SPARQL endpoint running at http://localhost:3030/ds/query 
     # containing the contents of files test1.rdf and test2.rdf.
     # (I use Jena Fuseki with default settings for testing.)
     class testOptions(object):
         verbose  = False
         endpoint = "http://localhost:3030/ds/query"
         prefix   = None
     options  = testOptions()
     prefixes = asqc.getPrefixes(options)+"PREFIX ex: <http://example.org/test#>\n"
     query    = "CONSTRUCT { ex:s1 ?p ?o } WHERE { ex:s1 ?p ?o }"
     (status,result) = asqc.querySparqlEndpoint("test", options, prefixes, query, None)
     assert status == 0, "Construct success status"
     assert len(result) == 2
     assert ( rdflib.URIRef("http://example.org/test#s1"),
              rdflib.URIRef("http://example.org/test#p1"),
              rdflib.URIRef("http://example.org/test#o1") ) in result
     query    = "CONSTRUCT { ex:notfound ?p ?o } WHERE { ex:notfound ?p ?o }"
     (status,result) = asqc.querySparqlEndpoint("test", options, prefixes, query, None)
     assert status == 1, "Construct not found status"
     return