Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vemonet committed Mar 18, 2023
1 parent ea8c4ef commit 5b1b4f1
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ __pycache__/
.ruff_cache/
.coverage
coverage.xml
htmlcov/

<tempfile.*

# Virtual envs
.env
Expand Down
4 changes: 2 additions & 2 deletions example/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def most_similar(query_results, ctx, part, eval_part):
title="SPARQL endpoint for RDFLib graph",
description="A SPARQL endpoint to serve machine learning models, or any other logic implemented in Python. \n[Source code](https://github.com/vemonet/rdflib-endpoint)",
version="0.1.0",
path="/sparql",
public_url="https://service.openpredict.137.120.31.102.nip.io/sparql",
path="/",
public_url="https://your-website-url/",
cors_enabled=True,
example_query=example_query,
)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ post-install-commands = [
[tool.hatch.envs.default.scripts]
test = "pytest {args}"
cov = "test --cov src {args}"
cov-report = "test --cov src --cov-report html {args} && python -m http.server 3000 --directory ./htmlcov"
dev = "uvicorn example.app.main:app --reload {args}"
fmt = [
"black src tests example",
Expand Down
2 changes: 2 additions & 0 deletions src/rdflib_endpoint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def run_convert(files: List[str], output: str, store: str = "default") -> None:
out_format = "xml"
elif output.endswith(".json") or output.endswith(".jsonld"):
out_format = "json-ld"
elif output.endswith(".trig"):
out_format = "trig"

g.serialize(output, format=out_format)

Expand Down
4 changes: 2 additions & 2 deletions tests/resources/custom_eval_todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ def test_custom_eval():
# )
# eval_endpoint = TestClient(eval_app)

response = eval_endpoint.get("/sparql?query=" + select_parent, headers={"accept": "application/json"})
response = eval_endpoint.get("/?query=" + select_parent, headers={"accept": "application/json"})
print(response.json())
assert response.status_code == 200
print(response.json()["results"]["bindings"])
assert str(response.json()["results"]["bindings"][0]["s"]["value"]) == "http://alice"

response = eval_endpoint.post("/sparql", data="query=" + select_parent, headers={"accept": "application/json"})
response = eval_endpoint.post("/", data="query=" + select_parent, headers={"accept": "application/json"})
assert response.status_code == 200
assert str(response.json()["results"]["bindings"][0]["s"]["value"]) == "http://alice"

Expand Down
22 changes: 21 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import glob
import os
import tempfile
import time
from multiprocessing import Process

Expand Down Expand Up @@ -33,7 +36,7 @@ def server(scope="module"):

def test_query_cli(server):
resp = requests.get(
"http://localhost:8000/sparql?query=" + select_all_query,
"http://localhost:8000/?query=" + select_all_query,
headers={"accept": "application/json"},
timeout=600,
)
Expand All @@ -43,3 +46,20 @@ def test_query_cli(server):
select_all_query = """SELECT * WHERE {
?s ?p ?o .
}"""


def test_convert():
with tempfile.NamedTemporaryFile(delete=True) as tmp_file:
runner = CliRunner()
result = runner.invoke(
cli,
["convert", pkg_resources.resource_filename("tests", "resources/test2.ttl"), "--output", str(tmp_file)],
)
assert result.exit_code == 0
with open(str(tmp_file)) as file:
content = file.read()
assert "ns0:s" in content

# Fix issue with python creating unnecessary temp files on disk
for f in glob.glob("<tempfile._TemporaryFileWrapper*"):
os.remove(f)
31 changes: 13 additions & 18 deletions tests/test_example_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@


def test_service_description():
response = endpoint.get("/sparql", headers={"accept": "text/turtle"})
response = endpoint.get("/", headers={"accept": "text/turtle"})
# print(response.text.strip())
assert response.status_code == 200
assert response.text.strip() == service_description

response = endpoint.post("/sparql", headers={"accept": "text/turtle"})
response = endpoint.post("/", headers={"accept": "text/turtle"})
assert response.status_code == 200
assert response.text.strip() == service_description

# Check for application/xml
response = endpoint.post("/sparql", headers={"accept": "application/xml"})
response = endpoint.post("/", headers={"accept": "application/xml"})
assert response.status_code == 200


def test_custom_concat():
response = endpoint.get("/sparql?query=" + custom_concat_query, headers={"accept": "application/json"})
response = endpoint.get("/?query=" + custom_concat_query, headers={"accept": "application/json"})
# print(response.json())
assert response.status_code == 200
assert response.json()["results"]["bindings"][0]["concat"]["value"] == "Firstlast"

response = endpoint.post(
"/sparql",
"/",
data="query=" + custom_concat_query,
headers={"accept": "application/json"},
)
Expand All @@ -36,15 +36,10 @@ def test_custom_concat():


def test_bad_request():
response = endpoint.get("/sparql?query=figarofigarofigaro", headers={"accept": "application/json"})
response = endpoint.get("/?query=figarofigarofigaro", headers={"accept": "application/json"})
assert response.status_code == 400


def test_redirect():
response = endpoint.get("/")
assert response.status_code == 200


custom_concat_query = """PREFIX myfunctions: <https://w3id.org/um/sparql-functions/>
SELECT ?concat ?concatLength WHERE {
BIND("First" AS ?first)
Expand All @@ -56,20 +51,20 @@ def test_redirect():
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sd: <http://www.w3.org/ns/sparql-service-description#> .
<https://service.openpredict.137.120.31.102.nip.io/sparql> a sd:Service ;
<https://w3id.org/um/openpredict/most_similar> a sd:Function .
<https://w3id.org/um/sparql-functions/custom_concat> a sd:Function .
<https://your-website-url/> a sd:Service ;
rdfs:label "SPARQL endpoint for RDFLib graph" ;
dc:description "A SPARQL endpoint to serve machine learning models, or any other logic implemented in Python. [Source code](https://github.com/vemonet/rdflib-endpoint)" ;
sd:defaultDataset [ a sd:Dataset ;
sd:defaultGraph [ a sd:Graph ] ] ;
sd:defaultEntailmentRegime ent:RDFS ;
sd:endpoint <https://service.openpredict.137.120.31.102.nip.io/sparql> ;
sd:endpoint <https://your-website-url/> ;
sd:extensionFunction <https://w3id.org/um/openpredict/most_similar>,
<https://w3id.org/um/sparql-functions/custom_concat> ;
sd:feature sd:DereferencesURIs ;
sd:resultFormat <http://www.w3.org/ns/formats/SPARQL_Results_CSV>,
<http://www.w3.org/ns/formats/SPARQL_Results_JSON> ;
sd:supportedLanguage sd:SPARQL11Query .
<https://w3id.org/um/openpredict/most_similar> a sd:Function .
<https://w3id.org/um/sparql-functions/custom_concat> a sd:Function ."""
sd:supportedLanguage sd:SPARQL11Query ."""
27 changes: 11 additions & 16 deletions tests/test_oxrdflib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,53 @@


def test_service_description():
response = endpoint.get("/sparql", headers={"accept": "text/turtle"})
response = endpoint.get("/", headers={"accept": "text/turtle"})
print(response.text.strip())
assert response.status_code == 200
# assert response.text.strip() == service_description

response = endpoint.post("/sparql", headers={"accept": "text/turtle"})
response = endpoint.post("/", headers={"accept": "text/turtle"})
assert response.status_code == 200
# assert response.text.strip() == service_description

# Check for application/xml
response = endpoint.post("/sparql", headers={"accept": "application/xml"})
response = endpoint.post("/", headers={"accept": "application/xml"})
assert response.status_code == 200


def test_custom_concat_json():
response = endpoint.get("/sparql?query=" + urllib.parse.quote(label_select), headers={"accept": "application/json"})
response = endpoint.get("/?query=" + urllib.parse.quote(label_select), headers={"accept": "application/json"})
# print(response.json())
assert response.status_code == 200
assert response.json()["results"]["bindings"][0]["label"]["value"] == "test value"

response = endpoint.post("/sparql", data="query=" + label_select, headers={"accept": "application/json"})
response = endpoint.post("/", data="query=" + label_select, headers={"accept": "application/json"})
assert response.status_code == 200
assert response.json()["results"]["bindings"][0]["label"]["value"] == "test value"


def test_select_noaccept_xml():
response = endpoint.post("/sparql", data="query=" + label_select)
response = endpoint.post("/", data="query=" + label_select)
assert response.status_code == 200
# assert response.json()['results']['bindings'][0]['concat']['value'] == "Firstlast"


def test_select_csv():
response = endpoint.post("/sparql", data="query=" + label_select, headers={"accept": "text/csv"})
response = endpoint.post("/", data="query=" + label_select, headers={"accept": "text/csv"})
assert response.status_code == 200
# assert response.json()['results']['bindings'][0]['concat']['value'] == "Firstlast"


def test_fail_select_turtle():
response = endpoint.post("/sparql", data="query=" + label_select, headers={"accept": "text/turtle"})
response = endpoint.post("/", data="query=" + label_select, headers={"accept": "text/turtle"})
assert response.status_code == 422
# assert response.json()['results']['bindings'][0]['concat']['value'] == "Firstlast"


def test_concat_construct_turtle():
# expected to return turtle
response = endpoint.post(
"/sparql",
"/",
data="query=" + label_construct,
headers={"accept": "application/json"},
)
Expand All @@ -73,23 +73,18 @@ def test_concat_construct_turtle():
def test_concat_construct_xml():
# expected to return turtle
response = endpoint.post(
"/sparql",
"/",
data="query=" + label_construct,
headers={"accept": "application/xml"},
)
assert response.status_code == 200


def test_bad_request():
response = endpoint.get("/sparql?query=figarofigarofigaro", headers={"accept": "application/json"})
response = endpoint.get("/?query=figarofigarofigaro", headers={"accept": "application/json"})
assert response.status_code == 400


def test_redirect():
response = endpoint.get("/")
assert response.status_code == 200


label_select = """PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?label WHERE {
?s rdfs:label ?label .
Expand Down
36 changes: 20 additions & 16 deletions tests/test_rdflib_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,53 @@


def test_service_description():
response = endpoint.get("/sparql", headers={"accept": "text/turtle"})
response = endpoint.get("/", headers={"accept": "text/turtle"})
print(response.text.strip())
assert response.status_code == 200
assert response.text.strip() == service_description

response = endpoint.post("/sparql", headers={"accept": "text/turtle"})
response = endpoint.post("/", headers={"accept": "text/turtle"})
assert response.status_code == 200
assert response.text.strip() == service_description

# Check for application/xml
response = endpoint.post("/sparql", headers={"accept": "application/xml"})
response = endpoint.post("/", headers={"accept": "application/xml"})
assert response.status_code == 200


def test_custom_concat_json():
response = endpoint.get("/sparql?query=" + concat_select, headers={"accept": "application/json"})
response = endpoint.get("/?query=" + concat_select, headers={"accept": "application/json"})
# print(response.json())
assert response.status_code == 200
assert response.json()["results"]["bindings"][0]["concat"]["value"] == "Firstlast"

response = endpoint.post("/sparql", data="query=" + concat_select, headers={"accept": "application/json"})
response = endpoint.post("/", data="query=" + concat_select, headers={"accept": "application/json"})
assert response.status_code == 200
assert response.json()["results"]["bindings"][0]["concat"]["value"] == "Firstlast"


def test_select_noaccept_xml():
response = endpoint.post("/sparql", data="query=" + concat_select)
response = endpoint.post("/", data="query=" + concat_select)
assert response.status_code == 200
# assert response.json()['results']['bindings'][0]['concat']['value'] == "Firstlast"


def test_select_csv():
response = endpoint.post("/sparql", data="query=" + concat_select, headers={"accept": "text/csv"})
response = endpoint.post("/", data="query=" + concat_select, headers={"accept": "text/csv"})
assert response.status_code == 200
# assert response.json()['results']['bindings'][0]['concat']['value'] == "Firstlast"


def test_fail_select_turtle():
response = endpoint.post("/sparql", data="query=" + concat_select, headers={"accept": "text/turtle"})
response = endpoint.post("/", data="query=" + concat_select, headers={"accept": "text/turtle"})
assert response.status_code == 422
# assert response.json()['results']['bindings'][0]['concat']['value'] == "Firstlast"


def test_concat_construct_turtle():
# expected to return turtle
response = endpoint.post(
"/sparql",
"/",
data="query=" + custom_concat_construct,
headers={"accept": "application/json"},
)
Expand All @@ -70,21 +70,25 @@ def test_concat_construct_turtle():
def test_concat_construct_xml():
# expected to return turtle
response = endpoint.post(
"/sparql",
"/",
data="query=" + custom_concat_construct,
headers={"accept": "application/xml"},
)
assert response.status_code == 200


def test_bad_request():
response = endpoint.get("/sparql?query=figarofigarofigaro", headers={"accept": "application/json"})
assert response.status_code == 400
def test_yasgui():
# expected to return turtle
response = endpoint.get(
"/",
headers={"accept": "text/html"},
)
assert response.status_code == 200


def test_redirect():
response = endpoint.get("/")
assert response.status_code == 200
def test_bad_request():
response = endpoint.get("/?query=figarofigarofigaro", headers={"accept": "application/json"})
assert response.status_code == 400


concat_select = """PREFIX myfunctions: <https://w3id.org/um/sparql-functions/>
Expand Down
2 changes: 1 addition & 1 deletion tests/test_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def server(scope="module"):

def test_query_serve(server):
resp = requests.get(
"http://localhost:8000/sparql?query=" + select_all_query,
"http://localhost:8000/?query=" + select_all_query,
headers={"accept": "application/json"},
timeout=600,
)
Expand Down

0 comments on commit 5b1b4f1

Please sign in to comment.