From 042d3ffe8007be0f9ec50b6752ac59bae9278ad1 Mon Sep 17 00:00:00 2001 From: binayak9932 Date: Thu, 24 Oct 2024 17:19:02 +0530 Subject: [PATCH 1/7] added test file test_positions.py --- web_app/tests/test_positions.py | 282 ++++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 web_app/tests/test_positions.py diff --git a/web_app/tests/test_positions.py b/web_app/tests/test_positions.py new file mode 100644 index 00000000..f558f12e --- /dev/null +++ b/web_app/tests/test_positions.py @@ -0,0 +1,282 @@ +from unittest.mock import Mock, patch + +import pytest +from fastapi import HTTPException +from fastapi.testclient import TestClient +from httpx import AsyncClient +from web_app.api.main import app +from web_app.contract_tools.mixins.deposit import DepositMixin + +client = TestClient(app) +app.dependency_overrides.clear() + + +# Test cases for /api/open-position +@pytest.mark.anyio +async def test_open_position_success(client: AsyncClient) -> None: + """ + Test for successfully opening a position using a valid position ID. + + Args: + client (AsyncClient): The test client for the FastAPI application. + + Returns: + None + """ + position_id = "valid_position_id" + with patch( + "web_app.db.crud.PositionDBConnector.open_position" + ) as mock_open_position: + mock_open_position.return_value = "Position successfully opened" + response = client.get(f"/api/open-position?position_id={position_id}") + assert response.status_code == 200 + assert response.json() == "Position successfully opened" + + +@pytest.mark.anyio +async def test_open_position_missing_position_data(client: AsyncClient) -> None: + """ + Test for missing position data, which should return a 404 error. + + Args: + client (AsyncClient): The test client for the FastAPI application. + + Returns: + None + """ + response = client.get("/api/open-position?position_id=") + assert response.status_code == 404 + assert response.json() == {"detail": "Position not found"} + + +# Test cases for /api/close-position +@pytest.mark.anyio +async def test_close_position_success(client: AsyncClient) -> None: + """ + Test for successfully closing a position using a valid position ID. + + Args: + client (AsyncClient): The test client for the FastAPI application. + + Returns: + None + """ + position_id = "valid_position_id" + with patch( + "web_app.db.crud.PositionDBConnector.close_position" + ) as mock_close_position: + mock_close_position.return_value = "Position successfully closed" + response = client.get(f"/api/close-position?position_id={position_id}") + assert response.status_code == 200 + assert response.json() == "Position successfully closed" + + +@pytest.mark.anyio +async def test_close_position_invalid_position_id(client: AsyncClient) -> None: + """ + Test for attempting to close a position using an invalid position ID, which should return a 404 error. + + Args: + client (AsyncClient): The test client for the FastAPI application. + + Returns: + None + """ + invalid_position_id = "invalid_position_id" + with patch( + "web_app.db.crud.PositionDBConnector.close_position" + ) as mock_close_position: + mock_close_position.side_effect = HTTPException( + status_code=404, detail="Position not Found" + ) + response = client.get(f"/api/close-position?position_id={invalid_position_id}") + assert response.status_code == 404 + assert response.json() == {"detail": "Position not Found"} + + +# Test cases for /api/get-repay-data +@pytest.mark.anyio +async def test_get_repay_data_success(client: AsyncClient) -> None: + """ + Test for successfully retrieving repayment data for a valid wallet ID and supply token. + + Args: + client (AsyncClient): The test client for the FastAPI application. + + Returns: + None + """ + supply_token = "valid_supply_token" + wallet_id = "valid_wallet_id" + mock_repay_data = { + "supply_token": "mock_supply_token", + "debt_token": "mock_debt_token", + "pool_key": { + "token0": "mock_token0", + "token1": "mock_token1", + "fee": "mock_fee", + "tick_spacing": "mock_tick_spacing", + "extension": "mock_extension", + }, + "supply_price": 100, + "debt_price": 200, + } + with ( + patch( + "web_app.contract_tools.mixins.deposit.DepositMixin.get_repay_data" + ) as mock_get_repay_data, + patch( + "web_app.db.crud.PositionDBConnector.get_contract_address_by_wallet_id" + ) as mock_get_contract_address, + patch( + "web_app.db.crud.PositionDBConnector.get_position_id_by_wallet_id" + ) as mock_get_position_id, + ): + mock_get_repay_data.return_value = mock_repay_data + mock_get_contract_address.return_value = "mock_contract_address" + mock_get_position_id.return_value = 123 + mock_get_repay_data.return_value = mock_repay_data + DepositMixin.get_repay_data = mock_get_repay_data + response = client.get( + f"/api/get-repay-data?supply_token={supply_token}&wallet_id={wallet_id}" + ) + mock_get_contract_address.assert_called_once_with(wallet_id) + mock_get_position_id.assert_called_once_with(wallet_id) + mock_get_repay_data.assert_called_once_with(supply_token) + expected_response = { + **mock_repay_data, + "contract_address": "mock_contract_address", + "position_id": "123", + } + assert response.status_code == 200 + assert response.json() == expected_response + + +@pytest.mark.anyio +async def test_get_repay_data_missing_wallet_id(client: AsyncClient) -> None: + """ + Test for missing wallet ID when attempting to retrieve repayment data, which should return a 404 error. + + Args: + client (AsyncClient): The test client for the FastAPI application. + + Returns: + None + """ + supply_token = "valid_supply_token" + wallet_id = "" + with ( + patch( + "web_app.contract_tools.mixins.deposit.DepositMixin.get_repay_data" + ) as mock_get_repay_data, + patch( + "web_app.db.crud.PositionDBConnector.get_contract_address_by_wallet_id" + ) as mock_get_contract_address, + patch( + "web_app.db.crud.PositionDBConnector.get_position_id_by_wallet_id" + ) as mock_get_position_id, + ): + mock_get_repay_data.return_value = None + mock_get_contract_address.side_effect = None + mock_get_position_id.side_effect = None + response = client.get( + f"/api/get-repay-data?supply_token={supply_token}&wallet_id={wallet_id}" + ) + assert response.status_code == 404 + assert response.json() == {"detail": "Wallet not found"} + + +# Test cases for /api/create_position_with_transaction_data +@pytest.mark.anyio +async def test_create_position_success(client: AsyncClient) -> None: + """ + Test for successfully creating a position with valid form data. + + Args: + client (AsyncClient): The test client for the FastAPI application. + + Returns: + None + """ + form_data = { + "wallet_id": "valid_wallet_id", + "token_symbol": "ETH", + "amount": "1000", + "multiplier": 2, + } + mock_position = Mock() + mock_position.id = 123 + mock_deposit_data = { + "caller": "mock_caller", + "pool_price": 100, + "pool_key": { + "token0": "mock_token0", + "token1": "mock_token1", + "fee": "mock_fee", + "tick_spacing": "mock_tick_spacing", + "extension": "mock_extension", + }, + "deposit_data": { + "token": "mock_token", + "amount": "mock_amount", + "multiplier": "mock_multiplier", + }, + "contract_address": "mock_contract_address", + "position_id": "123", + } + + with ( + patch( + "web_app.db.crud.PositionDBConnector.create_position" + ) as mock_create_position, + patch( + "web_app.contract_tools.mixins.deposit.DepositMixin.get_transaction_data" + ) as mock_get_transaction_data, + patch( + "web_app.db.crud.PositionDBConnector.get_contract_address_by_wallet_id" + ) as mock_get_contract_address, + ): + mock_create_position.return_value = mock_position + mock_get_transaction_data.return_value = mock_deposit_data + mock_get_contract_address.return_value = "mock_contract_address" + response = client.post("/api/create-position", json=form_data) + assert response.status_code == 200 + print(response.json()) + expected_response = { + "contract_address": "mock_contract_address", + "position_id": "123", + "caller": "mock_caller", + "pool_price": 100, + "pool_key": { + "token0": "mock_token0", + "token1": "mock_token1", + "fee": "mock_fee", + "tick_spacing": "mock_tick_spacing", + "extension": "mock_extension", + }, + "deposit_data": { + "token": "mock_token", + "amount": "mock_amount", + "multiplier": "mock_multiplier", + }, + } + assert response.json() == expected_response + + +@pytest.mark.anyio +async def test_create_position_invalid(client: AsyncClient) -> None: + """ + Test for attempting to create a position with invalid input data, which should return a 422 error. + + Args: + client (AsyncClient): The test client for the FastAPI application. + + Returns: + None + """ + response = client.post( + "/api/create-position", + json={"wallet_id": None, "token_symbol": "ETH", "amount": 100, "multiplier": 2}, + ) + assert response.status_code == 422 + assert "detail" in response.json() From 901286443037d46f969d9868ce1e7aa8a6b4f109 Mon Sep 17 00:00:00 2001 From: binayak9932 Date: Fri, 25 Oct 2024 00:30:52 +0530 Subject: [PATCH 2/7] made the requested changes --- web_app/tests/test_positions.py | 273 ++++++++++++++++++++++++-------- 1 file changed, 211 insertions(+), 62 deletions(-) diff --git a/web_app/tests/test_positions.py b/web_app/tests/test_positions.py index f558f12e..2a486f8c 100644 --- a/web_app/tests/test_positions.py +++ b/web_app/tests/test_positions.py @@ -1,3 +1,4 @@ +import uuid from unittest.mock import Mock, patch import pytest @@ -11,7 +12,6 @@ app.dependency_overrides.clear() -# Test cases for /api/open-position @pytest.mark.anyio async def test_open_position_success(client: AsyncClient) -> None: """ @@ -29,7 +29,7 @@ async def test_open_position_success(client: AsyncClient) -> None: ) as mock_open_position: mock_open_position.return_value = "Position successfully opened" response = client.get(f"/api/open-position?position_id={position_id}") - assert response.status_code == 200 + assert response.ok assert response.json() == "Position successfully opened" @@ -49,7 +49,6 @@ async def test_open_position_missing_position_data(client: AsyncClient) -> None: assert response.json() == {"detail": "Position not found"} -# Test cases for /api/close-position @pytest.mark.anyio async def test_close_position_success(client: AsyncClient) -> None: """ @@ -61,13 +60,13 @@ async def test_close_position_success(client: AsyncClient) -> None: Returns: None """ - position_id = "valid_position_id" + position_id = str(uuid.uuid4()) with patch( "web_app.db.crud.PositionDBConnector.close_position" ) as mock_close_position: mock_close_position.return_value = "Position successfully closed" response = client.get(f"/api/close-position?position_id={position_id}") - assert response.status_code == 200 + assert response.ok assert response.json() == "Position successfully closed" @@ -94,33 +93,78 @@ async def test_close_position_invalid_position_id(client: AsyncClient) -> None: assert response.json() == {"detail": "Position not Found"} -# Test cases for /api/get-repay-data +@pytest.mark.parametrize( + "supply_token, wallet_id, mock_repay_data", + [ + ( + "valid_supply_token", + "valid_wallet_id", + { + "supply_token": "mock_supply_token", + "debt_token": "mock_debt_token", + "pool_key": { + "token0": "mock_token0", + "token1": "mock_token1", + "fee": "mock_fee", + "tick_spacing": "mock_tick_spacing", + "extension": "mock_extension", + }, + "supply_price": 100, + "debt_price": 200, + }, + ), + ( + "invalid_supply_token", + "valid_wallet_id", + { + "supply_token": "mock_supply_token", + "debt_token": "mock_debt_token", + "pool_key": { + "token0": "mock_token0", + "token1": "mock_token1", + "fee": "mock_fee", + "tick_spacing": "mock_tick_spacing", + "extension": "mock_extension", + }, + "supply_price": 0, + "debt_price": 0, + }, + ), + ( + "valid_supply_token", + "invalid_wallet_id", + { + "supply_token": "mock_supply_token", + "debt_token": "mock_debt_token", + "pool_key": { + "token0": "mock_token0", + "token1": "mock_token1", + "fee": "mock_fee", + "tick_spacing": "mock_tick_spacing", + "extension": "mock_extension", + }, + "supply_price": 0, + "debt_price": 0, + }, + ), + ], +) @pytest.mark.anyio -async def test_get_repay_data_success(client: AsyncClient) -> None: +async def test_get_repay_data_success( + client: AsyncClient, supply_token, wallet_id, mock_repay_data +) -> None: """ - Test for successfully retrieving repayment data for a valid wallet ID and supply token. + Test for successfully retrieving repayment data for different combinations of wallet ID and supply token. Args: client (AsyncClient): The test client for the FastAPI application. + supply_token (str): The token used for supply. + wallet_id (str): The wallet ID of the user. + mock_repay_data (dict): Mocked repayment data. Returns: None """ - supply_token = "valid_supply_token" - wallet_id = "valid_wallet_id" - mock_repay_data = { - "supply_token": "mock_supply_token", - "debt_token": "mock_debt_token", - "pool_key": { - "token0": "mock_token0", - "token1": "mock_token1", - "fee": "mock_fee", - "tick_spacing": "mock_tick_spacing", - "extension": "mock_extension", - }, - "supply_price": 100, - "debt_price": 200, - } with ( patch( "web_app.contract_tools.mixins.deposit.DepositMixin.get_repay_data" @@ -148,23 +192,38 @@ async def test_get_repay_data_success(client: AsyncClient) -> None: "contract_address": "mock_contract_address", "position_id": "123", } - assert response.status_code == 200 + assert response.ok assert response.json() == expected_response +@pytest.mark.parametrize( + "supply_token, wallet_id, expected_status, expected_response", + [ + ("valid_supply_token", "", 404, {"detail": "Wallet not found"}), + ("valid_supply_token", None, 404, {"detail": "Wallet not found"}), + ( + "valid_supply_token", + "invalid_wallet_id", + 404, + {"detail": "Wallet not found"}, + ), + ], +) @pytest.mark.anyio -async def test_get_repay_data_missing_wallet_id(client: AsyncClient) -> None: +async def test_get_repay_data_missing_wallet_id( + client: AsyncClient, supply_token, wallet_id, expected_status, expected_response +) -> None: """ - Test for missing wallet ID when attempting to retrieve repayment data, which should return a 404 error. + Test for missing or invalid wallet ID when attempting to retrieve repayment data, which should return a 404 error. Args: client (AsyncClient): The test client for the FastAPI application. - + supply_token (str): The supply token used for repayment. + wallet_id (str): The wallet ID of the user. + expected_status (int): Expected HTTP status code. Returns: None """ - supply_token = "valid_supply_token" - wallet_id = "" with ( patch( "web_app.contract_tools.mixins.deposit.DepositMixin.get_repay_data" @@ -182,13 +241,91 @@ async def test_get_repay_data_missing_wallet_id(client: AsyncClient) -> None: response = client.get( f"/api/get-repay-data?supply_token={supply_token}&wallet_id={wallet_id}" ) - assert response.status_code == 404 - assert response.json() == {"detail": "Wallet not found"} + assert response.status_code == expected_status + assert response.json() == expected_response -# Test cases for /api/create_position_with_transaction_data +@pytest.mark.parametrize( + "wallet_id, token_symbol, amount, multiplier, expected_response", + [ + ( + "valid_wallet_id", + "ETH", + "1000", + 2, + { + "contract_address": "mock_contract_address", + "position_id": "123", + "caller": "mock_caller", + "pool_price": 100, + "pool_key": { + "token0": "mock_token0", + "token1": "mock_token1", + "fee": "mock_fee", + "tick_spacing": "mock_tick_spacing", + "extension": "mock_extension", + }, + "deposit_data": { + "token": "mock_token", + "amount": "mock_amount", + "multiplier": "mock_multiplier", + }, + }, + ), + ( + "valid_wallet_id_2", + "BTC", + "500", + 1, + { + "contract_address": "mock_contract_address", + "position_id": "123", + "caller": "mock_caller", + "pool_price": 100, + "pool_key": { + "token0": "mock_token0", + "token1": "mock_token1", + "fee": "mock_fee", + "tick_spacing": "mock_tick_spacing", + "extension": "mock_extension", + }, + "deposit_data": { + "token": "mock_token", + "amount": "mock_amount", + "multiplier": "mock_multiplier", + }, + }, + ), + ( + "valid_wallet_id_3", + "SOL", + "1500", + 3, + { + "contract_address": "mock_contract_address", + "position_id": "123", + "caller": "mock_caller", + "pool_price": 100, + "pool_key": { + "token0": "mock_token0", + "token1": "mock_token1", + "fee": "mock_fee", + "tick_spacing": "mock_tick_spacing", + "extension": "mock_extension", + }, + "deposit_data": { + "token": "mock_token", + "amount": "mock_amount", + "multiplier": "mock_multiplier", + }, + }, + ), + ], +) @pytest.mark.anyio -async def test_create_position_success(client: AsyncClient) -> None: +async def test_create_position_success( + client: AsyncClient, wallet_id, token_symbol, amount, multiplier, expected_response +) -> None: """ Test for successfully creating a position with valid form data. @@ -199,10 +336,10 @@ async def test_create_position_success(client: AsyncClient) -> None: None """ form_data = { - "wallet_id": "valid_wallet_id", - "token_symbol": "ETH", - "amount": "1000", - "multiplier": 2, + "wallet_id": wallet_id, + "token_symbol": token_symbol, + "amount": amount, + "multiplier": multiplier, } mock_position = Mock() mock_position.id = 123 @@ -240,43 +377,55 @@ async def test_create_position_success(client: AsyncClient) -> None: mock_get_transaction_data.return_value = mock_deposit_data mock_get_contract_address.return_value = "mock_contract_address" response = client.post("/api/create-position", json=form_data) - assert response.status_code == 200 - print(response.json()) - expected_response = { - "contract_address": "mock_contract_address", - "position_id": "123", - "caller": "mock_caller", - "pool_price": 100, - "pool_key": { - "token0": "mock_token0", - "token1": "mock_token1", - "fee": "mock_fee", - "tick_spacing": "mock_tick_spacing", - "extension": "mock_extension", - }, - "deposit_data": { - "token": "mock_token", - "amount": "mock_amount", - "multiplier": "mock_multiplier", - }, - } + assert response.ok assert response.json() == expected_response +@pytest.mark.parametrize( + "wallet_id, token_symbol, amount, multiplier, expected_status", + [ + ("valid_wallet_id", "ETH", 100, 2, 200), + (None, "ETH", 100, 2, 422), + ("valid_wallet_id", "", 100, 2, 422), + ("valid_wallet_id", None, 100, 2, 422), + ("valid_wallet_id", "BTC", "invalid_amount", 2, 422), + ("valid_wallet_id", "BTC", -50, 2, 422), + ("valid_wallet_id", "BTC", None, 2, 422), + ("valid_wallet_id", "BTC", "50", 2, 422), + ("valid_wallet_id", "BTC", 100, "invalid_multiplier", 422), + ("valid_wallet_id", "BTC", 100, None, 422), + ("valid_wallet_id", "BTC", 100, "1.5", 422), + ("valid_wallet_id", "BTC", 100, -1, 422), + ], +) @pytest.mark.anyio -async def test_create_position_invalid(client: AsyncClient) -> None: +async def test_create_position_invalid( + client: AsyncClient, wallet_id, token_symbol, amount, multiplier, expected_status +) -> None: """ - Test for attempting to create a position with invalid input data, which should return a 422 error. + Test for attempting to create a position with various valid and invalid input data. + Should return 422 for invalid data and 200 for valid data. Args: client (AsyncClient): The test client for the FastAPI application. + wallet_id: The wallet ID of the user. + token_symbol: The symbol of the token. + amount: The amount to be used. + multiplier: The multiplier value. + expected_status: The expected HTTP status code. Returns: None """ response = client.post( "/api/create-position", - json={"wallet_id": None, "token_symbol": "ETH", "amount": 100, "multiplier": 2}, + json={ + "wallet_id": wallet_id, + "token_symbol": token_symbol, + "amount": amount, + "multiplier": multiplier, + }, ) - assert response.status_code == 422 - assert "detail" in response.json() + assert response.status_code == expected_status + if expected_status == 422: + assert "detail" in response.json() From 1ff75755db66377e2c488baa8f7cc616fcca7581 Mon Sep 17 00:00:00 2001 From: binayak9932 Date: Fri, 25 Oct 2024 02:39:52 +0530 Subject: [PATCH 3/7] Save changes --- web_app/tests/test_positions.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/web_app/tests/test_positions.py b/web_app/tests/test_positions.py index 2a486f8c..05f3d864 100644 --- a/web_app/tests/test_positions.py +++ b/web_app/tests/test_positions.py @@ -335,12 +335,6 @@ async def test_create_position_success( Returns: None """ - form_data = { - "wallet_id": wallet_id, - "token_symbol": token_symbol, - "amount": amount, - "multiplier": multiplier, - } mock_position = Mock() mock_position.id = 123 mock_deposit_data = { From 0a976e5a8f4215326efb0e1eeb4c76fa67df665a Mon Sep 17 00:00:00 2001 From: binayak9932 Date: Fri, 25 Oct 2024 02:51:18 +0530 Subject: [PATCH 4/7] changes --- web_app/tests/test_positions.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/web_app/tests/test_positions.py b/web_app/tests/test_positions.py index 05f3d864..f63c9706 100644 --- a/web_app/tests/test_positions.py +++ b/web_app/tests/test_positions.py @@ -370,7 +370,15 @@ async def test_create_position_success( mock_create_position.return_value = mock_position mock_get_transaction_data.return_value = mock_deposit_data mock_get_contract_address.return_value = "mock_contract_address" - response = client.post("/api/create-position", json=form_data) + response = await client.post( + "/api/create-position-with-transaction-data", + data={ + "wallet_id": wallet_id, + "token_symbol": token_symbol, + "amount": amount, + "multiplier": multiplier, + }, + ) assert response.ok assert response.json() == expected_response From 9423b507af3ae7003df1d7e558aacb9603ab2496 Mon Sep 17 00:00:00 2001 From: binayak9932 Date: Fri, 25 Oct 2024 02:53:21 +0530 Subject: [PATCH 5/7] final --- web_app/tests/test_positions.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/web_app/tests/test_positions.py b/web_app/tests/test_positions.py index f63c9706..ebc96cd1 100644 --- a/web_app/tests/test_positions.py +++ b/web_app/tests/test_positions.py @@ -370,15 +370,7 @@ async def test_create_position_success( mock_create_position.return_value = mock_position mock_get_transaction_data.return_value = mock_deposit_data mock_get_contract_address.return_value = "mock_contract_address" - response = await client.post( - "/api/create-position-with-transaction-data", - data={ - "wallet_id": wallet_id, - "token_symbol": token_symbol, - "amount": amount, - "multiplier": multiplier, - }, - ) + response = await client.post("/api/create-position-with-transaction-data",data={"wallet_id": wallet_id,"token_symbol": token_symbol,"amount": amount,"multiplier": multiplier,},) assert response.ok assert response.json() == expected_response From f77d03ec49718f17c93d78d2cb8edf701ae5d510 Mon Sep 17 00:00:00 2001 From: binayak9932 Date: Fri, 25 Oct 2024 13:00:33 +0530 Subject: [PATCH 6/7] pylint --- web_app/tests/test_positions.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/web_app/tests/test_positions.py b/web_app/tests/test_positions.py index ebc96cd1..3443d68b 100644 --- a/web_app/tests/test_positions.py +++ b/web_app/tests/test_positions.py @@ -1,6 +1,16 @@ +""" +test_positions.py + +This module contains unit tests for the positions functionality within the web_app. +It verifies the creation, retrieval, updating, and deletion of positions, ensuring +that all edge cases and error scenarios are appropriately handled. + +""" + + + import uuid from unittest.mock import Mock, patch - import pytest from fastapi import HTTPException from fastapi.testclient import TestClient @@ -73,7 +83,8 @@ async def test_close_position_success(client: AsyncClient) -> None: @pytest.mark.anyio async def test_close_position_invalid_position_id(client: AsyncClient) -> None: """ - Test for attempting to close a position using an invalid position ID, which should return a 404 error. + Test for attempting to close a position using an invalid position IC + which should return a 404 error. Args: client (AsyncClient): The test client for the FastAPI application. @@ -154,7 +165,8 @@ async def test_get_repay_data_success( client: AsyncClient, supply_token, wallet_id, mock_repay_data ) -> None: """ - Test for successfully retrieving repayment data for different combinations of wallet ID and supply token. + Test for successfully retrieving repayment data for + different combinations of wallet ID and supply token. Args: client (AsyncClient): The test client for the FastAPI application. @@ -214,7 +226,8 @@ async def test_get_repay_data_missing_wallet_id( client: AsyncClient, supply_token, wallet_id, expected_status, expected_response ) -> None: """ - Test for missing or invalid wallet ID when attempting to retrieve repayment data, which should return a 404 error. + Test for missing or invalid wallet ID when attempting to retrieve repayment data, + which should return a 404 error. Args: client (AsyncClient): The test client for the FastAPI application. @@ -370,7 +383,15 @@ async def test_create_position_success( mock_create_position.return_value = mock_position mock_get_transaction_data.return_value = mock_deposit_data mock_get_contract_address.return_value = "mock_contract_address" - response = await client.post("/api/create-position-with-transaction-data",data={"wallet_id": wallet_id,"token_symbol": token_symbol,"amount": amount,"multiplier": multiplier,},) + response = await client.post( + "/api/create-position-with-transaction-data", + data={ + "wallet_id": wallet_id, + "token_symbol": token_symbol, + "amount": amount, + "multiplier": multiplier, + }, + ) assert response.ok assert response.json() == expected_response From ee388987950667c0b92c7ef86c0f098682ee4a2e Mon Sep 17 00:00:00 2001 From: binayak9932 Date: Fri, 25 Oct 2024 15:05:33 +0530 Subject: [PATCH 7/7] fixes --- web_app/tests/test_positions.py | 136 +++++++++++--------------------- 1 file changed, 48 insertions(+), 88 deletions(-) diff --git a/web_app/tests/test_positions.py b/web_app/tests/test_positions.py index 3443d68b..ee627eda 100644 --- a/web_app/tests/test_positions.py +++ b/web_app/tests/test_positions.py @@ -1,16 +1,15 @@ """ test_positions.py - This module contains unit tests for the positions functionality within the web_app. It verifies the creation, retrieval, updating, and deletion of positions, ensuring that all edge cases and error scenarios are appropriately handled. -""" - +""" import uuid from unittest.mock import Mock, patch + import pytest from fastapi import HTTPException from fastapi.testclient import TestClient @@ -26,10 +25,8 @@ async def test_open_position_success(client: AsyncClient) -> None: """ Test for successfully opening a position using a valid position ID. - Args: client (AsyncClient): The test client for the FastAPI application. - Returns: None """ @@ -39,7 +36,7 @@ async def test_open_position_success(client: AsyncClient) -> None: ) as mock_open_position: mock_open_position.return_value = "Position successfully opened" response = client.get(f"/api/open-position?position_id={position_id}") - assert response.ok + assert response.is_success assert response.json() == "Position successfully opened" @@ -47,10 +44,8 @@ async def test_open_position_success(client: AsyncClient) -> None: async def test_open_position_missing_position_data(client: AsyncClient) -> None: """ Test for missing position data, which should return a 404 error. - Args: client (AsyncClient): The test client for the FastAPI application. - Returns: None """ @@ -63,10 +58,8 @@ async def test_open_position_missing_position_data(client: AsyncClient) -> None: async def test_close_position_success(client: AsyncClient) -> None: """ Test for successfully closing a position using a valid position ID. - Args: client (AsyncClient): The test client for the FastAPI application. - Returns: None """ @@ -76,19 +69,17 @@ async def test_close_position_success(client: AsyncClient) -> None: ) as mock_close_position: mock_close_position.return_value = "Position successfully closed" response = client.get(f"/api/close-position?position_id={position_id}") - assert response.ok + assert response.is_success assert response.json() == "Position successfully closed" @pytest.mark.anyio async def test_close_position_invalid_position_id(client: AsyncClient) -> None: """ - Test for attempting to close a position using an invalid position IC + Test for attempting to close a position using an invalid position ID, which should return a 404 error. - Args: client (AsyncClient): The test client for the FastAPI application. - Returns: None """ @@ -165,15 +156,14 @@ async def test_get_repay_data_success( client: AsyncClient, supply_token, wallet_id, mock_repay_data ) -> None: """ - Test for successfully retrieving repayment data for - different combinations of wallet ID and supply token. - + Test for successfully retrieving repayment data for + different combinations of + wallet ID and supply token. Args: client (AsyncClient): The test client for the FastAPI application. supply_token (str): The token used for supply. wallet_id (str): The wallet ID of the user. mock_repay_data (dict): Mocked repayment data. - Returns: None """ @@ -204,39 +194,22 @@ async def test_get_repay_data_success( "contract_address": "mock_contract_address", "position_id": "123", } - assert response.ok + assert response.is_success assert response.json() == expected_response -@pytest.mark.parametrize( - "supply_token, wallet_id, expected_status, expected_response", - [ - ("valid_supply_token", "", 404, {"detail": "Wallet not found"}), - ("valid_supply_token", None, 404, {"detail": "Wallet not found"}), - ( - "valid_supply_token", - "invalid_wallet_id", - 404, - {"detail": "Wallet not found"}, - ), - ], -) @pytest.mark.anyio -async def test_get_repay_data_missing_wallet_id( - client: AsyncClient, supply_token, wallet_id, expected_status, expected_response -) -> None: +async def test_get_repay_data_missing_wallet_id(client: AsyncClient) -> None: """ - Test for missing or invalid wallet ID when attempting to retrieve repayment data, + Test for missing wallet ID when attempting to retrieve repayment data, which should return a 404 error. - Args: client (AsyncClient): The test client for the FastAPI application. - supply_token (str): The supply token used for repayment. - wallet_id (str): The wallet ID of the user. - expected_status (int): Expected HTTP status code. Returns: None """ + supply_token = "valid_supply_token" + wallet_id = "" with ( patch( "web_app.contract_tools.mixins.deposit.DepositMixin.get_repay_data" @@ -254,8 +227,8 @@ async def test_get_repay_data_missing_wallet_id( response = client.get( f"/api/get-repay-data?supply_token={supply_token}&wallet_id={wallet_id}" ) - assert response.status_code == expected_status - assert response.json() == expected_response + assert response.status_code == 404 + assert response.json() == {"detail": "Wallet not found"} @pytest.mark.parametrize( @@ -287,7 +260,7 @@ async def test_get_repay_data_missing_wallet_id( ), ( "valid_wallet_id_2", - "BTC", + "ETH", "500", 1, { @@ -311,7 +284,7 @@ async def test_get_repay_data_missing_wallet_id( ), ( "valid_wallet_id_3", - "SOL", + "ETH", "1500", 3, { @@ -337,16 +310,10 @@ async def test_get_repay_data_missing_wallet_id( ) @pytest.mark.anyio async def test_create_position_success( - client: AsyncClient, wallet_id, token_symbol, amount, multiplier, expected_response + wallet_id, token_symbol, amount, multiplier, expected_response ) -> None: """ Test for successfully creating a position with valid form data. - - Args: - client (AsyncClient): The test client for the FastAPI application. - - Returns: - None """ mock_position = Mock() mock_position.id = 123 @@ -380,57 +347,50 @@ async def test_create_position_success( "web_app.db.crud.PositionDBConnector.get_contract_address_by_wallet_id" ) as mock_get_contract_address, ): + mock_create_position.return_value = mock_position mock_get_transaction_data.return_value = mock_deposit_data mock_get_contract_address.return_value = "mock_contract_address" - response = await client.post( - "/api/create-position-with-transaction-data", - data={ - "wallet_id": wallet_id, - "token_symbol": token_symbol, - "amount": amount, - "multiplier": multiplier, - }, - ) - assert response.ok - assert response.json() == expected_response + + async with AsyncClient(app=app, base_url="http://test") as async_client: + response = await async_client.post( + "/api/create-position", + json={ + "wallet_id": wallet_id, + "token_symbol": token_symbol, + "amount": amount, + "multiplier": multiplier, + }, + ) + assert ( + response.status_code == 200 + ), f"Expected status code 200 but got {response.status_code}" + assert ( + response.json() == expected_response + ), f"Response JSON does not match expected response" @pytest.mark.parametrize( "wallet_id, token_symbol, amount, multiplier, expected_status", [ - ("valid_wallet_id", "ETH", 100, 2, 200), (None, "ETH", 100, 2, 422), - ("valid_wallet_id", "", 100, 2, 422), - ("valid_wallet_id", None, 100, 2, 422), - ("valid_wallet_id", "BTC", "invalid_amount", 2, 422), - ("valid_wallet_id", "BTC", -50, 2, 422), - ("valid_wallet_id", "BTC", None, 2, 422), - ("valid_wallet_id", "BTC", "50", 2, 422), - ("valid_wallet_id", "BTC", 100, "invalid_multiplier", 422), - ("valid_wallet_id", "BTC", 100, None, 422), - ("valid_wallet_id", "BTC", 100, "1.5", 422), - ("valid_wallet_id", "BTC", 100, -1, 422), + (12345, "", 100, 2, 422), + (12345, None, 100, 2, 422), + (12345, "ETH", "invalid_amount", 2, 422), + (12345, "ETH", -50, 2, 422), + (12345, "ETH", None, 2, 422), + (12345, "ETH", "50", 2, 422), + (12345, "ETH", 100, "invalid_multiplier", 422), + (12345, "ETH", 100, None, 422), + (12345, "ETH", 100, "1.5", 422), ], ) -@pytest.mark.anyio -async def test_create_position_invalid( - client: AsyncClient, wallet_id, token_symbol, amount, multiplier, expected_status -) -> None: +def test_create_position_invalid( + wallet_id, token_symbol, amount, multiplier, expected_status +): """ Test for attempting to create a position with various valid and invalid input data. Should return 422 for invalid data and 200 for valid data. - - Args: - client (AsyncClient): The test client for the FastAPI application. - wallet_id: The wallet ID of the user. - token_symbol: The symbol of the token. - amount: The amount to be used. - multiplier: The multiplier value. - expected_status: The expected HTTP status code. - - Returns: - None """ response = client.post( "/api/create-position",