Skip to content

Commit

Permalink
fix test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Elsie-ND committed Dec 1, 2024
1 parent 7dda2eb commit 1ca6173
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
30 changes: 15 additions & 15 deletions web_app/tests/db/test_PositionDBConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,27 +161,26 @@ def test_get_total_amounts_for_open_positions(mock_position_db_connector):
assert result == Decimal(1000.0)


def test_delete_all_user_positions_success(mock_session):
@patch("web_app.db.connectors.PositionDBConnector")
def test_delete_all_user_positions_success(mock_position_db_connector):
"""Test successfully deleting all positions for a user."""
user_id = uuid.uuid4()
mock_positions = [
Position(id=uuid.uuid4(), user_id=user_id, token_symbol="BTC", amount="10"),
Position(id=uuid.uuid4(), user_id=user_id, token_symbol="ETH", amount="5"),
]
mock_session = mock_position_db_connector.Session.return_value
mock_session.query.return_value.filter_by.return_value.all.return_value = (
mock_positions
)

position_connector = PositionDBConnector()
with patch.object(position_connector, "Session", return_value=mock_session):
position_connector.delete_all_user_positions(user_id)
position_connector.delete_all_user_positions(user_id)

mock_session.query.assert_called_once_with(Position)
mock_session.query.return_value.filter_by.assert_called_once_with(
user_id=user_id
)
assert mock_session.delete.call_count == len(mock_positions)
mock_session.commit.assert_called_once()
mock_session.query.assert_called_once_with(Position)
mock_session.query.return_value.filter_by.assert_called_once_with(user_id=user_id)
assert mock_session.delete.call_count == len(mock_positions)
mock_session.commit.assert_called_once()


### Negative Test Cases ###
Expand Down Expand Up @@ -249,15 +248,16 @@ def test_get_position_id_by_wallet_id_no_positions(mock_position_db_connector):
assert result is None


def test_delete_all_user_positions_failure(mock_session):
@patch("web_app.db.connectors.PositionDBConnector")
def test_delete_all_user_positions_failure(mock_position_db_connector):
"""Test failure during deletion of all positions for a user."""
user_id = uuid.uuid4()
mock_session = mock_position_db_connector.Session.return_value
mock_session.query.side_effect = SQLAlchemyError("Database error")

position_connector = PositionDBConnector()
with patch.object(position_connector, "Session", return_value=mock_session):
with pytest.raises(SQLAlchemyError):
position_connector.delete_all_user_positions(user_id)

mock_session.query.assert_called_once_with(Position)
mock_session.rollback.assert_called_once()
with pytest.raises(SQLAlchemyError):
position_connector.delete_all_user_positions(user_id)

mock_session.rollback.assert_called_once()
17 changes: 12 additions & 5 deletions web_app/tests/db/test_user_dbconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest
from sqlalchemy.exc import SQLAlchemyError
from web_app.db.crud import UserDBConnector
from web_app.db.crud import AirDropDBConnector, UserDBConnector
from web_app.db.models import AirDrop, User


Expand Down Expand Up @@ -100,12 +100,15 @@ def test_delete_all_users_airdrop_success(user_db):
AirDrop(id=1, user_id=user_id),
AirDrop(id=2, user_id=user_id),
]
with patch.object(user_db, "Session", return_value=mock_session):
with patch.object(
"web_app.db.crud.AirDropDBConnector.Session", return_value=mock_session
):
mock_session.query.return_value.filter_by.return_value.all.return_value = (
mock_airdrops
)

user_db.delete_all_users_airdrop(user_id)
air_drop_connector = AirDropDBConnector()
air_drop_connector.delete_all_users_airdrop(user_id)

mock_session.query.assert_called_once_with(AirDrop)
mock_session.query.return_value.filter_by.assert_called_once_with(
Expand All @@ -122,9 +125,13 @@ def test_delete_all_users_airdrop_failure(user_db):
user_id = "123e4567-e89b-12d3-a456-426614174000"
mock_session = MagicMock()
mock_session.query.side_effect = SQLAlchemyError("Database error")
with patch.object(user_db, "Session", return_value=mock_session):
with patch.object(
"web_app.db.crud.AirDropDBConnector.Session", return_value=mock_session
):
air_drop_connector = AirDropDBConnector()

with pytest.raises(SQLAlchemyError):
user_db.delete_all_users_airdrop(user_id)
air_drop_connector.delete_all_users_airdrop(user_id)

mock_session.query.assert_called_once_with(AirDrop)
mock_session.rollback.assert_called_once()

0 comments on commit 1ca6173

Please sign in to comment.