Skip to content

Commit

Permalink
Merge branch 'dev_onto_clean' of github.com:mboudet/flaskomics into d…
Browse files Browse the repository at this point in the history
…ev_onto_clean
  • Loading branch information
mboudet committed Jun 24, 2022
2 parents b600618 + 41480ad commit 9d396a5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 27 deletions.
12 changes: 8 additions & 4 deletions askomics/api/ontology.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import traceback
import sys
from askomics.api.auth import api_auth, login_required
from askomics.api.auth import api_auth
from askomics.libaskomics.OntologyManager import OntologyManager

from flask import (Blueprint, current_app, jsonify, request, session)
Expand All @@ -10,7 +10,6 @@

@onto_bp.route("/api/ontology/<short_ontology>/autocomplete", methods=["GET"])
@api_auth
@login_required
def autocomplete(short_ontology):
"""Get the default sparql query
Expand All @@ -19,8 +18,13 @@ def autocomplete(short_ontology):
json
"""

if "user" not in session and current_app.iniconfig.getboolean("askomics", "protect_public"):
return jsonify({
"error": True,
"errorMessage": "Ontology {} not found".format(short_ontology),
"results": []
}), 401
try:
# Disk space
om = OntologyManager(current_app, session)
ontology = om.get_ontology(short_name=short_ontology)
if not ontology:
Expand All @@ -45,7 +49,7 @@ def autocomplete(short_ontology):
"error": True,
"errorMessage": str(e),
"results": []
}), 404
}), 500

return jsonify({
"error": False,
Expand Down
7 changes: 4 additions & 3 deletions config/askomics.ini.template
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ ldap_mail_attribute = mail
#ldap_password_reset_link =
#ldap_account_link =

# Max results returned for autocompletion
autocomplete_max_results = 10


[triplestore]
# name of the triplestore, can be virtuoso or fuseki
triplestore = virtuoso
Expand Down Expand Up @@ -128,9 +132,6 @@ preview_limit = 25
# All queries are launched on all graphes (speedup queries)
single_tenant=False

# Max results returned for autocompletion
autocomplete_max_results = 10

[federation]
# Query engine can be corese or fedx
#query_engine = corese
Expand Down
3 changes: 3 additions & 0 deletions config/askomics.test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ ldap_surname_attribute = sn
ldap_mail_attribute = mail
#ldap_password_reset_link =
#ldap_account_link =
autocomplete_max_results = 20

[triplestore]
# name of the triplestore, can be virtuoso or fuseki
Expand Down Expand Up @@ -132,3 +133,5 @@ local_endpoint=http://askomics-host:8891/sparql
# Sentry dsn to report python and js errors in a sentry instance
# server_dsn = https://00000000000000000000000000000000@exemple.org/1
# frontend_dsn = https://00000000000000000000000000000000@exemple.org/2

# Max results returned for autocompletion
25 changes: 13 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def upload_file_url(self, file_url):
files.download_url(file_url, "1")
return files.date

def integrate_file(self, info, public=False, set_graph=False):
def integrate_file(self, info, public=False, set_graph=False, graph=""):
"""Summary
Parameters
Expand All @@ -310,6 +310,9 @@ def integrate_file(self, info, public=False, set_graph=False):
files_handler = FilesHandler(self.app, self.session)
files_handler.handle_files([info["id"], ])

# TODO: Fix this. Why do we need the virtuoso url?
graph = graph or "http://virtuoso:8890/sparql"

for file in files_handler.files:

dataset_info = {
Expand All @@ -321,7 +324,7 @@ def integrate_file(self, info, public=False, set_graph=False):
}

dataset = Dataset(self.app, self.session, dataset_info)
dataset.save_in_db("http://virtuoso:8890/sparql", set_graph=set_graph)
dataset.save_in_db(graph, set_graph=set_graph)

if file.type == "csv/tsv":
file.integrate(dataset.id, info["columns_type"], public=public)
Expand Down Expand Up @@ -463,17 +466,15 @@ def upload_and_integrate_ontology(self):
# integrate
int_ontology = self.integrate_file({
"id": 1,
})
}, set_graph=True, graph="http://localhost:8891/sparql-auth")

return {
"ontology": {
"upload": up_ontology,
"timestamp": int_ontology["timestamp"],
"start": int_ontology["start"],
"end": int_ontology["end"],
"graph": int_ontology["graph"],
"endpoint": int_ontology["endpoint"]
},
"upload": up_ontology,
"timestamp": int_ontology["timestamp"],
"start": int_ontology["start"],
"end": int_ontology["end"],
"graph": int_ontology["graph"],
"endpoint": int_ontology["endpoint"]
}

def create_result(self, has_form=False):
Expand Down Expand Up @@ -623,7 +624,7 @@ def create_ontology(self):
"""Create ontology"""
data = self.upload_and_integrate_ontology()
om = OntologyManager(self.app, self.session)
om.add_ontology("AgrO ontology", "http://purl.obolibrary.org/obo/agro.owl", "AGRO", 1, data["graph"], "local")
om.add_ontology("AgrO ontology", "http://purl.obolibrary.org/obo/agro.owl", "AGRO", 1, data["graph"], data['endpoint'], "local")
return data["graph"], data["endpoint"]

@staticmethod
Expand Down
8 changes: 4 additions & 4 deletions tests/test_api_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,9 @@ def test_add_ontology(self, client):
assert response.status_code == 401

client.log_user("jdoe")
data = client.upload_and_integrate_ontology()
graph = data["graph"]
endpoint = data["endpoint"]
graph_data = client.upload_and_integrate_ontology()
graph = graph_data["graph"]
endpoint = graph_data["endpoint"]

response = client.client.post('/api/admin/addontology', json=data)

Expand All @@ -660,7 +660,7 @@ def test_add_ontology(self, client):
"id": 1,
"name": "AgrO ontology",
"uri": "http://purl.obolibrary.org/obo/agro.owl",
"short_name": "OBO",
"short_name": "AGRO",
"type": "local",
"dataset_id": 1,
"dataset_name": "agro_min.ttl",
Expand Down
30 changes: 26 additions & 4 deletions tests/test_api_ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
class TestApiOntology(AskomicsTestCase):
"""Test AskOmics API /api/ontology/<someting>"""

def test_local_autocompletion_protected(self, client):
""" Test autocompletion on missing ontology"""
query = "blabla"
client.set_config("askomics", "protect_public", "true")
response = client.client.get('/api/ontology/AGRO/autocomplete?q={}'.format(query))

assert response.status_code == 401
assert len(response.json["results"]) == 0

def test_local_autocompletion_missing_ontology(self, client):
""" Test autocompletion on missing ontology"""
query = "blabla"
response = client.client.get('/api/ontology/AGRO/autocomplete?q={}'.format(query))

assert response.status_code == 404
assert len(response.json["results"]) == 0

def test_local_autocompletion(self, client):
"""test /api/ontology/AGRO/autocomplete route"""
client.create_two_users()
Expand Down Expand Up @@ -31,12 +48,17 @@ def test_local_autocompletion(self, client):
"no-till",
"puddling process",
"mulch-till",
"ridge-till"
"ridge-till",
"strip-till",
"aerial application"
]

assert response.status_code == 200
assert len(response.json["results"]) == 10
assert response.json["results"] == expected
assert len(response.json["results"]) == 12

# SPARQL order is not reliable, so we make sure to return everything
# If it fails, skip this
assert self.equal_objects(response.json["results"], expected)

query = "irrigation"
response = client.client.get('/api/ontology/AGRO/autocomplete?q={}'.format(query))
Expand All @@ -48,4 +70,4 @@ def test_local_autocompletion(self, client):

assert response.status_code == 200
assert len(response.json["results"]) == 2
assert response.json["results"] == expected
assert self.equal_objects(response.json["results"], expected)

0 comments on commit 9d396a5

Please sign in to comment.