def setUp(self): self.conn = SparqlDB('http://dbpedia.org/sparql')
def before_request(): sparql_db = SparqlDB(app.config['SPARQL_ENDPOINT']) sparql_db.add_prefix('vcard', 'http://www.w3.org/TR/vcard-rdf/#') sparql_db.add_prefix('prov', 'http://www.w3.org/ns/prov#') sparql_db.add_prefix('http', 'http://www.w3.org/2011/http#') sparql_db.add_prefix('xsd', 'http://www.w3.org/2001/XMLSchema#') sparql_db.add_prefix('bcube', 'http://purl.org/esip/bcube#') sparql_db.add_prefix('dcat', 'http://www.w3.org/TR/vocab-dcat/#') sparql_db.add_prefix('owl', 'http://www.w3.org/2002/07/owl#') sparql_db.add_prefix('foaf', 'http://xmlns.com/foaf/0.1/') sparql_db.add_prefix('pcube', 'http://purl.org/BCube/#') sparql_db.add_prefix('dct', 'http://purl.org/dc/terms/') sparql_db.add_prefix('dc', 'http://purl.org/dc/elements/1.1/') g.db = sparql_db
class TestBasicCases(unittest.TestCase): def setUp(self): self.conn = SparqlDB('http://dbpedia.org/sparql') def tearDown(self): self.conn._prefixes = {} def test_basic_query(self): """ initial test. ensure that we can connect to the dbpedia sparql endpoint for testing and do a simple query with custom ontology binding. """ self.assertEquals(isinstance(self.conn, SparqlDB), True) self.conn.add_prefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#") self.assertEquals(self.conn._prefixes_query, "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>") query = """SELECT ?label WHERE { <http://dbpedia.org/resource/Boulder> rdfs:label ?label } """ res = self.conn.query(query) self.assertIsNotNone(res) self.assertTrue(isinstance(res, dict)) for result in res["bindings"]: if result['label']['xml:lang'] == 'en': self.assertEquals(result['label']['value'], 'Boulder') if result['label']['xml:lang'] == 'es': self.assertEquals(result['label']['value'], 'Bloque (roca)') def test_other_query(self): """ tests that we can do more complicated queries, this time using the built-in ontologies from dbpedia: http://dbpedia-live.openlinksw.com/sparql?nsdecl """ # Bands from Denver, why not! query = """SELECT ?name ?place WHERE { ?place rdfs:label "Denver"@en . ?band dbo:hometown ?place . ?band rdf:type dbo:Band . ?band rdfs:label ?name . FILTER langMatches(lang(?name),'en') } """ res = self.conn.query(query) self.assertIsNotNone(res) bands = [] for result in res["bindings"]: bands.append(result['name']['value']) self.assertTrue("DeVotchKa" in bands) self.assertTrue("Flobots" in bands) self.assertTrue("The Lumineers" in bands) def test_multiple_users(self): """ tests that we can send multiple concurrent requests to the sparql endpoint """ results = [] def user(query): result = self.conn.query(query) if result is not None: results.append((result['bindings'][0], query)) queries = ["""SELECT ?label WHERE { <http://dbpedia.org/resource/Boulder> rdfs:label ?label FILTER langMatches(lang(?label),'en') } """, """SELECT DISTINCT count (?res) as ?count WHERE { ?res geo:lat ?v. } """, """SELECT ?country WHERE{ ?country dct:subject <http://dbpedia.org/resource/Category:Countries_in_Europe> } """ ] users = [] for i in range(3): t = threading.Thread(target=user, args=(queries[i],)) users.append(t) t.start() for t in users: t.join() # when we get the results we assert that they came from the proper query for result, query in results: if 'label' in result: self.assertTrue(query == queries[0]) if 'count' in result: self.assertTrue(query == queries[1]) if 'country' in result: self.assertTrue(query == queries[2])