From 4b14085d487bcfec84ad14e07c0f746a1cdf7747 Mon Sep 17 00:00:00 2001 From: francastell Date: Tue, 6 Feb 2024 16:34:37 -0500 Subject: [PATCH 1/2] GW-153 publication by geneset id --- src/geneweaver/api/controller/genesets.py | 34 ++++++++++++ tests/controllers/test_genesets.py | 64 ++++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/src/geneweaver/api/controller/genesets.py b/src/geneweaver/api/controller/genesets.py index 60be6ce..0682dd9 100644 --- a/src/geneweaver/api/controller/genesets.py +++ b/src/geneweaver/api/controller/genesets.py @@ -10,6 +10,7 @@ from geneweaver.api import dependencies as deps from geneweaver.api.schemas.auth import UserInternal from geneweaver.api.services import geneset as genset_service +from geneweaver.api.services import publications as publication_service from geneweaver.core.enum import GeneIdentifier from geneweaver.db import geneset as db_geneset from typing_extensions import Annotated @@ -122,3 +123,36 @@ def get_geneset_metadata( raise HTTPException(status_code=500, detail=api_message.UNEXPECTED_ERROR) return response + + +@router.get("/{geneset_id}/publication") +def get_publication_for_geneset( + geneset_id: Annotated[ + int, Path(format="int64", minimum=0, maxiumum=9223372036854775807) + ], + user: UserInternal = Security(deps.full_user), + cursor: Optional[deps.Cursor] = Depends(deps.cursor), +) -> dict: + """Get the publication associated with the geneset.""" + geneset_resp = genset_service.get_geneset_metadata(cursor, geneset_id, user, True) + + if "error" in geneset_resp: + if geneset_resp.get("message") == api_message.ACCESS_FORBIDDEN: + raise HTTPException(status_code=403, detail=api_message.ACCESS_FORBIDDEN) + else: + raise HTTPException(status_code=500, detail=api_message.UNEXPECTED_ERROR) + + geneset = geneset_resp.get("geneset") + if geneset is None: + raise HTTPException(status_code=404, detail=api_message.RECORD_NOT_FOUND_ERROR) + + pub_id = geneset.get("publication_id") + if pub_id is None: + raise HTTPException(status_code=404, detail=api_message.RECORD_NOT_FOUND_ERROR) + + pub_resp = publication_service.get_publication(cursor, pub_id) + + if pub_resp.get("publication") is None: + raise HTTPException(status_code=404, detail=api_message.RECORD_NOT_FOUND_ERROR) + + return pub_resp diff --git a/tests/controllers/test_genesets.py b/tests/controllers/test_genesets.py index 16d18f8..f8d718e 100644 --- a/tests/controllers/test_genesets.py +++ b/tests/controllers/test_genesets.py @@ -3,10 +3,12 @@ import pytest -from tests.data import test_geneset_data +from tests.data import test_geneset_data, test_publication_data geneset_by_id_resp = test_geneset_data.get("geneset_by_id_resp") geneset_w_gene_id_type_resp = test_geneset_data.get("geneset_w_gene_id_type_resp") +geneset_metadata_w_pub_info = test_geneset_data.get("geneset_metadata_w_pub_info") +publication_by_id_resp = test_publication_data.get("publication_by_id") @patch("geneweaver.api.services.geneset.get_geneset") @@ -79,3 +81,63 @@ def test_invalid_gene_type_id(mock_service_get_geneset_w_gene_id_type, client): assert "ctx" in response.json()["detail"][0] assert response.status_code == 422 + + +@patch("geneweaver.api.services.geneset.get_geneset_metadata") +@patch("geneweaver.api.services.geneset.is_geneset_readable_by_user") +def test_get_geneset_metadata(mock_genset_is_readable, mock_get_genenset, client): + """Test get geneset metadata.""" + mock_genset_is_readable.return_value = True + mock_get_genenset.return_value = geneset_by_id_resp + response = client.get("/api/genesets/1234/metadata") + + assert response.status_code == 200 + assert response.json() == geneset_by_id_resp + + +@patch("geneweaver.api.services.geneset.get_geneset_metadata") +@patch("geneweaver.api.services.geneset.is_geneset_readable_by_user") +def test_get_geneset_metadata_w_publication( + mock_genset_is_readable, mock_get_genenset, client +): + """Test get geneset ID data response.""" + mock_genset_is_readable.return_value = True + mock_get_genenset.return_value = geneset_metadata_w_pub_info + + response = client.get("/api/genesets/1234/metadata?include_pub_info=true") + assert response.status_code == 200 + assert response.json() == geneset_metadata_w_pub_info + + +@patch("geneweaver.api.services.geneset.get_geneset_metadata") +@patch("geneweaver.api.services.geneset.is_geneset_readable_by_user") +@patch("geneweaver.api.services.publications.get_publication") +def test_publication_for_geneset( + mock_pub_service_call, mock_genset_is_readable, mock_get_genenset_metadata, client +): + """Test valid url request to get publication for a geneset.""" + mock_genset_is_readable.return_value = True + mock_get_genenset_metadata.return_value = geneset_metadata_w_pub_info + + mock_pub_service_call.return_value = publication_by_id_resp + + response = client.get("/api/genesets/1234/publication") + + assert response.status_code == 200 + assert response.json() == publication_by_id_resp + + +@patch("geneweaver.api.services.geneset.get_geneset_metadata") +@patch("geneweaver.api.services.geneset.is_geneset_readable_by_user") +@patch("geneweaver.api.services.publications.get_publication") +def test_publication_not_found_for_geneset( + mock_pub_service_call, mock_genset_is_readable, mock_get_genenset_metadata, client +): + """Test get publication for a geneset with not found record.""" + mock_genset_is_readable.return_value = True + mock_get_genenset_metadata.return_value = geneset_by_id_resp.get("geneset") + mock_pub_service_call.return_value = {"publication": None} + + response = client.get("/api/genesets/1234/publication") + + assert response.status_code == 404 From 7df58fac4e16681cd5dea5219c2b5caf37bceb70 Mon Sep 17 00:00:00 2001 From: francastell Date: Wed, 7 Feb 2024 08:47:16 -0500 Subject: [PATCH 2/2] version update --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 4a90902..ebec045 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "geneweaver-api" -version = "0.2.0a3" +version = "0.2.0a4" description = "description" authors = ["Jax Computational Sciences "] packages = [