Skip to content

Commit

Permalink
G3-239 geneset threshold update endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
francastell committed May 2, 2024
1 parent d5f4c1b commit 6c73b85
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 32 deletions.
21 changes: 4 additions & 17 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "geneweaver-api"
version = "0.5.0a0"
version = "0.5.0a1"
description = "The Geneweaver API"
authors = [
"Alexander Berger <[email protected]>",
Expand All @@ -17,11 +17,10 @@ packages = [

[tool.poetry.dependencies]
python = "^3.9"

geneweaver-core = "^0.9.1"
fastapi = {extras = ["all"], version = "^0.99.1"}
uvicorn = {extras = ["standard"], version = "^0.24.0"}
geneweaver-db = "^0.3.3"
geneweaver-db = "^0.3.4"
psycopg-pool = "^3.1.7"
requests = "^2.31.0"
python-jose = {extras = ["cryptography"], version = "^3.3.0"}
Expand Down
9 changes: 4 additions & 5 deletions src/geneweaver/api/controller/genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
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 geneweaver.core.schema.score import GenesetScoreType
from geneweaver.core.enum import GeneIdentifier, GenesetTier, Species
from geneweaver.core.schema.score import GenesetScoreType
from typing_extensions import Annotated

from . import message as api_message
Expand Down Expand Up @@ -277,13 +277,12 @@ def put_geneset_threshold(
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
) -> dict:
"""Set geneset threshold for geneset owner."""
response = genset_service.update_geneset_threshold(cursor, geneset_id, gene_score_type, user)
response = genset_service.update_geneset_threshold(
cursor, geneset_id, gene_score_type, user
)

if "error" in response:
if response.get("message") == api_message.ACCESS_FORBIDDEN:
raise HTTPException(status_code=403, detail=api_message.ACCESS_FORBIDDEN)
else:
raise HTTPException(status_code=500, detail=api_message.UNEXPECTED_ERROR)

return response

17 changes: 10 additions & 7 deletions src/geneweaver/api/services/geneset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from geneweaver.core.schema.score import GenesetScoreType
from geneweaver.db import gene as db_gene
from geneweaver.db import geneset as db_geneset
from geneweaver.db import threshold as db_threshold
from geneweaver.db import geneset_value as db_geneset_value
from geneweaver.db import threshold as db_threshold
from psycopg import Cursor


Expand Down Expand Up @@ -250,18 +250,21 @@ def map_geneset_homology(
def update_geneset_threshold(
cursor: Cursor, geneset_id: int, geneset_score: GenesetScoreType, user: User
) -> dict:
""" set geneset threshold if user is the owner"""

"""Set geneset threshold if user is the owner."""
try:
if user is None or user.id is None:
return {"error": True, "message": message.ACCESS_FORBIDDEN}

# if not db_geneset.user_is_owner(cursor=cursor, user_id=user.id, geneset_id=geneset_id):
# return {"error": True, "message": message.ACCESS_FORBIDDEN}
if not db_geneset.user_is_owner(
cursor=cursor, user_id=user.id, geneset_id=geneset_id
):
return {"error": True, "message": message.ACCESS_FORBIDDEN}

db_threshold.set_geneset_threshold(cursor=cursor, geneset_id=geneset_id, geneset_score_type=geneset_score)
db_threshold.set_geneset_threshold(
cursor=cursor, geneset_id=geneset_id, geneset_score_type=geneset_score
)
return {}

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

28 changes: 28 additions & 0 deletions tests/controllers/test_genesets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for geneset API."""

import json
from unittest.mock import patch

from geneweaver.api.controller import message
Expand All @@ -11,6 +12,7 @@
geneset_metadata_w_pub_info = test_geneset_data.get("geneset_metadata_w_pub_info")
publication_by_id_resp = test_publication_data.get("publication_by_id")
geneset_genes_values_resp = test_geneset_data.get("geneset_genes_values_resp_1")
geneset_threshold_update_req = test_geneset_data.get("geneset_threshold_update_req")


@patch("geneweaver.api.services.geneset.get_geneset")
Expand Down Expand Up @@ -228,3 +230,29 @@ def test_get_geneset_gene_values_errors(mock_get_geneset_gene_values, client):
mock_get_geneset_gene_values.return_value = {"data": None}
response = client.get("/api/genesets/1234/values")
assert response.status_code == 404


@patch("geneweaver.api.services.geneset.update_geneset_threshold")
def test_set_geneset_endpoint_response(mock_update_geneset_threshold, client):
"""Test set geneset threshold endpoint."""
mock_update_geneset_threshold.return_value = {}

response = client.put(
"/api/genesets/1234/threshold", data=json.dumps(geneset_threshold_update_req)
)
assert response.status_code == 200
assert response.json() == {}


@patch("geneweaver.api.services.geneset.update_geneset_threshold")
def test_set_geneset_endpoint_response_errors(mock_update_geneset_threshold, client):
"""Test set geneset threshold endpoint errors."""
mock_update_geneset_threshold.return_value = {
"error": True,
"message": message.ACCESS_FORBIDDEN,
}

response = client.put(
"/api/genesets/1234/threshold", data=json.dumps(geneset_threshold_update_req)
)
assert response.status_code == 403
3 changes: 3 additions & 0 deletions tests/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
"geneset_genes_values_resp_1": json.loads(geneset_list_response_json).get(
"geneset_genes_values_resp_1"
),
"geneset_threshold_update_req": json.loads(geneset_list_response_json).get(
"geneset_threshold_update_req"
),
}

# Gene homolog ids test data
Expand Down
5 changes: 5 additions & 0 deletions tests/data/geneset.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,5 +296,10 @@
"value": 1.0
}
]
},
"geneset_threshold_update_req": {
"score_type": "p-value",
"threshold_low": 0,
"threshold": 0.05
}
}
50 changes: 50 additions & 0 deletions tests/services/test_genset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from geneweaver.api.schemas.auth import User
from geneweaver.api.services import geneset
from geneweaver.core.enum import GeneIdentifier, GenesetTier, Species
from geneweaver.core.schema.score import GenesetScoreType

from tests.data import test_geneset_data

Expand All @@ -15,6 +16,7 @@
geneset_w_gene_id_type_resp = test_geneset_data.get("geneset_w_gene_id_type_resp")
geneset_metadata_w_pub_info = test_geneset_data.get("geneset_metadata_w_pub_info")
geneset_genes_values_resp = test_geneset_data.get("geneset_genes_values_resp_1")
geneset_threshold_update_req = test_geneset_data.get("geneset_threshold_update_req")
mock_user = User()
mock_user.id = 1

Expand Down Expand Up @@ -278,3 +280,51 @@ def test_get_geneset_gene_values_invalid_user(mock_db_geneset_value):
response = geneset.get_geneset_gene_values(None, user=None, geneset_id=1234)
assert response.get("error") is True
assert response.get("message") == message.ACCESS_FORBIDDEN


@patch("geneweaver.api.services.geneset.db_geneset")
@patch("geneweaver.api.services.geneset.db_threshold")
def test_geneset_thershold_update(mock_db_threshold, mock_db_geneset):
"""Test geneset gene value data response."""
mock_db_threshold.set_geneset_threshold.return_value = None
mock_db_geneset.user_is_owner.return_value = True
geneset_threshold = GenesetScoreType(**geneset_threshold_update_req)

response = geneset.update_geneset_threshold(
cursor=None, user=mock_user, geneset_id=1234, geneset_score=geneset_threshold
)
assert response == {}


@patch("geneweaver.api.services.geneset.db_geneset")
@patch("geneweaver.api.services.geneset.db_threshold")
def test_geneset_thershold_update_errors(mock_db_threshold, mock_db_geneset):
"""Test geneset gene value data response."""
mock_db_threshold.set_geneset_threshold.return_value = None
mock_db_geneset.user_is_owner.return_value = False
geneset_threshold = GenesetScoreType(**geneset_threshold_update_req)

# user is not the geneset owner
response = geneset.update_geneset_threshold(
cursor=None, user=mock_user, geneset_id=1234, geneset_score=geneset_threshold
)
assert response.get("error") is True
assert response.get("message") == message.ACCESS_FORBIDDEN

# user is not logged-in
response = geneset.update_geneset_threshold(
cursor=None, user=None, geneset_id=1234, geneset_score=geneset_threshold
)
assert response.get("error") is True
assert response.get("message") == message.ACCESS_FORBIDDEN

# db error
mock_db_geneset.user_is_owner.return_value = True
mock_db_threshold.set_geneset_threshold.side_effect = Exception("ERROR")
with pytest.raises(expected_exception=Exception):
geneset.update_geneset_threshold(
cursor=None,
user=mock_user,
geneset_id=1234,
geneset_score=geneset_threshold,
)

0 comments on commit 6c73b85

Please sign in to comment.