From e5daf651f2b7afefce0aabb0721c607b540070e4 Mon Sep 17 00:00:00 2001 From: Olisa Agbafor Date: Sat, 23 Nov 2024 08:01:29 +0100 Subject: [PATCH 1/3] Test DepositDBConnector --- web_app/tests/db/deposit_db_tests.py | 98 ++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 web_app/tests/db/deposit_db_tests.py diff --git a/web_app/tests/db/deposit_db_tests.py b/web_app/tests/db/deposit_db_tests.py new file mode 100644 index 00000000..a33fee00 --- /dev/null +++ b/web_app/tests/db/deposit_db_tests.py @@ -0,0 +1,98 @@ +import unittest +from unittest.mock import MagicMock, patch +from decimal import Decimal +from web_app.db.crud import DepositDBConnector +from web_app.db.models import User, Vault + +class TestCreateVault(unittest.TestCase): + def setUp(self): + self.connector = DepositDBConnector() + self.connector.write_to_db = MagicMock() + + def test_create_vault_success(self): + user = User(id=1) + vault = self.connector.create_vault(user, "BTC", "1000") + self.connector.write_to_db.assert_called_once() + self.assertEqual(vault.user_id, user.id) + self.assertEqual(vault.symbol, "BTC") + self.assertEqual(vault.amount, "1000") + + def test_create_vault_db_failure(self): + self.connector.write_to_db.side_effect = Exception("DB write failed") + user = User(id=1) + with self.assertRaises(Exception) as context: + self.connector.create_vault(user, "BTC", "1000") + self.assertIn("DB write failed", str(context.exception)) + + + +class TestGetVault(unittest.TestCase): + def setUp(self): + self.connector = DepositDBConnector() + self.connector.get_object_by_field = MagicMock() + + @patch("your_module.DepositDBConnector.Session") + def test_get_vault_success(self, mock_session): + mock_db = mock_session.return_value.__enter__.return_value + user = User(id=1, wallet_id="wallet_123") + vault = Vault(user_id=1, symbol="BTC", amount="1000") + + self.connector.get_object_by_field.return_value = user + mock_db.query().filter_by().first.return_value = vault + + result = self.connector.get_vault("wallet_123", "BTC") + self.assertEqual(result, vault) + + def test_get_vault_user_not_found(self): + self.connector.get_object_by_field.return_value = None + result = self.connector.get_vault("invalid_wallet", "BTC") + self.assertIsNone(result) + + @patch("your_module.DepositDBConnector.Session") + def test_get_vault_not_found(self, mock_session): + mock_db = mock_session.return_value.__enter__.return_value + user = User(id=1, wallet_id="wallet_123") + + self.connector.get_object_by_field.return_value = user + mock_db.query().filter_by().first.return_value = None + + result = self.connector.get_vault("wallet_123", "BTC") + self.assertIsNone(result) + + +class TestAddVaultBalance(unittest.TestCase): + def setUp(self): + self.connector = DepositDBConnector() + self.connector.get_vault = MagicMock() + + @patch("your_module.DepositDBConnector.Session") + def test_add_vault_balance_success(self, mock_session): + mock_db = mock_session.return_value.__enter__.return_value + vault = Vault(id=1, user_id=1, symbol="BTC", amount="1000") + self.connector.get_vault.return_value = vault + + self.connector.add_vault_balance("wallet_123", "BTC", "500") + mock_db.query().filter_by().update.assert_called_once_with(amount="1500") + mock_db.commit.assert_called_once() + + def test_add_vault_balance_vault_not_found(self): + self.connector.get_vault.return_value = None + self.connector.add_vault_balance("wallet_123", "BTC", "500") + self.connector.get_vault.assert_called_once_with("wallet_123", "BTC") + + +class TestGetVaultBalance(unittest.TestCase): + def setUp(self): + self.connector = DepositDBConnector() + self.connector.get_vault = MagicMock() + + def test_get_vault_balance_success(self): + vault = Vault(user_id=1, symbol="BTC", amount="1000") + self.connector.get_vault.return_value = vault + result = self.connector.get_vault_balance("wallet_123", "BTC") + self.assertEqual(result, "1000") + + def test_get_vault_balance_vault_not_found(self): + self.connector.get_vault.return_value = None + result = self.connector.get_vault_balance("wallet_123", "BTC") + self.assertIsNone(result) From 22a44c932c6314c4edc63988a388b182593e31a5 Mon Sep 17 00:00:00 2001 From: Olisa Agbafor Date: Sat, 23 Nov 2024 16:36:01 +0100 Subject: [PATCH 2/3] Fix pylint issues and update tests to use mock fixtures --- web_app/tests/db/deposit_db_tests.py | 187 +++++++++++++-------------- 1 file changed, 93 insertions(+), 94 deletions(-) diff --git a/web_app/tests/db/deposit_db_tests.py b/web_app/tests/db/deposit_db_tests.py index a33fee00..b628bdd7 100644 --- a/web_app/tests/db/deposit_db_tests.py +++ b/web_app/tests/db/deposit_db_tests.py @@ -1,98 +1,97 @@ -import unittest -from unittest.mock import MagicMock, patch +""" +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 -class TestCreateVault(unittest.TestCase): - def setUp(self): - self.connector = DepositDBConnector() - self.connector.write_to_db = MagicMock() - - def test_create_vault_success(self): - user = User(id=1) - vault = self.connector.create_vault(user, "BTC", "1000") - self.connector.write_to_db.assert_called_once() - self.assertEqual(vault.user_id, user.id) - self.assertEqual(vault.symbol, "BTC") - self.assertEqual(vault.amount, "1000") - - def test_create_vault_db_failure(self): - self.connector.write_to_db.side_effect = Exception("DB write failed") - user = User(id=1) - with self.assertRaises(Exception) as context: - self.connector.create_vault(user, "BTC", "1000") - self.assertIn("DB write failed", str(context.exception)) - - - -class TestGetVault(unittest.TestCase): - def setUp(self): - self.connector = DepositDBConnector() - self.connector.get_object_by_field = MagicMock() - - @patch("your_module.DepositDBConnector.Session") - def test_get_vault_success(self, mock_session): - mock_db = mock_session.return_value.__enter__.return_value - user = User(id=1, wallet_id="wallet_123") - vault = Vault(user_id=1, symbol="BTC", amount="1000") - - self.connector.get_object_by_field.return_value = user - mock_db.query().filter_by().first.return_value = vault - - result = self.connector.get_vault("wallet_123", "BTC") - self.assertEqual(result, vault) - - def test_get_vault_user_not_found(self): - self.connector.get_object_by_field.return_value = None - result = self.connector.get_vault("invalid_wallet", "BTC") - self.assertIsNone(result) - - @patch("your_module.DepositDBConnector.Session") - def test_get_vault_not_found(self, mock_session): - mock_db = mock_session.return_value.__enter__.return_value - user = User(id=1, wallet_id="wallet_123") - - self.connector.get_object_by_field.return_value = user - mock_db.query().filter_by().first.return_value = None - - result = self.connector.get_vault("wallet_123", "BTC") - self.assertIsNone(result) - - -class TestAddVaultBalance(unittest.TestCase): - def setUp(self): - self.connector = DepositDBConnector() - self.connector.get_vault = MagicMock() - - @patch("your_module.DepositDBConnector.Session") - def test_add_vault_balance_success(self, mock_session): - mock_db = mock_session.return_value.__enter__.return_value - vault = Vault(id=1, user_id=1, symbol="BTC", amount="1000") - self.connector.get_vault.return_value = vault - - self.connector.add_vault_balance("wallet_123", "BTC", "500") - mock_db.query().filter_by().update.assert_called_once_with(amount="1500") - mock_db.commit.assert_called_once() - - def test_add_vault_balance_vault_not_found(self): - self.connector.get_vault.return_value = None - self.connector.add_vault_balance("wallet_123", "BTC", "500") - self.connector.get_vault.assert_called_once_with("wallet_123", "BTC") - - -class TestGetVaultBalance(unittest.TestCase): - def setUp(self): - self.connector = DepositDBConnector() - self.connector.get_vault = MagicMock() - - def test_get_vault_balance_success(self): - vault = Vault(user_id=1, symbol="BTC", amount="1000") - self.connector.get_vault.return_value = vault - result = self.connector.get_vault_balance("wallet_123", "BTC") - self.assertEqual(result, "1000") - - def test_get_vault_balance_vault_not_found(self): - self.connector.get_vault.return_value = None - result = self.connector.get_vault_balance("wallet_123", "BTC") - self.assertIsNone(result) + +@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) + + +# 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)} + ) From ab767b96413e3115bc27fa30f209113500436c6f Mon Sep 17 00:00:00 2001 From: Olisa Agbafor Date: Sun, 24 Nov 2024 08:51:51 +0100 Subject: [PATCH 3/3] Added negative test scenarios and synced with main branch --- web_app/tests/db/deposit_db_tests.py | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/web_app/tests/db/deposit_db_tests.py b/web_app/tests/db/deposit_db_tests.py index b628bdd7..3d834b51 100644 --- a/web_app/tests/db/deposit_db_tests.py +++ b/web_app/tests/db/deposit_db_tests.py @@ -66,6 +66,20 @@ def test_create_vault_success( 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: @@ -95,3 +109,20 @@ def test_add_balance_success( 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", + )