Skip to content

Commit

Permalink
G3-9 unit tests, linting
Browse files Browse the repository at this point in the history
  • Loading branch information
francastell committed Dec 20, 2023
1 parent 4262684 commit ea94e06
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 193 deletions.
412 changes: 251 additions & 161 deletions poetry.lock

Large diffs are not rendered by default.

11 changes: 3 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ packages = [
]

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

geneweaver-core = "^0.8.0a0"
fastapi = {extras = ["all"], version = "^0.99.1"}
uvicorn = {extras = ["standard"], version = "^0.23.2"}

#fastapi = "^0.79.0"
#fastapi = {version = "^0.79.0"}
#uvicorn = {extras = ["standard"], version = "^0.18.2"}

uvicorn = {extras = ["standard"], version = "^0.24.0"}
geneweaver-db = "^0.2.0a0"
psycopg-pool = "^3.1.7"
requests = "^2.31.0"
Expand All @@ -28,6 +22,7 @@ python-jose = {extras = ["cryptography"], version = "^3.3.0"}
geneweaver-testing = "^0.0.3"
pytest-asyncio = "^0.21.0"
magicmock = "^0.03"
pylint= "^3.0.3"

[tool.ruff]
select = ['F', 'E', 'W', 'A', 'C90', 'N', 'B', 'ANN', 'D', 'I', 'ERA', 'PD', 'NPY', 'PT']
Expand Down
2 changes: 1 addition & 1 deletion src/geneweaver/api/controller/genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_geneset(
if response.get("message") == api_message.ACCESS_FORBIDEN:
raise HTTPException(status_code=403, detail=api_message.ACCESS_FORBIDEN)
else:
raise HTTPException(status_code=500, detail=api_message.ACCESS_FORBIDEN)
raise HTTPException(status_code=500, detail=api_message.UNEXPECTED_ERROR)

return response

Expand Down
4 changes: 2 additions & 2 deletions src/geneweaver/api/controller/message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Costant key/value messages"""
"""Constants key/value messages"""


##Errors
ACCESS_FORBIDEN="Forbidden"
UNEXPECTED_ERROR="Unexpected Error"
UNEXPECTED_ERROR="Unexpected Error"
2 changes: 1 addition & 1 deletion src/geneweaver/api/services/geneset.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ def is_geneset_readable_by_user(
logger.error(err)
raise err

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

import importlib.resources
import json
from unittest.mock import Mock
Expand All @@ -23,50 +25,54 @@
response_mock.json.return_value = test_data

### Mock dependencies
m1 = Mock()
def mock_full_user() -> m1.AsyncMock:
return m1
m2 = Mock()
def mock_full_user() -> Mock:
""" User auth mock"""
m1 = Mock()
return m1.AsyncMock()

def mock_cursor() -> psycopg.Cursor:
""" DB cursor mock"""
m2 = Mock()
return m2.AsyncMock()
m3 = Mock()
def mock_cursor_obj() -> psycopg.Cursor:
return m3.AsyncMock()

app.dependency_overrides.update(
{full_user: mock_full_user, cursor: mock_cursor}
)

@patch.object(client, 'get', return_value=response_mock)
def test_get_geneset(mock_client):
response = client.get("/genesets/1234")
""" test get request for geneset ID"""
response = client.get("/api/genesets/1234")
assert response.status_code == 200

@patch('geneweaver.api.services.geneset.get_geneset')
@patch('geneweaver.api.services.geneset.is_geneset_readable_by_user')
def test_get_geneset_response(mock_genset_is_readable, mock_get_genenset):
""" test get geneset ID data response"""
mock_genset_is_readable.return_value = True
mock_get_genenset.return_value = test_data

response = client.get("/genesets/1234")
response = client.get("/api/genesets/1234")
assert response.status_code == 200
assert response.json() == test_data

@patch('geneweaver.api.services.geneset.db_is_readable')
def test_get_geneset_forbidden(mock_genset_is_readable):
""" test forbidden response"""

mock_genset_is_readable.return_value = False
response = client.get("/genesets/1234")
response = client.get("/api/genesets/1234")

assert response.json() == {'detail': 'Forbidden'}
assert response.status_code == 403

@patch('geneweaver.api.services.geneset.db_is_readable')
def test_get_geneset_unexpected_error(mock_genset_is_readable):
""" test unexpected error response"""

mock_genset_is_readable.side_effect = Exception

with pytest.raises(Exception):
response = client.get("/genesets/1234")
response = client.get("/api/genesets/1234")
assert response.json() == {'detail': 'Unexpected error'}
assert response.status_code == 500
26 changes: 17 additions & 9 deletions tests/services/test_genset.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
""" Tests for geneset Service"""

import importlib.resources
import json
from unittest.mock import patch

import pytest

import geneweaver.api.controller.message as message
import geneweaver.api.services.geneset as geneset
from geneweaver.api.controller import message
from geneweaver.api.services import geneset

## Load test data
# Opening JSON file
Expand All @@ -16,28 +18,33 @@
@patch('geneweaver.api.services.geneset.db_geneset')
@patch('geneweaver.api.services.geneset.db_geneset_value')
@patch('geneweaver.api.services.geneset.is_geneset_readable_by_user')
def test_get_geneset_no_errors(mock_genset_readable_func, mock_db_geneset, mock_db_genset_value):
def test_get_geneset(mock_genset_readable_func, mock_db_geneset, mock_db_genset_value):
""" test basic get geneset by ID"""

mock_genset_readable_func.return_value = True
mock_db_geneset.by_id.return_value = {}
mock_db_genset_value.by_id.return_value = {}
response = geneset.get_geneset(1234, None, None)
assert response.get('error') == None
assert response.get('error') is None


@patch('geneweaver.api.services.geneset.is_geneset_readable_by_user')
def test_get_geneset_no_user_access(mock_genset_readable_func):
""" test get geneset by ID with no user access"""

mock_genset_readable_func.return_value = False
response = geneset.get_geneset(1234, None, None)
assert response.get('error') == True
assert response.get('error') is True
assert response.get('message') == message.ACCESS_FORBIDEN


@patch('geneweaver.api.services.geneset.db_geneset')
@patch('geneweaver.api.services.geneset.db_geneset_value')
@patch('geneweaver.api.services.geneset.is_geneset_readable_by_user')
def test_get_geneset_returned_values(mock_genset_readable_func, mock_db_genset_value, mock_db_geneset):
def test_get_geneset_returned_values(mock_genset_readable_func,
mock_db_genset_value,
mock_db_geneset):
""" test get geneset by ID data response structure"""

mock_genset_readable_func.return_value = True
mock_db_geneset.by_id.return_value = test_data.get('geneset')
Expand All @@ -51,18 +58,19 @@ def test_get_geneset_returned_values(mock_genset_readable_func, mock_db_genset_v
@patch('geneweaver.api.services.geneset.db_is_readable')
@patch('geneweaver.api.services.geneset.User')
def test_is_redable_by_user(mock_user, mock_genset_is_readable):
""" test is geneset ID readable by passed user"""

mock_genset_is_readable.return_value = True
response = geneset.is_geneset_readable_by_user(1234, mock_user, None)
assert response == True
assert response is True


@patch('geneweaver.db.geneset.is_readable')
@patch('geneweaver.api.services.geneset.User')
def test_is_redable_by_user_error(mock_user, mock_genset_is_readable):
""" test is geneset ID readable with server error"""

mock_genset_is_readable.sideEffect = Exception

with pytest.raises(Exception):
geneset.is_geneset_readable_by_user(1234, mock_user, None)


0 comments on commit ea94e06

Please sign in to comment.