diff --git a/pyproject.toml b/pyproject.toml index b683fa8..4e738fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "geneweaver-api" -version = "0.4.0a4" +version = "0.4.0a5" description = "The Geneweaver API" authors = [ "Alexander Berger ", diff --git a/src/geneweaver/api/controller/species.py b/src/geneweaver/api/controller/species.py index ec45a9f..1423931 100644 --- a/src/geneweaver/api/controller/species.py +++ b/src/geneweaver/api/controller/species.py @@ -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"]) @@ -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 diff --git a/src/geneweaver/api/services/species.py b/src/geneweaver/api/services/species.py index a619396..b93d480 100644 --- a/src/geneweaver/api/services/species.py +++ b/src/geneweaver/api/services/species.py @@ -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 @@ -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) diff --git a/tests/controllers/test_species.py b/tests/controllers/test_species.py index 05ee2ee..477ca23 100644 --- a/tests/controllers/test_species.py +++ b/tests/controllers/test_species.py @@ -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 diff --git a/tests/services/test_species.py b/tests/services/test_species.py index a65e6f9..1472378 100644 --- a/tests/services/test_species.py +++ b/tests/services/test_species.py @@ -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 @@ -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] + )