Skip to content

Commit

Permalink
using ontology_helper to check against POSIXPath before download (#…
Browse files Browse the repository at this point in the history
…197)

* using ontology_helper and check against FilreReference

* no more TODO

* checking ontology availability before posixPath check

* test with no ontology

* adapted test

* adapted test

* test with extra annotations

* proper fixture

* is_ontology_available property and TODO

* adapted test

* resetting ontology availability

* test with simple regex
  • Loading branch information
burnout87 authored Aug 29, 2024
1 parent eaa45ce commit 239a63c
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 4 deletions.
16 changes: 14 additions & 2 deletions nb2workflow/nbadapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class ModOntology(Ontology):
def __init__(self, ontology_path):
super().__init__(ontology_path)
self.lock = Lock()
self._is_ontology_available = True

def get_datatype_restriction(self, param_uri):
self.lock.acquire()
Expand All @@ -74,6 +75,11 @@ def get_datatype_restriction(self, param_uri):
logger.warning(f'Unknown datatype for owl_uri {param_uri}')
return dt

@property
def is_ontology_available(self):
# TODO will be developed properly in the ontology_helper
return self._is_ontology_available

ontology = ModOntology(oda_ontology_path)
oda_prefix = str([x[1] for x in ontology.g.namespaces() if x[0] == 'oda'][0])

Expand Down Expand Up @@ -813,9 +819,15 @@ def download_file(self, file_url, tmpdir):
def handle_url_params(self, parameters, tmpdir, context={}):
adapted_parameters = copy.deepcopy(parameters)
exceptions = []
posix_path_with_annotations_pattern = re.compile(rf"^{re.escape(oda_prefix)}.*_POSIXPath_")
for input_par_name, input_par_obj in self.input_parameters.items():
# TODO use oda_api.ontology_helper
if input_par_obj['owl_type'] == f"{oda_prefix}POSIXPath":
if ontology.is_ontology_available:
parameter_hierarchy = ontology.get_parameter_hierarchy(input_par_obj['owl_type'])
is_posix_path = f"{oda_prefix}POSIXPath" in parameter_hierarchy
else:
is_posix_path = f"{oda_prefix}POSIXPath" == input_par_obj['owl_type'] or \
posix_path_with_annotations_pattern.match(input_par_obj['owl_type']) is not None
if is_posix_path:
arg_par_value = parameters.get(input_par_name, None)
if arg_par_value is None:
arg_par_value = input_par_obj['default_value']
Expand Down
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

import nb2workflow.service
from importlib import reload
import rdflib as rdf

from oda_api.ontology_helper import ODA, ODAS

@pytest.fixture
def test_notebook():
Expand Down Expand Up @@ -51,6 +54,7 @@ def app(test_notebook):
app = nb2workflow.service.app
app.notebook_adapters = nb2workflow.nbadapter.find_notebooks(test_notebook)
nb2workflow.service.setup_routes(app)
nb2workflow.nbadapter.ontology._is_ontology_available = True
print("creating app")
return app

Expand All @@ -63,10 +67,19 @@ def app_low_download_limit():
for nb, nba_obj in app_low_download_limit.notebook_adapters.items():
nba_obj.max_download_size = 1
nb2workflow.service.setup_routes(app_low_download_limit)
nb2workflow.nbadapter.ontology._is_ontology_available = True
print("creating app with low limit on the download of files")
return app_low_download_limit


# TODO improve this, as it requires changes also in the oda_api
@pytest.fixture
def app_not_available_ontology():
nb2workflow.nbadapter.ontology._is_ontology_available = False
yield



def kill_child_processes(parent_pid, sig=signal.SIGTERM):
try:
parent = psutil.Process(parent_pid)
Expand Down
14 changes: 13 additions & 1 deletion tests/test_input_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,30 @@ def app():
app = nb2workflow.service.app
app.notebook_adapters = nb2workflow.nbadapter.find_notebooks(testfiles_path)
nb2workflow.service.setup_routes(app)
nb2workflow.nbadapter.ontology._is_ontology_available = True
print("creating app")
return app


def test_posix_download_file(client):
r = client.get('/api/v1.0/get/testposixpath')
assert r.json['output']['output_file_download'] == 'file not downloaded'

def test_posix_download_file_no_ontology(client, app_not_available_ontology):
r = client.get('/api/v1.0/get/testposixpath', query_string={'fits_file_path': 'https://fits.gsfc.nasa.gov/samples/testkeys.fits'})
assert r.json['output']['output_file_download'] == 'file downloaded successfully'

def test_posix_download_file_no_ontology_extra_annotations(client, app_not_available_ontology):
r = client.get('/api/v1.0/get/testposixpath_extra_annotated', query_string={'fits_file_path': 'https://fits.gsfc.nasa.gov/samples/testkeys.fits'})
assert r.json['output']['output_file_download'] == 'file downloaded successfully'

def test_posix_download_file_with_arg(client):
r = client.get('/api/v1.0/get/testposixpath', query_string={'fits_file_path': 'https://fits.gsfc.nasa.gov/samples/testkeys.fits'})
assert r.json['output']['output_file_download'] == 'file downloaded successfully'

def test_posix_download_file_extra_annotations(client):
r = client.get('/api/v1.0/get/testposixpath_extra_annotated', query_string={'fits_file_path': 'https://fits.gsfc.nasa.gov/samples/testkeys.fits'})
assert r.json['output']['output_file_download'] == 'file downloaded successfully'

def test_posix_download_file_with_arg_low_download_limit(client, app_low_download_limit):
r = client.get('/api/v1.0/get/testposixpath', query_string={'fits_file_path': 'https://fits.gsfc.nasa.gov/samples/testkeys.fits'})
assert r.json['output'] == {}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_nbadapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_find_notebooks(caplog):
assert 'Ignoring pattern.' in caplog.text

nbas = find_notebooks(nb_dir)
assert len(nbas) == 8
assert len(nbas) == 9

nbas = find_notebooks(nb_dir, pattern=r'.*bool')
assert len(nbas) == 1
Expand Down
74 changes: 74 additions & 0 deletions tests/testfiles/testposixpath_extra_annotated.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"parameters"
]
},
"outputs": [],
"source": [
"fits_file_path = \"test.fits\" # oda:POSIXPath ; oda:label \"Test file path\" ; oda:description \"Description test file path\" ."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"output_file_download = 'file not downloaded'\n",
"if os.path.exists(fits_file_path) is True:\n",
" output_file_download = 'file downloaded successfully'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"outputs"
]
},
"outputs": [],
"source": [
"output_file_download"
]
}
],
"metadata": {
"interpreter": {
"hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit 239a63c

Please sign in to comment.