From 5b1b4f1d0d47006cb56652bd9cb13142fd47ed98 Mon Sep 17 00:00:00 2001 From: Vincent Emonet Date: Sat, 18 Mar 2023 13:29:55 +0100 Subject: [PATCH] fix tests --- .gitignore | 3 +++ example/app/main.py | 4 ++-- pyproject.toml | 1 + src/rdflib_endpoint/__main__.py | 2 ++ tests/resources/custom_eval_todo.py | 4 ++-- tests/test_cli.py | 22 +++++++++++++++++- tests/test_example_app.py | 31 +++++++++++-------------- tests/test_oxrdflib.py | 27 +++++++++------------- tests/test_rdflib_endpoint.py | 36 ++++++++++++++++------------- tests/test_serve.py | 2 +- 10 files changed, 76 insertions(+), 56 deletions(-) diff --git a/.gitignore b/.gitignore index d2c478c..3c93efe 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,9 @@ __pycache__/ .ruff_cache/ .coverage coverage.xml +htmlcov/ + + 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) diff --git a/tests/resources/custom_eval_todo.py b/tests/resources/custom_eval_todo.py index 944849f..1d2a705 100644 --- a/tests/resources/custom_eval_todo.py +++ b/tests/resources/custom_eval_todo.py @@ -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" diff --git a/tests/test_cli.py b/tests/test_cli.py index 649cdf6..75422a0 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,6 @@ +import glob +import os +import tempfile import time from multiprocessing import Process @@ -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, ) @@ -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(" SELECT ?concat ?concatLength WHERE { BIND("First" AS ?first) @@ -56,20 +51,20 @@ def test_redirect(): @prefix rdfs: . @prefix sd: . - a sd:Service ; + a sd:Function . + + a sd:Function . + + 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 ; + sd:endpoint ; sd:extensionFunction , ; sd:feature sd:DereferencesURIs ; sd:resultFormat , ; - sd:supportedLanguage sd:SPARQL11Query . - - a sd:Function . - - a sd:Function .""" + sd:supportedLanguage sd:SPARQL11Query .""" diff --git a/tests/test_oxrdflib.py b/tests/test_oxrdflib.py index 0b4082c..7317e37 100644 --- a/tests/test_oxrdflib.py +++ b/tests/test_oxrdflib.py @@ -16,45 +16,45 @@ 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" @@ -62,7 +62,7 @@ def test_fail_select_turtle(): def test_concat_construct_turtle(): # expected to return turtle response = endpoint.post( - "/sparql", + "/", data="query=" + label_construct, headers={"accept": "application/json"}, ) @@ -73,7 +73,7 @@ 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"}, ) @@ -81,15 +81,10 @@ def test_concat_construct_xml(): 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: SELECT ?label WHERE { ?s rdfs:label ?label . diff --git a/tests/test_rdflib_endpoint.py b/tests/test_rdflib_endpoint.py index c4eefef..1e305d8 100644 --- a/tests/test_rdflib_endpoint.py +++ b/tests/test_rdflib_endpoint.py @@ -13,45 +13,45 @@ 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" @@ -59,7 +59,7 @@ def test_fail_select_turtle(): def test_concat_construct_turtle(): # expected to return turtle response = endpoint.post( - "/sparql", + "/", data="query=" + custom_concat_construct, headers={"accept": "application/json"}, ) @@ -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: diff --git a/tests/test_serve.py b/tests/test_serve.py index 0c04d59..a69f01b 100644 --- a/tests/test_serve.py +++ b/tests/test_serve.py @@ -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, )