Debian Codegraph @

tests/resources/custom_eval_todo.py

raw
1 import rdflib
2 from fastapi.testclient import TestClient
3 from rdflib import URIRef
4 from rdflib.namespace import RDF, RDFS
5 from rdflib.plugins.sparql.evaluate import evalBGP
6
7 from rdflib_endpoint import SparqlEndpoint
8
9 # TODO: not used due to bug with FastAPI TestClient when using different apps in the tests
10
11
12 def custom_eval(ctx, part):
13 """Rewrite triple patterns to get super-classes"""
14 if part.name == "BGP":
15 # rewrite triples
16 triples = []
17 for t in part.triples:
18 if t[1] == RDF.type:
19 bnode = rdflib.BNode()
20 triples.append((t[0], t[1], bnode))
21 triples.append((bnode, RDFS.subClassOf, t[2]))
22 else:
23 triples.append(t)
24 # delegate to normal evalBGP
25 return evalBGP(ctx, triples)
26 raise NotImplementedError()
27
28
29 g = rdflib.Graph()
30 g.add((URIRef("http://human"), RDFS.subClassOf, URIRef("http://mammal")))
31 g.add((URIRef("http://alice"), RDF.type, URIRef("http://human")))
32
33 eval_app = SparqlEndpoint(graph=g, custom_eval=custom_eval, functions={})
34 eval_endpoint = TestClient(eval_app)
35
36
37 def test_custom_eval():
38 # eval_app = SparqlEndpoint(
39 # graph=g,
40 # custom_eval=custom_eval,
41 # functions={}
42 # )
43 # eval_endpoint = TestClient(eval_app)
44
45 response = eval_endpoint.get("/?query=" + select_parent, headers={"accept": "application/json"})
46 print(response.json())
47 assert response.status_code == 200
48 print(response.json()["results"]["bindings"])
49 assert str(response.json()["results"]["bindings"][0]["s"]["value"]) == "http://alice"
50
51 response = eval_endpoint.post("/", data="query=" + select_parent, headers={"accept": "application/json"})
52 assert response.status_code == 200
53 assert str(response.json()["results"]["bindings"][0]["s"]["value"]) == "http://alice"
54
55
56 select_parent = """SELECT * WHERE {
57 ?s a <http://mammal> .
58 }"""