From f2d2cd66eb160cac498732de4dd0ebeb5eab2ed2 Mon Sep 17 00:00:00 2001 From: PoulavBhowmick03 Date: Fri, 25 Oct 2024 17:37:02 +0530 Subject: [PATCH] updated the test lint --- web_app/tests/test_dashboard.py | 48 ++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/web_app/tests/test_dashboard.py b/web_app/tests/test_dashboard.py index 789288f5..25832740 100644 --- a/web_app/tests/test_dashboard.py +++ b/web_app/tests/test_dashboard.py @@ -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) @@ -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( @@ -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( @@ -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( @@ -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() @@ -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" ) @@ -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 ) @@ -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 = ( @@ -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 = (