Skip to content

Commit

Permalink
Merge pull request #249 from olisaagbafor/fix-245
Browse files Browse the repository at this point in the history
Test DepositDBConnector
  • Loading branch information
djeck1432 authored Nov 24, 2024
2 parents af9cd1f + 3d582e2 commit 0777b56
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions web_app/tests/db/deposit_db_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""
Test cases for DepositDBConnector functionality in web_app.
"""

from unittest.mock import MagicMock
from decimal import Decimal
import pytest

from web_app.db.crud import DepositDBConnector
from web_app.db.models import User, Vault


@pytest.fixture
def deposit_db_connector_fixture(mock_db_connector):
"""
Fixture to provide a mocked DepositDBConnector instance using mock_db_connector.
"""
connector = DepositDBConnector()
connector.Session = mock_db_connector.Session
connector.get_object_by_field = mock_db_connector.get_object_by_field
connector.write_to_db = mock_db_connector.write_to_db
return connector


@pytest.fixture
def mock_user_fixture():
"""
Mocked User instance.
"""
return User(id="user123", wallet_id="wallet123")


@pytest.fixture
def mock_vault_fixture():
"""
Mocked Vault instance.
"""
return Vault(id="vault123", user_id="user123", symbol="ETH", amount="100.00")


# pylint: disable=too-few-public-methods
class TestCreateVault:
"""
Tests for creating a vault using DepositDBConnector.
"""

def test_create_vault_success(
self,
deposit_db_connector: DepositDBConnector,
mock_user: User,
mock_db_connector,
):
"""
Test successful creation of a vault using fixtures.
"""
mock_db_connector.write_to_db = MagicMock() # Use mock_db_connector's method

vault = deposit_db_connector.create_vault(
user=mock_user,
symbol="BTC",
amount="50.00",
)

assert vault.symbol == "BTC"
assert vault.amount == "50.00"
assert vault.user_id == mock_user.id
mock_db_connector.write_to_db.assert_called_once_with(vault)

def test_create_vault_failure_invalid_user(
self,
deposit_db_connector: DepositDBConnector,
):
"""
Test failure when creating a vault with an invalid user.
"""
with pytest.raises(ValueError, match="Invalid user provided"):
deposit_db_connector.create_vault(
user=None,
symbol="BTC",
amount="50.00",
)


# pylint: disable=too-few-public-methods
class TestAddVaultBalance:
"""
Tests for adding to a vault's balance using DepositDBConnector.
"""

def test_add_balance_success(
self,
deposit_db_connector: DepositDBConnector,
mock_vault: Vault,
mock_db_connector,
):
"""
Test successfully adding to a vault's balance using fixtures.
"""
mock_db_connector.get_object_by_field = MagicMock(return_value=mock_vault)
mock_db_connector.Session().query().filter_by().update = MagicMock()

deposit_db_connector.add_vault_balance(
wallet_id="wallet123",
symbol="ETH",
amount="50.00",
)

updated_amount = Decimal(mock_vault.amount) + Decimal("50.00")
mock_db_connector.Session().query().filter_by().update.assert_called_once_with(
{"amount": str(updated_amount)}
)

def test_add_balance_failure_vault_not_found(
self,
deposit_db_connector: DepositDBConnector,
mock_db_connector,
):
"""
Test failure when adding to a vault balance that doesn't exist.
"""
mock_db_connector.get_object_by_field = MagicMock(return_value=None)

with pytest.raises(ValueError, match="Vault not found"):
deposit_db_connector.add_vault_balance(
wallet_id="invalid_wallet",
symbol="ETH",
amount="50.00",
)

0 comments on commit 0777b56

Please sign in to comment.