Skip to content

Commit 9d9644a

Browse files
authored
Dom fix (#135)
* added extra converter test for JSON round trip * fixed issue #134 * fixed issue #133 * fixed a bug where water was returned as a valid chemical
1 parent fc0ddfe commit 9d9644a

File tree

11 files changed

+42
-5
lines changed

11 files changed

+42
-5
lines changed

ptmd/api/queries/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ def get_chemicals() -> tuple[Response, int]:
2828
2929
:return: tuple containing a JSON response and a status code
3030
"""
31-
return jsonify({"data": [dict(chemical) for chemical in Chemical.query.filter(Chemical.ptx_code < 997).all()]}), 200
31+
return jsonify({"data": [dict(chemical) for chemical in Chemical.query.filter(
32+
Chemical.ptx_code < 997, Chemical.ptx_code > 0
33+
).all()]}), 200
3234

3335

3436
def get_organisations() -> tuple[Response, int]:

ptmd/api/queries/files/create.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from ptmd.database.queries.timepoints import create_timepoints_hours
1818
from ptmd.api.queries.utils import check_role
1919
from ptmd.database import get_shipped_file
20+
from ptmd.exceptions import TimepointValueError
2021

2122

2223
class CreateGDriveFile:
@@ -95,5 +96,7 @@ def create_gdrive_file() -> tuple[Response, int]:
9596
payload: CreateGDriveFile = CreateGDriveFile()
9697
response: dict = payload.generate_file(user=get_current_user().id)
9798
return jsonify({"data": response}), 200
99+
except TimepointValueError as e:
100+
return jsonify({"message": str(e)}), 400
98101
except Exception as e:
99102
return jsonify({"message": str(e)}), 400

ptmd/api/queries/files/isa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def convert_to_isa(file_id: int) -> tuple[Response, int]:
1515
:return: a tuple containing the response and the status code
1616
"""
1717
try:
18-
response: list[dict] = convert_file_to_isa(file_id)
18+
response: dict = convert_file_to_isa(file_id)[0]
1919
except ValueError as e:
2020
return jsonify({'message': str(e)}), 400
2121
except FileNotFoundError as e:

ptmd/database/queries/timepoints.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
from ptmd.config import session
44
from ptmd.database.models import Timepoint
5+
from ptmd.exceptions import TimepointValueError
56

67

78
def create_timepoints_hours(values: list[int]) -> list[Timepoint]:
@@ -11,6 +12,8 @@ def create_timepoints_hours(values: list[int]) -> list[Timepoint]:
1112
"""
1213
timepoints: list[Timepoint] = []
1314
for i, value in enumerate(values):
15+
if not value or not isinstance(value, int):
16+
raise TimepointValueError
1417
timepoint = Timepoint(value=value, unit='hours', label=f'TP{i + 1}')
1518
session.add(timepoint)
1619
timepoints.append(timepoint)

ptmd/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ class TokenInvalidError(APIError):
3939
def __init__(self) -> None:
4040
""" Constructor """
4141
self.message: str = "Invalid token"
42+
43+
44+
class TimepointValueError(APIError):
45+
""" Exception raised when a timepoint value is invalid """
46+
47+
def __init__(self) -> None:
48+
""" Constructor """
49+
self.message: str = "Timepoint value must be a positive integer"

ptmd/lib/isa/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def convert(self) -> list[dict]:
5555
study: Study = Study(
5656
filename=self.filename,
5757
sources=[self.blank_source],
58-
characteristic_categories=[ORGANISM_OA, SEX_OA, REPLICATE_OA, BOX_OA, POSITION_OA]
58+
characteristic_categories=[ORGANISM_OA, SEX_OA, REPLICATE_OA, BOX_OA, POSITION_OA],
59+
units=[HOURS_OA]
5960
)
6061
self.create_samples(study)
6162
study.factors = list(self.factors.values())

ptmd/lib/isa/ontologies.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" Ontology annotations and sources to create the ISA investigations.
22
"""
3+
from uuid import uuid4
34

45
from isatools.model import OntologyAnnotation, OntologySource
56

@@ -68,7 +69,8 @@
6869
SEX_OA: OntologyAnnotation = OntologyAnnotation(term_source=PATO, term='biological sex', term_accession='PATO:0000047')
6970
EXPOSURE_OA: OntologyAnnotation = OntologyAnnotation(term_source=NCIT, term='Exposure', term_accession='NCIT:C17941')
7071
SAMPLING_OA: OntologyAnnotation = OntologyAnnotation(term_source=NCIT, term='Sampling', term_accession='NCIT:C25662')
71-
HOURS_OA: OntologyAnnotation = OntologyAnnotation(term_source=UO, term='hour', term_accession='UO:0000032')
72+
HOURS_OA: OntologyAnnotation = OntologyAnnotation(term_source=UO, term='hour', term_accession='UO:0000032',
73+
id_=f"#unit/{uuid4()}")
7274
ORGANISM_NA_OA: OntologyAnnotation = OntologyAnnotation(term_source=NCIT, term='N/A', term_accession='NCIT:C48660')
7375
BOX_OA: OntologyAnnotation = OntologyAnnotation(term_source=NCIT, term='Box', term_accession='NCIT:C43178')
7476
POSITION_OA: OntologyAnnotation = OntologyAnnotation(

tests/test_api/test_queries/test_files/test_create.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ptmd.api import app
88
from ptmd.const import ALLOWED_DOSE_VALUES
99
from ptmd.database.models import Timepoint
10+
from ptmd.exceptions import TimepointValueError
1011

1112

1213
class MockGoogleAuth(GoogleAuth):
@@ -101,6 +102,13 @@ def test_create_gdrive_file(self, mock_get_shipped_file,
101102
data=dumps(data))
102103
self.assertEqual(response.json['data']['file_url'], 'a')
103104

105+
mock_timepoints.side_effect = TimepointValueError
106+
data['timepoints'] = [None, 1]
107+
response = client.post('/api/files', headers={'Authorization': f'Bearer {123}', **HEADERS},
108+
data=dumps(data))
109+
self.assertEqual(response.json, {'message': "Timepoint value must be a positive integer"})
110+
self.assertEqual(response.status_code, 400)
111+
104112
@patch('ptmd.api.queries.files.create.get_shipped_file', return_value=False)
105113
def test_create_gdrive_error(self,
106114
mock_get_shipped_file,

tests/test_api/test_queries/test_files/test_isa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_convert_success(self, mock_jwt_verify_flask, mock_jwt_verify_utils, moc
3636
mock_user().id = 1
3737
mock_user().role = 'admin'
3838
with patch('ptmd.api.queries.files.isa.convert_file_to_isa') as mock_convert:
39-
mock_convert.return_value = {'message': 'SUCCESS'}
39+
mock_convert.return_value = [{'message': 'SUCCESS'}]
4040
with app.test_client() as client:
4141
response = client.get('/api/files/1/isa', headers=HEADERS)
4242
self.assertEqual(response.json, {'message': 'SUCCESS'})

tests/test_database/test_queries/test_timepoints.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
from ptmd.database.queries import create_timepoints_hours
6+
from ptmd.exceptions import TimepointValueError
67

78

89
class TestCreateTimepointsHours(TestCase):
@@ -14,3 +15,8 @@ def test_create_timepoints_hours(self, mock_session):
1415
self.assertEqual(mock_session.add.call_count, 2)
1516
self.assertEqual(dict(timepoints[0]), {'value': 1, 'unit': 'hours', 'label': 'TP1', 'files': []})
1617
self.assertEqual(dict(timepoints[1]), {'value': 2, 'unit': 'hours', 'label': 'TP2', 'files': []})
18+
19+
def test_create_timepoint_errors(self):
20+
with self.assertRaises(TimepointValueError) as context:
21+
create_timepoints_hours([None, 1])
22+
self.assertTrue('Timepoint value must be a positive integer' in str(context.exception))

0 commit comments

Comments
 (0)