Skip to content

Commit

Permalink
Merge pull request #73 from TheJacksonLaboratory/G3-270-search-endpoint
Browse files Browse the repository at this point in the history
G3-270 search endpoint
  • Loading branch information
francastell authored Jun 25, 2024
2 parents b0178ac + 1cfb882 commit 3d40dfe
Show file tree
Hide file tree
Showing 6 changed files with 127 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.7.0a1"
version = "0.7.0a2"
description = "The Geneweaver API"
authors = [
"Alexander Berger <[email protected]>",
Expand Down
3 changes: 2 additions & 1 deletion src/geneweaver/api/controller/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from fastapi import APIRouter, FastAPI, Security
from geneweaver.api import __version__
from geneweaver.api import dependencies as deps
from geneweaver.api.controller import genes, genesets, publications, species
from geneweaver.api.controller import genes, genesets, publications, search, species
from geneweaver.api.core.config import settings

app = FastAPI(
Expand All @@ -30,5 +30,6 @@
api_router.include_router(genes.router)
api_router.include_router(publications.router)
api_router.include_router(species.router)
api_router.include_router(search.router)

app.include_router(api_router, prefix=settings.API_PREFIX)
4 changes: 4 additions & 0 deletions src/geneweaver/api/controller/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@
PUBMED_ID = "Show only results with this PubMed ID"
SEARCH_TEXT = "Return genesets that match this search text"
WITH_PUBLICATION = "Include publication info in the return"
GS_PUB_SEARCH_ENTITIES = "Entites to search, genesets and/or publications"
GS_PUB_SEARCH_TEXT = (
"Gensets and/or publications search types ('genesets', 'publications') "
)
70 changes: 70 additions & 0 deletions src/geneweaver/api/controller/search.py
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
"""Endpoints related to searching."""

from typing import List, Optional

from fastapi import APIRouter, Depends, Query, Security
from geneweaver.api import dependencies as deps
from geneweaver.api.schemas.apimodels import GsPubSearchType, SeachResponse
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 typing_extensions import Annotated

from . import message as api_message

router = APIRouter(prefix="/search", tags=["search"])


@router.get("")
def search(
entities: Annotated[
List[GsPubSearchType], Query(description=api_message.GS_PUB_SEARCH_TEXT)
],
search_text: Annotated[str, Query(description=api_message.SEARCH_TEXT)],
user: UserInternal = Security(deps.optional_full_user),
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
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,
) -> SeachResponse:
"""Search genesets and publications."""
data = {}
response = SeachResponse(data=data)
if "genesets" in entities:
geneset_response = genset_service.get_visible_genesets(
cursor=cursor,
user=user,
search_text=search_text,
limit=limit,
offset=offset,
)

data["geneset"] = geneset_response.get("data")

if "publications" in entities:
pub_response = publication_service.get(
cursor,
search_text=search_text,
limit=limit,
offset=offset,
)

data["publications"] = pub_response.get("data")

response.data = data

return response
14 changes: 14 additions & 0 deletions src/geneweaver/api/schemas/apimodels.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Models for API requests."""

from enum import Enum
from typing import Iterable, List, Optional

from geneweaver.core.enum import GeneIdentifier, Species
Expand Down Expand Up @@ -89,3 +90,16 @@ class NewPubmedRecord(BaseModel):

pub_id: int
pubmed_id: int


class GsPubSearchType(str, Enum):
"""Enum model for genesets and publication search types."""

GENESETS = "genesets"
PUBLICATIONS = "publications"


class SeachResponse(CollectionResponse):
"""Model for search response endpoint."""

data: dict
36 changes: 36 additions & 0 deletions tests/controllers/test_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Tests for search API."""

from unittest.mock import patch

from tests.data import test_geneset_data, test_publication_data

get_publications = test_publication_data.get("get_publications")
geneset_by_id_resp = test_geneset_data.get("geneset_by_id_resp")


@patch("geneweaver.api.services.publications.get")
def test_pub_search(mock_pub_service_call, client):
"""Test search for publications data response."""
mock_pub_service_call.return_value = get_publications

response = client.get(
url="/api/search/", params={"entities": "publications", "search_text": "gene"}
)

assert response.status_code == 200
assert response.json().get("data").get("publications") == get_publications.get(
"data"
)


@patch("geneweaver.api.services.geneset.get_visible_genesets")
def test_genesets_search_response(mock_get_visible_genesets, client):
"""Test search for geneset data response."""
mock_data = geneset_by_id_resp.get("geneset")
mock_get_visible_genesets.return_value = mock_data

response = client.get(
url="/api/search/", params={"entities": "genesets", "search_text": "gene"}
)
assert response.status_code == 200
assert response.json().get("data").get("genesets") == mock_data.get("data")

0 comments on commit 3d40dfe

Please sign in to comment.