diff --git a/pyproject.toml b/pyproject.toml index 3c9baa4..b14ca13 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "geneweaver-api" -version = "0.7.0a0" +version = "0.7.0a1" description = "The Geneweaver API" authors = [ "Alexander Berger ", diff --git a/src/geneweaver/api/controller/publications.py b/src/geneweaver/api/controller/publications.py index ba22330..3c80ef8 100644 --- a/src/geneweaver/api/controller/publications.py +++ b/src/geneweaver/api/controller/publications.py @@ -2,7 +2,7 @@ from typing import Optional -from fastapi import APIRouter, Depends, HTTPException, Path, Security +from fastapi import APIRouter, Depends, HTTPException, Path, Query, Security from geneweaver.api import dependencies as deps from geneweaver.api.schemas.apimodels import NewPubmedRecord from geneweaver.api.schemas.auth import UserInternal @@ -15,6 +15,72 @@ router = APIRouter(prefix="/publications", tags=["publications"]) +@router.get("") +def get_publication( + cursor: Optional[deps.Cursor] = Depends(deps.cursor), + publication_id: Annotated[ + int, Query(format="int64", minimum=0, maxiumum=9223372036854775807) + ] = None, + authors: Optional[str] = None, + title: Optional[str] = None, + abstract: Optional[str] = None, + journal: Optional[str] = None, + volume: Optional[str] = None, + pages: Optional[str] = None, + month: Optional[str] = None, + year: Optional[str] = None, + pubmed_id: Annotated[ + Optional[int], + Query( + format="int64", + minimum=0, + maxiumum=9223372036854775807, + description=api_message.PUBMED_ID, + ), + ] = None, + search_text: Annotated[ + Optional[str], Query(description=api_message.SEARCH_TEXT) + ] = None, + limit: Annotated[ + Optional[int], + Query( + format="int64", + minimum=0, + maxiumum=1000, + description=api_message.LIMIT, + ), + ] = 10, + offset: Annotated[ + Optional[int], + Query( + format="int64", + minimum=0, + maxiumum=9223372036854775807, + description=api_message.OFFSET, + ), + ] = None, +) -> dict: + """Get all publication publications with optional filters.""" + response = publication_service.get( + cursor, + pub_id=publication_id, + authors=authors, + title=title, + abstract=abstract, + journal=journal, + volume=volume, + pages=pages, + month=month, + year=year, + pubmed=pubmed_id, + search_text=search_text, + limit=limit, + offset=offset, + ) + + return response + + @router.get("/{publication_id}") def get_publication_by_id( publication_id: Annotated[ diff --git a/src/geneweaver/api/services/publications.py b/src/geneweaver/api/services/publications.py index 23eca4f..fc15e32 100644 --- a/src/geneweaver/api/services/publications.py +++ b/src/geneweaver/api/services/publications.py @@ -1,5 +1,7 @@ """Service functions for publications.""" +from typing import Optional + from fastapi.logger import logger from geneweaver.api.controller import message from geneweaver.api.schemas.auth import User @@ -76,3 +78,62 @@ def add_pubmed_record(cursor: Cursor, user: User, pubmed_id: str) -> dict: except Exception as err: logger.error(err) raise err + + +def get( + cursor: Cursor, + pub_id: Optional[int] = None, + authors: Optional[str] = None, + title: Optional[str] = None, + abstract: Optional[str] = None, + journal: Optional[str] = None, + volume: Optional[str] = None, + pages: Optional[str] = None, + month: Optional[str] = None, + year: Optional[str] = None, + pubmed: Optional[str] = None, + search_text: Optional[str] = None, + limit: Optional[int] = None, + offset: Optional[int] = None, +) -> dict: + """Get publications by some criteria. + + :param cursor: A database cursor. + :param pub_id: Show only results with this publication id + :param authors: Show only results with these authors + :param title: Show only results with this title + :param abstract: Show only results with this abstract + :param journal: Show only results with this journal + :param volume: Show only results with volume + :param pages: Show only results with these pages + :param month: Show only results with this publication month + :param year: Show only results with publication year + :param pubmed: Show only results with pubmed id + :param search_text: Show only results that match this search text (using PostgreSQL + full-text search). + :param limit: Limit the number of results. + :param offset: Offset the results. + """ + try: + results = db_publication.get( + cursor=cursor, + pub_id=pub_id, + authors=authors, + title=title, + abstract=abstract, + journal=journal, + volume=volume, + pages=pages, + month=month, + year=year, + pubmed=pubmed, + search_text=search_text, + limit=limit, + offset=offset, + ) + + except Exception as err: + logger.error(err) + raise err + + return {"data": results} diff --git a/tests/controllers/test_publications.py b/tests/controllers/test_publications.py index c6267a6..7278e9e 100644 --- a/tests/controllers/test_publications.py +++ b/tests/controllers/test_publications.py @@ -10,6 +10,7 @@ publication_by_pubmed_id_resp = test_publication_data.get("publication_by_pubmed_id") add_pubmed_info = test_publication_data.get("add_pubmed_info") add_pubmed_resp = test_publication_data.get("add_pubmed_resp") +get_publications = test_publication_data.get("get_publications") @patch("geneweaver.api.services.publications.get_publication") @@ -110,3 +111,29 @@ def test_add_pubmed_valid_errors(mock_pub_service_call, client): } response = client.put(url="/api/publications/1234") assert response.status_code == 500 + + +@patch("geneweaver.api.services.publications.get") +def test_get_req(mock_pub_service_call, client): + """Test valid url request to get publication by id.""" + mock_pub_service_call.return_value = get_publications + + response = client.get( + url="/api/publications/", + params={ + "pub_id": 1, + "authors": "Author1, Author2", + "title": "Title1", + "abstract": "Abstract1", + "journal": "Journal1", + "volume": "Volume1", + "pages": "Pages1", + "month": "Month1", + "year": "Year1", + "pubmed": "123456", + "search_text": "something", + }, + ) + + assert response.status_code == 200 + assert response.json() == get_publications diff --git a/tests/data/__init__.py b/tests/data/__init__.py index 285088b..8c07089 100644 --- a/tests/data/__init__.py +++ b/tests/data/__init__.py @@ -104,8 +104,10 @@ ), "add_pubmed_info": json.loads(publications_json).get("add_pubmed_info"), "add_pubmed_resp": json.loads(publications_json).get("add_pubmed_resp"), + "get_publications": json.loads(publications_json).get("get_publications"), } + # Json web token keys data test_jwt_keys_data = { "test_private_key": json.loads(jwt_test_keys_json).get("private_key"), diff --git a/tests/data/publications.json b/tests/data/publications.json index 3ae66ed..fda0c79 100644 --- a/tests/data/publications.json +++ b/tests/data/publications.json @@ -37,5 +37,69 @@ "add_pubmed_resp": { "pubmed_id": 1234, "pub_id": 79325 + }, + "get_publications": { + "data": [ + { + "id": 79322, + "authors": "D Nadeau, C Marchand", + "title": "Change in the kinetics of sulphacetamide tissue distribution in Walker tumor-bearing rats.", + "abstract": "The effect of Walker tumor on sulphacetamide distribution was studied in rats 21 days after tumor implantation in a hind leg. After oral administration of sulphacetamide (5 and 20 min), the concentration of the drug was found to be lower in the plasma and liver of tumor-bearing rats when compared with that of control group. However, 90 min after sulphacetamide administration, the concentration of the drug in these same tissues was found to be higher in tumor-bearing rats than in control animals. Whereas the tumor had no apparent effect on sulphacetamide concentration in the brain, drug concentrations in the fat tissue of tumor-bearing rats were constantly higher than those of control animals. These changes in sulphacentamide disposition kinetics could be explained in part by delay in gastrointestinal absorption of the drug. Contrary to what was observed after oral administration, constantly higher drug concentrations were found in the plasma of tumor-bearing rats after iv injection of sulphacetamide. Furthermore, the half-life of sulphacetamide in these same animals was much higher than in control animals. It is concluded that, in Walker tumor-bearing rats, there are changes in the kinetics of sulphacetamide which are functions of the route of administration of the drug.", + "journal": "Drug metabolism and disposition: the biological fate of chemicals", + "volume": "3", + "pages": "565-76", + "month": null, + "year": "1975", + "pubmed_id": "1234" + }, + { + "id": 79323, + "authors": "D Nadeau, C Marchand", + "title": "Change in the kinetics of sulphacetamide tissue distribution in Walker tumor-bearing rats.", + "abstract": "The effect of Walker tumor on sulphacetamide distribution was studied in rats 21 days after tumor implantation in a hind leg. After oral administration of sulphacetamide (5 and 20 min), the concentration of the drug was found to be lower in the plasma and liver of tumor-bearing rats when compared with that of control group. However, 90 min after sulphacetamide administration, the concentration of the drug in these same tissues was found to be higher in tumor-bearing rats than in control animals. Whereas the tumor had no apparent effect on sulphacetamide concentration in the brain, drug concentrations in the fat tissue of tumor-bearing rats were constantly higher than those of control animals. These changes in sulphacentamide disposition kinetics could be explained in part by delay in gastrointestinal absorption of the drug. Contrary to what was observed after oral administration, constantly higher drug concentrations were found in the plasma of tumor-bearing rats after iv injection of sulphacetamide. Furthermore, the half-life of sulphacetamide in these same animals was much higher than in control animals. It is concluded that, in Walker tumor-bearing rats, there are changes in the kinetics of sulphacetamide which are functions of the route of administration of the drug.", + "journal": "Drug metabolism and disposition: the biological fate of chemicals", + "volume": "3", + "pages": "565-76", + "month": null, + "year": "1975", + "pubmed_id": "1234" + }, + { + "id": 79324, + "authors": "D Nadeau, C Marchand", + "title": "Change in the kinetics of sulphacetamide tissue distribution in Walker tumor-bearing rats.", + "abstract": "The effect of Walker tumor on sulphacetamide distribution was studied in rats 21 days after tumor implantation in a hind leg. After oral administration of sulphacetamide (5 and 20 min), the concentration of the drug was found to be lower in the plasma and liver of tumor-bearing rats when compared with that of control group. However, 90 min after sulphacetamide administration, the concentration of the drug in these same tissues was found to be higher in tumor-bearing rats than in control animals. Whereas the tumor had no apparent effect on sulphacetamide concentration in the brain, drug concentrations in the fat tissue of tumor-bearing rats were constantly higher than those of control animals. These changes in sulphacentamide disposition kinetics could be explained in part by delay in gastrointestinal absorption of the drug. Contrary to what was observed after oral administration, constantly higher drug concentrations were found in the plasma of tumor-bearing rats after iv injection of sulphacetamide. Furthermore, the half-life of sulphacetamide in these same animals was much higher than in control animals. It is concluded that, in Walker tumor-bearing rats, there are changes in the kinetics of sulphacetamide which are functions of the route of administration of the drug.", + "journal": "Drug metabolism and disposition: the biological fate of chemicals", + "volume": "3", + "pages": "565-76", + "month": null, + "year": "1975", + "pubmed_id": "1234" + }, + { + "id": 79325, + "authors": "D Nadeau, C Marchand", + "title": "Change in the kinetics of sulphacetamide tissue distribution in Walker tumor-bearing rats.", + "abstract": "The effect of Walker tumor on sulphacetamide distribution was studied in rats 21 days after tumor implantation in a hind leg. After oral administration of sulphacetamide (5 and 20 min), the concentration of the drug was found to be lower in the plasma and liver of tumor-bearing rats when compared with that of control group. However, 90 min after sulphacetamide administration, the concentration of the drug in these same tissues was found to be higher in tumor-bearing rats than in control animals. Whereas the tumor had no apparent effect on sulphacetamide concentration in the brain, drug concentrations in the fat tissue of tumor-bearing rats were constantly higher than those of control animals. These changes in sulphacentamide disposition kinetics could be explained in part by delay in gastrointestinal absorption of the drug. Contrary to what was observed after oral administration, constantly higher drug concentrations were found in the plasma of tumor-bearing rats after iv injection of sulphacetamide. Furthermore, the half-life of sulphacetamide in these same animals was much higher than in control animals. It is concluded that, in Walker tumor-bearing rats, there are changes in the kinetics of sulphacetamide which are functions of the route of administration of the drug.", + "journal": "Drug metabolism and disposition: the biological fate of chemicals", + "volume": "3", + "pages": "565-76", + "month": null, + "year": "1975", + "pubmed_id": "1234" + }, + { + "id": 79327, + "authors": "D Nadeau, C Marchand", + "title": "Change in the kinetics of sulphacetamide tissue distribution in Walker tumor-bearing rats.", + "abstract": "The effect of Walker tumor on sulphacetamide distribution was studied in rats 21 days after tumor implantation in a hind leg. After oral administration of sulphacetamide (5 and 20 min), the concentration of the drug was found to be lower in the plasma and liver of tumor-bearing rats when compared with that of control group. However, 90 min after sulphacetamide administration, the concentration of the drug in these same tissues was found to be higher in tumor-bearing rats than in control animals. Whereas the tumor had no apparent effect on sulphacetamide concentration in the brain, drug concentrations in the fat tissue of tumor-bearing rats were constantly higher than those of control animals. These changes in sulphacentamide disposition kinetics could be explained in part by delay in gastrointestinal absorption of the drug. Contrary to what was observed after oral administration, constantly higher drug concentrations were found in the plasma of tumor-bearing rats after iv injection of sulphacetamide. Furthermore, the half-life of sulphacetamide in these same animals was much higher than in control animals. It is concluded that, in Walker tumor-bearing rats, there are changes in the kinetics of sulphacetamide which are functions of the route of administration of the drug.", + "journal": "Drug metabolism and disposition: the biological fate of chemicals", + "volume": "3", + "pages": "565-76", + "month": null, + "year": "1975", + "pubmed_id": "1234" + } + ] } } \ No newline at end of file diff --git a/tests/services/test_publications.py b/tests/services/test_publications.py index 388be32..43d0394 100644 --- a/tests/services/test_publications.py +++ b/tests/services/test_publications.py @@ -14,6 +14,7 @@ publication_by_pubmed_id_resp = test_publication_data.get("publication_by_pubmed_id") add_pubmed_info = test_publication_data.get("add_pubmed_info") add_pubmed_resp = test_publication_data.get("add_pubmed_resp") +get_publications = test_publication_data.get("get_publications") mock_user = User() mock_user.id = 1 @@ -118,3 +119,48 @@ def test_add_pubmed_publication_errors(mock_db_publication, mock_pubmed): response = pub_service.add_pubmed_record( cursor=None, user=mock_user, pubmed_id=1234 ) + + +@patch("geneweaver.api.services.publications.db_publication") +def test_get_publications(mock_db_publication): + """Test get publication by ID data response structure.""" + mock_db_publication.get.return_value = get_publications.get("data") + + response = pub_service.get( + cursor=None, + pub_id=1, + authors="Author1, Author2", + title="Title1", + abstract="Abstract1", + journal="Journal1", + volume="Volume1", + pages="Pages1", + month="Month1", + year="Year1", + pubmed="123456", + search_text="something", + ) + + assert response.get("data") == get_publications.get("data") + + +@patch("geneweaver.api.services.publications.db_publication") +def test_get_publications_w_filter_errors(mock_db_publication): + """Test get publication errors.""" + # unexpected error + mock_db_publication.get.side_effect = Exception() + with pytest.raises(expected_exception=Exception): + pub_service.get( + cursor=None, + pub_id=1, + authors="Author1, Author2", + title="Title1", + abstract="Abstract1", + journal="Journal1", + volume="Volume1", + pages="Pages1", + month="Month1", + year="Year1", + pubmed="123456", + search_text="something", + )