| 1 |
from example.main import app |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
from rdflib import Graph |
| 4 |
|
| 5 |
from rdflib_endpoint.sparql_router import SD |
| 6 |
|
| 7 |
# Use app defined in example folder |
| 8 |
endpoint = TestClient(app) |
| 9 |
|
| 10 |
|
| 11 |
def test_service_description(): |
| 12 |
# Check GET turtle |
| 13 |
response = endpoint.get("/", headers={"accept": "text/turtle"}) |
| 14 |
print(response.text) |
| 15 |
assert response.status_code == 200 |
| 16 |
g = Graph() |
| 17 |
g.parse(data=response.text, format="turtle") |
| 18 |
assert any(g.triples((None, SD.endpoint, None))), "Missing sd:endpoint in service description" |
| 19 |
assert any(g.triples((None, SD.extensionFunction, None))), "Missing sd:extensionFunction in service description" |
| 20 |
assert len(list(g.triples((None, SD.extensionFunction, None)))) == 2, "Expected exactly 2 extension functions" |
| 21 |
assert len(list(g.triples((None, SD.namedGraph, None)))) == 2, "Expected exactly 2 named graphs" |
| 22 |
|
| 23 |
# Check POST turtle |
| 24 |
response = endpoint.post("/", headers={"accept": "text/turtle"}) |
| 25 |
assert response.status_code == 200 |
| 26 |
g = Graph() |
| 27 |
g.parse(data=response.text, format="turtle") |
| 28 |
assert any(g.triples((None, SD.endpoint, None))), "Missing sd:endpoint in service description" |
| 29 |
assert any(g.triples((None, SD.extensionFunction, None))), "Missing sd:extensionFunction in service description" |
| 30 |
assert len(list(g.triples((None, SD.extensionFunction, None)))) == 2, "Expected exactly 2 extension functions" |
| 31 |
|
| 32 |
# Check POST XML |
| 33 |
response = endpoint.post("/", headers={"accept": "application/xml"}) |
| 34 |
assert response.status_code == 200 |
| 35 |
g = Graph() |
| 36 |
g.parse(data=response.text, format="xml") |
| 37 |
assert any(g.triples((None, SD.endpoint, None))), "Missing sd:endpoint in service description" |
| 38 |
assert any(g.triples((None, SD.extensionFunction, None))), "Missing sd:extensionFunction in service description" |
| 39 |
assert len(list(g.triples((None, SD.extensionFunction, None)))) == 2, "Expected exactly 2 extension functions" |
| 40 |
|
| 41 |
|
| 42 |
def test_custom_concat(): |
| 43 |
response = endpoint.get("/", params={"query": custom_concat_query}, headers={"accept": "application/json"}) |
| 44 |
# print(response.json()) |
| 45 |
assert response.status_code == 200 |
| 46 |
assert response.json()["results"]["bindings"][0]["concat"]["value"] == "Firstlast" |
| 47 |
|
| 48 |
response = endpoint.post( |
| 49 |
"/", |
| 50 |
data={"query": custom_concat_query}, |
| 51 |
headers={"accept": "application/json"}, |
| 52 |
) |
| 53 |
assert response.status_code == 200 |
| 54 |
assert response.json()["results"]["bindings"][0]["concat"]["value"] == "Firstlast" |
| 55 |
|
| 56 |
|
| 57 |
def test_bad_request(): |
| 58 |
response = endpoint.get("/?query=figarofigarofigaro", headers={"accept": "application/json"}) |
| 59 |
assert response.status_code == 400 |
| 60 |
|
| 61 |
|
| 62 |
custom_concat_query = """PREFIX myfunctions: <https://w3id.org/sparql-functions/> |
| 63 |
SELECT ?concat ?concatLength WHERE { |
| 64 |
BIND("First" AS ?first) |
| 65 |
BIND(myfunctions:custom_concat(?first, "last") AS ?concat) |
| 66 |
}""" |