Skip to content

Commit

Permalink
Merge pull request #26 from TheJacksonLaboratory/G3-153-publication-f…
Browse files Browse the repository at this point in the history
…or-geneset

G3-153 publication by geneset id
  • Loading branch information
francastell authored Feb 7, 2024
2 parents 840a0f9 + 6d0c043 commit 7521f62
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "geneweaver-api"
version = "0.2.0a3"
version = "0.2.0a4"
description = "description"
authors = ["Jax Computational Sciences <[email protected]>"]
packages = [
Expand Down
34 changes: 34 additions & 0 deletions src/geneweaver/api/controller/genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
64 changes: 63 additions & 1 deletion tests/controllers/test_genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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

0 comments on commit 7521f62

Please sign in to comment.