Skip to content

Commit

Permalink
updated the test lint
Browse files Browse the repository at this point in the history
  • Loading branch information
PoulavBhowmick03 committed Oct 25, 2024
1 parent 719e39e commit f2d2cd6
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions web_app/tests/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,27 @@

@app.exception_handler(Exception)
async def generic_exception_handler(request: Request, exc: Exception):
"""
Generic exception handler for unexpected server errors.
This function handles uncaught exceptions and returns a JSON response with
a 500 status code, indicating an internal server error. It is registered as
an exception handler for all exceptions not handled by specific exception
handlers.
Args:
request (Request): The incoming HTTP request that caused the exception.
exc (Exception): The exception instance raised during the request.
Returns:
JSONResponse: A JSON response containing a "detail" key with a message
indicating an internal server error, with a 500 status code.
"""
return JSONResponse(
status_code=500,
content={"detail": "Internal Server Error"},
)


client = TestClient(router)


Expand Down Expand Up @@ -65,8 +80,10 @@ async def generic_exception_handler(request: Request, exc: Exception):

@pytest.mark.asyncio
async def test_get_dashboard_success():

"""Test successful retrieval of dashboard data."""

wallet_id = "0x1234567890abcdef"

with patch(
"web_app.api.dashboard.position_db_connector.get_contract_address_by_wallet_id"
) as mock_get_contract_address_by_wallet_id, patch(
Expand Down Expand Up @@ -115,8 +132,10 @@ async def test_get_dashboard_success():

@pytest.mark.asyncio
async def test_get_dashboard_no_positions():

"""Test handling of wallet with no positions."""

wallet_id = "0x1234567890abcdef"

with patch(
"web_app.api.dashboard.position_db_connector.get_contract_address_by_wallet_id"
) as mock_get_contract_address_by_wallet_id, patch(
Expand Down Expand Up @@ -146,8 +165,10 @@ async def test_get_dashboard_no_positions():

@pytest.mark.asyncio
async def test_get_dashboard_no_contract_address():

"""Test handling of missing contract address."""

wallet_id = "0x1234567890abcdef"

with patch(
"web_app.api.dashboard.position_db_connector.get_contract_address_by_wallet_id"
) as mock_get_contract_address_by_wallet_id, patch(
Expand Down Expand Up @@ -178,12 +199,25 @@ async def test_get_dashboard_no_contract_address():

@pytest.fixture
def mock_db_connector():
"""
Fixture that provides a mocked instance of the PositionDBConnector
used in the dashboard module. This mock allows for controlled testing
of database-related functions without accessing the actual database,
enabling the simulation of different responses and conditions.
"""
with patch("web_app.api.dashboard.position_db_connector") as mock:
yield mock


@pytest.fixture
def mock_dashboard_mixin():
"""
Fixture that provides a mocked instance of the DashboardMixin used
in the dashboard module. This mock allows for controlled testing of
external service interactions (e.g., wallet balances and ZkLend positions)
without relying on real API calls, enabling the simulation of various
responses and error conditions.
"""
with patch("web_app.contract_tools.mixins.dashboard.DashboardMixin") as mock:
mock.get_wallet_balances = AsyncMock()
mock.get_zklend_position = AsyncMock()
Expand All @@ -192,7 +226,9 @@ def mock_dashboard_mixin():

@pytest.mark.asyncio
async def test_invalid_wallet_id(mock_db_connector):

"""Test handling of invalid wallet ID."""

mock_db_connector.get_contract_address_by_wallet_id.side_effect = ValueError(
"Invalid wallet ID"
)
Expand All @@ -203,7 +239,9 @@ async def test_invalid_wallet_id(mock_db_connector):

@pytest.mark.asyncio
async def test_empty_positions(mock_db_connector, mock_dashboard_mixin):

"""Test handling of wallet with no positions."""

mock_db_connector.get_contract_address_by_wallet_id.return_value = (
MOCK_CONTRACT_ADDRESS
)
Expand All @@ -222,6 +260,7 @@ async def test_empty_positions(mock_db_connector, mock_dashboard_mixin):

@pytest.mark.asyncio
async def test_external_service_errors(mock_db_connector):

"""Test handling of external service failures."""

mock_db_connector.get_contract_address_by_wallet_id.return_value = (
Expand All @@ -238,6 +277,7 @@ async def test_external_service_errors(mock_db_connector):

@pytest.mark.asyncio
async def test_zklend_service_error(mock_db_connector):

"""Test handling of ZkLend service failure."""

mock_db_connector.get_contract_address_by_wallet_id.return_value = (
Expand Down

0 comments on commit f2d2cd6

Please sign in to comment.