Skip to content

Commit 96b2ca0

Browse files
de-codeHazalCiplak
andauthored
Implement evaluation content endpoint by delegating to provider (#406)
* Implement evaluation content endpoint by delegating to provider * Rename test class to align with naming conventions * Add error handling for evaluation content retrieval and update provider method signature * Added test_should_return_none_for_invalid_evaluation_id * implemented test_should_return_evaluation_content_for_valid_evaluation_id --------- Co-authored-by: HazalCiplak <hazalciplak@gmail.com>
1 parent 08cd7ae commit 96b2ca0

File tree

6 files changed

+87
-4
lines changed

6 files changed

+87
-4
lines changed

data_hub_api/docmaps/v2/api_input_typing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{
88
'hypothesis_id': str,
99
'annotation_created_timestamp': datetime,
10+
'annotation_content': str,
1011
'tags': list,
1112
'uri': str,
1213
'source_version': str,

data_hub_api/docmaps/v2/api_router.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ def get_enhanced_preprints_docmaps_by_manuscript_id_by_publisher_elife(manuscrip
3434

3535
@router.get("/v2/evaluation/get-by-evaluation-id", response_class=HTMLResponse)
3636
def get_evaluation_text_by_evaluation_id(evaluation_id: str):
37-
evaluation_text = 'dummy evaluation text for evaluation_id: ' + evaluation_id
38-
return evaluation_text
37+
evaluation_content = docmaps_provider.get_evaluation_content_by_id(evaluation_id)
38+
if not evaluation_content:
39+
raise HTTPException(
40+
status_code=404,
41+
detail=f"No evaluation content available for evaluation_id: {evaluation_id}"
42+
)
43+
return evaluation_content
3944

4045
return router

data_hub_api/docmaps/v2/provider.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,16 @@ def iter_docmaps_index_json_stream(self):
8787
LOGGER.exception('Error while streaming docmaps index as JSON: %s', e)
8888
raise
8989
LOGGER.info('Finished streaming docmaps index as JSON.')
90+
91+
def get_evaluation_content_by_id(self, evaluation_id: str) -> Optional[str]:
92+
LOGGER.debug('Getting evaluation content for evaluation_id: %s', evaluation_id)
93+
bq_result_list = self._query_results_cache.get_or_load(
94+
load_fn=self._load_query_results_from_bq
95+
)
96+
LOGGER.debug('bq_result_list: %r', bq_result_list)
97+
for bq_result in bq_result_list:
98+
for manuscript_version in bq_result.get('manuscript_versions', []):
99+
for evaluation in manuscript_version.get('evaluations', []):
100+
if evaluation['hypothesis_id'] == evaluation_id:
101+
return evaluation['annotation_content']
102+
return None

tests/unit_tests/docmaps/v2/api_router_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
PREPRINT_DOI = '10.1101/doi1'
1111
MANUSCRIPT_ID = 'manuscript_id_1'
12+
EVALUATION_ID_1 = 'evaluation_content_id_1'
13+
EVALUATION_CONTENT_1 = 'evaluation content for evaluation_content_id_1'
1214

1315

1416
@pytest.fixture(name='docmaps_provider_mock')
@@ -69,3 +71,32 @@ def test_should_return_docmap_from_provider_by_publisher_for_individual_manuscri
6971
)
7072
assert response.status_code == 200
7173
assert response.json() == article_docmap_list[0]
74+
75+
def test_should_return_not_found_status_code_for_invalid_evaluation_id(
76+
self,
77+
docmaps_provider_mock: MagicMock
78+
):
79+
docmaps_provider_mock.get_evaluation_content_by_id.return_value = None
80+
client = create_test_client(docmaps_provider_mock)
81+
response = client.get(
82+
'/v2/evaluation/get-by-evaluation-id',
83+
params={'evaluation_id': 'non_existent_evaluation_id_1'}
84+
)
85+
assert response.status_code == 404
86+
87+
def test_should_return_evaluation_content_from_provider(
88+
self,
89+
docmaps_provider_mock: MagicMock
90+
):
91+
docmaps_provider_mock.get_evaluation_content_by_id.return_value = EVALUATION_CONTENT_1
92+
client = create_test_client(docmaps_provider_mock)
93+
response = client.get(
94+
'/v2/evaluation/get-by-evaluation-id',
95+
params={'evaluation_id': EVALUATION_ID_1}
96+
)
97+
docmaps_provider_mock.get_evaluation_content_by_id.assert_called_with(
98+
EVALUATION_ID_1
99+
)
100+
assert response.status_code == 200
101+
assert response.text == EVALUATION_CONTENT_1
102+
assert response.headers['content-type'] == 'text/html; charset=utf-8'

tests/unit_tests/docmaps/v2/provider_test.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
get_docmap_item_for_query_result_item,
1212
DocmapsProvider
1313
)
14-
from tests.unit_tests.docmaps.v2.test_data import DOCMAPS_QUERY_RESULT_ITEM_1
14+
from tests.unit_tests.docmaps.v2.test_data import (
15+
DOCMAPS_QUERY_RESULT_EVALUATION_1,
16+
DOCMAPS_QUERY_RESULT_ITEM_1,
17+
MANUSCRIPT_VERSION_1
18+
)
1519

1620

1721
@pytest.fixture(name='iter_dict_from_bq_query_mock', autouse=True)
@@ -26,7 +30,7 @@ def _get_docmaps_index_dict(provider: DocmapsProvider) -> dict:
2630
)
2731

2832

29-
class TestEnhancedPreprintsDocmapsProvider:
33+
class TestDocmapsProvider:
3034
def test_should_create_index_with_non_empty_docmaps(
3135
self,
3236
iter_dict_from_bq_query_mock: MagicMock
@@ -55,3 +59,27 @@ def test_should_cache_docmaps_query_results(
5559
assert docmaps_index['docmaps'] == [
5660
get_docmap_item_for_query_result_item(cast(ApiInput, DOCMAPS_QUERY_RESULT_ITEM_1))
5761
]
62+
63+
def test_should_return_none_for_invalid_evaluation_id(
64+
self,
65+
iter_dict_from_bq_query_mock: MagicMock
66+
):
67+
iter_dict_from_bq_query_mock.return_value = []
68+
docmaps_provider = DocmapsProvider()
69+
assert docmaps_provider.get_evaluation_content_by_id('not_found_id_1') is None
70+
71+
def test_should_return_evaluation_content_for_valid_evaluation_id(
72+
self,
73+
iter_dict_from_bq_query_mock: MagicMock
74+
):
75+
iter_dict_from_bq_query_mock.return_value = [{
76+
**DOCMAPS_QUERY_RESULT_ITEM_1,
77+
'manuscript_versions': [{
78+
**MANUSCRIPT_VERSION_1, # type: ignore
79+
'evaluations': [DOCMAPS_QUERY_RESULT_EVALUATION_1]
80+
}]
81+
}]
82+
docmaps_provider = DocmapsProvider()
83+
assert docmaps_provider.get_evaluation_content_by_id(
84+
DOCMAPS_QUERY_RESULT_EVALUATION_1['hypothesis_id']
85+
) == DOCMAPS_QUERY_RESULT_EVALUATION_1['annotation_content']

tests/unit_tests/docmaps/v2/test_data.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,13 @@
231231
ANNOTATION_CREATED_TIMESTAMP_2 = datetime.fromisoformat('2021-01-01T01:02:03+00:00')
232232
ANNOTATION_CREATED_TIMESTAMP_3 = datetime.fromisoformat('2022-01-01T01:02:03+00:00')
233233

234+
ANNOTATION_CONTENT_1 = 'annotation_content_1'
235+
ANNOTATION_CONTENT_2 = 'annotation_content_2'
236+
234237
DOCMAPS_QUERY_RESULT_EVALUATION_1: ApiEvaluationInput = {
235238
'hypothesis_id': HYPOTHESIS_ID_1,
236239
'annotation_created_timestamp': ANNOTATION_CREATED_TIMESTAMP_1,
240+
'annotation_content': ANNOTATION_CONTENT_1,
237241
'tags': [],
238242
'uri': PREPRINT_LINK_1,
239243
'source_version': PREPRINT_VERSION_1,
@@ -243,6 +247,7 @@
243247
DOCMAPS_QUERY_RESULT_EVALUATION_2: ApiEvaluationInput = {
244248
'hypothesis_id': HYPOTHESIS_ID_2,
245249
'annotation_created_timestamp': ANNOTATION_CREATED_TIMESTAMP_2,
250+
'annotation_content': ANNOTATION_CONTENT_2,
246251
'tags': [],
247252
'uri': PREPRINT_LINK_2,
248253
'source_version': PREPRINT_VERSION_2,

0 commit comments

Comments
 (0)