Skip to content

Commit

Permalink
Tests changed to use wikimedia SPARQL endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Mar 29, 2021
1 parent 4ded221 commit 04c4e76
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/sparqlClient/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function query(RequestInterface $request): Statement {
$request = $request->withHeader('Accept', 'application/json');
$response = $this->client->sendRequest($request);
if ($response->getStatusCode() !== 200) {
throw new SparqlException($response);
throw new SparqlException("Query execution failed with HTTP " . $response->getStatusCode() . " " . $response->getReasonPhrase());
}
return new Statement($response, $this->dataFactory);
}
Expand All @@ -41,7 +41,7 @@ public function askQuery(RequestInterface $request): bool {
$request = $request->withHeader('Accept', 'application/json');
$response = $this->client->sendRequest($request);
if ($response->getStatusCode() !== 200) {
throw new SparqlException($response);
throw new SparqlException("Query execution failed with HTTP " . $response->getStatusCode() . " " . $response->getReasonPhrase());
}
// https://www.w3.org/TR/sparql11-results-json/
$body = (string) $response->getBody();
Expand Down
26 changes: 23 additions & 3 deletions tests/SparqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testSelect(): void {
$gc = new \GuzzleHttp\Client();
$df = new DataFactory();
$c = new Connection($gc, $df);
$url = 'https://arche-sparql.acdh-dev.oeaw.ac.at/sparql?query=select%20%3Fa%20%3Fb%20%3Fc%20where%20%7B%3Fa%20%3Fb%20%3Fc%7D%20limit%2010';
$url = 'https://query.wikidata.org/sparql?query=select%20%3Fa%20%3Fb%20%3Fc%20where%20%7B%3Fa%20%3Fb%20%3Fc%7D%20limit%2010';

$s = $c->query(new \GuzzleHttp\Psr7\Request('GET', $url));
$d1 = iterator_to_array($s);
Expand Down Expand Up @@ -68,15 +68,35 @@ public function testAsk(): void {
$gc = new \GuzzleHttp\Client();
$df = new DataFactory();
$c = new Connection($gc, $df);
$url = 'https://arche-sparql.acdh-dev.oeaw.ac.at/sparql?query=ask%20%7B%3Chttp%3A%2F%2Ffoo%3E%20%3Chttp%3A%2F%2Fbar%3E%20%3Chttp%3A%2F%2Fbaz%3E%7D';
$url = 'https://query.wikidata.org/sparql?query=ask%20%7B%3Chttp%3A%2F%2Ffoo%3E%20%3Chttp%3A%2F%2Fbar%3E%20%3Chttp%3A%2F%2Fbaz%3E%7D';
$this->assertFalse($c->askQuery(new \GuzzleHttp\Psr7\Request('GET', $url)));
}

public function testExceptions(): void {
$gc = new \GuzzleHttp\Client();
$df = new DataFactory();
$c = new Connection($gc, $df);

$url = 'https://arche-sparql.acdh-dev.oeaw.ac.at/sparql?query=select%20%3Fa%20%3Fb%20%3Fc%20where%20%7B%3Fa%20%3Fb%20%3Fc%7D%20limit%2010';
$url = 'https://query.wikidata.org/sparql?query=select%20%3Fa%20%3Fb%20%3Fc%20where%20%7B%3Fa%20%3Fb%20%3Fc%7D%20limit%2010';
try {
$c->askQuery(new \GuzzleHttp\Psr7\Request('GET', $url));
$this->assertTrue(false);
} catch (SparqlException $ex) {
$this->assertStringStartsWith('Not an ASK query response', $ex->getMessage());
}

$url = 'https://query.wikidata.org/sparql?query=wrongQuery';
try {
$c->askQuery(new \GuzzleHttp\Psr7\Request('GET', $url));
$this->assertTrue(false);
} catch (SparqlException $ex) {
$this->assertStringStartsWith('Query execution failed with HTTP', $ex->getMessage());
}
try {
$c->query(new \GuzzleHttp\Psr7\Request('GET', $url));
$this->assertTrue(false);
} catch (SparqlException $ex) {
$this->assertStringStartsWith('Query execution failed with HTTP', $ex->getMessage());
}
}
}

0 comments on commit 04c4e76

Please sign in to comment.