From 15af7120199d5b6ccedeb7913baa92134b035c4f Mon Sep 17 00:00:00 2001 From: Alexander Berger Date: Wed, 31 Jan 2024 17:05:22 -0500 Subject: [PATCH] Updating enum usage for consistency with geneweaver-core updates --- src/geneweaver/api/controller/genesets.py | 61 +++++------------------ src/geneweaver/api/schemas/apimodels.py | 20 ++------ src/geneweaver/api/services/geneset.py | 2 +- tests/controllers/test_genesets.py | 5 +- 4 files changed, 20 insertions(+), 68 deletions(-) diff --git a/src/geneweaver/api/controller/genesets.py b/src/geneweaver/api/controller/genesets.py index 712f602..8e81812 100644 --- a/src/geneweaver/api/controller/genesets.py +++ b/src/geneweaver/api/controller/genesets.py @@ -5,16 +5,14 @@ from tempfile import TemporaryDirectory from typing import Optional -from fastapi import APIRouter, Depends, HTTPException, Query, Security +from fastapi import APIRouter, Depends, HTTPException, Path, Security from fastapi.responses import FileResponse from geneweaver.api import dependencies as deps -from geneweaver.api.schemas.apimodels import gene_id_type_options from geneweaver.api.schemas.auth import UserInternal from geneweaver.api.services import geneset as genset_service from geneweaver.core.enum import GeneIdentifier -from geneweaver.core.schema.geneset import GenesetUpload from geneweaver.db import geneset as db_geneset -from geneweaver.db import geneset_value as db_geneset_value +from typing_extensions import Annotated from . import message as api_message @@ -33,20 +31,17 @@ def get_visible_genesets( @router.get("/{geneset_id}") def get_geneset( - geneset_id: int, + geneset_id: Annotated[ + int, Path(format="int64", minimum=0, maxiumum=9223372036854775807) + ], user: UserInternal = Security(deps.full_user), cursor: Optional[deps.Cursor] = Depends(deps.cursor), - # using int type and adding doc options here as using GeneIdentifier - # won't produce the right docs for gene identifier options - gene_id_type: Optional[int] = Query( - None, description=f"Options: {gene_id_type_options}" - ), + gene_id_type: Optional[GeneIdentifier] = None, ) -> dict: """Get a geneset by ID. Optional filter results by gene identifier type.""" if gene_id_type: - gene_identifier_type = get_gene_identifier_type(gene_id_type) response = genset_service.get_geneset_w_gene_id_type( - cursor, geneset_id, user, gene_identifier_type + cursor, geneset_id, user, gene_id_type ) else: response = genset_service.get_geneset(cursor, geneset_id, user) @@ -62,24 +57,21 @@ def get_geneset( @router.get("/{geneset_id}/file", response_class=FileResponse) def get_export_geneset_by_id_type( - geneset_id: int, + geneset_id: Annotated[ + int, Path(format="int64", minimum=0, maxiumum=9223372036854775807) + ], user: UserInternal = Security(deps.full_user), cursor: Optional[deps.Cursor] = Depends(deps.cursor), temp_dir: TemporaryDirectory = Depends(deps.get_temp_dir), - # using int type and adding doc options here as using - # GeneIdentifier won't produce the right docs for gene identifier options - gene_id_type: Optional[int] = Query( - None, description=f"Options: {gene_id_type_options}" - ), -) -> dict: + gene_id_type: Optional[GeneIdentifier] = None, +) -> FileResponse: """Export geneset into JSON file. Search by ID and optional gene identifier type.""" timestr = time.strftime("%Y%m%d-%H%M%S") # Validate gene identifier type if gene_id_type: - gene_identifier_type = get_gene_identifier_type(gene_id_type) response = genset_service.get_geneset_w_gene_id_type( - cursor, geneset_id, user, gene_identifier_type + cursor, geneset_id, user, gene_id_type ) else: response = genset_service.get_geneset(cursor, geneset_id, user) @@ -107,30 +99,3 @@ def get_export_geneset_by_id_type( media_type="application/octet-stream", filename=geneset_filename, ) - - -def upload_geneset( - geneset: GenesetUpload, - user: UserInternal = Security(deps.full_user), - cursor: Optional[deps.Cursor] = Depends(deps.cursor), -) -> dict: - """Upload a geneset.""" - db_geneset_value.format_geneset_values_for_file_insert(geneset.gene_list) - return {"geneset_id": 0} - - -def get_gene_identifier_type(gene_id_type: int) -> GeneIdentifier: - """Get a valid GeneIdentifier object. Raise HTTP exception if invalid value. - - @param gene_id_type: gene identifier type - @return: GeneIdentifier obj or HTTP exception if invalid id value. - """ - try: - gene_identifier = GeneIdentifier(gene_id_type) - except ValueError as err: - raise HTTPException( - status_code=400, - detail=f"{api_message.GENE_IDENTIFIER_TYPE_VALUE_ERROR}, " - f"valid options= {gene_id_type_options}", - ) from err - return gene_identifier diff --git a/src/geneweaver/api/schemas/apimodels.py b/src/geneweaver/api/schemas/apimodels.py index ab2a99c..de83817 100644 --- a/src/geneweaver/api/schemas/apimodels.py +++ b/src/geneweaver/api/schemas/apimodels.py @@ -1,13 +1,9 @@ """Models for API requests.""" from typing import Iterable, Optional -from fastapi import Query from geneweaver.core.enum import GeneIdentifier, Species from pydantic import BaseModel -gene_id_type_options = [f"{choice.name} ({choice.value})" for choice in GeneIdentifier] -species_type_options = [f"{choice.name} ({choice.value})" for choice in Species] - class GeneIdMappingResp(BaseModel): """Model for gene id mapping.""" @@ -19,15 +15,7 @@ class GeneIdMappingReq(BaseModel): """Model for gene id mapping request.""" source_ids: Iterable[str] - target_gene_id_type: GeneIdentifier = Query( - description=f"Options: {gene_id_type_options}" - ) - source_gene_id_type: Optional[GeneIdentifier] = Query( - None, description=f"Options: {gene_id_type_options}" - ) - target_species: Optional[Species] = Query( - None, description=f"Options: {species_type_options}" - ) - source_species: Optional[Species] = Query( - None, description=f"Options: {species_type_options}" - ) + target_gene_id_type: GeneIdentifier + source_gene_id_type: Optional[GeneIdentifier] = None + target_species: Optional[Species] = None + source_species: Optional[Species] = None diff --git a/src/geneweaver/api/services/geneset.py b/src/geneweaver/api/services/geneset.py index aa8b496..35090a4 100644 --- a/src/geneweaver/api/services/geneset.py +++ b/src/geneweaver/api/services/geneset.py @@ -54,7 +54,7 @@ def get_geneset_w_gene_id_type( original_gene_id_type = gene_id_type genedb_sp_id = db_gene.gene_database_by_id(cursor, gene_id_type)[0]["sp_id"] - if genedb_sp_id != 0 and geneset["sp_id"] != genedb_sp_id: + if genedb_sp_id != 0 and geneset["species_id"] != genedb_sp_id: mapping_across_species = True gene_id_type = GeneIdentifier(7) diff --git a/tests/controllers/test_genesets.py b/tests/controllers/test_genesets.py index e525169..79a4d08 100644 --- a/tests/controllers/test_genesets.py +++ b/tests/controllers/test_genesets.py @@ -2,7 +2,6 @@ from unittest.mock import patch import pytest -from geneweaver.api.controller import message from tests.data import test_geneset_data @@ -78,5 +77,5 @@ def test_invalid_gene_type_id(mock_service_get_geneset_w_gene_id_type, client): mock_service_get_geneset_w_gene_id_type.return_value = geneset_w_gene_id_type_resp response = client.get("/api/genesets/1234/file?gene_id_type=25") - assert message.GENE_IDENTIFIER_TYPE_VALUE_ERROR in response.json()["detail"] - assert response.status_code == 400 + assert 'ctx' in response.json()["detail"][0] + assert response.status_code == 422