| 1 |
from fastapi.testclient import TestClient |
| 2 |
from rdflib import RDF, RDFS, Graph, Literal, URIRef |
| 3 |
|
| 4 |
from rdflib_endpoint import SparqlEndpoint |
| 5 |
|
| •6 |
g = Graph() |
| 7 |
g.add((URIRef("http://subject"), RDF.type, URIRef("http://object"))) |
| 8 |
g.add((URIRef("http://subject"), RDFS.label, Literal("test value"))) |
| 9 |
|
| 10 |
|
| 11 |
app = SparqlEndpoint(graph=g) |
| 12 |
|
| 13 |
endpoint = TestClient(app) |
| 14 |
|
| 15 |
|
| 16 |
def test_service_description(): |
| 17 |
response = endpoint.get("/", headers={"accept": "text/turtle"}) |
| 18 |
# print(response.text.strip()) |
| 19 |
assert response.status_code == 200 |
| 20 |
|
| 21 |
response = endpoint.post("/", headers={"accept": "text/turtle"}) |
| 22 |
assert response.status_code == 200 |
| 23 |
|
| 24 |
# Check for application/xml |
| 25 |
response = endpoint.post("/", headers={"accept": "application/xml"}) |
| 26 |
assert response.status_code == 200 |
| 27 |
|
| 28 |
|
| 29 |
def test_custom_concat_json(): |
| 30 |
response = endpoint.get("/?query=", params={"query": label_select}, headers={"accept": "application/json"}) |
| 31 |
assert response.status_code == 200 |
| 32 |
assert response.json()["results"]["bindings"][0]["label"]["value"] == "test value" |
| 33 |
|
| 34 |
response = endpoint.post("/", data={"query": label_select}, headers={"accept": "application/json"}) |
| 35 |
assert response.status_code == 200 |
| 36 |
assert response.json()["results"]["bindings"][0]["label"]["value"] == "test value" |
| 37 |
|
| 38 |
|
| 39 |
def test_select_noaccept_xml(): |
| 40 |
response = endpoint.post("/", data={"query": label_select}) |
| 41 |
assert response.status_code == 200 |
| 42 |
# assert response.json()['results']['bindings'][0]['concat']['value'] == "Firstlast" |
| 43 |
|
| 44 |
|
| 45 |
def test_select_csv(): |
| 46 |
response = endpoint.post("/", data={"query": label_select}, headers={"accept": "text/csv"}) |
| 47 |
assert response.status_code == 200 |
| 48 |
# assert response.json()['results']['bindings'][0]['concat']['value'] == "Firstlast" |
| 49 |
|
| 50 |
|
| 51 |
def test_select_turtle(): |
| 52 |
response = endpoint.post("/", data={"query": label_select}, headers={"accept": "text/turtle"}) |
| 53 |
assert response.status_code == 200 |
| 54 |
assert response.headers["content-type"] == "application/sparql-results+xml" |
| 55 |
# assert response.json()['results']['bindings'][0]['concat']['value'] == "Firstlast" |
| 56 |
|
| 57 |
|
| 58 |
def test_concat_construct_turtle(): |
| 59 |
# expected to return turtle |
| 60 |
response = endpoint.post( |
| 61 |
"/", |
| 62 |
data="query=" + label_construct, |
| 63 |
headers={"accept": "application/json"}, |
| 64 |
) |
| 65 |
assert response.status_code == 200 |
| 66 |
# assert response.json()['results']['bindings'][0]['concat']['value'] == "Firstlast" |
| 67 |
|
| 68 |
|
| 69 |
def test_concat_construct_xml(): |
| 70 |
# expected to return turtle |
| 71 |
response = endpoint.post( |
| 72 |
"/", |
| 73 |
data="query=" + label_construct, |
| 74 |
headers={"accept": "application/xml"}, |
| 75 |
) |
| 76 |
assert response.status_code == 200 |
| 77 |
|
| 78 |
|
| 79 |
def test_bad_request(): |
| 80 |
response = endpoint.get("/?query=figarofigarofigaro", headers={"accept": "application/json"}) |
| 81 |
assert response.status_code == 400 |
| 82 |
|
| 83 |
|
| 84 |
label_select = """PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 85 |
SELECT ?label WHERE { |
| 86 |
?s rdfs:label ?label . |
| 87 |
}""" |
| 88 |
|
| 89 |
|
| 90 |
label_construct = """PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 91 |
CONSTRUCT { |
| 92 |
<http://test> <http://label> ?label . |
| 93 |
} WHERE { |
| 94 |
?s rdfs:label ?label . |
| 95 |
}""" |