From c5fd26cb3ee14d8fde977858ac0390a77617b9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elisi=C3=A1rio=20Couto?= Date: Mon, 8 Sep 2025 19:51:19 +0100 Subject: [PATCH] Create temporary database for testing instead of using configured database - Add temp_db_path fixture to create temporary database file for tests - Add mock_db_path fixture to mock Path.home() for database path resolution - Update all account API tests to use temporary database - Ensure test database is properly cleaned up after tests - Prevent test data from polluting the actual configured database - All 94 tests still pass with temporary database setup --- tests/conftest.py | 35 ++++++++++++++++++++++++++++++++- tests/unit/test_api_accounts.py | 22 +++++++++++++++++---- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index ba94be4..58c3460 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -21,7 +21,18 @@ def temp_config_dir(): @pytest.fixture -def mock_config(temp_config_dir): +def temp_db_path(): + """Create a temporary database file for testing.""" + with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as tmp_file: + db_path = Path(tmp_file.name) + yield db_path + # Clean up the temporary database file after test + if db_path.exists(): + db_path.unlink() + + +@pytest.fixture +def mock_config(temp_config_dir, temp_db_path): """Mock configuration for testing.""" config_data = { "gocardless": { @@ -72,6 +83,28 @@ def api_client(fastapi_app): return TestClient(fastapi_app) +@pytest.fixture +def mock_db_path(temp_db_path): + """Mock the database path to use temporary database for testing.""" + from pathlib import Path + + # Create the expected directory structure + temp_home = temp_db_path.parent + config_dir = temp_home / ".config" / "leggen" + config_dir.mkdir(parents=True, exist_ok=True) + + # Create the expected database path + expected_db_path = config_dir / "leggen.db" + + # Mock Path.home to return our temp directory + def mock_home(): + return temp_home + + # Patch Path.home in the main pathlib module + with patch.object(Path, "home", staticmethod(mock_home)): + yield expected_db_path + + @pytest.fixture def sample_bank_data(): """Sample bank/institution data for testing.""" diff --git a/tests/unit/test_api_accounts.py b/tests/unit/test_api_accounts.py index ddf89eb..55b9024 100644 --- a/tests/unit/test_api_accounts.py +++ b/tests/unit/test_api_accounts.py @@ -9,7 +9,12 @@ class TestAccountsAPI: """Test account-related API endpoints.""" def test_get_all_accounts_success( - self, api_client, mock_config, mock_auth_token, sample_account_data + self, + api_client, + mock_config, + mock_auth_token, + sample_account_data, + mock_db_path, ): """Test successful retrieval of all accounts from database.""" mock_accounts = [ @@ -61,7 +66,12 @@ class TestAccountsAPI: assert account["balances"][0]["amount"] == 100.50 def test_get_account_details_success( - self, api_client, mock_config, mock_auth_token, sample_account_data + self, + api_client, + mock_config, + mock_auth_token, + sample_account_data, + mock_db_path, ): """Test successful retrieval of specific account details from database.""" mock_account = { @@ -109,7 +119,7 @@ class TestAccountsAPI: assert len(account["balances"]) == 1 def test_get_account_balances_success( - self, api_client, mock_config, mock_auth_token + self, api_client, mock_config, mock_auth_token, mock_db_path ): """Test successful retrieval of account balances from database.""" mock_balances = [ @@ -161,6 +171,7 @@ class TestAccountsAPI: mock_auth_token, sample_account_data, sample_transaction_data, + mock_db_path, ): """Test successful retrieval of account transactions from database.""" mock_transactions = [ @@ -211,6 +222,7 @@ class TestAccountsAPI: mock_auth_token, sample_account_data, sample_transaction_data, + mock_db_path, ): """Test retrieval of full transaction details from database.""" mock_transactions = [ @@ -254,7 +266,9 @@ class TestAccountsAPI: assert transaction["iban"] == "LT313250081177977789" assert "raw_transaction" in transaction - def test_get_account_not_found(self, api_client, mock_config, mock_auth_token): + def test_get_account_not_found( + self, api_client, mock_config, mock_auth_token, mock_db_path + ): """Test handling of non-existent account.""" with ( patch("leggend.config.config", mock_config),