Skip to content

Commit

Permalink
Merge pull request #42 from TheJacksonLaboratory/G3-193-species-by-id…
Browse files Browse the repository at this point in the history
…-endpoint

G3 193 species by id endpoint
  • Loading branch information
francastell authored Mar 21, 2024
2 parents facad5e + ccefa9f commit 24629ff
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 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.4.0a4"
version = "0.4.0a5"
description = "The Geneweaver API"
authors = [
"Alexander Berger <alexander.berger@jax.org>",
Expand Down
12 changes: 11 additions & 1 deletion src/geneweaver/api/controller/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fastapi import APIRouter, Depends, Query
from geneweaver.api import dependencies as deps
from geneweaver.api.services import species as species_service
from geneweaver.core.enum import GeneIdentifier
from geneweaver.core.enum import GeneIdentifier, Species
from typing_extensions import Annotated

router = APIRouter(prefix="/species", tags=["species"])
Expand All @@ -22,3 +22,13 @@ def get_species(
response = species_service.get_species(cursor, taxonomy_id, reference_gene_id_type)

return response


@router.get("/{species_id}")
def get_species_by_id(
species_id: Species, cursor: Optional[deps.Cursor] = Depends(deps.cursor)
) -> dict:
"""Get species."""
response = species_service.get_species_by_id(cursor, species_id)

return response
33 changes: 27 additions & 6 deletions src/geneweaver/api/services/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional

from fastapi.logger import logger
from geneweaver.core.enum import GeneIdentifier
from geneweaver.core.enum import GeneIdentifier, Species
from geneweaver.db import species as db_species
from psycopg import Cursor

Expand All @@ -23,14 +23,35 @@ def get_species(
try:
species = db_species.get(cursor, taxonomy_id, reference_gene_id_type)
for species_record in species:
ref_gene_id_type = species_record.get("reference_gene_identifier", None)
if ref_gene_id_type:
species_record["reference_gene_identifier"] = GeneIdentifier(
ref_gene_id_type
)
decode_gene_identifier(species_record)

return {"species": species}

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


def get_species_by_id(cursor: Cursor, species: Species) -> dict:
"""Get species from DB.
@param cursor: DB cursor
@param species: species id
@return: dictionary response (species).
"""
try:
species_rsp = db_species.get_by_id(cursor, species)
decode_gene_identifier(species_rsp)

return {"species": species_rsp}

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


def decode_gene_identifier(species: Species) -> None:
"""Decode gene identifier from DB to Enum str value."""
ref_gene_id_type = species.get("reference_gene_identifier", None)
if ref_gene_id_type:
species["reference_gene_identifier"] = GeneIdentifier(ref_gene_id_type)
11 changes: 11 additions & 0 deletions tests/controllers/test_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ def test_valid_species_url_gene_id_type_req(mock_species_service_call, client):

assert response.status_code == 200
assert response.json() == species_by_gene_id_type_flybase


@patch("geneweaver.api.services.species.get_species_by_id")
def test_valid_url_species_by_id(mock_species_service_call, client):
"""Test valid url request to get species by id."""
mock_species_service_call.return_value = species_by_gene_id_type_flybase

response = client.get(url="/api/species/5")

assert response.status_code == 200
assert response.json() == species_by_gene_id_type_flybase
34 changes: 32 additions & 2 deletions tests/services/test_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest
from geneweaver.api.services import species as species_service
from geneweaver.core.enum import GeneIdentifier
from geneweaver.core.enum import GeneIdentifier, Species

from tests.data import get_species_db_resp, test_species_data

Expand Down Expand Up @@ -63,8 +63,38 @@ def test_get_species_by_gene_id_type_and_taxonomy(mock_db_species):

@patch("geneweaver.api.services.species.db_species")
def test_get_species_with_error(mock_db_species):
"""Test error in DB call."""
"""Test get species error in DB call."""
mock_db_species.get.side_effect = Exception("ERROR")

with pytest.raises(expected_exception=Exception):
species_service.get(None)


@patch("geneweaver.api.services.species.db_species")
def test_get_species_by_id(mock_db_species):
"""Test speccies by species id."""
mock_db_species.get_by_id.return_value = species_by_gene_id_type_flybase[0]

response = species_service.get_species_by_id(None, Species(5))

assert response.get("species") == species_by_gene_id_type_flybase[0]


@patch("geneweaver.api.services.species.db_species")
def test_get_species_by_id_with_error(mock_db_species):
"""Test species by id error in DB call."""
mock_db_species.get_by_id.side_effect = Exception("ERROR")

with pytest.raises(expected_exception=Exception):
species_service.get_species_by_id(None, Species(5))


def test_get_decode_gene_identifier():
"""Test decode gene identifier."""
species = species_by_gene_id_type_flybase[0].copy()
species_service.decode_gene_identifier(species)

assert (
species
== test_species_data.get("species_by_gene_id_type_flybase").get("species")[0]
)

0 comments on commit 24629ff

Please sign in to comment.