Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: test cases for Add deposit #403

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions web_app/tests/test_positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import uuid
from datetime import datetime
from decimal import Decimal
from unittest.mock import Mock, patch

import pytest
Expand All @@ -17,6 +18,7 @@
from httpx import AsyncClient

from web_app.api.main import app
from web_app.api.position import add_extra_deposit

app.dependency_overrides.clear()

Expand Down Expand Up @@ -487,3 +489,128 @@ async def test_get_user_positions_no_positions(client: AsyncClient) -> None:
assert response.status_code == 200
data = response.json()
assert data == []

@pytest.mark.parametrize(
"position_id, amount, mock_position, expected_response",
[
(
1,
"100.0",
{
"id": 1,
"token_symbol": "ETH",
"amount": "1000",
"status": "opened"
},
{"detail": "Successfully added extra deposit"}
),
(
123,
"50.5",
{
"id": 123,
"token_symbol": "ETH",
"amount": "500",
"status": "opened"
},
{"detail": "Successfully added extra deposit"}
),
(
999,
"75.25",
{
"id": 999,
"token_symbol": "ETH",
"amount": "750",
"status": "opened"
},
{"detail": "Successfully added extra deposit"}
),
],
)
@pytest.mark.anyio
async def test_add_extra_deposit_success(
client: TestClient,
position_id: int,
amount: str,
mock_position: dict,
expected_response: dict,
) -> None:
"""
Test for successfully adding extra deposit to a position.

"""
with (
patch(
"web_app.db.crud.PositionDBConnector.get_position_by_id"
) as mock_get_position,
patch(
"web_app.db.crud.PositionDBConnector.add_extra_deposit_to_position"
) as mock_add_deposit,
):
mock_get_position.return_value = mock_position
mock_add_deposit.return_value = None

response = client.post(
f"/api/add-extra-deposit/{position_id}?amount={amount}"
)

assert response.status_code == 200
assert response.json() == expected_response
mock_get_position.assert_called_once_with(position_id)
mock_add_deposit.assert_called_once_with(mock_position, amount)


@pytest.mark.parametrize(
"position_id, amount, error_status, error_detail",
[
(
None,
"100.0",
404,
"Position ID is required"
),
(
1,
"",
404,
"Amount is required"
),
(
999,
"100.0",
404,
"Position not found"
),
(
"invalid",
"100.0",
422,
"Invalid position ID format"
),
],
)
@pytest.mark.anyio
async def test_add_extra_deposit_failure(
client: TestClient,
position_id: int,
amount: str,
error_status: int,
error_detail: str,
) -> None:
"""
Test various failure scenarios when adding extra deposit to a position.

"""
with patch(
"web_app.db.crud.PositionDBConnector.get_position_by_id"
) as mock_get_position:
if error_detail == "Position not found":
mock_get_position.return_value = None

response = client.post(
f"/api/add-extra-deposit/{position_id}?amount={amount}"
)

assert response.status_code == error_status
assert response.json() == {"detail": error_detail}
86 changes: 86 additions & 0 deletions web_app/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,89 @@ async def test_subscribe_to_notification(
assert response.status_code == expected_status_code
if expected_response:
assert response.json() == expected_response

@pytest.mark.asyncio
@patch("web_app.api.routes.user.CLIENT.withdraw_all")
@patch("web_app.api.routes.user.user_db.get_contract_address_by_wallet_id")
@pytest.mark.parametrize(
"wallet_id, contract_address, withdrawal_results, expected_status_code, expected_response",
[
# Positive case - successful withdrawal
(
"0x27994c503bd8c32525fbdaf9d398bdd4e86757988c64581b055a06c5955ea49",
"0x698b63df00be56ba39447c9b9ca576ffd0edba0526d98b3e8e4a902ffcf12f0",
{"ETH": "success", "USDT": "success"},
200,
{
"detail": "Successfully initiated withdrawals for all tokens",
"results": {"ETH": "success", "USDT": "success"}
}
),
# Negative case - contract not found
(
"invalid_wallet_id",
None,
None,
404,
{"detail": "Contract not found"}
),
# Negative case - empty wallet_id
(
"",
None,
None,
404,
{"detail": "Contract not found"}
),
# Edge case - valid wallet but no tokens to withdraw
(
"0x27994c503bd8c32525fbdaf9d398bdd4e86757988c64581b055a06c5955ea49",
"0x698b63df00be56ba39447c9b9ca576ffd0edba0526d98b3e8e4a902ffcf12f0",
{},
200,
{
"detail": "Successfully initiated withdrawals for all tokens",
"results": {}
}
),
],
)
async def test_withdraw_all(
mock_get_contract_address: MagicMock,
mock_withdraw_all: MagicMock,
client: client,
wallet_id: str,
contract_address: str,
withdrawal_results: dict,
expected_status_code: int,
expected_response: dict,
) -> None:
"""
Test withdraw_all endpoint with various scenarios

:param mock_get_contract_address: Mock for get_contract_address_by_wallet_id
:param mock_withdraw_all: Mock for CLIENT.withdraw_all
:param client: FastAPI test client
:param wallet_id: Wallet ID to test
:param contract_address: Expected contract address
:param withdrawal_results: Mock results from withdrawal operation
:param expected_status_code: Expected HTTP status code
:param expected_response: Expected response body
:return: None
"""
# Configure mocks
mock_get_contract_address.return_value = contract_address
if withdrawal_results is not None:
mock_withdraw_all.return_value = withdrawal_results

response = client.post(
url="/api/withdraw-all",
params={"wallet_id": wallet_id},
)

assert response.status_code == expected_status_code
assert response.json() == expected_response

mock_get_contract_address.assert_called_once_with(wallet_id)
if contract_address:
mock_withdraw_all.assert_called_once_with(contract_address)
Loading