Skip to content

Commit

Permalink
Merge pull request #41 from TheJacksonLaboratory/G3-195-gene-endpoint
Browse files Browse the repository at this point in the history
G3 195 gene endpoint
  • Loading branch information
francastell authored Mar 21, 2024
2 parents daecf3c + ac4c51f commit facad5e
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 102 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ such as DB, Webserver, or APIs. Create appropriate mocks for external resources

To execute tests, from the command line run:

pytest
pytest tests --cov=geneweaver.api --cov-report term --cov-report html

### Continuous Integration & Deployment
When a PR is crated in GitHub, it will automatically trigger a workflow that will run
Expand Down
206 changes: 110 additions & 96 deletions poetry.lock

Large diffs are not rendered by default.

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.4.0a3"
version = "0.4.0a4"
description = "The Geneweaver API"
authors = [
"Alexander Berger <alexander.berger@jax.org>",
Expand Down
16 changes: 14 additions & 2 deletions src/geneweaver/api/controller/genes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Endpoints related to genes."""
from typing import Optional

from fastapi import APIRouter, Depends, Query
from fastapi import APIRouter, Depends, Path, Query
from geneweaver.api import dependencies as deps
from geneweaver.api.schemas.apimodels import (
GeneIdHomologReq,
Expand Down Expand Up @@ -34,7 +34,7 @@ def get_genes(
Query(
format="int64",
minimum=0,
maxiumum=9223372036854775807,
maxiumum=1000,
description=api_message.LIMIT,
),
] = None,
Expand All @@ -58,6 +58,18 @@ def get_genes(
return response


@router.get("/{gene_id}/preferred")
def get_gene_preferred(
gene_id: Annotated[
int, Path(format="int64", minimum=0, maxiumum=9223372036854775807)
],
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
) -> dict:
"""Get preferred gene for a given gene ode_id."""
response = genes_service.get_gene_preferred(cursor, gene_id)
return response


@router.post("/homologs", response_model=GeneIdMappingResp)
def get_related_gene_ids(
gene_id_mapping: GeneIdHomologReq,
Expand Down
17 changes: 17 additions & 0 deletions src/geneweaver/api/services/genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ def get_genes(
return {"genes": gene_list}


def get_gene_preferred(cursor: Cursor, gene_id: int) -> dict:
"""Get preferred gene from DB.
:param cursor: The database cursor.
:param gene_id: The id of the gene to get.
@return: dictionary with gene.
"""
try:
gene = db_gene.get_preferred(cursor, gene_id)

except Exception as err:
logger.error(err)
raise err

return {"gene": gene}


def get_homolog_ids(
cursor: Cursor,
gene_id_list: Iterable,
Expand Down
12 changes: 12 additions & 0 deletions tests/controllers/test_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# genes
genes_list_10 = test_genes_data.get("genes_list_10")
gene_preferred_resp_1 = test_genes_data.get("gene_preferred_resp_1")

# gene homolog ids test data
gene_ids_homolog_req_1 = test_gene_homolog_data.get(
Expand Down Expand Up @@ -180,3 +181,14 @@ def test_invalid_param_gene_get_req(mock_gene_call, client):
assert response.status_code == 422
response = client.get(url="/api/genes?preferred=abc")
assert response.status_code == 422


@patch("geneweaver.api.services.genes.get_gene_preferred")
def test_valid_get_preferred_gene_req(mock_gene_call, client):
"""Test valid get preferred gene request."""
mock_gene_call.return_value = gene_preferred_resp_1

response = client.get(url="/api/genes/1000/preferred")

assert response.status_code == 200
assert response.json() == gene_preferred_resp_1
3 changes: 2 additions & 1 deletion tests/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@
## geneweaver genes test data
test_genes_data = {
"genes_list_10": json.loads(genes_json).get("genes_list_10"),
"gene_preferred_resp_1": json.loads(genes_json).get("gene_preferred_resp_1"),
}


def get_species_db_resp(species_data: dict) -> dict:
"""Get species data as returned by DB."""
species = species_data.get("species")
Expand All @@ -128,4 +130,3 @@ def get_species_db_resp(species_data: dict) -> dict:
).as_int()

return species

10 changes: 10 additions & 0 deletions tests/data/genes.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,15 @@
"date": "2020-05-05"
}
]
},
"gene_preferred_resp_1": {
"gene": {
"id": 1000,
"reference_id": "Il1r2",
"gene_database": 7,
"species": 1,
"preferred": true,
"date": "2020-05-05"
}
}
}
23 changes: 22 additions & 1 deletion tests/services/test_gene.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# genes
genes_list_10 = test_genes_data.get("genes_list_10")
gene_preferred_resp_1 = test_genes_data.get("gene_preferred_resp_1")

# gene homolog ids test data
gene_ids_homolog_req_1 = test_gene_homolog_data.get(
Expand Down Expand Up @@ -235,7 +236,7 @@ def test_get_gene_error(mock_db_gene):

@patch("geneweaver.api.services.genes.db_gene")
def test_get_gene(mock_db_gene):
"""Test error in DB call."""
"""Test get gene."""
mock_db_gene.get.return_value = genes_list_10

response = genes.get_genes(None)
Expand All @@ -254,3 +255,23 @@ def test_get_gene(mock_db_gene):
)

assert response.get("error") is None


@patch("geneweaver.api.services.genes.db_gene")
def test_get_gene_preferred(mock_db_gene):
"""Test get gene preferred."""
mock_db_gene.get_preferred.return_value = gene_preferred_resp_1

response = genes.get_gene_preferred(None, gene_id=1000)

assert response.get("error") is None
assert response.get("gene") == gene_preferred_resp_1


@patch("geneweaver.api.services.genes.db_gene")
def test_get_gene_preferred_error(mock_db_gene):
"""Test error in DB call gene_preferred."""
mock_db_gene.get_preferred.side_effect = Exception("ERROR")

with pytest.raises(expected_exception=Exception):
genes.get_gene_preferred(None, gene_id=1000)

0 comments on commit facad5e

Please sign in to comment.